1

I am using Com.GitHub.java parser for generating java code. i am facing a problem for generating extends keywords This line "extendsList.add(new ClassOrInterfaceType("CustomEndpointResource"));". This statement is showing deprecated .that means it gives a warning. How can i avoid this warning ? So, i can not use this statement. any alternative way other than this deprecated statement (extendsList.add(new ClassOrInterfaceType).

My Source code:

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.expr.StringLiteralExpr;
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.addSingleMemberAnnotation("Path",new StringLiteralExpr("myproduct"));
        restEndpointClass.addMarkerAnnotation("RequestScoped");
        
        
        var injectedfield=restEndpointClass.addField("CreateMyProduct", "CreateMyProduct", Modifier.Keyword.PRIVATE);
        injectedfield.addMarkerAnnotation("Inject");
        
        NodeList<ClassOrInterfaceType> extendsList = new NodeList<>();
        extendsList.add(new ClassOrInterfaceType("CustomEndpointResource"));
        restEndpointClass.setExtendedTypes(extendsList);
​
        
    
        System.out.println(compilationUnit);
​
    }
}

Expected output of my code:

class productCreate extends ABC {

}

2 Answers2

0

There can be multiple ways to avoid using the deprecated constructor. E.g. you can use the following instead:

    ClassOrInterfaceType extendClass = new ClassOrInterfaceType();
    extendClass.setName(new SimpleName("CustomEndpointResource"));
    extendsList.add(extendClass);
Aqeel Ashiq
  • 1,988
  • 5
  • 24
  • 57
0

i am facing another problem for generating this annotation line : @Path("/{uuid}").

Expected output of my code:

class productGet {

 @Path("/{uuid}")
public Response getProduct() throws ServletException {

}

}

  • You added this question as an answer. Add this to your question. Also Add source code for this method and annotation in your question. So that we can copy your source code and run it and modify it to answer your question. – Aqeel Ashiq Sep 03 '22 at 10:40