Skip to content

Commit

Permalink
Added support for different variable names for assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
sgartner03 committed May 6, 2024
1 parent b112412 commit b69f701
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 91 deletions.
182 changes: 92 additions & 90 deletions src/main/java/com/gepardec/wor/lord/BinaryProxyToWeb.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,116 +51,118 @@ public String getDescription() {

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaIsoVisitor<ExecutionContext>() {
return new JavaIsoVisitor<>() {

private final JavaTemplate NEW_BOOLEAN = JavaTemplate.builder("final boolean useWeb = true;\n")
.contextSensitive()
.build();

private final JavaTemplate IF_INITIALIZATION = JavaTemplate.builder(
"""
if(useWeb) {
AuMhHostInfoResponseDto #{} = callSvcWeb(req);
} else {
#{any()};
}
""")
"""
if(useWeb) {
AuMhHostInfoResponseDto #{} = callSvcWeb(req);
} else {
#{any()};
}
""")
.contextSensitive()
.build();
private final JavaTemplate IF_RETURN = JavaTemplate.builder(
"""
if(useWeb) {
return callSvcWeb(req);
} else {
#{any()};
}
""")
"""
if(useWeb) {
return callSvcWeb(req);
} else {
#{any()};
}
""")
.contextSensitive()
.build();
private final JavaTemplate IF_ASSIGNMENT = JavaTemplate.builder(
"""
if(useWeb) {
ret = callSvcWeb(req);
} else {
#{any()};
}
""")
"""
if(useWeb) {
#{} = callSvcWeb(req);
} else {
#{any()};
}
""")
.contextSensitive()
.build();
private final JavaTemplate IF_INVOCATION = JavaTemplate.builder(
"""
if(useWeb) {
callSvcWeb(req);
} else {
#{any()};
}
""")
"""
if(useWeb) {
callSvcWeb(req);
} else {
#{any()};
}
""")
.contextSensitive()
.build();


@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
final J.MethodDeclaration m = super.visitMethodDeclaration(method, ctx);

List<Statement> statements = m.getBody().getStatements();
String first = statements.get(0) + ";";

if (first.equals(NEW_BOOLEAN.getCode())) {
return m;
}

List<?> invocations = statements
.stream()
.filter(t -> t.print(getCursor()).contains(METHOD_NAME))
.toList();


if (invocations.isEmpty()) {
return m;
}

J.MethodDeclaration me = NEW_BOOLEAN.apply(
updateCursor(m),
m.getBody().getCoordinates().firstStatement());


for (Object o : invocations) {
Statement invocation = (Statement) o;
if (invocation instanceof J.VariableDeclarations decl) {
String varName = decl.getVariables().get(0).getName().getSimpleName();
me = IF_INITIALIZATION.apply(
updateCursor(me),
decl.getCoordinates().replace(),
varName,
invocation);
}

if (invocation instanceof J.Return returnStatement) {
me = IF_RETURN.apply(
updateCursor(me),
returnStatement.getCoordinates().replace(),
invocation);
}

if (invocation instanceof J.Assignment assignment) {
me = IF_ASSIGNMENT.apply(
updateCursor(me),
assignment.getCoordinates().replace(),
invocation);
}

if (invocation instanceof J.MethodInvocation methodInvocation) {
me = IF_INVOCATION.apply(
updateCursor(me),
methodInvocation.getCoordinates().replace(),
invocation);
}
}

return me;
@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
final J.MethodDeclaration m = super.visitMethodDeclaration(method, ctx);

List<Statement> statements = m.getBody().getStatements();
String first = statements.get(0) + ";";

if (first.equals(NEW_BOOLEAN.getCode())) {
return m;
}

List<?> invocations = statements
.stream()
.filter(t -> t.print(getCursor()).contains(METHOD_NAME))
.toList();


if (invocations.isEmpty()) {
return m;
}

J.MethodDeclaration me = NEW_BOOLEAN.apply(
updateCursor(m),
m.getBody().getCoordinates().firstStatement());


for (Object o : invocations) {
Statement invocation = (Statement) o;
if (invocation instanceof J.VariableDeclarations decl) {
String varName = decl.getVariables().get(0).getName().getSimpleName();
me = IF_INITIALIZATION.apply(
updateCursor(me),
decl.getCoordinates().replace(),
varName,
invocation);
}

};
if (invocation instanceof J.Return returnStatement) {
me = IF_RETURN.apply(
updateCursor(me),
returnStatement.getCoordinates().replace(),
invocation);
}

if (invocation instanceof J.Assignment assignment) {
String varName = assignment.getVariable().toString();
me = IF_ASSIGNMENT.apply(
updateCursor(me),
assignment.getCoordinates().replace(),
varName,
invocation);
}

if (invocation instanceof J.MethodInvocation methodInvocation) {
me = IF_INVOCATION.apply(
updateCursor(me),
methodInvocation.getCoordinates().replace(),
invocation);
}
}

return me;
}

};
}
}
73 changes: 72 additions & 1 deletion src/test/java/com/gepardec/wor/lord/BinaryProxyToWebTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,78 @@ public class AuMhHostInfoRequestDto {}
"""
)
);
}@DocumentExample
}

@DocumentExample
@Test
public void whenCallsInAssignmentsWithDifferentVariableNames_thenAddWebForDifferentVariables() {
LOG.info("Start Test");
rewriteRun(
//language=java
java(
"""
package com.gepardec.wor.lord;
public class Test {
public AuMhHostInfoResponseDto getAuMhHostInfo(AuMhHostInfoRequestDto req) {
AuMhHostInfoResponseDto ret;
ret = callSvcProxy(req);
AuMhHostInfoResponseDto ret2;
ret2 = callSvcProxy(req);
return ret;
}
AuMhHostInfoResponseDto callSvcProxy(AuMhHostInfoRequestDto req) {return null;}
AuMhHostInfoResponseDto callSvcWeb(AuMhHostInfoRequestDto req) {return null;}
}
public class AuMhHostInfoResponseDto {
public Integer getCallStatus() {
return null;
}
}
public class AuMhHostInfoRequestDto {}
""",
"""
package com.gepardec.wor.lord;
public class Test {
public AuMhHostInfoResponseDto getAuMhHostInfo(AuMhHostInfoRequestDto req) {
final boolean useWeb = true;
AuMhHostInfoResponseDto ret;
if (useWeb) {
ret = callSvcWeb(req);
} else {
ret = callSvcProxy(req);
}
AuMhHostInfoResponseDto ret2;
if (useWeb) {
ret2 = callSvcWeb(req);
} else {
ret2 = callSvcProxy(req);
}
return ret;
}
AuMhHostInfoResponseDto callSvcProxy(AuMhHostInfoRequestDto req) {return null;}
AuMhHostInfoResponseDto callSvcWeb(AuMhHostInfoRequestDto req) {return null;}
}
public class AuMhHostInfoResponseDto {
public Integer getCallStatus() {
return null;
}
}
public class AuMhHostInfoRequestDto {}
"""
)
);
}


@DocumentExample
@Test
public void whenCallsWithDifferentVariableNames_thenAddWebForDifferentVariables() {
LOG.info("Start Test");
Expand Down

0 comments on commit b69f701

Please sign in to comment.