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

Print messages once, as to not flood console #36

Merged
merged 5 commits into from
Dec 12, 2023
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 @@ -33,7 +33,6 @@
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.NestingKind;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic.Kind;
Expand Down Expand Up @@ -126,10 +125,6 @@ public void visitClassDef(JCTree.JCClassDecl classDecl) {
.collect(Collectors.toList());
JCTree.JCClassDecl copy = treeMaker.ClassDef(classDecl.mods, classDecl.name, classDecl.typarams, classDecl.extending, classDecl.implementing, com.sun.tools.javac.util.List.from(membersWithoutConstructor));

String classNameIncludingOuter = descriptor.classDecl.sym.packge().toString().isEmpty() ?
descriptor.classDecl.sym.className() :
descriptor.classDecl.sym.className().substring(descriptor.classDecl.sym.packge().toString().length() + 1);

String templateFqn = classDecl.sym.fullname.toString() + "Recipe";
String templateCode = copy.toString().trim();

Expand Down Expand Up @@ -685,7 +680,7 @@ private TemplateDescriptor validate(Context context, JCCompilationUnit cu) {
for (JCTree member : classDecl.getMembers()) {
if (member instanceof JCTree.JCMethodDecl && !beforeTemplates.contains(member) && member != afterTemplate) {
for (JCTree.JCAnnotation annotation : getTemplateAnnotations(((JCTree.JCMethodDecl) member), UNSUPPORTED_ANNOTATIONS::contains)) {
processingEnv.getMessager().printMessage(Kind.NOTE, "The @" + annotation.annotationType + " is currently not supported", ((JCTree.JCMethodDecl) member).sym);
printNoteOnce("The @" + annotation.annotationType + " is currently not supported", ((JCTree.JCMethodDecl) member));
valid = false;
}
}
Expand All @@ -706,22 +701,22 @@ private boolean validateTemplateMethod(JCTree.JCMethodDecl template) {
boolean valid = true;
// TODO: support all Refaster method-level annotations
for (JCTree.JCAnnotation annotation : getTemplateAnnotations(template, UNSUPPORTED_ANNOTATIONS::contains)) {
processingEnv.getMessager().printMessage(Kind.NOTE, "The @" + annotation.annotationType + " is currently not supported", template.sym);
printNoteOnce("The @" + annotation.annotationType + " is currently not supported", template);
valid = false;
}
// TODO: support all Refaster parameter-level annotations
for (JCTree.JCVariableDecl parameter : template.getParameters()) {
for (JCTree.JCAnnotation annotation : getTemplateAnnotations(parameter, UNSUPPORTED_ANNOTATIONS::contains)) {
processingEnv.getMessager().printMessage(Kind.NOTE, "The @" + annotation.annotationType + " annotation is currently not supported", template.sym);
printNoteOnce("The @" + annotation.annotationType + " annotation is currently not supported", template);
valid = false;
}
if (parameter.vartype instanceof ParameterizedTypeTree || parameter.vartype.type instanceof Type.TypeVar) {
processingEnv.getMessager().printMessage(Kind.NOTE, "Generics are currently not supported", template.sym);
printNoteOnce("Generics are currently not supported", template);
valid = false;
}
}
if (template.restype instanceof ParameterizedTypeTree || template.restype.type instanceof Type.TypeVar) {
processingEnv.getMessager().printMessage(Kind.NOTE, "Generics are currently not supported", template.sym);
printNoteOnce("Generics are currently not supported", template);
valid = false;
}
valid &= new TreeScanner() {
Expand All @@ -736,7 +731,7 @@ boolean validate(JCTree tree) {
public void visitIdent(JCTree.JCIdent jcIdent) {
if (jcIdent.sym != null
&& jcIdent.sym.packge().getQualifiedName().contentEquals("com.google.errorprone.refaster")) {
processingEnv.getMessager().printMessage(Kind.NOTE, jcIdent.type.tsym.getQualifiedName() + " is not supported", template.sym);
printNoteOnce(jcIdent.type.tsym.getQualifiedName() + " is not supported", template);
valid = false;
}
}
Expand Down Expand Up @@ -770,6 +765,14 @@ private boolean resolve(Context context, JCCompilationUnit cu) {

}

private final Set<String> printedMessages = new HashSet<>();

private void printNoteOnce(String message, JCTree.JCMethodDecl template) {
if (printedMessages.add(message)) {
processingEnv.getMessager().printMessage(Kind.NOTE, message, template.sym);
}
}

private static List<JCTree.JCAnnotation> getTemplateAnnotations(MethodTree method, Predicate<String> typePredicate) {
List<JCTree.JCAnnotation> result = new ArrayList<>();
for (AnnotationTree annotation : method.getModifiers().getAnnotations()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ void generateRecipe(String recipeName) {
.withClasspath(classpath())
.compile(JavaFileObjects.forResource("refaster/" + recipeName + ".java"));
assertThat(compilation).succeeded();
assertThat(compilation).hadNoteCount(0);
compilation.generatedSourceFiles().forEach(System.out::println);
assertThat(compilation)
.generatedSourceFile("foo/" + recipeName + "Recipe")
Expand All @@ -55,17 +56,18 @@ void generateRecipe(String recipeName) {

@ParameterizedTest
@ValueSource(strings = {
// "ShouldSupportNestedClasses",
"ShouldSupportNestedClasses",
"ShouldAddImports",
// "MultipleDereferences",
// "Matching",
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to disable these tests?

"MultipleDereferences",
"Matching",
})
void nestedRecipes(String recipeName) {
Compilation compilation = javac()
.withProcessors(new RefasterTemplateProcessor())
.withClasspath(classpath())
.compile(JavaFileObjects.forResource("refaster/" + recipeName + ".java"));
assertThat(compilation).succeeded();
assertThat(compilation).hadNoteCount(0);
compilation.generatedSourceFiles().forEach(System.out::println);
assertThat(compilation) // Recipes (plural)
.generatedSourceFile("foo/" + recipeName + "Recipes")
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/refaster/MatchingRecipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import static org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor.EmbeddingOption.*;


public final class MatchingRecipes extends Recipe {
public class MatchingRecipes extends Recipe {
@Override
public String getDisplayName() {
return "Static analysis";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import java.io.IOException;
import java.nio.file.Path;

public final class MultipleDereferencesRecipes extends Recipe {
public class MultipleDereferencesRecipes extends Recipe {
@Override
public String getDisplayName() {
return "`MultipleDereferences` Refaster recipes";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import static org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor.EmbeddingOption.*;


public final class ShouldSupportNestedClassesRecipes extends Recipe {
public class ShouldSupportNestedClassesRecipes extends Recipe {
@Override
public String getDisplayName() {
return "`ShouldSupportNestedClasses` Refaster recipes";
Expand Down Expand Up @@ -95,7 +95,7 @@ public J visitBinary(J.Binary elem, ExecutionContext ctx) {
}

@NonNullApi
static class AnotherClassRecipe extends Recipe {
public static class AnotherClassRecipe extends Recipe {

@Override
public String getDisplayName() {
Expand Down