-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Found another ECJ Annotation Processor Bug
- Loading branch information
Showing
10 changed files
with
180 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ remotejdk* | |
zulu* | ||
*.jar | ||
*.tar.gz | ||
bin/ | ||
bin*/ | ||
src-gen/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
-d | ||
bin-proc | ||
-s | ||
src-gen | ||
-verbose | ||
--release | ||
11 | ||
src-proc/sample/processor/api/GeneratedResourceFamilyDefinition.java | ||
src-proc/sample/processor/api/WrapperDefinition.java | ||
src-proc/sample/processor/api/ResourceDefinition.java | ||
src-proc/sample/processor/GeneratorAnnotationProcessor.java |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
-d | ||
bin | ||
-s | ||
src-gen | ||
-verbose | ||
--release | ||
11 | ||
-processorpath | ||
bin-proc | ||
-processor | ||
sample.processor.GeneratorAnnotationProcessor | ||
-classpath | ||
bin-proc | ||
src/sample/processor/usage/testing/ITestResource.java | ||
src/sample/processor/usage/checks/ISomeChecks.java | ||
src/sample/processor/usage/MyResources.java |
72 changes: 72 additions & 0 deletions
72
ecj-test/src-proc/sample/processor/GeneratorAnnotationProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package sample.processor; | ||
|
||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.util.Set; | ||
|
||
import javax.annotation.processing.AbstractProcessor; | ||
import javax.annotation.processing.RoundEnvironment; | ||
import javax.annotation.processing.SupportedAnnotationTypes; | ||
import javax.annotation.processing.SupportedSourceVersion; | ||
import javax.lang.model.SourceVersion; | ||
import javax.lang.model.element.Element; | ||
import javax.lang.model.element.PackageElement; | ||
import javax.lang.model.element.TypeElement; | ||
import javax.tools.JavaFileObject; | ||
|
||
import sample.processor.api.GeneratedResourceFamilyDefinition; | ||
|
||
@SupportedAnnotationTypes({ "sample.processor.api.GeneratedResourceFamilyDefinition", | ||
"sample.processor.api.WrapperDefinition", "sample.processor.api.ResourceDefinition" }) | ||
@SupportedSourceVersion(SourceVersion.RELEASE_8) | ||
public class GeneratorAnnotationProcessor extends AbstractProcessor { | ||
|
||
@Override | ||
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { | ||
|
||
for (TypeElement annotation : annotations) { | ||
Set<? extends Element> annotatedElements = roundEnv.getElementsAnnotatedWith(annotation); | ||
|
||
for (Element element : annotatedElements) { | ||
GeneratedResourceFamilyDefinition annotation2 = element | ||
.getAnnotation(GeneratedResourceFamilyDefinition.class); | ||
if (annotation2 != null) { | ||
String wrapperClassName = annotation2.wrapper().className(); | ||
if (wrapperClassName == null || wrapperClassName.trim().isEmpty()) | ||
continue; | ||
|
||
try { | ||
writeWrapperFile(wrapperClassName, (TypeElement) element); | ||
} catch (IOException e) { | ||
throw new IllegalStateException("Unable to generate output. " + e.getMessage(), e); | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private void writeWrapperFile(String simpleClassName, TypeElement annotatedElement) throws IOException { | ||
String packageName = ((PackageElement) annotatedElement.getEnclosingElement()).getQualifiedName() + ".wrappers"; | ||
|
||
String wrapperClassName = packageName + "." + simpleClassName; | ||
JavaFileObject builderFile = processingEnv.getFiler().createSourceFile(wrapperClassName, annotatedElement); | ||
|
||
try (PrintWriter out = new PrintWriter(builderFile.openWriter())) { | ||
|
||
out.print("package "); | ||
out.print(packageName); | ||
out.println(";"); | ||
out.println(); | ||
|
||
out.print("public class "); | ||
out.print(simpleClassName); | ||
out.println(" {"); | ||
out.println(); | ||
|
||
out.println("}"); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
ecj-test/src-proc/sample/processor/api/GeneratedResourceFamilyDefinition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package sample.processor.api; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.TYPE}) | ||
public @interface GeneratedResourceFamilyDefinition { | ||
|
||
Class<?> feature(); | ||
|
||
WrapperDefinition wrapper() default @WrapperDefinition(className=""); | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
ecj-test/src-proc/sample/processor/api/ResourceDefinition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package sample.processor.api; | ||
|
||
public @interface ResourceDefinition { | ||
|
||
Class<?> family(); | ||
|
||
String[] checks() default {}; | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
ecj-test/src-proc/sample/processor/api/WrapperDefinition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package sample.processor.api; | ||
|
||
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 WrapperDefinition { | ||
|
||
/** | ||
* name of the wrapper class to generate. | ||
*/ | ||
String className(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package sample.processor.usage; | ||
|
||
import sample.processor.api.GeneratedResourceFamilyDefinition; | ||
import sample.processor.api.WrapperDefinition; | ||
import sample.processor.usage.testing.ITestResource; | ||
|
||
@GeneratedResourceFamilyDefinition(wrapper = @WrapperDefinition(className = "MyResourceWrapperForTesting"), feature = ITestResource.class) | ||
public class MyResources { | ||
|
||
public void doSomething() { | ||
// whatever | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package sample.processor.usage.checks; | ||
|
||
public interface ISomeChecks { | ||
public final static String CHECK_FOO = "Is Foo Present"; | ||
public final static String CHECK_BAR = "Is Bar Absent"; | ||
} |
19 changes: 19 additions & 0 deletions
19
ecj-test/src/sample/processor/usage/testing/ITestResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package sample.processor.usage.testing; | ||
|
||
import sample.processor.api.ResourceDefinition; | ||
import sample.processor.usage.MyResources; | ||
import sample.processor.usage.checks.ISomeChecks; | ||
import sample.processor.usage.wrappers.MyResourceWrapperForTesting; | ||
|
||
/** | ||
* A Test resource | ||
*/ | ||
@ResourceDefinition(family = MyResources.class, checks = { ISomeChecks.CHECK_FOO }) | ||
public interface ITestResource { | ||
|
||
/** | ||
* For testing use the generated {@link MyResourceWrapperForTesting} | ||
*/ | ||
String TEST_NAME = "foobar"; | ||
|
||
} |