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

Add or remove imports #10

Merged
merged 3 commits into from
Aug 11, 2023
Merged
Changes from 1 commit
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 @@ -239,10 +239,10 @@ public void visitClassDef(JCTree.JCClassDecl classDecl) {
recipe.append(" return new JavaVisitor<ExecutionContext>() {\n");
for (Map.Entry<String, JCTree.JCMethodDecl> entry : befores.entrySet()) {
recipe.append(" final JavaTemplate " + entry.getKey() + " = JavaTemplate.compile(this, \"" + entry.getKey() + "\", "
+ toLambda(entry.getValue()) + ").build();\n");
+ toLambda(entry.getValue()) + ").build();\n");
}
recipe.append(" final JavaTemplate " + after + " = JavaTemplate.compile(this, \"" + after + "\", "
+ toLambda(descriptor.afterTemplate) + ").build();\n");
+ toLambda(descriptor.afterTemplate) + ").build();\n");
recipe.append("\n");

List<String> lstTypes = LST_TYPE_MAP.get(getType(descriptor.beforeTemplates.get(0)));
Expand All @@ -257,6 +257,17 @@ public void visitClassDef(JCTree.JCClassDecl classDecl) {
recipe.append(" return elem;\n");
recipe.append(" }\n");
}

for (String import_ : imports) {
recipe.append(" maybeRemoveImport(\"" + import_ + "\");\n");
recipe.append(" maybeAddImport(\"" + import_ + "\");\n");
}
for (String import_ : staticImports) {
recipe.append(" maybeRemoveImport(\"" + import_ + "\");\n");
int dot = import_.lastIndexOf('.');
recipe.append(" maybeAddImport(\"" + import_.substring(0, dot) + "\", \"" + import_.substring(dot + 1) + "\");\n");
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was surprised we don't seem to separate imports, even though they end up OK into each of the before/after templates. That meant here I've had to both add and remove them maybe; which was good enough to get working, but not something that I'd be confident relying on.


recipe.append(" JavaTemplate.Matcher matcher;\n");
String predicate = befores.keySet().stream().map(b -> "(matcher = " + b + ".matcher(getCursor())).find()").collect(Collectors.joining(" || "));
recipe.append(" if (" + predicate + ") {\n");
Expand Down Expand Up @@ -494,7 +505,7 @@ boolean validate(JCTree tree) {
@Override
public void visitIdent(JCTree.JCIdent jcIdent) {
if (jcIdent.sym != null
&& jcIdent.sym.packge().getQualifiedName().contentEquals("com.google.errorprone.refaster")) {
&& jcIdent.sym.packge().getQualifiedName().contentEquals("com.google.errorprone.refaster")) {
processingEnv.getMessager().printMessage(Kind.NOTE, jcIdent.type.tsym.getQualifiedName() + " is not supported", template.sym);
valid = false;
}
Expand Down Expand Up @@ -534,16 +545,16 @@ private static List<JCTree.JCAnnotation> getTemplateAnnotations(MethodTree metho
for (AnnotationTree annotation : method.getModifiers().getAnnotations()) {
Tree type = annotation.getAnnotationType();
if (type.getKind() == Tree.Kind.IDENTIFIER && ((JCTree.JCIdent) type).sym != null
&& typePredicate.test(((JCTree.JCIdent) type).sym.getQualifiedName().toString())) {
&& typePredicate.test(((JCTree.JCIdent) type).sym.getQualifiedName().toString())) {
result.add((JCTree.JCAnnotation) annotation);
} else if (type.getKind() == Tree.Kind.IDENTIFIER && ((JCTree.JCAnnotation) annotation).attribute != null
&& ((JCTree.JCAnnotation) annotation).attribute.type instanceof Type.ClassType
&& ((JCTree.JCAnnotation) annotation).attribute.type.tsym != null
&& typePredicate.test(((JCTree.JCAnnotation) annotation).attribute.type.tsym.getQualifiedName().toString())) {
&& ((JCTree.JCAnnotation) annotation).attribute.type instanceof Type.ClassType
&& ((JCTree.JCAnnotation) annotation).attribute.type.tsym != null
&& typePredicate.test(((JCTree.JCAnnotation) annotation).attribute.type.tsym.getQualifiedName().toString())) {
result.add((JCTree.JCAnnotation) annotation);
} else if (type.getKind() == Tree.Kind.MEMBER_SELECT && type instanceof JCTree.JCFieldAccess
&& ((JCTree.JCFieldAccess) type).sym != null
&& typePredicate.test(((JCTree.JCFieldAccess) type).sym.getQualifiedName().toString())) {
&& ((JCTree.JCFieldAccess) type).sym != null
&& typePredicate.test(((JCTree.JCFieldAccess) type).sym.getQualifiedName().toString())) {
result.add((JCTree.JCAnnotation) annotation);
}
}
Expand All @@ -555,12 +566,12 @@ private static List<JCTree.JCAnnotation> getTemplateAnnotations(VariableTree par
for (AnnotationTree annotation : parameter.getModifiers().getAnnotations()) {
Tree type = annotation.getAnnotationType();
if (type.getKind() == Tree.Kind.IDENTIFIER
&& ((JCTree.JCIdent) type).sym != null
&& typePredicate.test(((JCTree.JCIdent) type).sym.getQualifiedName().toString())) {
&& ((JCTree.JCIdent) type).sym != null
&& typePredicate.test(((JCTree.JCIdent) type).sym.getQualifiedName().toString())) {
result.add((JCTree.JCAnnotation) annotation);
} else if (type.getKind() == Tree.Kind.MEMBER_SELECT && type instanceof JCTree.JCFieldAccess
&& ((JCTree.JCFieldAccess) type).sym != null
&& typePredicate.test(((JCTree.JCFieldAccess) type).sym.getQualifiedName().toString())) {
&& ((JCTree.JCFieldAccess) type).sym != null
&& typePredicate.test(((JCTree.JCFieldAccess) type).sym.getQualifiedName().toString())) {
result.add((JCTree.JCAnnotation) annotation);
}
}
Expand Down Expand Up @@ -611,8 +622,8 @@ public JavacProcessingEnvironment getJavacProcessingEnvironment(Object procEnv)
}

processingEnv.getMessager().printMessage(Kind.WARNING, "Can't get the delegate of the gradle " +
"IncrementalProcessingEnvironment. " +
"OpenRewrite's template processor won't work.");
"IncrementalProcessingEnvironment. " +
"OpenRewrite's template processor won't work.");
return null;
}

Expand Down