title: Java custom annotation and dynamic loading
body: <p>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</p>
  
  <pre><code>@Retention(RetentionPolicy.RUNTIME)
  @Target(ElementType.TYPE)
  @Inherited
  public @interface Synchronised {
      String tableName();
      SynchronisationType synchronisationType();
  }
  </code></pre>
  
  <p>In android project I use this annotation to mark a model class</p>
  
  <pre><code>@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;
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>Model loading method (in library project):</p>
  
  <pre><code>public static List&lt;Class&gt; 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&lt;Class&gt; classes = new ArrayList&lt;Class&gt;();
          Enumeration&lt;String&gt; 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&lt;?&gt; 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;     
      }
  </code></pre>
  
  <p>[SOLUTION] The class loader needs to be chained like this:</p>
  
  <pre><code>PathClassLoader classLoader2 = new PathClassLoader(apkName, Thread.currentThread().getContextClassLoader());
  DexClassLoader classLoader = new DexClassLoader(apkName, new ContextWrapper(context).getCacheDir().getAbsolutePath(), null, classLoader2);
  </code></pre>
  
  <p>If you still have no idea what I'm talking about, thanks for reading this lengthy post (-.</p>