Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java custom annotation and dynamic loading

I'm trying to develop ORM for database synchronisation and decided to give Java reflection a go. I have a library that defines Synchronised annotation like this

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
public @interface Synchronised {
    String tableName();
    SynchronisationType synchronisationType();
}

In android project I use this annotation to mark a model class

@Synchronised(tableName="UserAccounts", synchronisationType=SynchronisationType.DownloadOnly)
public class UserAccount {   
    @SynchronisedField(fieldName="CompanyFk")
    private int companyId;
    @SynchronisedField(fieldName="Id")
    private int userId;
    @SynchronisedField(fieldName="Username")
    private String username;
    @SynchronisedField(fieldName="Password")
    private String password;
    @SynchronisedField(fieldName="Salt")
    private String salt;
    @SynchronisedField(fieldName="IsLocked")
    private boolean isLocked;
    @SynchronisedField(fieldName="HasMobileAccess")
    private boolean hasMobileAccess;
}

After some investigation a "loader" method was finally written that allows discovering model classes in current apk. It should retrieve all classes that are marked as "Synchronised", but the issue here is that getAttribute(Synchronised.class) doesn't work. Manually iterating annotations and searching for the attribute (instanceof, etc.) doesn't work either. When debugging I noticed that annotation coming from reflection is actually a proxy (getClass() gives "class Proxy2", while annotation.AnnotationType() returns correct name) - this explains why non of my previous attempts succeeded. When trying to cast directly - a cast exception is thrown (understandably). Basically I'm at a loss here so any idea is welcomed.

Model loading method (in library project):

public static List<Class> getModels(Context context, String packageName)
        throws IOException, URISyntaxException, ClassNotFoundException,
            NameNotFoundException {

        String apkName = context.getPackageManager().getApplicationInfo(packageName, 0).sourceDir;
        DexFile dexFile = new DexFile(apkName);
        //PathClassLoader classLoader = new PathClassLoader(apkName, ClassLoader.getSystemClassLoader());       
        PathClassLoader classLoader = new PathClassLoader(apkName, Thread.currentThread().getContextClassLoader());

        List<Class> classes = new ArrayList<Class>();
        Enumeration<String> entries = dexFile.entries();

        while (entries.hasMoreElements()) {

            String entry = entries.nextElement();
            // only check items that exist in source package and not in libraries, etc.
            if (entry.startsWith(packageName)) {
                Log.d(TAG, "Entry: " + entry);

                Class<?> entryClass = classLoader.loadClass(entry);//dexFile.loadClass(entry, classLoader);
                if (entryClass != null) {
                    Annotation[] annotations = entryClass.getAnnotations();
                    for (Annotation annotation : annotations) {
                        if (annotation instanceof Synchronised) {
                            classes.add(entryClass);
                        }
                    }
                }
            }               
        }

        return classes;     
    }

[SOLUTION] The class loader needs to be chained like this:

PathClassLoader classLoader2 = new PathClassLoader(apkName, Thread.currentThread().getContextClassLoader());
DexClassLoader classLoader = new DexClassLoader(apkName, new ContextWrapper(context).getCacheDir().getAbsolutePath(), null, classLoader2);

If you still have no idea what I'm talking about, thanks for reading this lengthy post (-.

like image 242
Audrius Avatar asked Feb 24 '11 14:02

Audrius


1 Answers

Get just annotation that you are interested in:

Annotation<Synchronised> annotation = entryClass.getAnnotation(Synchronised.class);

Update:

The problem is that DexFile.getClass() apparently returns a proxy. OP has found the anser and updated the question with a solution.

like image 143
Peter Knego Avatar answered Oct 07 '22 00:10

Peter Knego