'Post-compilation removal of annotations from byte code
We are using a library that contains beans that are annotated with JAXB annotations. nothing in the way we use these classes depends on JAXB. in other words, we don't need JAXB and do not depend on the annotations.
However, because the annotations exist, they end up being referenced by other classes that process annotations. This requires me to bundle JAXB in our application, which isn't allowed, because JAXB is in the javax.* package (Android doesn't allow "core libraries" to be included in your application).
So, with this in mind, I'm looking for a way to remove the annotations from the compiled byte code. I know there are utilities for manipulating byte code, but this is quite new to me. How can I get started?
Solution 1:[1]
I have used ByteBuddy library to remove annotations. Unfortunately I was unable to remove annotation with high level api, so I used ASM api. Here is example, how you can remove @Deprecated annotation from field of a class:
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.asm.AsmVisitorWrapper;
import net.bytebuddy.description.field.FieldDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.jar.asm.AnnotationVisitor;
import net.bytebuddy.jar.asm.FieldVisitor;
import net.bytebuddy.jar.asm.Opcodes;
import net.bytebuddy.jar.asm.Type;
import net.bytebuddy.matcher.ElementMatchers;
import java.lang.annotation.Annotation;
import java.util.Arrays;
public class Test {
public static class Foo {
@Deprecated
public Integer bar;
}
public static void main(String[] args) throws Exception {
System.out.println("Annotations before processing " + getAnnotationsString(Foo.class));
Class<? extends Foo> modifiedClass = new ByteBuddy()
.redefine(Foo.class)
.visit(new AsmVisitorWrapper.ForDeclaredFields()
.field(ElementMatchers.isAnnotatedWith(Deprecated.class),
new AsmVisitorWrapper.ForDeclaredFields.FieldVisitorWrapper() {
@Override
public FieldVisitor wrap(TypeDescription instrumentedType,
FieldDescription.InDefinedShape fieldDescription,
FieldVisitor fieldVisitor) {
return new FieldVisitor(Opcodes.ASM5, fieldVisitor) {
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
if (Type.getDescriptor(Deprecated.class).equals(desc)) {
return null;
}
return super.visitAnnotation(desc, visible);
}
};
}
}))
// can't use the same name, because Test$Foo is already loaded
.name("Test$Foo1")
.make()
.load(Test.class.getClassLoader())
.getLoaded();
System.out.println("Annotations after processing " + getAnnotationsString(modifiedClass));
}
private static String getAnnotationsString(Class<? extends Foo> clazz) throws NoSuchFieldException {
Annotation[] annotations = clazz.getDeclaredField("bar").getDeclaredAnnotations();
return Arrays.toString(annotations);
}
}
Solution 2:[2]
ProGuard will also do this, in addition to obfuscating your code.
Solution 3:[3]
There is an additional AntTask Purge Annotation References Ant Task, which
Purge references to annotations out of the java bytecode/classfiles (remove the @Anno tag from annotated elements). Now you can use annotations to check constellations in bytecode after compilation but remove the used annos before releasing the jars.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Nikolay Elenkov |
| Solution 3 | oers |
