diff --git a/src/main/java/org/openrewrite/java/migrate/javax/AddScopeToInjectedClass.java b/src/main/java/org/openrewrite/java/migrate/javax/AddScopeToInjectedClass.java index 53967137b0..b8ecf7ee93 100644 --- a/src/main/java/org/openrewrite/java/migrate/javax/AddScopeToInjectedClass.java +++ b/src/main/java/org/openrewrite/java/migrate/javax/AddScopeToInjectedClass.java @@ -52,9 +52,11 @@ public TreeVisitor getScanner(Set injectedTypes) { @Override public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) { J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, ctx); - for (JavaType.Variable variable : cd.getType().getMembers()) { - if (variableTypeRequiresScope(variable)) { - injectedTypes.add(((JavaType.FullyQualified) variable.getType()).getFullyQualifiedName()); + if(cd.getType() != null) { + for (JavaType.Variable variable : cd.getType().getMembers()) { + if (variableTypeRequiresScope(variable)) { + injectedTypes.add(((JavaType.FullyQualified) variable.getType()).getFullyQualifiedName()); + } } } return cd; @@ -85,7 +87,7 @@ public J.CompilationUnit visitCompilationUnit(J.CompilationUnit compilationUnit, for (J.ClassDeclaration aClass : cu.getClasses()) { if (aClass.getType() != null && injectedTypes.contains(aClass.getType().getFullyQualifiedName())) { return (J.CompilationUnit) new AnnotateTypesVisitor(JAVAX_ENTERPRISE_CONTEXT_DEPENDENT) - .visit(cu, injectedTypes, getCursor().getParentTreeCursor()); + .visitNonNull(cu, injectedTypes, getCursor().getParentTreeCursor()); } } return cu; diff --git a/src/main/java/org/openrewrite/java/migrate/javax/AnnotateTypesVisitor.java b/src/main/java/org/openrewrite/java/migrate/javax/AnnotateTypesVisitor.java index 78345b3b0c..c9462df74f 100644 --- a/src/main/java/org/openrewrite/java/migrate/javax/AnnotateTypesVisitor.java +++ b/src/main/java/org/openrewrite/java/migrate/javax/AnnotateTypesVisitor.java @@ -20,7 +20,6 @@ import org.openrewrite.java.JavaParser; import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.tree.J; -import org.openrewrite.java.tree.TypeUtils; import java.util.Comparator; import java.util.Set; @@ -36,7 +35,8 @@ public AnnotateTypesVisitor(String annotationToBeAdded) { String className = split[split.length - 1]; String packageName = this.annotationToBeAdded.substring(0, this.annotationToBeAdded.lastIndexOf(".")); this.annotationMatcher = new AnnotationMatcher("@" + this.annotationToBeAdded); - String interfaceAsString = String.format("package %s; public @interface %s {}", packageName, className); + String interfaceAsString = String.format("package %s\npublic @interface %s {}", packageName, className); + //noinspection LanguageMismatch this.template = JavaTemplate.builder("@" + className) .imports(this.annotationToBeAdded) .javaParser(JavaParser.fromJavaVersion().dependsOn(interfaceAsString)) @@ -46,10 +46,11 @@ public AnnotateTypesVisitor(String annotationToBeAdded) { @Override public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Set injectedTypes) { J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, injectedTypes); - if (injectedTypes.contains(TypeUtils.asFullyQualified(cd.getType()).getFullyQualifiedName()) && + if (cd.getType() != null && injectedTypes.contains(cd.getType().getFullyQualifiedName()) && cd.getLeadingAnnotations().stream().noneMatch(annotationMatcher::matches)) { maybeAddImport(annotationToBeAdded); - return template.apply(getCursor(), cd.getCoordinates().addAnnotation(Comparator.comparing(J.Annotation::getSimpleName))); + cd = template.apply(getCursor(), cd.getCoordinates().addAnnotation(Comparator.comparing(J.Annotation::getSimpleName))); + cd = maybeAutoFormat(classDecl, cd, cd.getName(), injectedTypes, getCursor()); } return cd; } diff --git a/src/test/java/org/openrewrite/java/migrate/UpgradeScalaTest.java b/src/test/java/org/openrewrite/java/migrate/UpgradeScalaTest.java deleted file mode 100644 index ebecd638f7..0000000000 --- a/src/test/java/org/openrewrite/java/migrate/UpgradeScalaTest.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright 2024 the original author or authors. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * https://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.java.migrate; - -import org.junit.jupiter.api.Test; -import org.openrewrite.DocumentExample; -import org.openrewrite.test.RecipeSpec; -import org.openrewrite.test.RewriteTest; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.openrewrite.maven.Assertions.pomXml; - -class UpgradeScalaTest implements RewriteTest { - - @Override - public void defaults(RecipeSpec spec) { - spec.recipeFromResource( - "/META-INF/rewrite/scala.yml", - "org.openrewrite.scala.migrate.UpgradeScala_2_12"); - } - - @DocumentExample - @Test - void upgradeScalaDependencies() { - rewriteRun( - pomXml( - //language=xml - """ - - 4.0.0 - com.mycompany.app - my-app - 1 - - - org.scala-lang - scala-library - 2.12.0 - - - - """, - after -> after.after(str -> str).afterRecipe( - doc -> assertThat(doc.getRoot() - .getChild("dependencies").get() - .getChild("dependency").get() - .getChildValue("version").get()) - .matches("2.12.[1-9]\\d")) - ) - ); - } -} diff --git a/src/test/java/org/openrewrite/java/migrate/javax/AddColumnAnnotationTest.java b/src/test/java/org/openrewrite/java/migrate/javax/AddColumnAnnotationTest.java index 470fc023e7..668896319e 100644 --- a/src/test/java/org/openrewrite/java/migrate/javax/AddColumnAnnotationTest.java +++ b/src/test/java/org/openrewrite/java/migrate/javax/AddColumnAnnotationTest.java @@ -39,33 +39,33 @@ void addColumnAnnotationAlongElementCollection() { java( """ import java.util.List; - + import javax.persistence.ElementCollection; import javax.persistence.Entity; import javax.persistence.Id; - + @Entity public class ElementCollectionEntity { @Id private int id; - + @ElementCollection private List listofStrings; } """, """ import java.util.List; - + import javax.persistence.Column; import javax.persistence.ElementCollection; import javax.persistence.Entity; import javax.persistence.Id; - + @Entity public class ElementCollectionEntity { @Id private int id; - + @Column(name = "element") @ElementCollection private List listofStrings; @@ -86,12 +86,12 @@ void updateColumnAnnotationWithoutExistingAttributes() { import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Column; - + @Entity public class ElementCollectionEntity { @Id private int id; - + @ElementCollection @Column private List listofStrings; @@ -102,12 +102,12 @@ public class ElementCollectionEntity { import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Column; - + @Entity public class ElementCollectionEntity { @Id private int id; - + @ElementCollection @Column(name = "element") private List listofStrings; @@ -128,12 +128,12 @@ void updateColumnAnnotationWithExistingAttributes() { import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Column; - + @Entity public class ElementCollectionEntity { @Id private int id; - + @Column(nullable = false, length = 512) @ElementCollection private List listofStrings; @@ -145,12 +145,12 @@ public class ElementCollectionEntity { import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Column; - + @Entity public class ElementCollectionEntity { @Id private int id; - + @Column(name = "element", nullable = false, length = 512) @ElementCollection private List listofStrings; @@ -171,15 +171,15 @@ void addColumnToApplicableFieldWhileAvoidingOthers() { import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Column; - + @Entity public class ElementCollectionEntity { @Id private int id; - + @Column private List listOfInts; - + @ElementCollection private List listofStrings; } @@ -190,15 +190,15 @@ public class ElementCollectionEntity { import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Column; - + @Entity public class ElementCollectionEntity { @Id private int id; - + @Column private List listOfInts; - + @Column(name = "element") @ElementCollection private List listofStrings; @@ -224,7 +224,7 @@ void doNotChangeColumnWithoutSiblingElementCollection() { public class ElementCollectionEntity { @Id private int id; - + @Column private List listofStrings; } @@ -244,12 +244,12 @@ void doNotChangeColumnWithExistingNameAttribute() { import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Column; - + @Entity public class ElementCollectionEntity { @Id private int id; - + @ElementCollection @Column(name = "test") private List listofStrings; @@ -271,12 +271,12 @@ void doNotAddColumnMappingToTransient() { import javax.persistence.Id; import javax.persistence.Column; import javax.persistence.Transient; - + @Entity public class ElementCollectionEntity { @Id private int id; - + @Transient @ElementCollection private List listofStrings; @@ -296,11 +296,11 @@ void doNotAddColumnMappingIfClassNotEntity() { import javax.persistence.ElementCollection; import javax.persistence.Id; import javax.persistence.Column; - + public class ElementCollectionEntity { @Id private int id; - + @ElementCollection private List listofStrings; } @@ -319,19 +319,19 @@ void handleInnerClass() { import javax.persistence.ElementCollection; import javax.persistence.Entity; import javax.persistence.Id; - + @Entity public class ElementCollectionEntity { @Id private int id; - + @ElementCollection private List listofStrings; - + class InnerClass { @Id private int id2; - + @ElementCollection private List listofStrings2; } @@ -339,25 +339,25 @@ class InnerClass { """, """ import java.util.List; - + import javax.persistence.Column; import javax.persistence.ElementCollection; import javax.persistence.Entity; import javax.persistence.Id; - + @Entity public class ElementCollectionEntity { @Id private int id; - + @Column(name = "element") @ElementCollection private List listofStrings; - + class InnerClass { @Id private int id2; - + @Column(name = "element") @ElementCollection private List listofStrings2;