Problem with annotation

  • chdw

    chdw - 2006-12-04

    ProGuard will clear method's annotation?
    I wrote three classes:
    package chdw;

    import java.lang.annotation.*;
    @Target({ElementType.METHOD, ElementType.TYPE})
    public @interface MyAnno {
        public String value();
    package chdw;

    public class MyClass {
        public void c() {
    package chdw;

    public class Test {
        public static void main(String[] args) throws Exception {
            MyAnno anno = MyClass.class.getAnnotation(MyAnno.class);
            System.out.println(anno == null ? "NULL" : "MyAnno:" + anno.value());
            java.lang.reflect.Method m = MyClass.class.getMethods()[0];
            anno = m.getAnnotation(MyAnno.class);
            System.out.println(anno == null ? "NULL" : "MyAnno:" + anno.value());

    Before obfuscate, Program will output:


    after obfuscate with pro
    -injars  a.jar
    -outjars b.jar
    -libraryjars <java.home>/lib/rt.jar
    -keepclasseswithmembers public class * {
        public static void main(java.lang.String[]);
    -keepattributes Signature,*Annotation*
    -keepclasseswithmembernames class * {
        native <methods>;
    -keepclassmembers class * extends java.lang.Enum {
        public static **[] values();
        public static ** valueOf(java.lang.String);
    -keepclassmembers class * implements {
        static final long serialVersionUID;
        private void writeObject(;
        private void readObject(;
        java.lang.Object writeReplace();
        java.lang.Object readResolve();
    //end pro

    Program will output:

    It seem proguard ignore method's annotations.

    How can I fix this problem?

    • Eric Lafortune

      Eric Lafortune - 2006-12-04

      Dear chdw,

      You are trying to retrieve the method "void c()" by introspection. ProGuard can't detect this; it thinks the method is not used in the code and removes it in the shrinking step. As a result, the first method of the processed version of MyClass is "hashCode", which doesn't have any annotations.

      In this case, the obfuscated program works if you explicitly keep the method "void c()", or if you switch off shrinking (with the option "-dontshrink"). In general, introspection and obfuscation don't mix well without special care.


  • Omer

    Omer - 2012-10-13

    A have proble with collection Annotation du ring run time

    avax.persistence.PersistenceException:  Unable to configure EntityManagerFactory
            at org.hibernate.ejb.Ejb3Configuration.configure(
            at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(
            at javax.persistence.Persistence.createEntityManagerFactory(
            at com.baotec.nkap.classic.swing.jr.c(Unknown Source)
            at com.baotec.nkap.a.a.e.a(Unknown Source)
            at com.baotec.nkap.a.a.a.a(Unknown Source)
            at com.baotec.pattern.a.a.<init>(Unknown Source)
            at com.baotec.nkap.a.a.a.<init>(Unknown Source)
            at com.baotec.nkap.classic.swing.ClassicAdminApplication.<init>(Unknown Source)
            at com.baotec.nkap.classic.swing.ClassicAdminApplication.main(Unknown Source)
    Caused by: org.hibernate.AnnotationException: Collection has neither generic type or OneToMany.targetEntity() defined: com.baotec.pattern.accounting.domain.Account.f
            at org.hibernate.cfg.annotations.CollectionBinder.getCollectionType(
            at org.hibernate.cfg.annotations.CollectionBinder.bind(
            at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(
            at org.hibernate.cfg.AnnotationBinder.bindClass(
            at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(
            at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(
            at org.hibernate.cfg.Configuration.buildMappings(
            at org.hibernate.ejb.Ejb3Configuration.buildMappings(
            at org.hibernate.ejb.EventListenerConfigurator.configure(
            at org.hibernate.ejb.Ejb3Configuration.configure(
            at org.hibernate.ejb.Ejb3Configuration.configure(
            at org.hibernate.ejb.Ejb3Configuration.configure(
            … 9 more

  • Omer

    Omer - 2012-10-13

    I have used

    -keepattributes RuntimeVisibleAnnotations

    -keep @javax.persistence.* class * {



Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks