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

Improve message regarding duplicated annotated constructors (II) #190

Merged
merged 6 commits into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import javax.lang.model.element.VariableElement;
import javax.lang.model.util.ElementScanner6;
import java.io.IOException;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import javax.lang.model.element.Modifier;
Expand All @@ -26,18 +27,23 @@
@SupportedAnnotationTypes("*")
@MetaInfServices(Processor.class)
public class ConstructorProcessor extends AbstractProcessorImpl {
/* private */ final static String MESSAGE = "Only one annotated constructor (@DataBoundConstructor annotation or @stapler-constructor javadoc) is permitted per class";

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
try {
ElementScanner6<Void, Void> scanner = new ElementScanner6<Void, Void>() {
Set<Element> enclosingElementsWritten = new HashSet<>();
boolean messagePrinted;

@Override
public Void visitExecutable(ExecutableElement e, Void aVoid) {
if(e.getAnnotation(DataBoundConstructor.class)!=null) {
write(e);
writeOrAddOnlyOneMessage(e);
} else {
String javadoc = getJavadoc(e);
if(javadoc!=null && javadoc.contains("@stapler-constructor")) {
write(e);
writeOrAddOnlyOneMessage(e);
}
}

Expand All @@ -48,6 +54,15 @@ public Void visitExecutable(ExecutableElement e, Void aVoid) {
public Void visitUnknown(Element e, Void aVoid) {
return DEFAULT_VALUE;
}

private void writeOrAddOnlyOneMessage(ExecutableElement e) {
if (enclosingElementsWritten.add(e.getEnclosingElement())) {
write(e);
} else if (!messagePrinted){
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, MESSAGE, e);
messagePrinted = true;
}
}
};

for (Element e : roundEnv.getRootElements()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,56 @@ public class ConstructorProcessorTest {
assertTrue(msg, msg.contains("abstract"));
}

// TODO nested classes use qualified rather than binary name
// TODO behavior when multiple @DataBoundConstructor's specified on a single class - error?
//issue-179
@Test public void duplicatedConstructor1() {
Compilation compilation = new Compilation();
compilation.addSource("some.pkg.Stuff").
addLine("package some.pkg;").
addLine("import org.kohsuke.stapler.DataBoundConstructor;").
addLine("public class Stuff {").
addLine(" @DataBoundConstructor public Stuff() {}").
addLine(" @DataBoundConstructor public Stuff(int i) {}").
addLine("}");
compilation.doCompile(null, "-source", "6");
List<Diagnostic<? extends JavaFileObject>> diagnostics = compilation.getDiagnostics();
assertEquals(1, diagnostics.size());
String msg = diagnostics.get(0).getMessage(Locale.ENGLISH);
assertTrue(msg, msg.contains(ConstructorProcessor.MESSAGE));
}

//issue-179
@Test public void duplicatedConstructor2() {
Compilation compilation = new Compilation();
compilation.addSource("some.pkg.Stuff").
addLine("package some.pkg;").
addLine("import org.kohsuke.stapler.DataBoundConstructor;").
addLine("public class Stuff {").
addLine(" @DataBoundConstructor public Stuff() {}").
addLine(" /**").
addLine(" @stapler-constructor Another constructor").
addLine(" **/").
addLine(" public Stuff(int i) {}").
addLine("}");
compilation.doCompile(null, "-source", "6");
List<Diagnostic<? extends JavaFileObject>> diagnostics = compilation.getDiagnostics();
assertEquals(1, diagnostics.size());
String msg = diagnostics.get(0).getMessage(Locale.ENGLISH);
assertTrue(msg, msg.contains(ConstructorProcessor.MESSAGE));
}

//issue-179
@Test public void duplicatedButNotAnnotatedConstructor() {
Compilation compilation = new Compilation();
compilation.addSource("some.pkg.Stuff").
addLine("package some.pkg;").
addLine("import org.kohsuke.stapler.DataBoundConstructor;").
addLine("public class Stuff {").
addLine(" @DataBoundConstructor public Stuff() {}").
addLine(" public Stuff(int i) {}").
addLine("}");
compilation.doCompile(null, "-source", "6");
List<Diagnostic<? extends JavaFileObject>> diagnostics = compilation.getDiagnostics();
assertEquals(0, diagnostics.size());
}
// TODO nested classes use qualified rather than binary name
}