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 debugging support for annotation processors #575

Merged
merged 1 commit into from
Aug 23, 2024
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 @@ -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);
Comment on lines +72 to +74
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was logging an object without a toString() method, which made it impossible to expect a specific value in the tests because the default Object#toString prints a memory address. By printing the name instead, we have a reliable thing to check for in tests, as well as prettier debug output.


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 @@
})
@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 @@
})
@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 @@
@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 @@
})
@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 @@
})
@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 @@
})
@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 @@
})
@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,10 +172,12 @@
})
@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

// issue-526
@Inline(
Expand All @@ -181,7 +191,10 @@
})
@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
Loading