aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/javassist/reflect/ClassMetaobject.java
blob: 6c0de5558083c315696abacc3c595ec02fc7f4ea (plain)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 * This file is part of the Javassist toolkit.
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License.  You may obtain a copy of the License at
 * either http://www.mozilla.org/MPL/.
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
 * the License for the specific language governing rights and limitations
 * under the License.
 *
 * The Original Code is Javassist.
 *
 * The Initial Developer of the Original Code is Shigeru Chiba.  Portions
 * created by Shigeru Chiba are Copyright (C) 1999-2003 Shigeru Chiba.
 * All Rights Reserved.
 *
 * Contributor(s):
 *
 * The development of this software is supported in part by the PRESTO
 * program (Sakigake Kenkyu 21) of Japan Science and Technology Corporation.
 */

package javassist.reflect;

import java.lang.reflect.*;
import java.io.Serializable;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javassist.CtClass;

/**
 * A runtime class metaobject.
 *
 * <p>A <code>ClassMetaobject</code> is created for every
 * class of reflective objects.  It can be used to hold values
 * shared among the reflective objects of the same class.
 *
 * @see javassist.reflect.Metaobject
 */
public class ClassMetaobject implements Serializable {
    /**
     * The base-level methods controlled by a metaobject
     * are renamed so that they begin with
     * <code>methodPrefix "_m_"</code>.
     */
    static final String methodPrefix = "_m_";
    static final int methodPrefixLen = 3;

    private Class javaClass;
    private Constructor[] constructors;
    private Method[] methods;

    /**
     * Specifies how a <code>java.lang.Class</code> object is loaded.
     *
     * <p>If true, it is loaded by:
     * <ul><pre>Thread.currentThread().getContextClassLoader().loadClass()</pre></ul>
     * <p>If false, it is loaded by <code>Class.forName()</code>.
     * The default value is false.
     */
    public static boolean useContextClassLoader = false;

    /**
     * Constructs a <code>ClassMetaobject</code>.
     *
     * @param params	<code>params[0]</code> is the name of the class
     *			of the reflective objects.
     */
    public ClassMetaobject(String[] params)
    {
	try {
	    javaClass = getClassObject(params[0]);
	}
	catch (ClassNotFoundException e) {
	    javaClass = null;
	}

	constructors = javaClass.getConstructors();
	methods = null;
    }

    private void writeObject(ObjectOutputStream out) throws IOException {
	out.writeUTF(javaClass.getName());
    }

    private void readObject(ObjectInputStream in)
	throws IOException, ClassNotFoundException
    {
	javaClass = getClassObject(in.readUTF());
	constructors = javaClass.getConstructors();
	methods = null;
    }

    private Class getClassObject(String name) throws ClassNotFoundException {
	if (useContextClassLoader)
	    return Thread.currentThread().getContextClassLoader()
		   .loadClass(name);
	else
	    return Class.forName(name);
    }

    /**
     * Obtains the <code>java.lang.Class</code> representing this class.
     */
    public final Class getJavaClass() {
	return javaClass;
    }

    /**
     * Obtains the name of this class.
     */
    public final String getName() {
	return javaClass.getName();
    }

    /**
     * Returns true if <code>obj</code> is an instance of this class.
     */
    public final boolean isInstance(Object obj) {
	return javaClass.isInstance(obj);
    }

    /**
     * Creates a new instance of the class.
     *
     * @param args		the arguments passed to the constructor.
     */
    public final Object newInstance(Object[] args)
	throws CannotCreateException
    {
	int n = constructors.length;
	for (int i = 0; i < n; ++i) {
	    try {
		return constructors[i].newInstance(args);
	    }
	    catch (IllegalArgumentException e) {
		// try again
	    }
	    catch (InstantiationException e) {
		throw new CannotCreateException(e);
	    }
	    catch (IllegalAccessException e) {
		throw new CannotCreateException(e);
	    }
	    catch (InvocationTargetException e) {
		throw new CannotCreateException(e);
	    }
	}

	throw new CannotCreateException("no constructor matches");
    }

