@@ -62,7 +62,8 @@ public int getIndex (String methodName, Class... paramTypes) {
62
62
public int getIndex (String methodName , int paramsCount ) {
63
63
for (int i = 0 , n = methodNames .length ; i < n ; i ++)
64
64
if (methodNames [i ].equals (methodName ) && parameterTypes [i ].length == paramsCount ) return i ;
65
- throw new IllegalArgumentException ("Unable to find non-private method: " + methodName + " with " + paramsCount + " params." );
65
+ throw new IllegalArgumentException (
66
+ "Unable to find non-private method: " + methodName + " with " + paramsCount + " params." );
66
67
}
67
68
68
69
public String [] getMethodNames () {
@@ -77,7 +78,11 @@ public Class[] getReturnTypes () {
77
78
return returnTypes ;
78
79
}
79
80
81
+ /** @param type Must not be the Object class, an interface, a primitive type, or void. */
80
82
static public MethodAccess get (Class type ) {
83
+ if (type .getSuperclass () == null )
84
+ throw new IllegalArgumentException ("The type must not be the Object class, an interface, a primitive type, or void." );
85
+
81
86
ArrayList <Method > methods = new ArrayList <Method >();
82
87
boolean isInterface = type .isInterface ();
83
88
if (!isInterface ) {
@@ -116,11 +121,11 @@ static public MethodAccess get (Class type) {
116
121
} catch (ClassNotFoundException ignored2 ) {
117
122
String accessClassNameInternal = accessClassName .replace ('.' , '/' );
118
123
String classNameInternal = className .replace ('.' , '/' );
119
-
124
+
120
125
ClassWriter cw = new ClassWriter (ClassWriter .COMPUTE_MAXS );
121
126
MethodVisitor mv ;
122
- cw .visit (V1_1 , ACC_PUBLIC + ACC_SUPER , accessClassNameInternal , null , "com/esotericsoftware/reflectasm/MethodAccess" ,
123
- null );
127
+ cw .visit (V1_1 , ACC_PUBLIC + ACC_SUPER , accessClassNameInternal , null ,
128
+ "com/esotericsoftware/reflectasm/MethodAccess" , null );
124
129
{
125
130
mv = cw .visitMethod (ACC_PUBLIC , "<init>" , "()V" , null , null );
126
131
mv .visitCode ();
@@ -134,19 +139,19 @@ static public MethodAccess get (Class type) {
134
139
mv = cw .visitMethod (ACC_PUBLIC + ACC_VARARGS , "invoke" ,
135
140
"(Ljava/lang/Object;I[Ljava/lang/Object;)Ljava/lang/Object;" , null , null );
136
141
mv .visitCode ();
137
-
142
+
138
143
if (!methods .isEmpty ()) {
139
144
mv .visitVarInsn (ALOAD , 1 );
140
145
mv .visitTypeInsn (CHECKCAST , classNameInternal );
141
146
mv .visitVarInsn (ASTORE , 4 );
142
-
147
+
143
148
mv .visitVarInsn (ILOAD , 2 );
144
149
Label [] labels = new Label [n ];
145
150
for (int i = 0 ; i < n ; i ++)
146
151
labels [i ] = new Label ();
147
152
Label defaultLabel = new Label ();
148
153
mv .visitTableSwitchInsn (0 , labels .length - 1 , defaultLabel , labels );
149
-
154
+
150
155
StringBuilder buffer = new StringBuilder (128 );
151
156
for (int i = 0 ; i < n ; i ++) {
152
157
mv .visitLabel (labels [i ]);
@@ -155,10 +160,10 @@ static public MethodAccess get (Class type) {
155
160
else
156
161
mv .visitFrame (Opcodes .F_SAME , 0 , null , 0 , null );
157
162
mv .visitVarInsn (ALOAD , 4 );
158
-
163
+
159
164
buffer .setLength (0 );
160
165
buffer .append ('(' );
161
-
166
+
162
167
Class [] paramTypes = parameterTypes [i ];
163
168
Class returnType = returnTypes [i ];
164
169
for (int paramIndex = 0 ; paramIndex < paramTypes .length ; paramIndex ++) {
@@ -208,7 +213,7 @@ static public MethodAccess get (Class type) {
208
213
}
209
214
buffer .append (paramType .getDescriptor ());
210
215
}
211
-
216
+
212
217
buffer .append (')' );
213
218
buffer .append (Type .getDescriptor (returnType ));
214
219
int invoke ;
@@ -219,7 +224,7 @@ else if (Modifier.isStatic(methods.get(i).getModifiers()))
219
224
else
220
225
invoke = INVOKEVIRTUAL ;
221
226
mv .visitMethodInsn (invoke , classNameInternal , methodNames [i ], buffer .toString ());
222
-
227
+
223
228
switch (Type .getType (returnType ).getSort ()) {
224
229
case Type .VOID :
225
230
mv .visitInsn (ACONST_NULL );
@@ -249,10 +254,10 @@ else if (Modifier.isStatic(methods.get(i).getModifiers()))
249
254
mv .visitMethodInsn (INVOKESTATIC , "java/lang/Double" , "valueOf" , "(D)Ljava/lang/Double;" );
250
255
break ;
251
256
}
252
-
257
+
253
258
mv .visitInsn (ARETURN );
254
259
}
255
-
260
+
256
261
mv .visitLabel (defaultLabel );
257
262
mv .visitFrame (Opcodes .F_SAME , 0 , null , 0 , null );
258
263
}
0 commit comments