Skip to content

Commit

Permalink
Fixed Migration duplicated Java services creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
ylussaud committed Jan 25, 2024
1 parent 1ea4dd1 commit a3c3a5f
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
Expand Up @@ -72,7 +72,7 @@ final class AmbiguousServiceMethodRefactorVisitor extends ASTVisitor {
@Override
public boolean visit(MethodDeclaration method) {
if (method.getName().getIdentifier().equals(serviceName) && parametersAreMatching(method.parameters(),
serviceArguments)) {
serviceArguments) && !newMethodExists(method)) {
final ASTRewrite rewrite = ASTRewrite.create(method.getAST());

final MethodDeclaration newMethod = method.getAST().newMethodDeclaration();
Expand Down Expand Up @@ -127,6 +127,63 @@ public boolean visit(MethodDeclaration method) {
return false;
}

/**
* Tells if the new method we want to create for the given {@link MethodDeclaration} already exists in its
* parent {@link TypeDeclaration}.
*
* @param method
* the {@link MethodDeclaration}
* @return <code>true</code> if the new method we want to create for the given {@link MethodDeclaration}
* already exists in its parent {@link TypeDeclaration}, <code>false</code> otherwise
*/
private boolean newMethodExists(MethodDeclaration method) {
boolean res = false;

final String newMethodName = serviceName + ExpressionConverter.JAVA_SERVICE;
final TypeDeclaration typeDeclaration = (TypeDeclaration)method.getParent();
for (MethodDeclaration methodDeclaration : typeDeclaration.getMethods()) {
if (newMethodName.equals(methodDeclaration.getName().getIdentifier()) && haveSameParameterTypes(
method, methodDeclaration)) {
res = true;
break;
}
}
return res;
}

/**
* Tells if both given {@link MethodDeclaration} have the same parameter types.
*
* @param method
* first {@link MethodDeclaration}
* @param methodDeclaration
* second {@link MethodDeclaration}
* @return <code>true</code> if both given {@link MethodDeclaration} have the same parameter types,
* <code>false</code> otherwise
*/
private boolean haveSameParameterTypes(MethodDeclaration method, MethodDeclaration methodDeclaration) {
boolean res;

if (method.parameters().size() == methodDeclaration.parameters().size()) {
res = true;
for (int i = 0; i < method.parameters().size(); i++) {
final SingleVariableDeclaration methodParameter = (SingleVariableDeclaration)method
.parameters().get(i);
final SingleVariableDeclaration methodDeclarationParameter = (SingleVariableDeclaration)methodDeclaration
.parameters().get(i);
if (!methodParameter.getType().toString().equals(methodDeclarationParameter.getType()
.toString())) {
res = false;
break;
}
}
} else {
res = false;
}

return res;
}

/**
* Tells if the given {@link List} of arguments matches the given {@link List} of parameters.
*
Expand Down
Expand Up @@ -62,7 +62,8 @@ final class ServiceWithNoParameterMethodRefactorVisitor extends ASTVisitor {

@Override
public boolean visit(MethodDeclaration method) {
if (method.getName().getIdentifier().equals(serviceName) && method.parameters().isEmpty()) {
if (method.getName().getIdentifier().equals(serviceName) && method.parameters().isEmpty()
&& !newMethodExists(method)) {
final ASTRewrite rewrite = ASTRewrite.create(method.getAST());

final MethodDeclaration newMethod = method.getAST().newMethodDeclaration();
Expand Down Expand Up @@ -112,4 +113,30 @@ public boolean visit(MethodDeclaration method) {

return false;
}

/**
* Tells if the new method we want to create for the given {@link MethodDeclaration} already exists in its
* parent {@link TypeDeclaration}.
*
* @param method
* the {@link MethodDeclaration}
* @return <code>true</code> if the new method we want to create for the given {@link MethodDeclaration}
* already exists in its parent {@link TypeDeclaration}, <code>false</code> otherwise
*/
private boolean newMethodExists(MethodDeclaration method) {
boolean res = false;

final String newMethodName = serviceName + ExpressionConverter.JAVA_SERVICE;
final TypeDeclaration typeDeclaration = (TypeDeclaration)method.getParent();
for (MethodDeclaration methodDeclaration : typeDeclaration.getMethods()) {
if (newMethodName.equals(methodDeclaration.getName().getIdentifier()) && methodDeclaration
.parameters().size() == 1 && "Object".equals(((SingleVariableDeclaration)methodDeclaration
.parameters().get(0)).getType().toString())) {
res = true;
break;
}
}
return res;
}

}

0 comments on commit a3c3a5f

Please sign in to comment.