# Gradle plugin: cannot process well known annotations available from script classpath for classes loaded via URLClassLoader

**URL:** <https://discuss.gradle.org/t/gradle-plugin-cannot-process-well-known-annotations-available-from-script-classpath-for-classes-loaded-via-urlclassloader/1930>\
**Category:** Old Forum Archive\
**Created:** [April 1, 2014, 10:45am UTC](https://discuss.gradle.org/t/gradle-plugin-cannot-process-well-known-annotations-available-from-script-classpath-for-classes-loaded-via-urlclassloader/1930 "2014-04-01T10:45:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![davide\_cavestro](https://avatars.discourse-cdn.com/v4/letter/d/9de0a6/32.png) [@davide\_cavestro](https://discuss.gradle.org/u/davide_cavestro)\
**Post date:** [April 1, 2014, 10:45am UTC](https://discuss.gradle.org/t/gradle-plugin-cannot-process-well-known-annotations-available-from-script-classpath-for-classes-loaded-via-urlclassloader/1930/1 "2014-04-01T10:45:00Z")

</div>

I’m facing a weird behavior trying to port the maven-swagger-plugin to gradle: the plugin code should process some well known annotations (available from gradle script classpath) and test if they are used on the classes contained within the project artifacts (loaded through a URLClassLoader). The problem is that calling ‘‘Class.getAnnotation (Foo)’’ on a class loaded with the ‘‘URLClassLoader’’ returns ‘‘null’’ as the annotation instance seems to be actually a Proxy instance.

Follows an example of a gradle script failing to load the ‘‘Foo’’ annotation

```
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(cl.loadClass(“Foo”)) //would WORK def annotation = barClass.getAnnotation(Foo) //make the assertion FAIL assert annotation
```

The same code would work if the annotation class I’m searching for would be loaded through the ‘‘URLClassLoader’’. The problem is that the actual annotation check is implemented into a 3rd party library (from swagger) and should be executed by the gradle plugin. I’ve tried to share a test case at [https://gist.github.com/davidecavestro/9911631](https://gist.github.com/davidecavestro/9911631) but due to gist limitations I had to flatten file paths (replacing slashes with underscores). However the original test case had this structure

```
. ├── code
1. cd here and do a ‘gradle assemble’ │ ├── bar │ │ ├── build.gradle │ │ └── src │ │

└── main │ │

└── java │ │

└── Bar.java │ ├── build.gradle │ ├── foo │ │ ├── build.gradle │ │ └── src │ │

└── main │ │

└── java │ │

└── Foo.java │ └── settings.gradle ├── failing

1. cd here and simply run ‘gradle’: it fails │ └── build.gradle └── working

2. cd here and simply run ‘gradle’: it works

└── build.gradle
```

---

<div class="post-metadata">

**Author:** ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)\
**Post date:** [April 1, 2014, 11:14am UTC](https://discuss.gradle.org/t/gradle-plugin-cannot-process-well-known-annotations-available-from-script-classpath-for-classes-loaded-via-urlclassloader/1930/2 "2014-04-01T11:14:00Z")

</div>

This doesn’t look like a Gradle related problem to me. Classes loaded via different class loaders will never be considered the same, hence the assertion is expected to fail. Proxies are how annotation classes are implemented in the JDK. Using class loaders to perform code analysis is generally problematic; a better approach is to use a library such as ASM.

---

<div class="post-metadata">

**Author:** ![davide\_cavestro](https://avatars.discourse-cdn.com/v4/letter/d/9de0a6/32.png) [@davide\_cavestro](https://discuss.gradle.org/u/davide_cavestro)\
**Post date:** [April 1, 2014, 12:15pm UTC](https://discuss.gradle.org/t/gradle-plugin-cannot-process-well-known-annotations-available-from-script-classpath-for-classes-loaded-via-urlclassloader/1930/3 "2014-04-01T12:15:00Z")

</div>

I see. I’m going to propose swagger guys to replace the code analysis logic with another implementation possibly based on ASM. Is there in your opinion any workaround I could apply in the meantime, such as loading/running gradle plugin code through that URLClassloader?

---

<div class="post-metadata">

**Author:** ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)\
**Post date:** [April 1, 2014, 12:21pm UTC](https://discuss.gradle.org/t/gradle-plugin-cannot-process-well-known-annotations-available-from-script-classpath-for-classes-loaded-via-urlclassloader/1930/4 "2014-04-01T12:21:00Z")

</div>

You’ll have to load everything from the same class loader, whichever class loader that is.

---

<div class="post-metadata">

**Author:** ![davide\_cavestro](https://avatars.discourse-cdn.com/v4/letter/d/9de0a6/32.png) [@davide\_cavestro](https://discuss.gradle.org/u/davide_cavestro)\
**Post date:** [April 1, 2014, 3:35pm UTC](https://discuss.gradle.org/t/gradle-plugin-cannot-process-well-known-annotations-available-from-script-classpath-for-classes-loaded-via-urlclassloader/1930/5 "2014-04-01T15:35:00Z")

</div>

ok, so invoking ‘‘new URLClassLoader(urls, getClass().getClassLoader())’’ from the gradle plugin is enough to make it share the same annotation class instance with the code analysis logic (reflection based). Many thanks