    /**
     * Is invoked when <code>static</code> fields of the base-level
     * class are read and the runtime system intercepts it.
     * This method simply returns the value of the field.
     *
     * <p>Every subclass of this class should redefine this method.
     */
    public Object trapFieldRead(String name) {
	Class jc = getJavaClass();
	try {
	    return jc.getField(name).get(null);
	}
	catch (NoSuchFieldException e) {
	    throw new RuntimeException(e.toString());
	}
	catch (IllegalAccessException e) {
	    throw new RuntimeException(e.toString());
	}
    }

    /**
     * Is invoked when <code>static</code> fields of the base-level
     * class are modified and the runtime system intercepts it.
     * This method simply sets the field to the given value.
     *
     * <p>Every subclass of this class should redefine this method.
     */
    public void trapFieldWrite(String name, Object value) {
	Class jc = getJavaClass();
	try {
	    jc.getField(name).set(null, value);
	}
	catch (NoSuchFieldException e) {
	    throw new RuntimeException(e.toString());
	}
	catch (IllegalAccessException e) {
	    throw new RuntimeException(e.toString());
	}
    }

    /**
     * Invokes a method whose name begins with
     * <code>methodPrefix "_m_"</code> and the identifier.
     *
     * @exception CannotInvokeException		if the invocation fails.
     */
    static public Object invoke(Object target, int identifier, Object[] args)
	throws Throwable
    {
	Method[] allmethods = target.getClass().getMethods();
	int n = allmethods.length;
	String head = methodPrefix + identifier;
	for (int i = 0; i < n; ++i)
	    if (allmethods[i].getName().startsWith(head)) {
		try {
		    return allmethods[i].invoke(target, args);
		} catch (java.lang.reflect.InvocationTargetException e) {
		    throw e.getTargetException();
		} catch (java.lang.IllegalAccessException e) {
		    throw new CannotInvokeException(e);
		}
	    }

	throw new CannotInvokeException("cannot find a method");
    }

    /**
     * Is invoked when <code>static</code> methods of the base-level
     * class are called and the runtime system intercepts it.
     * This method simply executes the intercepted method invocation
     * with the original parameters and returns the resulting value.
     *
     * <p>Every subclass of this class should redefine this method.
     */
    public Object trapMethodcall(int identifier, Object[] args) 
	throws Throwable
    {
	try {
	    Method[] m = getReflectiveMethods();
	    return m[identifier].invoke(null, args);
	}
	catch (java.lang.reflect.InvocationTargetException e) {
	    throw e.getTargetException();
	}
        catch (java.lang.IllegalAccessException e) {
	    throw new CannotInvokeException(e);
	}
    }

    /**
     * Returns an array of the methods defined on the given reflective
     * object.  This method is for the internal use only.
     */
    public final Method[] getReflectiveMethods() {
	if (methods != null)
	    return methods;

	Class baseclass = getJavaClass();
	Method[] allmethods = baseclass.getMethods();
	int n = allmethods.length;
	methods = new Method[n];
	for (int i = 0; i < n; ++i) {
	    Method m = allmethods[i];
	    if (m.getDeclaringClass() == baseclass) {
		String mname = m.getName();
		if (mname.startsWith(methodPrefix)) {
		    int k = 0;
		    for (int j = methodPrefixLen;; ++j) {
			char c = mname.charAt(j);
			if ('0' <= c && c <= '9')
			    k = k * 10 + c - '0';
			else
			    break;
		    }

		    methods[k] = m;
		}
	    }
	}

	return methods;
    }

    /**
     * Returns the name of the method specified
     * by <code>identifier</code>.
     */
    public final String getMethodName(int identifier) {
	String mname = getReflectiveMethods()[identifier].getName();
	int j = ClassMetaobject.methodPrefixLen;
	for (;;) {
	    char c = mname.charAt(j++);
	    if (c < '0' || '9' < c)
		break;
	}

	return mname.substring(j);
    }

    /**
     * Returns an array of <code>Class</code> objects representing the
     * formal parameter types of the method specified
     * by <code>identifier</code>.
     */
    public final Class[] getParameterTypes(int identifier) {
	return getReflectiveMethods()[identifier].getParameterTypes();
    }

    /**
     * Returns a <code>Class</code> objects representing the
     * return type of the method specified by <code>identifier</code>.
     */
    public final Class getReturnType(int identifier) {
	return getReflectiveMethods()[identifier].getReturnType();
    }
}