Coverage Report - org.webmacro.util.PropertyMethod
 
Classes in this File Line Coverage Branch Coverage Complexity
PropertyMethod
77%
28/36
57%
8/14
2.6
 
 1  
 /*
 2  
  * Copyright (C) 1998-2000 Semiotek Inc.  All Rights Reserved.
 3  
  *
 4  
  * Redistribution and use in source and binary forms, with or without
 5  
  * modification, are permitted under the terms of either of the following
 6  
  * Open Source licenses:
 7  
  *
 8  
  * The GNU General Public License, version 2, or any later version, as
 9  
  * published by the Free Software Foundation
 10  
  * (http://www.fsf.org/copyleft/gpl.html);
 11  
  *
 12  
  *  or
 13  
  *
 14  
  * The Semiotek Public License (http://webmacro.org/LICENSE.)
 15  
  *
 16  
  * This software is provided "as is", with NO WARRANTY, not even the
 17  
  * implied warranties of fitness to purpose, or merchantability. You
 18  
  * assume all risks and liabilities associated with its use.
 19  
  *
 20  
  * See www.webmacro.org for more information on the WebMacro project.
 21  
  */
 22  
 
 23  
 
 24  
 package org.webmacro.util;
 25  
 
 26  
 import org.webmacro.Context;
 27  
 import org.webmacro.Macro;
 28  
 import org.webmacro.PropertyException;
 29  
 
 30  
 /**
 31  
  * A property method can function as part of a name in a
 32  
  * property, and represents a method call that should be used
 33  
  * to resolve that portion of the name.
 34  
  * <p>
 35  
  * For example: a.b.get("C").d is equivalent to a.b.hi.d, you can
 36  
  * also use this to access methods which are not normally available
 37  
  * through the regular introspection algorithms, for example:
 38  
  * #set $thing = a.get("thing")
 39  
  * <p>
 40  
  * The arguments supplied to a PropertyMethod can be a list
 41  
  * including Macro objects which need to be resolved
 42  
  * against a context. The introspection process will supply the
 43  
  * context and resolve these references at execution time.
 44  
  */
 45  
 public class PropertyMethod implements Named
 46  
 {
 47  
 
 48  
     private Object _args;
 49  
     private String _name;
 50  
     private boolean _reference;
 51  
 
 52  
     /**
 53  
      * Create a new PropertyMethod
 54  
      * @param name the name of the method to call
 55  
      * @param args the arguments, including Macro objects
 56  
      */
 57  
     public PropertyMethod (String name, Object[] args)
 58  540
     {
 59  540
         _name = name;
 60  540
         _args = args;
 61  540
         _reference = false;
 62  540
     }
 63  
 
 64  
     /**
 65  
      * Create a new PropertyMethod
 66  
      * @param name the name of the method to call
 67  
      * @param args the arguments, including Macro objects
 68  
      */
 69  
     public PropertyMethod (String name, Macro args)
 70  1588
     {
 71  1588
         _name = name;
 72  1588
         _args = args;
 73  1588
         _reference = true;
 74  1588
     }
 75  
 
 76  
     /**
 77  
      * Return the name of this PropertyMethod
 78  
      */
 79  
     final public String getName ()
 80  
     {
 81  80790
         return _name;
 82  
     }
 83  
 
 84  
     /**
 85  
      * Return a signature of this method
 86  
      */
 87  
     final public String toString ()
 88  
     {
 89  6
         if (_reference)
 90  
         {
 91  2
             return _name + _args.toString();
 92  
         }
 93  4
         Object[] argList = (Object[]) _args;
 94  4
         StringBuffer vname = new StringBuffer();
 95  4
         vname.append(_name);
 96  4
         vname.append("(");
 97  4
         for (int i = 0; i < argList.length; i++)
 98  
         {
 99  0
             if (i != 0)
 100  
             {
 101  0
                 vname.append(",");
 102  
             }
 103  0
             vname.append(argList[i]);
 104  
         }
 105  4
         vname.append(")");
 106  4
         return vname.toString();
 107  
     }
 108  
 
 109  
 
 110  
     /**
 111  
      * Return the arguments for this method, after resolving them
 112  
      * against the supplied context. Any arguments which are of
 113  
      * type Macro will be resolved into a regular
 114  
      * object via the Macro.evaluate method.
 115  
      * @exception PropertyException a Macro in the arguments failed to resolve against the supplied context
 116  
      */
 117  
     final public Object[] getArguments (Context context)
 118  
             throws PropertyException
 119  
     {
 120  
         Object[] argList;
 121  80790
         if (_reference)
 122  
         {
 123  42696
             argList = (Object[]) ((Macro) _args).evaluate(context);
 124  
         }
 125  
         else
 126  
         {
 127  38094
             argList = (Object[]) _args;
 128  
         }
 129  
 
 130  80770
         Object ret[] = new Object[argList.length];
 131  80770
         System.arraycopy(argList, 0, ret, 0, argList.length);
 132  142918
         for (int i = 0; i < ret.length; i++)
 133  
         {
 134  62148
             while (ret[i] instanceof Macro)
 135  
             {
 136  0
                 Object repl = ((Macro) ret[i]).evaluate(context);
 137  0
                 if (repl == ret[i])
 138  
                 {
 139  0
                     break; // avoid infinite loop
 140  
                 }
 141  0
                 ret[i] = repl;
 142  0
             }
 143  
         }
 144  80770
         return ret;
 145  
     }
 146  
 
 147  
 }