/** *Reflectiontest.
*/ publicclass Main { privatestaticboolean FULL_ACCESS_CHECKS = false; // b/5861201 public Main() {} public Main(ArrayList<Integer> stuff) {}
void printMethodInfo(Method meth) { Class<?>[] params, exceptions; int i;
System.out.println("Method name is " + meth.getName());
System.out.println(" Declaring class is "
+ meth.getDeclaringClass().getName());
params = meth.getParameterTypes(); for (i = 0; i < params.length; i++)
System.out.println(" Arg " + i + ": " + params[i].getName());
exceptions = meth.getExceptionTypes(); for (i = 0; i < exceptions.length; i++)
System.out.println(" Exc " + i + ": " + exceptions[i].getName());
System.out.println(" Return type is " + meth.getReturnType().getName());
System.out.println(" Access flags are 0x"
+ Integer.toHexString(meth.getModifiers())); //System.out.println(" GenericStr is " + meth.toGenericString());
}
void printFieldInfo(Field field) {
System.out.println("Field name is " + field.getName());
System.out.println(" Declaring class is "
+ field.getDeclaringClass().getName());
System.out.println(" Field type is " + field.getType().getName());
System.out.println(" Access flags are 0x"
+ Integer.toHexString(field.getModifiers()));
}
field = target.getField("string1"); if (field.getDeclaringClass() != target) thrownew RuntimeException();
printFieldInfo(field);
String strVal = (String) field.get(instance);
System.out.println(" string1 value is '" + strVal + "'");
showStrings(instance);
field.set(instance, new String("a new string"));
strVal = (String) field.get(instance);
System.out.println(" string1 value is now '" + strVal + "'");
showStrings(instance);
try {
field.set(instance, new Object());
System.out.println("WARNING: able to store Object into String");
} catch (IllegalArgumentException iae) {
System.out.println(" got expected illegal obj store exc");
}
try {
String four;
field = target.getField("string4");
four = (String) field.get(instance);
System.out.println("WARNING: able to access string4: "
+ four);
} catch (IllegalAccessException iae) {
System.out.println(" got expected access exc");
} catch (NoSuchFieldException nsfe) {
System.out.println(" got the other expected access exc");
} try {
String three;
field = target.getField("string3");
three = (String) field.get(this);
System.out.println("WARNING: able to get string3 in wrong obj: "
+ three);
} catch (IllegalArgumentException iae) {
System.out.println(" got expected arg exc");
}
/* *Trysettingafieldtonull.
*/
String four;
field = target.getDeclaredField("string3");
field.set(instance, null);
/* *Dosomestuffwithlong.
*/ long longVal;
field = target.getField("pubLong");
longVal = field.getLong(instance);
System.out.println("pubLong initial value is " + Long.toHexString(longVal));
field.setLong(instance, 0x9988776655443322L);
longVal = field.getLong(instance);
System.out.println("pubLong new value is " + Long.toHexString(longVal));
field = target.getField("superInt"); if (field.getDeclaringClass() == target) thrownew RuntimeException();
printFieldInfo(field); int intVal = field.getInt(instance);
System.out.println(" superInt value is " + intVal);
Integer boxedIntVal = (Integer) field.get(instance);
System.out.println(" superInt boxed is " + boxedIntVal);
field.set(instance, new Integer(20202));
intVal = field.getInt(instance);
System.out.println(" superInt value is now " + intVal);
field.setShort(instance, (short)30303);
intVal = field.getInt(instance);
System.out.println(" superInt value (from short) is now " +intVal);
field.setInt(instance, 40404);
intVal = field.getInt(instance);
System.out.println(" superInt value is now " + intVal); try {
field.set(instance, newLong(123));
System.out.println("FAIL: expected exception not thrown");
} catch (IllegalArgumentException iae) {
System.out.println(" got expected long->int failure");
} try {
field.setLong(instance, 123);
System.out.println("FAIL: expected exception not thrown");
} catch (IllegalArgumentException iae) {
System.out.println(" got expected long->int failure");
} try {
field.set(instance, new String("abc"));
System.out.println("FAIL: expected exception not thrown");
} catch (IllegalArgumentException iae) {
System.out.println(" got expected string->int failure");
}
field = target.getField("superClassInt");
printFieldInfo(field); int superClassIntVal = field.getInt(instance);
System.out.println(" superClassInt value is " + superClassIntVal);
field = target.getField("staticDouble");
printFieldInfo(field); double staticDoubleVal = field.getDouble(null);
System.out.println(" staticDoubleVal value is " + staticDoubleVal);
excep = false; try {
field = target.getField("aPrivateInt");
printFieldInfo(field);
} catch (NoSuchFieldException nsfe) {
System.out.println("as expected: aPrivateInt not found");
excep = true;
} if (!excep)
System.out.println("BUG: got aPrivateInt");
field = target.getField("constantString");
printFieldInfo(field);
String val = (String) field.get(instance);
System.out.println(" Constant test value is " + val);
field = target.getField("cantTouchThis");
printFieldInfo(field);
intVal = field.getInt(instance);
System.out.println(" cantTouchThis is " + intVal); try {
field.setInt(instance, 99);
System.out.println("ERROR: set-final did not throw exception");
} catch (IllegalAccessException iae) {
System.out.println(" as expected: set-final throws exception");
}
intVal = field.getInt(instance);
System.out.println(" cantTouchThis is still " + intVal);
System.out.println(" " + field + " accessible=" + field.isAccessible());
field.setAccessible(true);
System.out.println(" " + field + " accessible=" + field.isAccessible());
field.setInt(instance, 87); // exercise int version
intVal = field.getInt(instance);
System.out.println(" cantTouchThis is now " + intVal);
field.set(instance, 88); // exercise Object version
intVal = field.getInt(instance);
System.out.println(" cantTouchThis is now " + intVal);
try {
System.out.println("checkType invoking null"); // Trigger an NPE at the target.
m.invoke(null, null, 0, 1);
System.out.println("ERROR: should throw InvocationTargetException");
} catch (InvocationTargetException ite) {
System.out.println("checkType got expected exception");
} catch (IllegalAccessException iae) {
iae.printStackTrace(System.out); return;
}
}
publicstaticvoid checkClinitForFields() throws Exception { // Loading a class constant shouldn't run <clinit>.
System.out.println("calling const-class FieldNoisyInitUser.class"); Class<?> niuClass = FieldNoisyInitUser.class;
System.out.println("called const-class FieldNoisyInitUser.class");
// Getting the declared fields doesn't run <clinit>.
Field[] fields = niuClass.getDeclaredFields();
System.out.println("got fields");
Field field = niuClass.getField("staticField");
System.out.println("got field");
field.get(null);
System.out.println("read field value");
// FieldNoisyInitUser should now be initialized, but FieldNoisyInit shouldn't be initialized yet.
FieldNoisyInitUser niu = new FieldNoisyInitUser();
FieldNoisyInit ni = new FieldNoisyInit();
System.out.println("");
}
publicstaticvoid checkClinitForMethods() throws Exception { // Loading a class constant shouldn't run <clinit>.
System.out.println("calling const-class MethodNoisyInitUser.class"); Class<?> niuClass = MethodNoisyInitUser.class;
System.out.println("called const-class MethodNoisyInitUser.class");
// Getting the declared methods doesn't run <clinit>.
Method[] methods = niuClass.getDeclaredMethods();
System.out.println("got methods");
// MethodNoisyInitUser should now be initialized, but MethodNoisyInit shouldn't be initialized yet.
MethodNoisyInitUser niu = new MethodNoisyInitUser();
MethodNoisyInit ni = new MethodNoisyInit();
System.out.println("");
}
/* *Testsomegenerictypestuff.
*/ public List<String> stringList; public Map<Integer,String> fancyMethod(ArrayList<String> blah) { returnnull; } publicstaticvoid checkGeneric() {
Field field; try {
field = Main.class.getField("stringList");
} catch (NoSuchFieldException nsfe) { thrownew RuntimeException(nsfe);
}
Type listType = field.getGenericType();
System.out.println("generic field: " + listType);
Type type1 = types1.get(0);
Type type2 = types2.get(0);
Type type3 = types3.get(0);
if (type1 instanceof ParameterizedType) {
System.out.println("type1 is a ParameterizedType");
} if (type2 instanceof ParameterizedType) {
System.out.println("type2 is a ParameterizedType");
} if (type3 instanceof ParameterizedType) {
System.out.println("type3 is a ParameterizedType");
}
if (type1.equals(type2)) {
System.out.println("type1("+type1+") equals type2("+type2+")");
} else {
System.out.println("type1("+type1+") does not equal type2("+type2+")");
}
if (type1.equals(type3)) {
System.out.println("type1("+type1+") equals type3("+type3+")");
} else {
System.out.println("type1("+type1+") does not equal type3("+type3+")");
} if (type1.hashCode() == type2.hashCode()) {
System.out.println("type1("+type1+") hashCode equals type2("+type2+") hashCode");
} else {
System.out.println( "type1("+type1+") hashCode does not equal type2("+type2+") hashCode");
}
if (type1.hashCode() == type3.hashCode()) {
System.out.println("type1("+type1+") hashCode equals type3("+type3+") hashCode");
} else {
System.out.println( "type1("+type1+") hashCode does not equal type3("+type3+") hashCode");
}
}
Type type1 = types1.get(0);
Type type2 = types2.get(0);
Type type3 = types3.get(0);
if (type1 instanceof GenericArrayType) {
System.out.println("type1 is a GenericArrayType");
} if (type2 instanceof GenericArrayType) {
System.out.println("type2 is a GenericArrayType");
} if (type3 instanceof GenericArrayType) {
System.out.println("type3 is a GenericArrayType");
}
if (type1.equals(type2)) {
System.out.println("type1("+type1+") equals type2("+type2+")");
} else {
System.out.println("type1("+type1+") does not equal type2("+type2+")");
}
if (type1.equals(type3)) {
System.out.println("type1("+type1+") equals type3("+type3+")");
} else {
System.out.println("type1("+type1+") does not equal type3("+type3+")");
} if (type1.hashCode() == type2.hashCode()) {
System.out.println("type1("+type1+") hashCode equals type2("+type2+") hashCode");
} else {
System.out.println( "type1("+type1+") hashCode does not equal type2("+type2+") hashCode");
}
if (type1.hashCode() == type3.hashCode()) {
System.out.println("type1("+type1+") hashCode equals type3("+type3+") hashCode");
} else {
System.out.println( "type1("+type1+") hashCode does not equal type3("+type3+") hashCode");
}
}
privatestaticvoid checkGetDeclaredConstructor() { try {
Method.class.getDeclaredConstructor().setAccessible(true);
System.out.println("Didn't get an exception from Method.class.getDeclaredConstructor().setAccessible");
} catch (SecurityException e) {
} catch (NoSuchMethodException e) {
} catch (Exception e) {
System.out.println(e);
} try {
Field.class.getDeclaredConstructor().setAccessible(true);
System.out.println("Didn't get an exception from Field.class.getDeclaredConstructor().setAccessible");
} catch (SecurityException e) {
} catch (NoSuchMethodException e) {
} catch (Exception e) {
System.out.println(e);
} try { Class.class.getDeclaredConstructor().setAccessible(true);
System.out.println("Didn't get an exception from Class.class.getDeclaredConstructor().setAccessible");
} catch (SecurityException e) {
} catch (NoSuchMethodException e) {
} catch (Exception e) {
System.out.println(e);
}
}
staticvoid checkPrivateFieldAccess() {
(new OtherClass()).test();
}
publicstaticvoid main(String[] args) throws Exception {
Main test = new Main();
test.run();
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.