javaparser

How could i inherit a class using github.parser without deprication warrings


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 {

}


Solution

  • 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);