From 0a956cd659d0cd4352a4a0204f7a25b7c07fc798 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Fri, 11 Aug 2023 20:53:12 +0200 Subject: [PATCH] Add or remove imports (#10) * Add or remove imports * Skip imports of java.lang --- .../template/RefasterTemplateProcessor.java | 47 +++++++++++++------ 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/openrewrite/java/template/RefasterTemplateProcessor.java b/src/main/java/org/openrewrite/java/template/RefasterTemplateProcessor.java index b0f82f3c..73841af5 100644 --- a/src/main/java/org/openrewrite/java/template/RefasterTemplateProcessor.java +++ b/src/main/java/org/openrewrite/java/template/RefasterTemplateProcessor.java @@ -239,10 +239,10 @@ public void visitClassDef(JCTree.JCClassDecl classDecl) { recipe.append(" return new JavaVisitor() {\n"); for (Map.Entry 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 lstTypes = LST_TYPE_MAP.get(getType(descriptor.beforeTemplates.get(0))); @@ -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"); @@ -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; } @@ -534,16 +551,16 @@ private static List 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); } } @@ -555,12 +572,12 @@ private static List 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); } } @@ -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; }