1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 package org.codehaus.groovy.runtime;
36
37 import groovy.lang.MetaMethod;
38
39 import java.lang.reflect.Modifier;
40
41 /***
42 * A MetaMethod implementation where the underlying method is really a static
43 * helper method on some class but it appears to be an instance method on a class.
44 *
45 * This implementation is used to add new methods to the JDK writing them as normal
46 * static methods with the first parameter being the class on which the method is added.
47 *
48 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
49 * @version $Revision: 1.1 $
50 */
51 public class NewInstanceMetaMethod extends MetaMethod {
52
53 private static final Class[] EMPTY_TYPE_ARRAY = {};
54
55 private MetaMethod metaMethod;
56 private Class[] logicalParameterTypes;
57
58 public NewInstanceMetaMethod(MetaMethod metaMethod) {
59 super(metaMethod);
60 this.metaMethod = metaMethod;
61 Class[] realParameterTypes = metaMethod.getParameterTypes();
62 int size = realParameterTypes.length;
63 if (size <= 1) {
64 logicalParameterTypes = EMPTY_TYPE_ARRAY;
65 }
66 else {
67 logicalParameterTypes = new Class[--size];
68 System.arraycopy(realParameterTypes, 1, logicalParameterTypes, 0, size);
69 }
70 }
71
72 public Class getDeclaringClass() {
73 return getBytecodeParameterTypes()[0];
74 }
75
76 public boolean isStatic() {
77 return false;
78 }
79
80 public int getModifiers() {
81
82 return super.getModifiers() ^ Modifier.STATIC;
83 }
84
85 public Class[] getParameterTypes() {
86 return logicalParameterTypes;
87 }
88
89 public Class[] getBytecodeParameterTypes() {
90 return super.getParameterTypes();
91 }
92
93 public Object invoke(Object object, Object[] arguments) throws Exception {
94
95 int size = arguments.length;
96 Object[] newArguments = new Object[size + 1];
97 newArguments[0] = object;
98 System.arraycopy(arguments, 0, newArguments, 1, size);
99 return metaMethod.invoke(null, newArguments);
100 }
101 }