/** * Creates a {@link FieldDeclaration}. * * @param modifiers * modifiers * @param type * type * @param variable * variable declarator * @return instance of {@link FieldDeclaration} */ public static FieldDeclaration createFieldDeclaration(int modifiers, Type type, VariableDeclarator variable) { List<VariableDeclarator> variables = new ArrayList<VariableDeclarator>(); variables.add(variable); FieldDeclaration ret = new FieldDeclaration(modifiers, type, variables); return ret; }
/** * In Java variables can be defined like the following: * int i, j, k; * * When mapping fields in xml this is not a problem. However when using annotation on a field, * Each field should be defined separately. This helper will deconstruct these fields such * that later AST analysis will not need to account for field defined on a separate line. */ public static void deconstructMultiDeclarations(Collection<FieldDeclaration> fields) { for (FieldDeclaration field : fields) { ClassOrInterfaceDeclaration parent = (ClassOrInterfaceDeclaration) field.getParentNode(); //these are chained together if (field.getVariables().size() > 1) { int index = parent.getMembers().indexOf(field); parent.getMembers().remove(index); List<FieldDeclaration> deconstructed = new ArrayList<FieldDeclaration>(); for (VariableDeclarator v : field.getVariables()) { FieldDeclaration f = new FieldDeclaration(field.getJavaDoc(), field.getModifiers(), field.getAnnotations(), field.getType(), Collections.singletonList(v)); f.setComment(field.getComment()); f.setParentNode(field.getParentNode()); deconstructed.add(f); } parent.getMembers().addAll(index, deconstructed); } } }
final public FieldDeclaration FieldDeclaration(Modifier modifier) throws ParseException { Type type; List variables = new LinkedList(); VariableDeclarator val; // Modifiers are already matched in the caller type = Type(); val = VariableDeclarator(); variables.add(val); label_13: while (true) { switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case COMMA: ; break; default: jj_la1[30] = jj_gen; break label_13; } jj_consume_token(COMMA); val = VariableDeclarator(); variables.add(val); } jj_consume_token(SEMICOLON); int line = modifier.beginLine; int column = modifier.beginColumn; if (line == -1) { line=type.getBeginLine(); column=type.getBeginColumn(); } FieldDeclaration tmp = new FieldDeclaration(line, column, token.endLine, token.endColumn, modifier.modifiers, modifier.annotations, type, variables); {if (true) return tmp;} throw new Error("Missing return statement in function"); }
final public FieldDeclaration FieldDeclaration(Modifier modifier) throws ParseException { Type type; List variables = new LinkedList(); VariableDeclarator val; // Modifiers are already matched in the caller type = Type(); val = VariableDeclarator(); variables.add(val); label_13: while (true) { switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case COMMA: ; break; default: jj_la1[30] = jj_gen; break label_13; } jj_consume_token(COMMA); val = VariableDeclarator(); variables.add(val); } jj_consume_token(SEMICOLON); int line = modifier.beginLine; int column = modifier.beginColumn; if (line == -1) { line=type.getBeginLine(); column=type.getBeginColumn(); } {if (true) return new FieldDeclaration(line, column, token.endLine, token.endColumn, popJavadoc(), modifier.modifiers, modifier.annotations, type, variables);} throw new Error("Missing return statement in function"); }
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))));
@Override public Node visit(FieldDeclaration _n, Object _arg) { JavadocComment javaDoc = cloneNodes(_n.getJavaDoc(), _arg); List<AnnotationExpr> annotations = visit(_n.getAnnotations(), _arg); Type type_ = cloneNodes(_n.getType(), _arg); List<VariableDeclarator> variables = visit(_n.getVariables(), _arg); Comment comment = cloneNodes(_n.getComment(), _arg); FieldDeclaration r = new FieldDeclaration( _n.getBeginLine(), _n.getBeginColumn(), _n.getEndLine(), _n.getEndColumn(), _n.getModifiers(), annotations, type_, variables ); r.setComment(comment); return r; }