Coverage Report - org.simpleframework.xml.core.Function
 
Classes in this File Line Coverage Branch Coverage Complexity
Function
76%
10/13
75%
3/4
2.333
 
 1  
 /*
 2  
  * Function.java February 2008
 3  
  *
 4  
  * Copyright (C) 2008, Niall Gallagher <niallg@users.sf.net>
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License");
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 15  
  * implied. See the License for the specific language governing 
 16  
  * permissions and limitations under the License.
 17  
  */
 18  
 
 19  
 package org.simpleframework.xml.core;
 20  
 
 21  
 import java.lang.reflect.Method;
 22  
 import java.util.Map;
 23  
 
 24  
 /**
 25  
  * The <code>Function</code> object is used to encapsulated the method
 26  
  * that is called when serializing an object. This contains details 
 27  
  * on the type of method represented and ensures that reflection is
 28  
  * not required each time the method is to be invoked.
 29  
  * 
 30  
  * @author Niall Gallagher
 31  
  */
 32  
 class Function {
 33  
    
 34  
    /**
 35  
     * This is the method that is to be invoked by the function.
 36  
     */
 37  
    private final Method method;
 38  
    
 39  
    /**
 40  
     * This is used to determine if the method takes the map.
 41  
     */
 42  
    private final boolean contextual;
 43  
    
 44  
    /**
 45  
     * Constructor for the <code>Function</code> object. This is used
 46  
     * to create an object that wraps the provided method it ensures
 47  
     * that no reflection is required when the method is to be called.
 48  
     * 
 49  
     * @param method this is the method that is to be wrapped by this
 50  
     */
 51  
    public Function(Method method) {
 52  0
       this(method, false);
 53  0
    }
 54  
    
 55  
    /**
 56  
     * Constructor for the <code>Function</code> object. This is used
 57  
     * to create an object that wraps the provided method it ensures
 58  
     * that no reflection is required when the method is to be called.
 59  
     * 
 60  
     * @param method this is the method that is to be wrapped by this
 61  
     * @param contextual determines if the method is a contextual one
 62  
     */
 63  44
    public Function(Method method, boolean contextual) {
 64  44
       this.contextual = contextual;
 65  44
       this.method = method;
 66  44
    }
 67  
    
 68  
    /**
 69  
     * This method used to invoke the callback method of the provided
 70  
     * object. This will acquire the session map from the context. If
 71  
     * the provided object is not null then this will return null.
 72  
     * 
 73  
     * @param context this is the context that contains the session
 74  
     * @param source this is the object to invoke the function on
 75  
     * 
 76  
     * @return this returns the result of the method invocation
 77  
     */
 78  
    public Object call(Context context, Object source) throws Exception {
 79  43
       if(source != null) {
 80  43
          Session session = context.getSession();
 81  43
          Map table = session.getMap();
 82  
       
 83  43
          if(contextual) {              
 84  13
             return method.invoke(source, table);           
 85  
          }
 86  30
          return method.invoke(source);
 87  
       }
 88  0
       return null;
 89  
    }
 90  
 }