Problem with annotation

chdw
2006-12-04
2012-12-17
  • chdw

    chdw - 2006-12-04

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

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

    @MyAnno("KKK")
    public class MyClass {
        @MyAnno("FFF")
        public void c() {
        }
    }
    //Test.java
    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:

    MyAnno:KKK
    MyAnno:FFF

    after obfuscate with pro
    //app.pro
    -injars  a.jar
    -outjars b.jar
    -libraryjars <java.home>/lib/rt.jar
    -keepclasseswithmembers public class * {
        public static void main(java.lang.String[]);
    }
    -printseeds
    -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 java.io.Serializable {
        static final long serialVersionUID;
        private void writeObject(java.io.ObjectOutputStream);
        private void readObject(java.io.ObjectInputStream);
        java.lang.Object writeReplace();
        java.lang.Object readResolve();
    }
    //end pro

    Program will output:
    MyAnno:KKK
    NULL

    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.

      Eric.

       
  • 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(Ejb3Configuration.java:265)
            at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:125)
            at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:52)
            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(CollectionBinder.java:521)
            at org.hibernate.cfg.annotations.CollectionBinder.bind(CollectionBinder.java:417)
            at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1614)
            at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:754)
            at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:546)
            at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:291)
            at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1148)
            at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1226)
            at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:173)
            at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:854)
            at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:191)
            at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:253)
            … 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