void write(final @Nullable Object value) throws Exception { if (value == null) { TypeDesc.NULL.write(null, this); } else if (value instanceof List) { TypeDesc.LIST.write(value, this); } else { final @Nullable TypeDesc typeDesc = class2typeDesc.get(value.getClass()); if (typeDesc == null) { throw new IllegalArgumentException( "missing type '" + value.getClass().getCanonicalName() + '\''); } typeDesc.write(value, this); } }
@Test public void testAnnotations() throws Exception { String testValue = "TEST"; String methodName = "getTest"; String className = "org.teatrove.trove.test.Annotated"; ClassFile cf = new ClassFile(className); Annotation ann = cf.addRuntimeVisibleAnnotation(TypeDesc.forClass(TestAnnotation.class)); ann.putMemberValue("value", testValue); MethodInfo ctor = cf.addDefaultConstructor(); CodeBuilder builder = new CodeBuilder(ctor); builder.loadThis(); builder.invokeSuperConstructor(); builder.returnVoid(); Modifiers mods = new Modifiers(Modifier.PUBLIC); MethodInfo getName = cf.addMethod(mods, methodName, TypeDesc.STRING); builder = new CodeBuilder(getName); builder.loadConstant(null); builder.returnValue(TypeDesc.STRING); getName.addRuntimeVisibleAnnotation(TypeDesc.forClass(Deprecated.class)); ClassInjector injector = ClassInjector.getInstance(); OutputStream os = injector.getStream(className); cf.writeTo(os); os.close(); Class<?> clazz = injector.loadClass(className); assertEquals("expected class name", className, clazz.getName()); TestAnnotation annotation = clazz.getAnnotation(TestAnnotation.class); assertNotNull("expected annotation", annotation); assertEquals("expected test value", testValue, annotation.value()); Method method = clazz.getMethod(methodName); assertNotNull("expected deprecated", method.getAnnotation(Deprecated.class)); }