Skip to content

Commit 55f165a

Browse files
authored
Fix checkstyle errors and suppress spotbug errors for ChangeSdkType (#5113)
1 parent ae2b8db commit 55f165a

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
lines changed

build-tools/src/main/resources/software/amazon/awssdk/spotbugs-suppressions.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,15 @@
213213
</Not>
214214
</Match>
215215

216+
<!-- Skip the warning since this source is forked from openrewrite -->
217+
<Match>
218+
<Or>
219+
<Class name="software.amazon.awssdk.migration.recipe.ChangeSdkType"/>
220+
<Class name="software.amazon.awssdk.migration.recipe.ChangeSdkType$ChangeTypeVisitor"/>
221+
</Or>
222+
<Bug pattern="NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE"/>
223+
</Match>
224+
216225
<Match>
217226
<Class name="software.amazon.awssdk.core.internal.waiters.ResponseOrException"/>
218227
<Bug pattern="NM_CLASS_NOT_EXCEPTION"/>

migration-tool/src/main/java/software/amazon/awssdk/migration/recipe/ChangeSdkType.java

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -92,21 +92,21 @@ public J visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ct
9292
}
9393

9494
@Override
95-
public J visitImport(J.Import import_, ExecutionContext ctx) {
96-
String currentFqcn = currentFqcn(import_);
95+
public J visitImport(J.Import anImport, ExecutionContext ctx) {
96+
String currentFqcn = currentFqcn(anImport);
9797
if (isV1Import(currentFqcn)) {
9898
JavaType.ShallowClass originalType = JavaType.ShallowClass.build(currentFqcn);
9999
String v2Equivalent = getV2Equivalent(currentFqcn);
100100

101101
JavaType targetType = JavaType.buildType(v2Equivalent);
102102

103103
oldTypeToNewType.put(currentFqcn, Pair.of(originalType, targetType));
104-
if (import_.getAlias() != null) {
105-
importAlias = import_.getAlias();
104+
if (anImport.getAlias() != null) {
105+
importAlias = anImport.getAlias();
106106
}
107107
}
108108

109-
return import_;
109+
return anImport;
110110
}
111111

112112
@Override
@@ -144,14 +144,14 @@ public J postVisit(J tree, ExecutionContext ctx) {
144144
J.MethodDeclaration method = (J.MethodDeclaration) currentTree;
145145
JavaType.Method mt = updateType(method.getMethodType());
146146
currentTree = method.withMethodType(mt)
147-
.withName(method.getName().withType(mt));
147+
.withName(method.getName().withType(mt));
148148

149149
} else if (currentTree instanceof J.MethodInvocation) {
150150

151151
J.MethodInvocation method = (J.MethodInvocation) currentTree;
152152
JavaType.Method mt = updateType(method.getMethodType());
153153
currentTree = method.withMethodType(mt)
154-
.withName(method.getName().withType(mt));
154+
.withName(method.getName().withType(mt));
155155

156156
} else if (currentTree instanceof J.NewClass) {
157157

@@ -184,12 +184,17 @@ private J postVisitSourceFile(JavaSourceFile tree, ExecutionContext ctx, J curre
184184
JavaType maybeType = anImport.getQualid().getType();
185185
if (maybeType instanceof JavaType.FullyQualified) {
186186
JavaType.FullyQualified type = (JavaType.FullyQualified) maybeType;
187-
if (originalType.getFullyQualifiedName().equals(type.getFullyQualifiedName())) {
187+
String fullyQualifiedName = originalType.getFullyQualifiedName();
188+
if (fullyQualifiedName.equals(type.getFullyQualifiedName())) {
189+
sourceFile = (JavaSourceFile) new RemoveImport<ExecutionContext>(fullyQualifiedName)
190+
.visit(sourceFile, ctx, getCursor().getParentOrThrow());
191+
} else if (originalType.getOwningClass() != null &&
192+
originalType.getOwningClass().getFullyQualifiedName()
193+
.equals(type.getFullyQualifiedName())) {
188194
sourceFile =
189-
(JavaSourceFile) new RemoveImport<ExecutionContext>(originalType.getFullyQualifiedName()).visit(sourceFile, ctx, getCursor().getParentOrThrow());
190-
} else if (originalType.getOwningClass() != null && originalType.getOwningClass().getFullyQualifiedName().equals(type.getFullyQualifiedName())) {
191-
sourceFile =
192-
(JavaSourceFile) new RemoveImport<ExecutionContext>(originalType.getOwningClass().getFullyQualifiedName()).visit(sourceFile, ctx, getCursor().getParentOrThrow());
195+
(JavaSourceFile) new RemoveImport<ExecutionContext>(
196+
originalType.getOwningClass().getFullyQualifiedName())
197+
.visit(sourceFile, ctx, getCursor().getParentOrThrow());
193198
}
194199
}
195200
}
@@ -207,7 +212,9 @@ private J postVisitSourceFile(JavaSourceFile tree, ExecutionContext ctx, J curre
207212
}
208213

209214
if (sourceFile != null) {
210-
sourceFile = sourceFile.withImports(ListUtils.map(sourceFile.getImports(), i -> visitAndCast(i, ctx, super::visitImport)));
215+
sourceFile = sourceFile.withImports(
216+
ListUtils.map(sourceFile.getImports(), i -> visitAndCast(i, ctx,
217+
super::visitImport)));
211218
}
212219

213220
currentTree = sourceFile;
@@ -222,8 +229,9 @@ public J visitFieldAccess(J.FieldAccess fieldAccess, ExecutionContext ctx) {
222229
JavaType targetType = entry.right();
223230
if (fieldAccess.isFullyQualifiedClassReference(originalType.getFullyQualifiedName())) {
224231
if (targetType instanceof JavaType.FullyQualified) {
225-
return updateOuterClassTypes(TypeTree.build(((JavaType.FullyQualified) targetType).getFullyQualifiedName())
226-
.withPrefix(fieldAccess.getPrefix()), targetType);
232+
return updateOuterClassTypes(
233+
TypeTree.build(((JavaType.FullyQualified) targetType).getFullyQualifiedName())
234+
.withPrefix(fieldAccess.getPrefix()), targetType);
227235
}
228236
} else {
229237
StringBuilder maybeClass = new StringBuilder();
@@ -465,9 +473,9 @@ private static boolean isV1Import(String currentFqcn) {
465473
return false;
466474
}
467475

468-
private static String currentFqcn(J.Import import_) {
476+
private static String currentFqcn(J.Import anImport) {
469477
JavaType.FullyQualified currentFqcn =
470-
TypeUtils.asFullyQualified(Optional.ofNullable(import_.getQualid()).map(J.FieldAccess::getType).orElse(null));
478+
TypeUtils.asFullyQualified(Optional.ofNullable(anImport.getQualid()).map(J.FieldAccess::getType).orElse(null));
471479
String curFqn = currentFqcn != null ? currentFqcn.getFullyQualifiedName() : null;
472480
return curFqn;
473481
}

migration-tool/src/main/java/software/amazon/awssdk/migration/recipe/utils/NamingConversionUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ public static String getV2Equivalent(String currentFqcn) {
4646
}
4747

4848
private static String getV2ClientEquivalent(String className) {
49-
if (className.startsWith("Abstract") ) {
49+
if (className.startsWith("Abstract")) {
5050
className = className.substring(8);
5151
}
52-
if (className.startsWith("Amazon") ) {
52+
if (className.startsWith("Amazon")) {
5353
className = className.substring(6);
5454
} else if (className.startsWith("AWS")) {
5555
className = className.substring(3);

0 commit comments

Comments
 (0)