0

I am using Com.GitHub.java parser for generating java code. i am facing a problem . problem is :

I have need bellow output . This line "String result = null;" need to show before try statement . But it is showing inside of try statement.

My Source code: https://pastebin.mozilla.org/McXbnU7w

Maven dependency:

<!-- https://mvnrepository.com/artifact/com.github.javaparser/javaparser-core -->
<dependency>
  <groupId>com.github.javaparser</groupId>
  <artifactId>javaparser-core</artifactId>
  <version>3.24.4</version>
</dependency>

Expected output of my code:

public Response saveProduct(ProductDto productDto) throws ServletException, IOException {
String result = null;
try {
createMyProduct.setProduct(productDto.getProduct());
createMyProduct.execute(parameterMap);
result = createMyProduct.getResult();

    } catch (BusinessException e) {
        return Response.status(Response.Status.BAD_REQUEST).entity(result).build();
    }
Aqeel Ashiq
  • 1,988
  • 5
  • 24
  • 57

1 Answers1

1

You need to add as many blocks as you need. E.g. String result = null should be treated as a separate block and try block should be another block. You cannot shove all statements in one block. Also in addingException method, you are adding try block into another BlockStmt. That is not needed. It will put extra curly braces. Here is updated code:

import java.io.IOException;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Modifier;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.Parameter;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.expr.NameExpr;
import com.github.javaparser.ast.expr.StringLiteralExpr;
import com.github.javaparser.ast.stmt.BlockStmt;
import com.github.javaparser.ast.stmt.CatchClause;
import com.github.javaparser.ast.stmt.EmptyStmt;
import com.github.javaparser.ast.stmt.ExpressionStmt;
import com.github.javaparser.ast.stmt.ReturnStmt;
import com.github.javaparser.ast.stmt.Statement;
import com.github.javaparser.ast.stmt.TryStmt;
import com.github.javaparser.ast.type.ClassOrInterfaceType;

public class ProductCreate2 {

  public static void main(String[] args) {
    CompilationUnit compilationUnit = new CompilationUnit();
    compilationUnit.setPackageDeclaration("org.meveo.mymodule.resource");
    compilationUnit.addImport("java.util", false, true);

    ClassOrInterfaceDeclaration restEndpointClass = compilationUnit.addClass("ProductCreate",Modifier.Keyword.PUBLIC);
    restEndpointClass.addField("CreateMyProduct", "createMyProduct", Modifier.Keyword.PRIVATE);

    NodeList<ClassOrInterfaceType> extendsList = new NodeList<>();
    extendsList.add(new ClassOrInterfaceType("CustomEndpointResource"));
    restEndpointClass.setExtendedTypes(extendsList);

    MethodDeclaration restMethod = restEndpointClass.addMethod("saveProduct", Modifier.Keyword.PUBLIC);
    restMethod.addParameter("ProductDto", "productDto");
    restMethod.setType("Response");
    restMethod.addAndGetAnnotation("POST");
    restMethod.addThrownException(IOException.class);
    //restMethod.addThrownException(ServletException.class);
    //restMethod.getBody().get().getStatements().add(new ExpressionStmt(new NameExpr("String result = null")));
    //restMethod.createBody().addStatement(new ExpressionStmt(new NameExpr("String result = // null")));
        


    BlockStmt blockStmt = new BlockStmt();


    blockStmt.addStatement(new ExpressionStmt(new NameExpr("String result = null")));

    BlockStmt blockStmt1 = new BlockStmt();
    blockStmt1.addStatement(new MethodCallExpr(new NameExpr("createMyProduct"), "setProduct").addArgument("productDto.getProduct()"));
    blockStmt1.addStatement(new MethodCallExpr(new NameExpr("createMyProduct"), "init").addArgument("parameterMap"));
    blockStmt1.addStatement(new MethodCallExpr(new NameExpr("createMyProduct"), "execute").addArgument("parameterMap"));
    blockStmt1.addStatement(new MethodCallExpr(new NameExpr("createMyProduct"), "finalize").addArgument("parameterMap"));
    blockStmt1.addStatement(new MethodCallExpr(new NameExpr("createMyProduct"), "getResult()"));

    Statement blockStmt2 =addingException(blockStmt1);
    blockStmt.addStatement(blockStmt2);

    restMethod.setBody(blockStmt);

    restMethod.getBody().get().getStatements().add(new ReturnStmt(new NameExpr("Response.status(Response.Status.OK).entity(result).build()")));

    System.out.println(compilationUnit);

  }

  private static Statement addingException(BlockStmt body) {
    TryStmt ts = new TryStmt();
    ts.setTryBlock(body);
    CatchClause cc = new CatchClause();
    String exceptionName = "e";
    cc.setParameter(new Parameter().setName(exceptionName).setType(Exception.class));
    BlockStmt cb = new BlockStmt();
    cb.addStatement(new ExpressionStmt(new NameExpr("return Response.status(Response.Status.BAD_REQUEST).entity(result).build()")));
    // cb.addStatement(new ThrowStmt(new NameExpr(exceptionName)));
    cc.setBody(cb);
    ts.setCatchClauses(new NodeList<>(cc));
    return ts;
  }

}
Aqeel Ashiq
  • 1,988
  • 5
  • 24
  • 57
  • thanks your solution , it is working partially . but got another problem : Problem : After executing your solution i got a additional bracket { after "String result = null;" line. How to can i avoid this unexpected {} bracket. my problematic output is : https://pastebin.mozilla.org/GFAmSa96 – Gulam Samdani Aug 29 '22 at 11:02
  • ah alright I did not notice. Actually you were wrapping the `TryStmt` in another `BlockStmt`. Why don't you just return `TryStmt` without wrapping it. I have updated my answer doing the same. – Aqeel Ashiq Aug 29 '22 at 11:28
  • ah got it the cause of this { issue. I understand that i have to return TryStmt without wrapping it. But How can i do this . TryStmt ts = new TryStmt(); ts.setTryBlock(body); can you please give me idea – Gulam Samdani Aug 29 '22 at 11:40
  • i am facing another problem in this class . How can i add @AutoWired annotation in class'es field level . https://pastebin.mozilla.org/XYvmeD1r – Gulam Samdani Aug 29 '22 at 18:59
  • @GulamSamdani That is a different question. Please post it as a different question.. And share the question link here. I'll check if I find something... – Aqeel Ashiq Aug 30 '22 at 08:02
  • i have create a new question . https://stackoverflow.com/questions/73561220/how-could-i-inherit-a-class-using-github-parser-without-deprication-warrings . @Syed Aqeel Ashiq – Gulam Samdani Aug 31 '22 at 19:55