Skip to content

Fix checkstyle errors and suppress spotbug errors for ChangeSdkType #5113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,15 @@
</Not>
</Match>

<!-- Skip the warning since this source is forked from openrewrite -->
<Match>
<Or>
<Class name="software.amazon.awssdk.migration.recipe.ChangeSdkType"/>
<Class name="software.amazon.awssdk.migration.recipe.ChangeSdkType$ChangeTypeVisitor"/>
</Or>
<Bug pattern="NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE"/>
</Match>

<Match>
<Class name="software.amazon.awssdk.core.internal.waiters.ResponseOrException"/>
<Bug pattern="NM_CLASS_NOT_EXCEPTION"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,21 @@ public J visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ct
}

@Override
public J visitImport(J.Import import_, ExecutionContext ctx) {
String currentFqcn = currentFqcn(import_);
public J visitImport(J.Import anImport, ExecutionContext ctx) {
String currentFqcn = currentFqcn(anImport);
if (isV1Import(currentFqcn)) {
JavaType.ShallowClass originalType = JavaType.ShallowClass.build(currentFqcn);
String v2Equivalent = getV2Equivalent(currentFqcn);

JavaType targetType = JavaType.buildType(v2Equivalent);

oldTypeToNewType.put(currentFqcn, Pair.of(originalType, targetType));
if (import_.getAlias() != null) {
importAlias = import_.getAlias();
if (anImport.getAlias() != null) {
importAlias = anImport.getAlias();
}
}

return import_;
return anImport;
}

@Override
Expand Down Expand Up @@ -144,14 +144,14 @@ public J postVisit(J tree, ExecutionContext ctx) {
J.MethodDeclaration method = (J.MethodDeclaration) currentTree;
JavaType.Method mt = updateType(method.getMethodType());
currentTree = method.withMethodType(mt)
.withName(method.getName().withType(mt));
.withName(method.getName().withType(mt));

} else if (currentTree instanceof J.MethodInvocation) {

J.MethodInvocation method = (J.MethodInvocation) currentTree;
JavaType.Method mt = updateType(method.getMethodType());
currentTree = method.withMethodType(mt)
.withName(method.getName().withType(mt));
.withName(method.getName().withType(mt));

} else if (currentTree instanceof J.NewClass) {

Expand Down Expand Up @@ -184,12 +184,17 @@ private J postVisitSourceFile(JavaSourceFile tree, ExecutionContext ctx, J curre
JavaType maybeType = anImport.getQualid().getType();
if (maybeType instanceof JavaType.FullyQualified) {
JavaType.FullyQualified type = (JavaType.FullyQualified) maybeType;
if (originalType.getFullyQualifiedName().equals(type.getFullyQualifiedName())) {
String fullyQualifiedName = originalType.getFullyQualifiedName();
if (fullyQualifiedName.equals(type.getFullyQualifiedName())) {
sourceFile = (JavaSourceFile) new RemoveImport<ExecutionContext>(fullyQualifiedName)
.visit(sourceFile, ctx, getCursor().getParentOrThrow());
} else if (originalType.getOwningClass() != null &&
originalType.getOwningClass().getFullyQualifiedName()
.equals(type.getFullyQualifiedName())) {
sourceFile =
(JavaSourceFile) new RemoveImport<ExecutionContext>(originalType.getFullyQualifiedName()).visit(sourceFile, ctx, getCursor().getParentOrThrow());
} else if (originalType.getOwningClass() != null && originalType.getOwningClass().getFullyQualifiedName().equals(type.getFullyQualifiedName())) {
sourceFile =
(JavaSourceFile) new RemoveImport<ExecutionContext>(originalType.getOwningClass().getFullyQualifiedName()).visit(sourceFile, ctx, getCursor().getParentOrThrow());
(JavaSourceFile) new RemoveImport<ExecutionContext>(
originalType.getOwningClass().getFullyQualifiedName())
.visit(sourceFile, ctx, getCursor().getParentOrThrow());
}
}
}
Expand All @@ -207,7 +212,9 @@ private J postVisitSourceFile(JavaSourceFile tree, ExecutionContext ctx, J curre
}

if (sourceFile != null) {
sourceFile = sourceFile.withImports(ListUtils.map(sourceFile.getImports(), i -> visitAndCast(i, ctx, super::visitImport)));
sourceFile = sourceFile.withImports(
ListUtils.map(sourceFile.getImports(), i -> visitAndCast(i, ctx,
super::visitImport)));
}

currentTree = sourceFile;
Expand All @@ -222,8 +229,9 @@ public J visitFieldAccess(J.FieldAccess fieldAccess, ExecutionContext ctx) {
JavaType targetType = entry.right();
if (fieldAccess.isFullyQualifiedClassReference(originalType.getFullyQualifiedName())) {
if (targetType instanceof JavaType.FullyQualified) {
return updateOuterClassTypes(TypeTree.build(((JavaType.FullyQualified) targetType).getFullyQualifiedName())
.withPrefix(fieldAccess.getPrefix()), targetType);
return updateOuterClassTypes(
TypeTree.build(((JavaType.FullyQualified) targetType).getFullyQualifiedName())
.withPrefix(fieldAccess.getPrefix()), targetType);
}
} else {
StringBuilder maybeClass = new StringBuilder();
Expand Down Expand Up @@ -465,9 +473,9 @@ private static boolean isV1Import(String currentFqcn) {
return false;
}

private static String currentFqcn(J.Import import_) {
private static String currentFqcn(J.Import anImport) {
JavaType.FullyQualified currentFqcn =
TypeUtils.asFullyQualified(Optional.ofNullable(import_.getQualid()).map(J.FieldAccess::getType).orElse(null));
TypeUtils.asFullyQualified(Optional.ofNullable(anImport.getQualid()).map(J.FieldAccess::getType).orElse(null));
String curFqn = currentFqcn != null ? currentFqcn.getFullyQualifiedName() : null;
return curFqn;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ public static String getV2Equivalent(String currentFqcn) {
}

private static String getV2ClientEquivalent(String className) {
if (className.startsWith("Abstract") ) {
if (className.startsWith("Abstract")) {
className = className.substring(8);
}
if (className.startsWith("Amazon") ) {
if (className.startsWith("Amazon")) {
className = className.substring(6);
} else if (className.startsWith("AWS")) {
className = className.substring(3);
Expand Down