Skip to content

Commit

Permalink
Improve debugging support for annotation processors
Browse files Browse the repository at this point in the history
  • Loading branch information
basil committed Aug 22, 2024
1 parent 33d75e0 commit ec4d66c
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ protected String getJavadoc(Element md) {
}

protected void notice(String msg, Element location) {
// IntelliJ flags this as an error. So disabling it for now.
// See http://youtrack.jetbrains.net/issue/IDEA-71822
// processingEnv.getMessager().printMessage(NOTE, msg, location);
processingEnv.getMessager().printMessage(Kind.NOTE, msg, location);
}

protected void writePropertyFile(Properties p, String name) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ private void write(ExecutableElement m) throws IOException {
}

TypeElement t = (TypeElement) m.getEnclosingElement();
FileObject f = createResource(
t.getQualifiedName().toString().replace('.', '/') + "/" + m.getSimpleName() + ".stapler");
notice("Generating " + f, m);
String name = t.getQualifiedName().toString().replace('.', '/') + "/" + m.getSimpleName() + ".stapler";
FileObject f = createResource(name);
notice("Generating " + name, m);

try (OutputStream os = f.openOutputStream()) {
os.write(buf.toString().getBytes(StandardCharsets.UTF_8));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,16 @@
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.karuslabs.elementary.Results;
import com.karuslabs.elementary.junit.JavacExtension;
import com.karuslabs.elementary.junit.annotations.Inline;
import com.karuslabs.elementary.junit.annotations.Options;
import com.karuslabs.elementary.junit.annotations.Processors;
import java.time.Year;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;
import java.util.Set;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

Expand All @@ -36,7 +33,10 @@ class ConstructorProcessorTest {
})
@Test
void basicOutput(Results results) {
assertEquals(Collections.emptyList(), results.diagnostics);
Set<String> diagnostics = results.diagnostics.stream()
.map(d -> d.getMessage(Locale.ENGLISH))
.collect(Collectors.toSet());
assertEquals(Set.of("Generating some/pkg/Stuff.stapler"), diagnostics);
assertEquals(
"{constructor=count,name}",
Utils.normalizeProperties(Utils.getGeneratedResource(results.sources, "some/pkg/Stuff.stapler")));
Expand All @@ -52,7 +52,10 @@ void basicOutput(Results results) {
})
@Test
void preAnnotationCompatibility(Results results) {
assertEquals(Collections.emptyList(), results.diagnostics);
Set<String> diagnostics = results.diagnostics.stream()
.map(d -> d.getMessage(Locale.ENGLISH))
.collect(Collectors.toSet());
assertEquals(Set.of("Generating some/pkg/Stuff.stapler"), diagnostics);
assertEquals(
"{constructor=name,count}",
Utils.normalizeProperties(Utils.getGeneratedResource(results.sources, "some/pkg/Stuff.stapler")));
Expand All @@ -70,7 +73,10 @@ void preAnnotationCompatibility(Results results) {
@Inline(name = "some.pkg.package-info", source = "package some.pkg;")
@Test
void JENKINS_11739(Results results) {
assertEquals(Collections.emptyList(), results.diagnostics);
Set<String> diagnostics = results.diagnostics.stream()
.map(d -> d.getMessage(Locale.ENGLISH))
.collect(Collectors.toSet());
assertEquals(Set.of("Generating some/pkg/Stuff.stapler"), diagnostics);
assertEquals(
"{constructor=count,name}",
Utils.normalizeProperties(Utils.getGeneratedResource(results.sources, "some/pkg/Stuff.stapler")));
Expand All @@ -87,10 +93,10 @@ void JENKINS_11739(Results results) {
})
@Test
void privateConstructor(Results results) {
List<Diagnostic<? extends JavaFileObject>> diagnostics = results.diagnostics;
assertEquals(1, diagnostics.size());
String msg = diagnostics.get(0).getMessage(Locale.ENGLISH);
assertTrue(msg.contains("public"), msg);
Set<String> diagnostics = results.diagnostics.stream()
.map(d -> d.getMessage(Locale.ENGLISH))
.collect(Collectors.toSet());
assertEquals(Set.of("@DataBoundConstructor must be applied to a public constructor"), diagnostics);
}

@Inline(
Expand All @@ -104,10 +110,12 @@ void privateConstructor(Results results) {
})
@Test
void abstractClass(Results results) {
List<Diagnostic<? extends JavaFileObject>> diagnostics = results.diagnostics;
assertEquals(1, diagnostics.size());
String msg = diagnostics.get(0).getMessage(Locale.ENGLISH);
assertTrue(msg.contains("abstract"), msg);
Set<String> diagnostics = results.diagnostics.stream()
.map(d -> d.getMessage(Locale.ENGLISH))
.collect(Collectors.toSet());
assertEquals(
Set.of("@DataBoundConstructor may not be used on an abstract class (only on concrete subclasses)"),
diagnostics);
}

// issue-179
Expand All @@ -123,10 +131,10 @@ void abstractClass(Results results) {
})
@Test
void duplicatedConstructor1(Results results) {
List<Diagnostic<? extends JavaFileObject>> diagnostics = results.diagnostics;
assertEquals(1, diagnostics.size());
String msg = diagnostics.get(0).getMessage(Locale.ENGLISH);
assertTrue(msg.contains(ConstructorProcessor.MESSAGE), msg);
Set<String> diagnostics = results.diagnostics.stream()
.map(d -> d.getMessage(Locale.ENGLISH))
.collect(Collectors.toSet());
assertEquals(Set.of("Generating some/pkg/Stuff.stapler", ConstructorProcessor.MESSAGE), diagnostics);
}

// issue-179
Expand All @@ -145,10 +153,10 @@ void duplicatedConstructor1(Results results) {
})
@Test
void duplicatedConstructor2(Results results) {
List<Diagnostic<? extends JavaFileObject>> diagnostics = results.diagnostics;
assertEquals(1, diagnostics.size());
String msg = diagnostics.get(0).getMessage(Locale.ENGLISH);
assertTrue(msg.contains(ConstructorProcessor.MESSAGE), msg);
Set<String> diagnostics = results.diagnostics.stream()
.map(d -> d.getMessage(Locale.ENGLISH))
.collect(Collectors.toSet());
assertEquals(Set.of("Generating some/pkg/Stuff.stapler", ConstructorProcessor.MESSAGE), diagnostics);
}

