/** * @return the (finally) built up Java class object. */ public JavaClass getJavaClass() { final int[] interfaces = getInterfaces(); final Field[] fields = getFields(); final Method[] methods = getMethods(); Attribute[] attributes = null; if (annotation_vec.isEmpty()) { attributes = getAttributes(); } else { // TODO: Sometime later, trash any attributes called 'RuntimeVisibleAnnotations' or 'RuntimeInvisibleAnnotations' final Attribute[] annAttributes = AnnotationEntryGen.getAnnotationAttributes(cp, getAnnotationEntries()); attributes = new Attribute[attribute_vec.size()+annAttributes.length]; attribute_vec.toArray(attributes); System.arraycopy(annAttributes,0,attributes,attribute_vec.size(),annAttributes.length); } // Must be last since the above calls may still add something to it final ConstantPool _cp = this.cp.getFinalConstantPool(); return new JavaClass(class_name_index, superclass_name_index, file_name, major, minor, super.getAccessFlags(), _cp, interfaces, fields, methods, attributes); }
/** * 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); }
/** * 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")); }
/** * 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 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()); }
final ClassGen cgen = new ClassGen(jc); final AnnotationEntryGen[] annotations = cgen.getAnnotationEntries(); assertTrue("Expected one annotation but found " + annotations.length, annotations.length == 1);