Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

javaparser

public static void main(String[] args) throws Exception {
    // creates an input stream for the file to be parsed
    FileInputStream in = new FileInputStream("test.java");

    CompilationUnit cu;
    try {
        // parse the file
        cu = JavaParser.parse(in);
    } finally {
        in.close();
    }

    // prints the resulting compilation unit to default system output
    System.out.println(cu.toString());
}
Comment

javaparser

public static void main(String[] args) throws Exception {
    // creates an input stream for the file to be parsed
    FileInputStream in = new FileInputStream("test.java");

    CompilationUnit cu;
    try {
        // parse the file
        cu = JavaParser.parse(in);
    } finally {
        in.close();
    }

    // visit and print the methods names
    new MethodVisitor().visit(cu, null);
}

/**
 * Simple visitor implementation for visiting MethodDeclaration nodes. 
 */
private static class MethodVisitor extends VoidVisitorAdapter {

    @Override
    public void visit(MethodDeclaration n, Object arg) {
        // here you can access the attributes of the method.
        // this method will be called for all methods in this 
        // CompilationUnit, including inner class methods
        System.out.println(n.getName());
    }
}
Comment

javaparser

public static void main(String[] args) throws Exception {
    // creates an input stream for the file to be parsed
    FileInputStream in = new FileInputStream("test.java");

    CompilationUnit cu;
    try {
        // parse the file
        cu = JavaParser.parse(in);
    } finally {
        in.close();
    }

    // visit and change the methods names and parameters
    new MethodChangerVisitor().visit(cu, null);

    // prints the changed compilation unit
    System.out.println(cu.toString());
}

/**
 * Simple visitor implementation for visiting MethodDeclaration nodes.
 */
private static class MethodChangerVisitor extends VoidVisitorAdapter {

    @Override
    public void visit(MethodDeclaration n, Object arg) {
        // change the name of the method to upper case
        n.setName(n.getName().toUpperCase());

        // create the new parameter
        Parameter newArg = ASTHelper.createParameter(ASTHelper.INT_TYPE, "value");

        // add the parameter to the method
        ASTHelper.addParameter(n, newArg);
    }

}
Comment

javaparser

public static void main(String[] args) throws Exception {
    // creates an input stream for the file to be parsed
    FileInputStream in = new FileInputStream("test.java");

    CompilationUnit cu;
    try {
        // parse the file
        cu = JavaParser.parse(in);
    } finally {
        in.close();
    }

    // change the methods names and parameters
    changeMethods(cu);

    // prints the changed compilation unit
    System.out.println(cu.toString());
}

private static void changeMethods(CompilationUnit cu) {
    List<TypeDeclaration> types = cu.getTypes();
    for (TypeDeclaration type : types) {
        List<BodyDeclaration> members = type.getMembers();
        for (BodyDeclaration member : members) {
            if (member instanceof MethodDeclaration) {
                MethodDeclaration method = (MethodDeclaration) member;
                changeMethod(method);
            }
        }
    }
}

private static void changeMethod(MethodDeclaration n) {
    // change the name of the method to upper case
    n.setName(n.getName().toUpperCase());

    // create the new parameter
    Parameter newArg = ASTHelper.createParameter(ASTHelper.INT_TYPE, "value");

    // add the parameter to the method
    ASTHelper.addParameter(n, newArg);
}
Comment

javaparser

public static void main(String[] args) throws Exception {
    // creates the compilation unit
    CompilationUnit cu = createCU();

    // prints the created compilation unit
    System.out.println(cu.toString());
}

/**
 * creates the compilation unit
 */
private static CompilationUnit createCU() {
    CompilationUnit cu = new CompilationUnit();
    // set the package
    cu.setPakage(new PackageDeclaration(ASTHelper.createNameExpr("java.parser.test")));

    // create the type declaration 
    ClassOrInterfaceDeclaration type = new ClassOrInterfaceDeclaration(ModifierSet.PUBLIC, false, "GeneratedClass");
    ASTHelper.addTypeDeclaration(cu, type);

    // create a method
    MethodDeclaration method = new MethodDeclaration(ModifierSet.PUBLIC, ASTHelper.VOID_TYPE, "main");
    method.setModifiers(ModifierSet.addModifier(method.getModifiers(), ModifierSet.STATIC));
    ASTHelper.addMember(type, method);

    // add a parameter to the method
    Parameter param = ASTHelper.createParameter(ASTHelper.createReferenceType("String", 0), "args");
    param.setVarArgs(true);
    ASTHelper.addParameter(method, param);

    // add a body to the method
    BlockStmt block = new BlockStmt();
    method.setBody(block);

    // add a statement do the method body
    NameExpr clazz = new NameExpr("System");
    FieldAccessExpr field = new FieldAccessExpr(clazz, "out");
    MethodCallExpr call = new MethodCallExpr(field, "println");
    ASTHelper.addArgument(call, new StringLiteralExpr("Hello World!"));
    ASTHelper.addStmt(block, call);

    return cu;
}
Comment

PREVIOUS NEXT
Code Example
Java :: java program to search item from name in class 
Java :: android snackbar message is behind back button 
Java :: java long data type 
Java :: java resultset to table 
Java :: méthode retourne nom classe java 
Java :: java background color 
Java :: refresh sharedpreferences going back java 
Java :: double if statement java 
Java :: bool in java 
Java :: java number reverse 
Java :: fill array java 
Java :: java continue statement 
Java :: Card view only top corner radius 
Java :: array of arraylist in java 
Java :: java timeout exception 
Java :: java arraylist initialize with 0 
Java :: priority queue java 
Java :: java final variable 
Java :: java switch statement 
Java :: java code for logarithm 
Java :: meaning of instantiated in java 
Java :: each loop in java 
Java :: find minimum element in a sorted and rotated array 
Java :: java decler variabel 
Java :: advantages of using java 
Java :: creating a properties object using a file 
Java :: minecraft addlayer(u) in the type rendererlivingentity<t is not applicable for the arguments 
Java :: Print Positives of array 
Java :: search and delete class files from jars 
Java :: intergers are appearing as string in Json 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =