Skip to content

Commit 54f4534

Browse files
committed
Throw exception for invalid types.
closes #59
1 parent ade0542 commit 54f4534

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

src/com/esotericsoftware/reflectasm/FieldAccess.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,11 @@ public void setFields (Field[] fields) {
108108

109109
abstract public float getFloat (Object instance, int fieldIndex);
110110

111+
/** @param type Must not be the Object class, an interface, a primitive type, or void. */
111112
static public FieldAccess get (Class type) {
113+
if (type.getSuperclass() == null)
114+
throw new IllegalArgumentException("The type must not be the Object class, an interface, a primitive type, or void.");
115+
112116
ArrayList<Field> fields = new ArrayList<Field>();
113117
Class nextClass = type;
114118
while (nextClass != Object.class) {
@@ -424,8 +428,8 @@ static private void insertSetPrimitive (ClassWriter cw, String classNameInternal
424428
break;
425429
case Type.DOUBLE:
426430
setterMethodName = "setDouble";
427-
loadValueInstruction = DLOAD; // (LLOAD and DLOAD actually load two slots)
428-
maxLocals++;
431+
loadValueInstruction = DLOAD;
432+
maxLocals++; // (LLOAD and DLOAD actually load two slots)
429433
break;
430434
default:
431435
setterMethodName = "set";

src/com/esotericsoftware/reflectasm/MethodAccess.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ public int getIndex (String methodName, Class... paramTypes) {
6262
public int getIndex (String methodName, int paramsCount) {
6363
for (int i = 0, n = methodNames.length; i < n; i++)
6464
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.");
6667
}
6768

6869
public String[] getMethodNames () {
@@ -77,7 +78,11 @@ public Class[] getReturnTypes () {
7778
return returnTypes;
7879
}
7980

81+
/** @param type Must not be the Object class, an interface, a primitive type, or void. */
8082
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+
8186
ArrayList<Method> methods = new ArrayList<Method>();
8287
boolean isInterface = type.isInterface();
8388
if (!isInterface) {
@@ -116,11 +121,11 @@ static public MethodAccess get (Class type) {
116121
} catch (ClassNotFoundException ignored2) {
117122
String accessClassNameInternal = accessClassName.replace('.', '/');
118123
String classNameInternal = className.replace('.', '/');
119-
124+
120125
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
121126
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);
124129
{
125130
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
126131
mv.visitCode();
@@ -134,19 +139,19 @@ static public MethodAccess get (Class type) {
134139
mv = cw.visitMethod(ACC_PUBLIC + ACC_VARARGS, "invoke",
135140
"(Ljava/lang/Object;I[Ljava/lang/Object;)Ljava/lang/Object;", null, null);
136141
mv.visitCode();
137-
142+
138143
if (!methods.isEmpty()) {
139144
mv.visitVarInsn(ALOAD, 1);
140145
mv.visitTypeInsn(CHECKCAST, classNameInternal);
141146
mv.visitVarInsn(ASTORE, 4);
142-
147+
143148
mv.visitVarInsn(ILOAD, 2);
144149
Label[] labels = new Label[n];
145150
for (int i = 0; i < n; i++)
146151
labels[i] = new Label();
147152
Label defaultLabel = new Label();
148153
mv.visitTableSwitchInsn(0, labels.length - 1, defaultLabel, labels);
149-
154+
150155
StringBuilder buffer = new StringBuilder(128);
151156
for (int i = 0; i < n; i++) {
152157
mv.visitLabel(labels[i]);
@@ -155,10 +160,10 @@ static public MethodAccess get (Class type) {
155160
else
156161
mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
157162
mv.visitVarInsn(ALOAD, 4);
158-
163+
159164
buffer.setLength(0);
160165
buffer.append('(');
161-
166+
162167
Class[] paramTypes = parameterTypes[i];
163168
Class returnType = returnTypes[i];
164169
for (int paramIndex = 0; paramIndex < paramTypes.length; paramIndex++) {
@@ -208,7 +213,7 @@ static public MethodAccess get (Class type) {
208213
}
209214
buffer.append(paramType.getDescriptor());
210215
}
211-
216+
212217
buffer.append(')');
213218
buffer.append(Type.getDescriptor(returnType));
214219
int invoke;
@@ -219,7 +224,7 @@ else if (Modifier.isStatic(methods.get(i).getModifiers()))
219224
else
220225
invoke = INVOKEVIRTUAL;
221226
mv.visitMethodInsn(invoke, classNameInternal, methodNames[i], buffer.toString());
222-
227+
223228
switch (Type.getType(returnType).getSort()) {
224229
case Type.VOID:
225230
mv.visitInsn(ACONST_NULL);
@@ -249,10 +254,10 @@ else if (Modifier.isStatic(methods.get(i).getModifiers()))
249254
mv.visitMethodInsn(INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;");
250255
break;
251256
}
252-
257+
253258
mv.visitInsn(ARETURN);
254259
}
255-
260+
256261
mv.visitLabel(defaultLabel);
257262
mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
258263
}

0 commit comments

Comments
 (0)