Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE-179] Improve message regarding duplicated annotated constructors #180

Closed
wants to merge 4 commits into from

Conversation

MRamonLeon
Copy link
Contributor

@MRamonLeon MRamonLeon commented Feb 25, 2020

Fixes #179

This PR improves the message when you have two annotated constructors in the same class + tests.

The error now is not descriptive:

[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] javax.annotation.processing.FilerException: Attempt to reopen a file for path /...ClassXXX.stapler
  	at com.sun.tools.javac.processing.JavacFiler.checkFileReopening(JavacFiler.java:535)

@jglick
Copy link
Member

jglick commented Feb 25, 2020

Try something like

diff --git core/src/main/java/org/kohsuke/stapler/jsr269/ConstructorProcessor.java core/src/main/java/org/kohsuke/stapler/jsr269/ConstructorProcessor.java
index 335074db3..c40b32d03 100644
--- core/src/main/java/org/kohsuke/stapler/jsr269/ConstructorProcessor.java
+++ core/src/main/java/org/kohsuke/stapler/jsr269/ConstructorProcessor.java
@@ -30,10 +30,15 @@ public class ConstructorProcessor extends AbstractProcessorImpl {
     public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
         try {
             ElementScanner6<Void, Void> scanner = new ElementScanner6<Void, Void>() {
+                boolean written;
                 @Override
                 public Void visitExecutable(ExecutableElement e, Void aVoid) {
                     if(e.getAnnotation(DataBoundConstructor.class)!=null) {
-                        write(e);
+                        if (written) {
+                            processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Only one @DataBoundConstructor is permitted per class", e);
+                        } else {
+                            write(e);
+                        }
                     } else {
                         String javadoc = getJavadoc(e);
                         if(javadoc!=null && javadoc.contains("@stapler-constructor")) {

It fails when annotation is in several inner classes
@MRamonLeon
Copy link
Contributor Author

@jglick This latest approach fails when the annotation is duplicated because of several annotated constructors in inner classes:

[ERROR] /home/rleon/develop/projects/stapler/core/src/test/java/org/kohsuke/stapler/DataBindingTest.java:[132,16] Only one annotated constructor (@DataBoundConstructor annotation or @stapler-constructor javadoc) is permitted per class

@MRamonLeon
Copy link
Contributor Author

Back to catching the exception

@jglick
Copy link
Member

jglick commented Feb 28, 2020

fails when the annotation is duplicated because of several annotated constructors in inner classes

Nested classes you mean. Adding a @DataBoundConstructor to an inner class would be illegal (and, I hope, reported already).

I think this is simply because the processor is being passed source units (typically meaning one top-level class), so boolean writable would need to be something more like Map<Something, Boolean>.

@jglick
Copy link
Member

jglick commented Jun 11, 2020

See #190.

@jglick jglick closed this Jun 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error used when DataBoundConstructor annotation is present more than once is not descriptive
2 participants