public static JClass resolveType(JClassContainer _package, String typeDefinition) { try { FieldDeclaration fieldDeclaration = (FieldDeclaration) JavaParser.parseBodyDeclaration(typeDefinition + " foo;"); ClassOrInterfaceType c = (ClassOrInterfaceType) ((ReferenceType) fieldDeclaration.getType()).getType(); return buildClass(_package, c, 0); } catch (ParseException e) { throw new GenerationException("Couldn't parse type: " + typeDefinition, e); } }
@Override public Boolean visit(final FieldDeclaration n1, final Node arg) { final FieldDeclaration n2 = (FieldDeclaration) arg; // javadoc are checked at CompilationUnit if (n1.getModifiers() != n2.getModifiers()) { return Boolean.FALSE; } if (!nodesEquals(n1.getAnnotations(), n2.getAnnotations())) { return Boolean.FALSE; } if (!nodeEquals(n1.getType(), n2.getType())) { return Boolean.FALSE; } if (!nodesEquals(n1.getVariables(), n2.getVariables())) { return Boolean.FALSE; } return Boolean.TRUE; }
@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; }
/** * 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); } } }
@Override public Node visit(final FieldDeclaration n, final A arg) { if (n.getJavaDoc() != null) { n.setJavaDoc((JavadocComment) n.getJavaDoc().accept(this, arg)); } final List<AnnotationExpr> annotations = n.getAnnotations(); if (annotations != null) { for (int i = 0; i < annotations.size(); i++) { annotations.set(i, (AnnotationExpr) annotations.get(i).accept(this, arg)); } removeNulls(annotations); } n.setType((Type) n.getType().accept(this, arg)); final List<VariableDeclarator> variables = n.getVariables(); for (int i = 0; i < variables.size(); i++) { variables.set(i, (VariableDeclarator) variables.get(i).accept(this, arg)); } removeNulls(variables); return n; }
@Override public void visit(FieldDeclaration n, Object arg) { for (VariableDeclarator var : n.getVariables()) { CGEntry currentVariable = new CGEntry(TYPE.VARIABLE, currentClass, currentMethod, var.getId().getName()); if (isDebug()) System.out.println("V:" + currentVariable); list.add(currentVariable); List<AnnotationExpr> annos = n.getAnnotations(); if (annos != null) { for (AnnotationExpr anno : annos) { if (isDebug()) System.out.println("A:" + anno.getName()); currentVariable.addAnnotation(new CGEntry(TYPE.ANNOTATION, currentClass, currentMethod, anno.getName().getName())); } } } super.visit(n, arg); }
public static String getFieldName(FieldDeclaration field) { if (field.getVariables().size() > 1) { throw new IllegalArgumentException("cannot handle multiple variable declarations on a single line. This should have been cleaned up earlier."); } return field.getVariables().get(0).getId().getName(); }
FieldDeclaration fieldDeclaration = new FieldDeclaration(Modifier.PRIVATE, fieldInfo.getType(), new VariableDeclarator(new VariableDeclaratorId(fieldName(fieldInfo.getName())))); fieldDeclaration.setAnnotations(new ArrayList<AnnotationExpr>()); if (fieldInfo.getAnnotationExprs() != null) { for (AnnotationExpr annotationExpr : fieldInfo.getAnnotationExprs()) { && !annotationExpr.getName().getName().equals("Skip") ) { fieldDeclaration.getAnnotations().add(annotationExpr); if (methodDeclaration.getName().equals(fieldInfo.getName().replace("get", "set"))) { if (methodDeclaration.getAnnotations() != null) fieldDeclaration.getAnnotations().addAll(methodDeclaration.getAnnotations());
@Test public void testRemoveAnnotation() throws Exception { CompilerAssert<File, File> helper = compiler("metamodel.template"); helper.assertCompile(); // JavaFile file = helper.assertJavaSource("metamodel.template.A"); ClassOrInterfaceDeclaration a = file.assertDeclaration(); FieldDeclaration decl = (FieldDeclaration)a.getMembers().get(0); decl.getAnnotations().clear(); file.assertSave(); // File ser = helper.getSourceOutput().getPath("juzu", "metamodel.ser"); MetaModelState unserialize = Tools.unserialize(MetaModelState.class, ser); ModuleMetaModel mm = (ModuleMetaModel)unserialize.metaModel; mm.getQueue().clear(); Tools.serialize(unserialize, ser); // helper.assertCompile(); // unserialize = Tools.unserialize(MetaModelState.class, ser); mm = (ModuleMetaModel)unserialize.metaModel; List<MetaModelEvent> events = mm.getQueue().clear(); assertEquals(1, events.size()); assertEquals(MetaModelEvent.BEFORE_REMOVE, events.get(0).getType()); assertInstanceOf(TemplateMetaModel.class, events.get(0).getObject()); }
/** * 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; }
public static boolean canFieldBeAnnotated(FieldDeclaration node) { final TypeDeclaration dclr = (TypeDeclaration) node.getParentNode(); if (!ModifierSet.isStatic(node.getModifiers()) && dclr.getParentNode() instanceof CompilationUnit) { //handling nested classes return true; } return false; }
@Override public void visit(final FieldDeclaration n, final A arg) { visitComment(n.getComment(), arg); if (n.getJavaDoc() != null) { n.getJavaDoc().accept(this, arg); } if (n.getAnnotations() != null) { for (final AnnotationExpr a : n.getAnnotations()) { a.accept(this, arg); } } n.getType().accept(this, arg); for (final VariableDeclarator var : n.getVariables()) { var.accept(this, arg); } }
@Override public Void visit(FieldDeclaration n, Visit v) { if (n.getVariables().size() == 1) { VariableDeclarator declarator = n.getVariables().get(0); VariableDeclaratorId id = declarator.getId(); NamedMemberSource fieldSource = new NamedMemberSource( id.getName(), clip(n), v.javaDoc(n)); ((Visit.TD)v).stack.getLast().addLast(fieldSource); } else { throw new UnsupportedOperationException("todo"); } return super.visit(n, v); }
@Test public void testRemoveAnnotation() throws Exception { CompilerAssert<File, File> helper = compiler("metamodel.template"); helper.assertCompile(); // JavaFile file = helper.assertJavaSource("metamodel.template.A"); ClassOrInterfaceDeclaration a = file.assertDeclaration(); FieldDeclaration decl = (FieldDeclaration)a.getMembers().get(0); decl.getAnnotations().clear(); file.assertSave(); // File ser = helper.getSourceOutput().getPath("juzu", "metamodel.ser"); MetaModelState unserialize = Tools.unserialize(MetaModelState.class, ser); ModuleMetaModel mm = (ModuleMetaModel)unserialize.metaModel; mm.getQueue().clear(); Tools.serialize(unserialize, ser); // helper.assertCompile(); // unserialize = Tools.unserialize(MetaModelState.class, ser); mm = (ModuleMetaModel)unserialize.metaModel; List<MetaModelEvent> events = mm.getQueue().clear(); assertEquals(1, events.size()); assertEquals(MetaModelEvent.BEFORE_REMOVE, events.get(0).getType()); assertInstanceOf(TemplateMetaModel.class, events.get(0).getObject()); }
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"); }
public void visit(FieldDeclaration n, Object arg) { printJavadoc(n.getJavaDoc(), arg); printMemberAnnotations(n.getAnnotations(), arg); printModifiers(n.getModifiers()); n.getType().accept(this, arg); printer.print(" "); for (Iterator<VariableDeclarator> i = n.getVariables().iterator(); i.hasNext();) { VariableDeclarator var = i.next(); var.accept(this, arg); if (i.hasNext()) { printer.print(", "); } } printer.print(";"); }
private String getTypeOrFieldNameForMsg(final BodyDeclaration n) { if (n instanceof TypeDeclaration) { return ((TypeDeclaration) n).getName(); } else if (n instanceof FieldDeclaration) { final FieldDeclaration fd = (FieldDeclaration) n; //this wont work for nested classes but we should be in nexted classes at this point final CompilationUnit unit = getCompilationUnit(n); final TypeDeclaration parent = unit.getTypes().get(0); Collection<String> variableNames = new ArrayList<String>(); if (fd.getVariables() != null) { for (VariableDeclarator vd : fd.getVariables()) { variableNames.add(vd.getId().getName()); } } return variableNames.size() == 1 ? parent.getName() + "." + variableNames.iterator().next() : parent.getName() + "." + variableNames.toString(); } return null; }
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"); }
public static JClass resolveType(JClassContainer _package, String typeDefinition) { try { FieldDeclaration fieldDeclaration = (FieldDeclaration) JavaParser.parseBodyDeclaration(typeDefinition + " foo;"); ClassOrInterfaceType c = (ClassOrInterfaceType) ((ReferenceType) fieldDeclaration.getType()).getType(); return buildClass(_package, c, 0); } catch (ParseException e) { throw new GenerationException("Couldn't parse type: " + typeDefinition, e); } }
@Override public R visit(final FieldDeclaration n, final A arg) { if (n.getJavaDoc() != null) { R result = n.getJavaDoc().accept(this, arg); if (result != null) { return result; if (n.getAnnotations() != null) { for (final AnnotationExpr a : n.getAnnotations()) { R result = n.getType().accept(this, arg); if (result != null) { return result; for (final VariableDeclarator var : n.getVariables()) {