Creating an invalid AST in JavaParser

107 views Asked by At

I am using the JavaParser library and would like to be able to create an invalid AST by omitting variable types. I am trying to create the following:

Input code:

package org.javaparser.samples;
public class Simple {
public static void main(String[] args) {
    int numberOne = 5;
    int numberTwo = 10;
}

Output code:

    package org.javaparser.samples;
    public class Simple {
    public static void main(String[] args) {
        ... numberOne = 5;
        ... numberTwo = 10;
    }
}

However after modifying the AST in this way I get an error

com.github.javaparser.ParseProblemException: Encountered unexpected token: "..." "..."
 Was expecting one of:
    "boolean"
    "byte"
    ...

I'm using a ModifierVisitor to change the variable type

 private static class VariableDeclaratorModifier extends ModifierVisitor<Void> {
        @Override
        public VariableDeclarator visit(VariableDeclarator vd, Void arg) {
            super.visit(vd, arg);
            vd.setType("...");
            return vd;
        }
    }

Is there any way I can achieve the desired output with JavaParser?

0

There are 0 answers