Skip to content

Instantly share code, notes, and snippets.

@davidecavestro
Created April 1, 2014 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidecavestro/9911631 to your computer and use it in GitHub Desktop.
Save davidecavestro/9911631 to your computer and use it in GitHub Desktop.
Urlclassloader and proxied annotations in gradle
apply plugin: 'java'
dependencies {
compile project (':foo')
}
apply plugin: 'java'
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Foo {
}
include 'foo'
include 'bar'
buildscript {
repositories {
flatDir dirs: ['../code/foo/build/libs']
}
dependencies {
classpath (':foo:')
}
}
import java.lang.annotation.Annotation
ClassLoader cl = new URLClassLoader(['../code/foo/build/libs/foo.jar', '../code/bar/build/libs/bar.jar'].collect {file(it).toURI().toURL()}.toArray (new URL[0]));
Class<?> barClass = cl.loadClass("Bar");
Annotation[] annotations = barClass.getAnnotations();
annotations.each {Annotation annotation->
logger.lifecycle ("Annotations contains: "+annotation.annotationType());
}
def annotation = barClass.getAnnotation(Foo) //FAILS
assert annotation
import java.lang.annotation.Annotation
ClassLoader cl = new URLClassLoader(['../code/foo/build/libs/foo.jar', '../code/bar/build/libs/bar.jar'].collect {file(it).toURI().toURL()}.toArray (new URL[0]));
Class<?> barClass = cl.loadClass("Bar");
Annotation[] annotations = barClass.getAnnotations();
annotations.each {Annotation annotation->
logger.lifecycle ("Annotations contains: "+annotation.annotationType());
}
def annotation = barClass.getAnnotation(cl.loadClass("Foo")) //WORKS
assert annotation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment