diff --git a/src/main/java/com/gepardec/wor/lord/BinaryProxyToWeb.java b/src/main/java/com/gepardec/wor/lord/BinaryProxyToWeb.java index 0982f3e..1a665b0 100644 --- a/src/main/java/com/gepardec/wor/lord/BinaryProxyToWeb.java +++ b/src/main/java/com/gepardec/wor/lord/BinaryProxyToWeb.java @@ -51,116 +51,118 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { - return new JavaIsoVisitor() { + 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 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 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; + } + + }; } } diff --git a/src/test/java/com/gepardec/wor/lord/BinaryProxyToWebTest.java b/src/test/java/com/gepardec/wor/lord/BinaryProxyToWebTest.java index a2fe195..f2d2558 100644 --- a/src/test/java/com/gepardec/wor/lord/BinaryProxyToWebTest.java +++ b/src/test/java/com/gepardec/wor/lord/BinaryProxyToWebTest.java @@ -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");