Skip to content

Commit

Permalink
Fix various test failures and warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
sambsnyd committed Oct 17, 2024
1 parent a21602a commit d67ebf8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ public TreeVisitor<?, ExecutionContext> getScanner(Set<String> 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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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))
Expand All @@ -46,10 +46,11 @@ public AnnotateTypesVisitor(String annotationToBeAdded) {
@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Set<String> 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;
}
Expand Down
65 changes: 0 additions & 65 deletions src/test/java/org/openrewrite/java/migrate/UpgradeScalaTest.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<String> listofStrings;
Expand All @@ -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<String> listofStrings;
Expand All @@ -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<String> listofStrings;
Expand All @@ -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<String> listofStrings;
Expand All @@ -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<String> listofStrings;
Expand All @@ -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<Integer> listOfInts;
@ElementCollection
private List<String> listofStrings;
}
Expand All @@ -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<Integer> listOfInts;
@Column(name = "element")
@ElementCollection
private List<String> listofStrings;
Expand All @@ -224,7 +224,7 @@ void doNotChangeColumnWithoutSiblingElementCollection() {
public class ElementCollectionEntity {
@Id
private int id;
@Column
private List<String> listofStrings;
}
Expand All @@ -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<String> listofStrings;
Expand All @@ -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<String> listofStrings;
Expand All @@ -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<String> listofStrings;
}
Expand All @@ -319,45 +319,45 @@ 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<String> listofStrings;
class InnerClass {
@Id
private int id2;
@ElementCollection
private List<String> listofStrings2;
}
}
""",
"""
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<String> listofStrings;
class InnerClass {
@Id
private int id2;
@Column(name = "element")
@ElementCollection
private List<String> listofStrings2;
Expand Down

0 comments on commit d67ebf8

Please sign in to comment.