Skip to content

Commit d67ebf8

Browse files
committed
Fix various test failures and warnings
1 parent a21602a commit d67ebf8

File tree

4 files changed

+47
-109
lines changed

4 files changed

+47
-109
lines changed

src/main/java/org/openrewrite/java/migrate/javax/AddScopeToInjectedClass.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@ public TreeVisitor<?, ExecutionContext> getScanner(Set<String> injectedTypes) {
5252
@Override
5353
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
5454
J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, ctx);
55-
for (JavaType.Variable variable : cd.getType().getMembers()) {
56-
if (variableTypeRequiresScope(variable)) {
57-
injectedTypes.add(((JavaType.FullyQualified) variable.getType()).getFullyQualifiedName());
55+
if(cd.getType() != null) {
56+
for (JavaType.Variable variable : cd.getType().getMembers()) {
57+
if (variableTypeRequiresScope(variable)) {
58+
injectedTypes.add(((JavaType.FullyQualified) variable.getType()).getFullyQualifiedName());
59+
}
5860
}
5961
}
6062
return cd;
@@ -85,7 +87,7 @@ public J.CompilationUnit visitCompilationUnit(J.CompilationUnit compilationUnit,
8587
for (J.ClassDeclaration aClass : cu.getClasses()) {
8688
if (aClass.getType() != null && injectedTypes.contains(aClass.getType().getFullyQualifiedName())) {
8789
return (J.CompilationUnit) new AnnotateTypesVisitor(JAVAX_ENTERPRISE_CONTEXT_DEPENDENT)
88-
.visit(cu, injectedTypes, getCursor().getParentTreeCursor());
90+
.visitNonNull(cu, injectedTypes, getCursor().getParentTreeCursor());
8991
}
9092
}
9193
return cu;

src/main/java/org/openrewrite/java/migrate/javax/AnnotateTypesVisitor.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.openrewrite.java.JavaParser;
2121
import org.openrewrite.java.JavaTemplate;
2222
import org.openrewrite.java.tree.J;
23-
import org.openrewrite.java.tree.TypeUtils;
2423

2524
import java.util.Comparator;
2625
import java.util.Set;
@@ -36,7 +35,8 @@ public AnnotateTypesVisitor(String annotationToBeAdded) {
3635
String className = split[split.length - 1];
3736
String packageName = this.annotationToBeAdded.substring(0, this.annotationToBeAdded.lastIndexOf("."));
3837
this.annotationMatcher = new AnnotationMatcher("@" + this.annotationToBeAdded);
39-
String interfaceAsString = String.format("package %s; public @interface %s {}", packageName, className);
38+
String interfaceAsString = String.format("package %s\npublic @interface %s {}", packageName, className);
39+
//noinspection LanguageMismatch
4040
this.template = JavaTemplate.builder("@" + className)
4141
.imports(this.annotationToBeAdded)
4242
.javaParser(JavaParser.fromJavaVersion().dependsOn(interfaceAsString))
@@ -46,10 +46,11 @@ public AnnotateTypesVisitor(String annotationToBeAdded) {
4646
@Override
4747
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Set<String> injectedTypes) {
4848
J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, injectedTypes);
49-
if (injectedTypes.contains(TypeUtils.asFullyQualified(cd.getType()).getFullyQualifiedName()) &&
49+
if (cd.getType() != null && injectedTypes.contains(cd.getType().getFullyQualifiedName()) &&
5050
cd.getLeadingAnnotations().stream().noneMatch(annotationMatcher::matches)) {
5151
maybeAddImport(annotationToBeAdded);
52-
return template.apply(getCursor(), cd.getCoordinates().addAnnotation(Comparator.comparing(J.Annotation::getSimpleName)));
52+
cd = template.apply(getCursor(), cd.getCoordinates().addAnnotation(Comparator.comparing(J.Annotation::getSimpleName)));
53+
cd = maybeAutoFormat(classDecl, cd, cd.getName(), injectedTypes, getCursor());
5354
}
5455
return cd;
5556
}

src/test/java/org/openrewrite/java/migrate/UpgradeScalaTest.java

Lines changed: 0 additions & 65 deletions
This file was deleted.

src/test/java/org/openrewrite/java/migrate/javax/AddColumnAnnotationTest.java

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -39,33 +39,33 @@ void addColumnAnnotationAlongElementCollection() {
3939
java(
4040
"""
4141
import java.util.List;
42-
42+
4343
import javax.persistence.ElementCollection;
4444
import javax.persistence.Entity;
4545
import javax.persistence.Id;
46-
46+
4747
@Entity
4848
public class ElementCollectionEntity {
4949
@Id
5050
private int id;
51-
51+
5252
@ElementCollection
5353
private List<String> listofStrings;
5454
}
5555
""",
5656
"""
5757
import java.util.List;
58-
58+
5959
import javax.persistence.Column;
6060
import javax.persistence.ElementCollection;
6161
import javax.persistence.Entity;
6262
import javax.persistence.Id;
63-
63+
6464
@Entity
6565
public class ElementCollectionEntity {
6666
@Id
6767
private int id;
68-
68+
6969
@Column(name = "element")
7070
@ElementCollection
7171
private List<String> listofStrings;
@@ -86,12 +86,12 @@ void updateColumnAnnotationWithoutExistingAttributes() {
8686
import javax.persistence.Entity;
8787
import javax.persistence.Id;
8888
import javax.persistence.Column;
89-
89+
9090
@Entity
9191
public class ElementCollectionEntity {
9292
@Id
9393
private int id;
94-
94+
9595
@ElementCollection
9696
@Column
9797
private List<String> listofStrings;
@@ -102,12 +102,12 @@ public class ElementCollectionEntity {
102102
import javax.persistence.Entity;
103103
import javax.persistence.Id;
104104
import javax.persistence.Column;
105-
105+
106106
@Entity
107107
public class ElementCollectionEntity {
108108
@Id
109109
private int id;
110-
110+
111111
@ElementCollection
112112
@Column(name = "element")
113113
private List<String> listofStrings;
@@ -128,12 +128,12 @@ void updateColumnAnnotationWithExistingAttributes() {
128128
import javax.persistence.Entity;
129129
import javax.persistence.Id;
130130
import javax.persistence.Column;
131-
131+
132132
@Entity
133133
public class ElementCollectionEntity {
134134
@Id
135135
private int id;
136-
136+
137137
@Column(nullable = false, length = 512)
138138
@ElementCollection
139139
private List<String> listofStrings;
@@ -145,12 +145,12 @@ public class ElementCollectionEntity {
145145
import javax.persistence.Entity;
146146
import javax.persistence.Id;
147147
import javax.persistence.Column;
148-
148+
149149
@Entity
150150
public class ElementCollectionEntity {
151151
@Id
152152
private int id;
153-
153+
154154
@Column(name = "element", nullable = false, length = 512)
155155
@ElementCollection
156156
private List<String> listofStrings;
@@ -171,15 +171,15 @@ void addColumnToApplicableFieldWhileAvoidingOthers() {
171171
import javax.persistence.Entity;
172172
import javax.persistence.Id;
173173
import javax.persistence.Column;
174-
174+
175175
@Entity
176176
public class ElementCollectionEntity {
177177
@Id
178178
private int id;
179-
179+
180180
@Column
181181
private List<Integer> listOfInts;
182-
182+
183183
@ElementCollection
184184
private List<String> listofStrings;
185185
}
@@ -190,15 +190,15 @@ public class ElementCollectionEntity {
190190
import javax.persistence.Entity;
191191
import javax.persistence.Id;
192192
import javax.persistence.Column;
193-
193+
194194
@Entity
195195
public class ElementCollectionEntity {
196196
@Id
197197
private int id;
198-
198+
199199
@Column
200200
private List<Integer> listOfInts;
201-
201+
202202
@Column(name = "element")
203203
@ElementCollection
204204
private List<String> listofStrings;
@@ -224,7 +224,7 @@ void doNotChangeColumnWithoutSiblingElementCollection() {
224224
public class ElementCollectionEntity {
225225
@Id
226226
private int id;
227-
227+
228228
@Column
229229
private List<String> listofStrings;
230230
}
@@ -244,12 +244,12 @@ void doNotChangeColumnWithExistingNameAttribute() {
244244
import javax.persistence.Entity;
245245
import javax.persistence.Id;
246246
import javax.persistence.Column;
247-
247+
248248
@Entity
249249
public class ElementCollectionEntity {
250250
@Id
251251
private int id;
252-
252+
253253
@ElementCollection
254254
@Column(name = "test")
255255
private List<String> listofStrings;
@@ -271,12 +271,12 @@ void doNotAddColumnMappingToTransient() {
271271
import javax.persistence.Id;
272272
import javax.persistence.Column;
273273
import javax.persistence.Transient;
274-
274+
275275
@Entity
276276
public class ElementCollectionEntity {
277277
@Id
278278
private int id;
279-
279+
280280
@Transient
281281
@ElementCollection
282282
private List<String> listofStrings;
@@ -296,11 +296,11 @@ void doNotAddColumnMappingIfClassNotEntity() {
296296
import javax.persistence.ElementCollection;
297297
import javax.persistence.Id;
298298
import javax.persistence.Column;
299-
299+
300300
public class ElementCollectionEntity {
301301
@Id
302302
private int id;
303-
303+
304304
@ElementCollection
305305
private List<String> listofStrings;
306306
}
@@ -319,45 +319,45 @@ void handleInnerClass() {
319319
import javax.persistence.ElementCollection;
320320
import javax.persistence.Entity;
321321
import javax.persistence.Id;
322-
322+
323323
@Entity
324324
public class ElementCollectionEntity {
325325
@Id
326326
private int id;
327-
327+
328328
@ElementCollection
329329
private List<String> listofStrings;
330-
330+
331331
class InnerClass {
332332
@Id
333333
private int id2;
334-
334+
335335
@ElementCollection
336336
private List<String> listofStrings2;
337337
}
338338
}
339339
""",
340340
"""
341341
import java.util.List;
342-
342+
343343
import javax.persistence.Column;
344344
import javax.persistence.ElementCollection;
345345
import javax.persistence.Entity;
346346
import javax.persistence.Id;
347-
347+
348348
@Entity
349349
public class ElementCollectionEntity {
350350
@Id
351351
private int id;
352-
352+
353353
@Column(name = "element")
354354
@ElementCollection
355355
private List<String> listofStrings;
356-
356+
357357
class InnerClass {
358358
@Id
359359
private int id2;
360-
360+
361361
@Column(name = "element")
362362
@ElementCollection
363363
private List<String> listofStrings2;

0 commit comments

Comments
 (0)