< prev index next > test/jdk/java/lang/invoke/defineHiddenClass/BasicTest.java
Print this page
import java.io.File;
import java.io.IOException;
import java.lang.classfile.ClassFile;
import java.lang.constant.ClassDesc;
import java.lang.invoke.MethodHandles.Lookup;
+ import java.lang.reflect.AccessFlag;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
// sanity check
Class<?> c = t.getClass();
Class<?>[] intfs = c.getInterfaces();
assertTrue(c.isHidden());
assertFalse(c.isPrimitive());
! assertTrue(intfs.length == 1);
! assertTrue(intfs[0] == HiddenTest.class);
assertTrue(c.getCanonicalName() == null);
String hcName = "HiddenClass";
String hcSuffix = "0x[0-9a-f]+";
assertTrue(c.getName().matches(hcName + "/" + hcSuffix));
// sanity check
Class<?> c = t.getClass();
Class<?>[] intfs = c.getInterfaces();
assertTrue(c.isHidden());
assertFalse(c.isPrimitive());
! assertTrue(intfs.length == 1 || intfs.length == 2);
! assertTrue(intfs[0] == HiddenTest.class || (intfs.length == 2 && intfs[1] == HiddenTest.class));
assertTrue(c.getCanonicalName() == null);
String hcName = "HiddenClass";
String hcSuffix = "0x[0-9a-f]+";
assertTrue(c.getName().matches(hcName + "/" + hcSuffix));
}
@DataProvider(name = "emptyClasses")
private Object[][] emptyClasses() {
return new Object[][] {
! new Object[] { "EmptyHiddenSynthetic", ACC_SYNTHETIC },
! new Object[] { "EmptyHiddenEnum", ACC_ENUM },
! new Object[] { "EmptyHiddenAbstractClass", ACC_ABSTRACT },
new Object[] { "EmptyHiddenInterface", ACC_ABSTRACT|ACC_INTERFACE },
new Object[] { "EmptyHiddenAnnotation", ACC_ANNOTATION|ACC_ABSTRACT|ACC_INTERFACE },
};
}
}
@DataProvider(name = "emptyClasses")
private Object[][] emptyClasses() {
return new Object[][] {
! new Object[] { "EmptyHiddenSynthetic", ACC_SYNTHETIC | ACC_IDENTITY },
! new Object[] { "EmptyHiddenEnum", ACC_ENUM | ACC_IDENTITY },
! new Object[] { "EmptyHiddenAbstractClass", ACC_ABSTRACT | ACC_IDENTITY },
new Object[] { "EmptyHiddenInterface", ACC_ABSTRACT|ACC_INTERFACE },
new Object[] { "EmptyHiddenAnnotation", ACC_ANNOTATION|ACC_ABSTRACT|ACC_INTERFACE },
};
}
* enum class containing constants of its type should not be a hidden
* class.
*/
@Test(dataProvider = "emptyClasses")
public void emptyHiddenClass(String name, int accessFlags) throws Exception {
! byte[] bytes = (accessFlags == ACC_ENUM) ? classBytes(name, CD_Enum, accessFlags)
: classBytes(name, accessFlags);
Class<?> hc = lookup().defineHiddenClass(bytes, false).lookupClass();
switch (accessFlags) {
! case ACC_SYNTHETIC:
assertTrue(hc.isSynthetic());
assertFalse(hc.isEnum());
assertFalse(hc.isAnnotation());
assertFalse(hc.isInterface());
break;
! case ACC_ENUM:
assertFalse(hc.isSynthetic());
assertTrue(hc.isEnum());
assertFalse(hc.isAnnotation());
assertFalse(hc.isInterface());
break;
! case ACC_ABSTRACT:
assertFalse(hc.isSynthetic());
assertFalse(hc.isEnum());
assertFalse(hc.isAnnotation());
assertFalse(hc.isInterface());
break;
* enum class containing constants of its type should not be a hidden
* class.
*/
@Test(dataProvider = "emptyClasses")
public void emptyHiddenClass(String name, int accessFlags) throws Exception {
! byte[] bytes = (accessFlags == (ACC_ENUM | ACC_IDENTITY)) ? classBytes(name, CD_Enum, accessFlags)
: classBytes(name, accessFlags);
Class<?> hc = lookup().defineHiddenClass(bytes, false).lookupClass();
switch (accessFlags) {
! case (ACC_SYNTHETIC | ACC_IDENTITY):
assertTrue(hc.isSynthetic());
assertFalse(hc.isEnum());
assertFalse(hc.isAnnotation());
assertFalse(hc.isInterface());
break;
! case (ACC_ENUM | ACC_IDENTITY):
assertFalse(hc.isSynthetic());
assertTrue(hc.isEnum());
assertFalse(hc.isAnnotation());
assertFalse(hc.isInterface());
break;
! case ACC_ABSTRACT | ACC_IDENTITY:
assertFalse(hc.isSynthetic());
assertFalse(hc.isEnum());
assertFalse(hc.isAnnotation());
assertFalse(hc.isInterface());
break;
break;
default:
throw new IllegalArgumentException("unexpected access flag: " + accessFlags);
}
assertTrue(hc.isHidden());
! assertTrue(hc.getModifiers() == (ACC_PUBLIC|accessFlags));
assertFalse(hc.isLocalClass());
assertFalse(hc.isMemberClass());
assertFalse(hc.isAnonymousClass());
assertFalse(hc.isArray());
}
break;
default:
throw new IllegalArgumentException("unexpected access flag: " + accessFlags);
}
assertTrue(hc.isHidden());
! assertEquals(hc.getModifiers(), (ACC_PUBLIC|accessFlags));
assertFalse(hc.isLocalClass());
assertFalse(hc.isMemberClass());
assertFalse(hc.isAnonymousClass());
assertFalse(hc.isArray());
}
< prev index next >