/** * Transform simple class from an immutable to a mutable object. */ public void testTransformClassToClassGen_SimpleTypes() throws ClassNotFoundException { final JavaClass jc = getTestClass(PACKAGE_BASE_NAME+".data.SimpleAnnotatedClass"); final ClassGen cgen = new ClassGen(jc); // Check annotations are correctly preserved final AnnotationEntryGen[] annotations = cgen.getAnnotationEntries(); assertTrue("Expected one annotation but found " + annotations.length, annotations.length == 1); }
/** * Transform simple class from an immutable to a mutable object. The class * is annotated with an annotation that uses an enum. */ public void testTransformClassToClassGen_EnumType() throws ClassNotFoundException { final JavaClass jc = getTestClass(PACKAGE_BASE_NAME+".data.AnnotatedWithEnumClass"); final ClassGen cgen = new ClassGen(jc); // Check annotations are correctly preserved final AnnotationEntryGen[] annotations = cgen.getAnnotationEntries(); assertTrue("Expected one annotation but found " + annotations.length, annotations.length == 1); }
/** * Transform complex class from an immutable to a mutable object. */ public void testTransformComplexClassToClassGen() throws ClassNotFoundException { final JavaClass jc = getTestClass(PACKAGE_BASE_NAME+".data.ComplexAnnotatedClass"); final ClassGen cgen = new ClassGen(jc); // Check annotations are correctly preserved final AnnotationEntryGen[] annotations = cgen.getAnnotationEntries(); assertTrue("Expected one annotation but found " + annotations.length, annotations.length == 1); final List<?> l = annotations[0].getValues(); boolean found = false; for (final Object name : l) { final ElementValuePairGen element = (ElementValuePairGen) name; if (element.getNameString().equals("dval")) { if (((SimpleElementValueGen) element.getValue()) .stringifyValue().equals("33.4")) { found = true; } } } assertTrue("Did not find double annotation value with value 33.4", found); }
/** * Load a class in and modify it with a new attribute - A SimpleAnnotation * annotation */ public void testModifyingClasses1() throws ClassNotFoundException { final JavaClass jc = getTestClass(PACKAGE_BASE_NAME+".data.SimpleAnnotatedClass"); final ClassGen cgen = new ClassGen(jc); final ConstantPoolGen cp = cgen.getConstantPool(); cgen.addAnnotationEntry(createFruitAnnotation(cp, "Pineapple")); assertTrue("Should now have two annotations but has " + cgen.getAnnotationEntries().length, cgen .getAnnotationEntries().length == 2); dumpClass(cgen, "SimpleAnnotatedClass.class"); assertTrue(wipe("SimpleAnnotatedClass.class")); }
assertTrue("Expected one annotation but found " + annotations.length, annotations.length == 1); final AnnotationEntryGen a = annotations[0]; assertTrue("That annotation should only have one value but has " + a.getValues().size(), a.getValues().size() == 1); final ElementValuePairGen nvp = a.getValues().get(0); final ElementValueGen value = nvp.getValue(); assertTrue("Value should be ArrayElementValueGen but is " + value, value instanceof ArrayElementValueGen); final ArrayElementValueGen arrayValue = (ArrayElementValueGen) value; assertTrue("Array value should be size one but is " + arrayValue.getElementValuesSize(), arrayValue .getElementValuesSize() == 1); final ElementValueGen innerValue = arrayValue.getElementValues().get(0); assertTrue( "Value in the array should be AnnotationElementValueGen but is " + innerValue, innerValue instanceof AnnotationElementValueGen); final AnnotationElementValueGen innerAnnotationValue = (AnnotationElementValueGen) innerValue; assertTrue("Should be called L"+PACKAGE_BASE_SIG+"/data/SimpleAnnotation; but is called: " + innerAnnotationValue.getAnnotation().getTypeName(), innerAnnotationValue.getAnnotation().getTypeSignature().equals(
final JavaClass jc = getClassFrom(".", "HelloWorld"); final AnnotationEntry[] as = jc.getAnnotationEntries(); assertTrue("Should be two AnnotationEntries but found " + as.length, as.length == 2); assertTrue( "Name of annotation 1 should be LSimpleAnnotation; but it is " + as[0].getAnnotationType(), as[0].getAnnotationType() .equals("LSimpleAnnotation;")); assertTrue( "Name of annotation 2 should be LSimpleAnnotation; but it is " + as[1].getAnnotationType(), as[1].getAnnotationType() final ElementValuePair[] vals = as[0].getElementValuePairs(); final ElementValuePair nvp = vals[0]; assertTrue( "Name of element in SimpleAnnotation should be 'id' but it is " + nvp.getNameString(), nvp.getNameString().equals("id")); final ElementValue ev = nvp.getValue(); assertTrue("Type of element value should be int but it is " + ev.getElementValueType(), ev.getElementValueType() == ElementValue.PRIMITIVE_INT); assertTrue("Value of element should be 4 but it is " + ev.stringifyValue(), ev.stringifyValue().equals("4")); assertTrue(createTestdataFile("HelloWorld.class").delete());
assertTrue( "Prior to dumping, main method should have 1 annotation but has " + i, i == 1); assertTrue("JavaClass should say 1 annotation on main method but says " + i, i == 1); final ClassGen cg2 = new ClassGen(jc2); assertTrue("The main 'Method' should have one annotation but has " + i, i == 1); final MethodGen mg = new MethodGen(m[0], cg2.getClassName(), cg2 assertTrue("The main 'MethodGen' should have one annotation but has " + i, i == 1); assertTrue(wipe("temp1", "HelloWorld.class"));
/** * Load a class in and modify it with a new attribute - A ComplexAnnotation * annotation */ public void testModifyingClasses2() throws ClassNotFoundException { final JavaClass jc = getTestClass(PACKAGE_BASE_NAME+".data.SimpleAnnotatedClass"); final ClassGen cgen = new ClassGen(jc); final ConstantPoolGen cp = cgen.getConstantPool(); cgen.addAnnotationEntry(createCombinedAnnotation(cp)); assertTrue("Should now have two annotations but has " + cgen.getAnnotationEntries().length, cgen .getAnnotationEntries().length == 2); dumpClass(cgen, "SimpleAnnotatedClass.class"); final JavaClass jc2 = getClassFrom(".", "SimpleAnnotatedClass"); jc2.getAnnotationEntries(); assertTrue(wipe("SimpleAnnotatedClass.class")); // System.err.println(jc2.toString()); }
assertTrue("The 'Method' should have one annotations but has " + mainMethod1.getAnnotationEntries().length, mainMethod1 .getAnnotationEntries().length == 1); final MethodGen mainMethod2 = new MethodGen(mainMethod1, cg2.getClassName(), cg2.getConstantPool()); assertTrue("The 'MethodGen' should have one annotations but has " + mainMethod2.getAnnotationEntries().length, mainMethod2 .getAnnotationEntries().length == 1); final Method mainMethod3 = cg3.getMethods()[1]; final int i = mainMethod3.getAnnotationEntries().length; assertTrue("The 'Method' should now have two annotations but has " + i, i == 2); assertTrue(wipe("temp2", "HelloWorld.class")); assertTrue(wipe("temp3", "HelloWorld.class"));