public int test(String[] args) throws Exception { Class cl = Class.forName( "org.apache.harmony.vts.test.vm.jvms.classFile.attributes.annotation.annotationDefault.methodinfo13.annotation13"); Object o = cl.getMethod("value", new Class[] {}).getDefaultValue(); if ("org.apache.harmony.vts.test.vm.jvms.classFile.attributes.annotation.annotationDefault.methodinfo13.annotation13ann" .equals(((Annotation) o).annotationType().getName())) return 104; return 105; }
private void checkAnnotation(Class<?> c, Class<? extends Annotation> expectedAnnotation) throws NoSuchMethodException, NoSuchFieldException { Class<? extends Annotation> annotation = c.getAnnotation(expectedAnnotation).annotationType(); assertEquals(expectedAnnotation, annotation); Method m = c.getMethod("testMethod"); annotation = m.getAnnotation(expectedAnnotation).annotationType(); assertEquals(expectedAnnotation, annotation); Field f = c.getField("testField"); annotation = f.getAnnotation(expectedAnnotation).annotationType(); assertEquals(expectedAnnotation, annotation); }
Method getSetMethod(Class<?> targetClass, Class<?> fieldType, String fieldName) { fieldName = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); String methodname = "set" + fieldName; Method method = null; try { method = targetClass.getMethod(methodname, new Class<?>[] {fieldType}); } catch (Exception e) { playerObjects.getLogFile().WriteLine(Formatting.exceptionToStackTrace(e)); } return method; }
Method getGetMethod(Class<?> targetClass, Class<?> fieldType, String fieldName) { fieldName = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); String methodname = "get" + fieldName; if (fieldType == boolean.class || fieldType == Boolean.class) { methodname = "is" + fieldName; } Method method = null; try { method = targetClass.getMethod(methodname, new Class<?>[0]); } catch (Exception e) { playerObjects.getLogFile().WriteLine(Formatting.exceptionToStackTrace(e)); } return method; }
public static void main(String[] args) throws Exception { Class<ClassTest> clazz = ClassTest.class; System.out.println("============================================="); Constructor[] ctors = clazz.getDeclaredConstructors(); System.out.println("classTest的全部构造器如下:"); for (Constructor c : ctors) { System.out.println(c); } System.out.println("============================================="); Constructor[] publicCtors = clazz.getConstructors(); System.out.println("ClassTest的全部public构造器如下:"); for (Constructor c : publicCtors) { System.out.println(c); } System.out.println("============================================="); Method[] mtds = clazz.getMethods(); System.out.println("ClassTest的全部public方法如下:"); for (Method md : mtds) { System.out.println(md); } System.out.println("============================================="); System.out.println("ClassTest带一个字符串参数的info方法为:" + clazz.getMethod("info", String.class)); Annotation[] ans = clazz.getAnnotations(); System.out.println("ClassTest的全部annotation为:"); for (Annotation an : ans) { System.out.println(an); } System.out.println("该元素上的@SuppressWarnings注释为:" + clazz.getAnnotation(SuppressWarnings.class)); System.out.println("============================================="); Class<?>[] inners = clazz.getDeclaredClasses(); System.out.println("ClassTest的全部内部类如下:"); for (Class c : inners) { System.out.println(c); } System.out.println("============================================="); Class inClazz = Class.forName("ClassTest$Inner"); System.out.println("inClazz对应的外部类为:" + inClazz.getDeclaringClass()); System.out.println("ClassTest的包为:" + clazz.getPackage()); System.out.println("ClassTest的父类:" + clazz.getSuperclass()); }
private void checkAnnotationMissing(Class<A> c) throws NoSuchMethodException, NoSuchFieldException { assertEquals(0, c.getAnnotations().length); assertEquals(0, c.getField("testField").getAnnotations().length); assertEquals(0, c.getMethod("testMethod").getAnnotations().length); }