// issue-179
Expand All @@ -164,8 +172,10 @@ void duplicatedConstructor2(Results results) {
})
@Test
void duplicatedButNotAnnotatedConstructor(Results results) {
List<Diagnostic<? extends JavaFileObject>> diagnostics = results.diagnostics;
assertEquals(0, diagnostics.size());
Set<String> diagnostics = results.diagnostics.stream()
.map(d -> d.getMessage(Locale.ENGLISH))
.collect(Collectors.toSet());
assertEquals(Set.of("Generating some/pkg/Stuff.stapler"), diagnostics);
}
// TODO nested classes use qualified rather than binary name

Check warning on line 180 in core/src/test/java/org/kohsuke/stapler/jsr269/ConstructorProcessorTest.java

View check run for this annotation

ci.jenkins.io / Open Tasks Scanner

TODO

NORMAL: nested classes use qualified rather than binary name

Expand All @@ -181,7 +191,10 @@ void duplicatedButNotAnnotatedConstructor(Results results) {
})
@Test
void reproducibleBuild(Results results) {
assertEquals(Collections.emptyList(), results.diagnostics);
Set<String> diagnostics = results.diagnostics.stream()
.map(d -> d.getMessage(Locale.ENGLISH))
.collect(Collectors.toSet());
assertEquals(Set.of("Generating some/pkg/Stuff.stapler"), diagnostics);
assertThat(
Utils.getGeneratedResource(results.sources, "some/pkg/Stuff.stapler"),
not(containsString(Integer.toString(Year.now().getValue()))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import com.karuslabs.elementary.junit.annotations.Inline;
import com.karuslabs.elementary.junit.annotations.Options;
import com.karuslabs.elementary.junit.annotations.Processors;
import java.util.Collections;
import java.util.Locale;
import java.util.Set;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.jvnet.hudson.annotation_indexer.AnnotationProcessorImpl;
Expand Down Expand Up @@ -36,7 +38,10 @@ private void assertEqualsCRLF(String s1, String s2) {
})
@Test
void basicOutput(Results results) {
assertEquals(Collections.emptyList(), results.diagnostics);
Set<String> diagnostics = results.diagnostics.stream()
.map(d -> d.getMessage(Locale.ENGLISH))
.collect(Collectors.toSet());
assertEquals(Set.of("Generating some/pkg/Stuff.javadoc"), diagnostics);
assertEqualsCRLF(
"some.pkg.Stuff\n",
Utils.getGeneratedResource(
Expand All @@ -60,7 +65,10 @@ void basicOutput(Results results) {
})
@Test
void noJavadoc(Results results) {
assertEquals(Collections.emptyList(), results.diagnostics);
Set<String> diagnostics = results.diagnostics.stream()
.map(d -> d.getMessage(Locale.ENGLISH))
.collect(Collectors.toSet());
assertEquals(Set.of("Generating some/pkg/Stuff.javadoc"), diagnostics);
assertEqualsCRLF(
"some.pkg.Stuff\n",
Utils.getGeneratedResource(
Expand Down Expand Up @@ -93,7 +101,10 @@ void noJavadoc(Results results) {
})
@Test
void subclassOfExportedBean(Results results) {
assertEquals(Collections.emptyList(), results.diagnostics);
Set<String> diagnostics = results.diagnostics.stream()
.map(d -> d.getMessage(Locale.ENGLISH))
.collect(Collectors.toSet());
assertEquals(Set.of("Generating some/pkg/Super.javadoc"), diagnostics);
/* #7188605: broken in JDK 6u33 + org.jvnet.hudson:annotation-indexer:1.2:
assertEquals("some.pkg.Stuff\n", Utils.getGeneratedResource(results.sources, "META-INF/services/annotations/org.kohsuke.stapler.export.ExportedBean"));
*/
Expand Down Expand Up @@ -125,7 +136,10 @@ void subclassOfExportedBean(Results results) {
})
@Test
void multiple(Results results) {
assertEquals(Collections.emptyList(), results.diagnostics);
Set<String> diagnostics = results.diagnostics.stream()
.map(d -> d.getMessage(Locale.ENGLISH))
.collect(Collectors.toSet());
assertEquals(Set.of("Generating some/pkg/Stuff.javadoc", "Generating some/pkg/MoreStuff.javadoc"), diagnostics);
assertEqualsCRLF(
"some.pkg.MoreStuff\nsome.pkg.Stuff\n",
Utils.getGeneratedResource(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import com.karuslabs.elementary.junit.annotations.Inline;
import com.karuslabs.elementary.junit.annotations.Options;
import com.karuslabs.elementary.junit.annotations.Processors;
import java.util.Collections;
import java.util.Locale;
import java.util.Set;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

Expand All @@ -28,7 +30,12 @@ class QueryParameterAnnotationProcessorTest {
})
@Test
void basicOutput(Results results) {
assertEquals(Collections.emptyList(), results.diagnostics);
Set<String> diagnostics = results.diagnostics.stream()
.map(d -> d.getMessage(Locale.ENGLISH))
.collect(Collectors.toSet());
assertEquals(
Set.of("Generating some/pkg/Stuff/doOneThing.stapler", "Generating some/pkg/Stuff/doAnother.stapler"),
diagnostics);
assertEquals("key", Utils.getGeneratedResource(results.sources, "some/pkg/Stuff/doOneThing.stapler"));
assertEquals("name,address", Utils.getGeneratedResource(results.sources, "some/pkg/Stuff/doAnother.stapler"));
}
Expand Down

0 comments on commit ec4d66c

Please sign in to comment.