Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Class.forName for primitive array types #372

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 12 additions & 17 deletions src/peers/gov/nasa/jpf/vm/JPF_java_lang_Class.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,27 +188,22 @@ public int forName__Ljava_lang_String_2__Ljava_lang_Class_2 (MJIEnv env,
// class of the method that includes the invocation of Class.forName()
ClassInfo cls = mi.getClassInfo();

String name;
// for array type, the component terminal must be resolved
if(clsName.charAt(0)=='[') {
name = Types.getComponentTerminal(clsName);
} else{
name = clsName;
}

// make the classloader of the class including the invocation of
// Class.forName() resolve the class with the given name
try {
cls.resolveReferencedClass(name);
} catch(LoadOnJPFRequired lre) {
env.repeatInvocation();
return MJIEnv.NULL;
String typeName = Types.getTypeSignature(clsName, false);
if (Types.isReferenceSignature(typeName)) {
String t = Types.isArray(typeName) ? Types.getComponentTerminal(typeName) : typeName;
try {
// make the classloader of the class including the invocation of
// Class.forName() resolve the class with the given name
cls.resolveReferencedClass(t);
} catch (LoadOnJPFRequired lre) {
env.repeatInvocation();
return MJIEnv.NULL;
}
}

// The class obtained here is the same as the resolved one, except
// if it represents an array type
ClassInfo ci = cls.getClassLoaderInfo().getResolvedClassInfo(clsName);

ClassInfo ci = cls.getClassLoaderInfo().getResolvedClassInfo(typeName);
return getClassObject(env, ci);
}

Expand Down
31 changes: 30 additions & 1 deletion src/tests/gov/nasa/jpf/test/java/lang/ClassTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,36 @@ public void testClassForName () throws ClassNotFoundException {
}
}
}


private void testClassForNameArray (String className, Class<?> expectedClass) throws ClassNotFoundException {
Class<?> clazz = Class.forName(className);
assertNotNull(clazz);
assertTrue(clazz.isArray());
assertEquals(className, clazz.getName());
assertSame(expectedClass, clazz);
}

@Test
public void testClassForNamePrimitiveArrays () throws ClassNotFoundException {
if (verifyNoPropertyViolation()) {
testClassForNameArray("[Z", boolean[].class);
testClassForNameArray("[I", int[].class);
testClassForNameArray("[C", char[].class);
testClassForNameArray("[D", double[].class);
testClassForNameArray("[F", float[].class);
testClassForNameArray("[I", int[].class);
testClassForNameArray("[J", long[].class);
testClassForNameArray("[S", short[].class);
}
}

@Test
public void testClassForNameReferenceArray () throws ClassNotFoundException {
if (verifyNoPropertyViolation()) {
testClassForNameArray("[L" + clsName + ";", ClassTest[].class);
}
}

@Test
public void testClassForNameException () throws ClassNotFoundException {
if (verifyUnhandledException("java.lang.ClassNotFoundException")) {
Expand Down
Loading