public static void main(String[] args) throws Exception { System.out.println(F.class.getField("a").getModifiers()); JavaClass aClass = Repository.lookupClass(F.class); ClassGen aGen = new ClassGen(aClass); for (Field field : aGen.getFields()) { if (field.getName().equals("a")) { int mods = field.getModifiers(); field.setModifiers(mods | Modifier.FINAL); } } final byte[] classBytes = aGen.getJavaClass().getBytes(); ClassLoader cl = new ClassLoader(null) { @Override protected synchronized Class<?> findClass(String name) throws ClassNotFoundException { return defineClass("F", classBytes, 0, classBytes.length); } }; Class<?> fWithoutDeprecated = cl.loadClass("F"); System.out.println(fWithoutDeprecated.getField("a").getModifiers()); }
/** * @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); }
/** * @return the (finally) built up Java class object. */ public JavaClass getJavaClass() { int[] interfaces = getInterfaces(); Field[] fields = getFields(); Method[] methods = getMethods(); Attribute[] attributes = getAttributes(); // Must be last since the above calls may still add something to it ConstantPool cp = this.cp.getFinalConstantPool(); return new JavaClass(class_name_index, superclass_name_index, file_name, major, minor, access_flags, cp, interfaces, fields, methods, attributes); }
final JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.AnnotatedFields"); final ClassGen clg = new ClassGen(clazz); Field f = clg.getFields()[0]; if (dbg) { System.err.println("Field in freshly constructed class is: " + f); f = clg.getFields()[1]; // there are two fields in the class, removing