/** * Creates a new {@link Parameter}. * * @param type * type of the parameter * @param name * name of the parameter * @return instance of {@link Parameter} */ public static Parameter createParameter(Type type, String name) { return new Parameter(type, new VariableDeclaratorId(name)); }
/** * Creates a {@link VariableDeclarationExpr}. * * @param type * type * @param name * name * @return instance of {@link VariableDeclarationExpr} */ public static VariableDeclarationExpr createVariableDeclarationExpr(Type type, String name) { List<VariableDeclarator> vars = new ArrayList<VariableDeclarator>(); vars.add(new VariableDeclarator(new VariableDeclaratorId(name))); return new VariableDeclarationExpr(type, vars); }
public class ClassCreator { 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.setPackage(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 field FieldDeclaration field = new FieldDeclaration(ModifierSet.PUBLIC, new ClassOrInterface(String.class.getName()), new VariableDeclarator(new VariableDeclaratorId("variableName"))) ASTHelper.addMember(type, field); return cu; } }
/** * Creates a {@link FieldDeclaration}. * * @param modifiers * modifiers * @param type * type * @param name * field name * @return instance of {@link FieldDeclaration} */ public static FieldDeclaration createFieldDeclaration(int modifiers, Type type, String name) { VariableDeclaratorId id = new VariableDeclaratorId(name); VariableDeclarator variable = new VariableDeclarator(id); return createFieldDeclaration(modifiers, type, variable); }
final public VariableDeclaratorId VariableDeclaratorId() throws ParseException { String name; int arrayCount = 0; int line; int column; jj_consume_token(IDENTIFIER); name = token.image; line=token.beginLine; column=token.beginColumn; label_14: while (true) { switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case LBRACKET: ; break; default: jj_la1[32] = jj_gen; break label_14; } jj_consume_token(LBRACKET); jj_consume_token(RBRACKET); arrayCount++; } {if (true) return new VariableDeclaratorId(line, column, token.endLine, token.endColumn,name, arrayCount);} throw new Error("Missing return statement in function"); }
final public VariableDeclaratorId VariableDeclaratorId() throws ParseException { String name; int arrayCount = 0; int line; int column; jj_consume_token(IDENTIFIER); name = token.image; line=token.beginLine; column=token.beginColumn; label_14: while (true) { switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case LBRACKET: ; break; default: jj_la1[32] = jj_gen; break label_14; } jj_consume_token(LBRACKET); jj_consume_token(RBRACKET); arrayCount++; } VariableDeclaratorId tmp = new VariableDeclaratorId(line, column, token.endLine, token.endColumn,name, arrayCount); {if (true) return tmp;} throw new Error("Missing return statement in function"); }
private NodeAndImports<MethodDeclaration> createPrimaryKeyCompareTo(Collection<FieldDescriptor> primaryKeyDescriptors, String enclosingClassName) { final MethodDeclaration compareTo = new MethodDeclaration(ModifierSet.PUBLIC, new PrimitiveType(PrimitiveType.Primitive.Int), "compareTo", Collections.singletonList(new Parameter(new ClassOrInterfaceType(enclosingClassName), new VariableDeclaratorId("other")))); compareTo.setAnnotations(Collections.<AnnotationExpr>singletonList(new MarkerAnnotationExpr(new NameExpr("Override")))); Expression compareToBuilderExpr = new ObjectCreationExpr(null, new ClassOrInterfaceType("CompareToBuilder"), Collections.<Expression>emptyList()); for (FieldDescriptor f : primaryKeyDescriptors) { final List<Expression> args = new ArrayList<Expression>(); args.add(new FieldAccessExpr(new ThisExpr(), f.getAttributeName())); args.add(new FieldAccessExpr(new NameExpr("other"), f.getAttributeName())); compareToBuilderExpr = new MethodCallExpr(compareToBuilderExpr, "append", args); } compareToBuilderExpr = new MethodCallExpr(compareToBuilderExpr, "toComparison"); final List<Statement> statements = new ArrayList<Statement>(); statements.add(new ReturnStmt(compareToBuilderExpr)); final BlockStmt equalsBody = new BlockStmt(statements); compareTo.setBody(equalsBody); return new NodeAndImports<MethodDeclaration>(compareTo, Collections.singleton(new ImportDeclaration(new QualifiedNameExpr(new NameExpr("org.apache.commons.lang.builder"), "CompareToBuilder"), false, false))); }
FieldDeclaration fieldDeclaration = new FieldDeclaration(Modifier.PRIVATE, fieldInfo.getType(), new VariableDeclarator(new VariableDeclaratorId(fieldName(fieldInfo.getName())))); fieldDeclaration.setAnnotations(new ArrayList<AnnotationExpr>()); if (fieldInfo.getAnnotationExprs() != null) {
final String attrName = fd.getAttributeName(); members.add(new FieldDeclaration(ModifierSet.PRIVATE, new ClassOrInterfaceType(simpleTypeName), new VariableDeclarator(new VariableDeclaratorId(attrName)))); Collections.singletonList(new Parameter(new ClassOrInterfaceType(simpleTypeName), new VariableDeclaratorId(attrName))));
@Override public Node visit(VariableDeclaratorId _n, Object _arg) { Comment comment = cloneNodes(_n.getComment(), _arg); VariableDeclaratorId r = new VariableDeclaratorId( _n.getBeginLine(), _n.getBeginColumn(), _n.getEndLine(), _n.getEndColumn(), _n.getName(), _n.getArrayCount() ); r.setComment(comment); return r; }
private NodeAndImports<MethodDeclaration> createPrimaryKeyEquals(Collection<FieldDescriptor> primaryKeyDescriptors, String enclosingClassName) { final MethodDeclaration equals = new MethodDeclaration(ModifierSet.PUBLIC, new PrimitiveType(PrimitiveType.Primitive.Boolean), "equals", Collections.singletonList(new Parameter(new ClassOrInterfaceType("Object"), new VariableDeclaratorId("other")))); equals.setAnnotations(Collections.<AnnotationExpr>singletonList(new MarkerAnnotationExpr(new NameExpr("Override")))); final Statement ifEqualNullStmt = new IfStmt(new BinaryExpr(new NameExpr("other"), new NullLiteralExpr(), BinaryExpr.Operator.equals), new ReturnStmt(new BooleanLiteralExpr(false)), null); final Statement ifEqualThisStmt = new IfStmt(new BinaryExpr(new NameExpr("other"), new ThisExpr(), BinaryExpr.Operator.equals), new ReturnStmt(new BooleanLiteralExpr(true)), null); final Statement ifEqualClassStmt = new IfStmt(new BinaryExpr(new MethodCallExpr(new NameExpr("other"), "getClass"), new MethodCallExpr(new ThisExpr(), "getClass"), BinaryExpr.Operator.notEquals), new ReturnStmt(new BooleanLiteralExpr(false)), null); final Statement rhsStmt = new ExpressionStmt(new VariableDeclarationExpr(ModifierSet.FINAL, new ClassOrInterfaceType(enclosingClassName), Collections.singletonList(new VariableDeclarator( new VariableDeclaratorId("rhs"), new CastExpr(new ClassOrInterfaceType(enclosingClassName), new NameExpr("other")))))); Expression equalsBuilderExpr = new ObjectCreationExpr(null, new ClassOrInterfaceType("EqualsBuilder"), Collections.<Expression>emptyList()); for (FieldDescriptor f : primaryKeyDescriptors) { final List<Expression> args = new ArrayList<Expression>(); args.add(new FieldAccessExpr(new ThisExpr(), f.getAttributeName())); args.add(new FieldAccessExpr(new NameExpr("rhs"), f.getAttributeName())); equalsBuilderExpr = new MethodCallExpr(equalsBuilderExpr, "append", args); } equalsBuilderExpr = new MethodCallExpr(equalsBuilderExpr, "isEquals"); final List<Statement> statements = new ArrayList<Statement>(); statements.add(ifEqualNullStmt); statements.add(ifEqualThisStmt); statements.add(ifEqualClassStmt); statements.add(rhsStmt); statements.add(new ReturnStmt(equalsBuilderExpr)); final BlockStmt equalsBody = new BlockStmt(statements); equals.setBody(equalsBody); return new NodeAndImports<MethodDeclaration>(equals, Collections.singleton(new ImportDeclaration(new QualifiedNameExpr(new NameExpr("org.apache.commons.lang.builder"), "EqualsBuilder"), false, false))); }