Skip to content

Commit

Permalink
Add or remove imports (#10)
Browse files Browse the repository at this point in the history
* Add or remove imports

* Skip imports of java.lang
  • Loading branch information
timtebeek authored Aug 11, 2023
1 parent e120b0d commit 0a956cd
Showing 1 changed file with 32 additions and 15 deletions.
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,23 @@ public void visitClassDef(JCTree.JCClassDecl classDecl) {
recipe.append(" return elem;\n");
recipe.append(" }\n");
}

for (String import_ : imports) {
if (import_.startsWith("java.lang.")) {
continue;
}
recipe.append(" maybeRemoveImport(\"" + import_ + "\");\n");
recipe.append(" maybeAddImport(\"" + import_ + "\");\n");
}
for (String import_ : staticImports) {
if (import_.startsWith("java.lang.")) {
continue;
}
recipe.append(" maybeRemoveImport(\"" + import_ + "\");\n");
int dot = import_.lastIndexOf('.');
recipe.append(" maybeAddImport(\"" + import_.substring(0, dot) + "\", \"" + import_.substring(dot + 1) + "\");\n");
}

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 +511,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 +551,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 +572,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 +628,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

0 comments on commit 0a956cd

Please sign in to comment.