View Javadoc

1   /*
2    * Copyright 2005 John G. Wilson
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   */
17  
18  package groovy.lang;
19  
20  import java.lang.reflect.Constructor;
21  import java.lang.reflect.Method;
22  import java.util.LinkedList;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.logging.Logger;
26  
27  import org.codehaus.groovy.ast.ClassNode;
28  import org.codehaus.groovy.runtime.MetaClassHelper;
29  
30  /***
31   * @author John Wilson
32   *
33   */
34  
35  public abstract class MetaClass {
36      protected static final Logger log = Logger.getLogger(MetaClass.class.getName());
37      protected static boolean useReflection = false;
38      public static final Object NO_METHOD_FOUND = new Object();
39      
40      public static boolean isUseReflection() {
41          return MetaClass.useReflection;
42      }
43  
44      /***
45       * Allows reflection to be enabled in situations where bytecode generation
46       * of method invocations causes issues.
47       *
48       * @param useReflection
49       */
50      public static void setUseReflection(boolean useReflection) {
51          MetaClass.useReflection = useReflection;
52      }
53  
54      protected final Class theClass;
55      
56      protected MetaClass(final Class theClass) {
57          this.theClass = theClass;
58      }
59      
60      public Object invokeMethod(Object object, String methodName, Object arguments) {
61          if (arguments == null) {
62              return invokeMethod(object, methodName, MetaClassHelper.EMPTY_ARRAY);
63          }
64          if (arguments instanceof Tuple) {
65              Tuple tuple = (Tuple) arguments;
66              return invokeMethod(object, methodName, tuple.toArray());
67          }
68          if (arguments instanceof Object[]) {
69              return invokeMethod(object, methodName, (Object[])arguments);
70          }
71          else {
72              return invokeMethod(object, methodName, new Object[]{arguments});
73          }
74      }
75      
76      public abstract Object invokeConstructor(Object[] arguments);
77      public abstract Object invokeMethod(Object object, String methodName, Object[] arguments);
78      public abstract Object invokeStaticMethod(Object object, String methodName, Object[] arguments);
79      public abstract Object getProperty(Object object, String property);
80      public abstract void setProperty(Object object, String property, Object newValue);
81      public abstract Object getAttribute(Object object, String attribute);
82      public abstract void setAttribute(Object object, String attribute, Object newValue);
83      
84      // Possibly Temp methods
85      public abstract List getMethods();
86      public abstract MetaMethod pickMethod(String methodName, Class[] arguments);
87      public abstract MetaMethod pickMethod(Object object, String methodName, Object[] arguments);
88      public abstract MetaMethod retrieveMethod(Object owner, String methodName, Object[] arguments);
89      public abstract MetaMethod retrieveMethod(String methodName, Class[] arguments);
90      public abstract MetaMethod retrieveStaticMethod(String methodName, Class[] arguments);
91      public abstract Constructor retrieveConstructor(Class[] arguments);
92      public abstract void addNewInstanceMethod(Method method);
93      public abstract void addNewStaticMethod(Method method);
94      public abstract void checkInitialised();
95      public abstract List getProperties();
96      public abstract void setProperties(Object bean, Map map);
97      public abstract ClassNode getClassNode();
98      public abstract List getMetaMethods();
99      public abstract Object invokeConstructorAt(Class at, Object[] arguments);
100 
101     // Possibly Temp fields
102     protected List newGroovyMethodsList = new LinkedList();
103 
104 }