private TypeDeclaration typeDeclarationFromCompilationUnit() { List<TypeDeclaration> typeDeclarations = compilationUnit.getTypes(); if (typeDeclarations == null) { throw new IllegalArgumentException("Source file does not contain a Java class"); } if (typeDeclarations.size() > 1) { throw new IllegalArgumentException("Source file contains more than one Java class"); } return typeDeclarations.get(0); }
private void initializeFieldDetails(CompilationUnit cu) { List<TypeDeclaration> types = cu.getTypes(); for (TypeDeclaration type : types) { List<BodyDeclaration> members = type.getMembers(); for (BodyDeclaration member : members) { if (member instanceof FieldDeclaration) { FieldDeclaration field = (FieldDeclaration) member; fields.add(field); } } } }
/** * Adds the given type declaration to the compilation unit. The list of * types will be initialized if it is <code>null</code>. * * @param cu * compilation unit * @param type * type declaration */ public static void addTypeDeclaration(CompilationUnit cu, TypeDeclaration type) { List<TypeDeclaration> types = cu.getTypes(); if (types == null) { types = new ArrayList<TypeDeclaration>(); cu.setTypes(types); } types.add(type); }
private String getTypeNameForMsg(final Node n) { final CompilationUnit unit = getCompilationUnit(n); final TypeDeclaration parent = unit.getTypes().get(0); return parent.getName(); } }
private void initializeMethodDetails(CompilationUnit cu) { String className = file.getName(); className = className.substring(0, className.indexOf(".")); 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; method.setJavaDoc(new JavadocComment("@deprecated This method is moved to the class {@link " + className + "}")); methods.add(method); } } } }
final String enclosingName = n.getPackage().getName() + "." + n.getTypes().get(0).getName();
final CompilationUnit compilationUnit = JavaParser.parse(reader, true); final List<TypeDeclaration> typeDeclarations = compilationUnit .getTypes(); if (typeDeclarations.size() > 1) { throw new ParseException(
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; }
@Override public Node visit(final CompilationUnit n, final A arg) { if (n.getPackage() != null) { n.setPackage((PackageDeclaration) n.getPackage().accept(this, arg)); } final List<ImportDeclaration> imports = n.getImports(); if (imports != null) { for (int i = 0; i < imports.size(); i++) { imports.set(i, (ImportDeclaration) imports.get(i).accept(this, arg)); } removeNulls(imports); } final List<TypeDeclaration> types = n.getTypes(); if (types != null) { for (int i = 0; i < types.size(); i++) { types.set(i, (TypeDeclaration) types.get(i).accept(this, arg)); } removeNulls(types); } return n; }
public void visit(CompilationUnit n, Object arg) { if (n.getPackage() != null) { n.getPackage().accept(this, arg); } if (n.getImports() != null) { for (ImportDeclaration i : n.getImports()) { i.accept(this, arg); } printer.printLn(); } if (n.getTypes() != null) { for (Iterator<TypeDeclaration> i = n.getTypes().iterator(); i.hasNext();) { i.next().accept(this, arg); printer.printLn(); if (i.hasNext()) { printer.printLn(); } } } }
public ClassOrInterfaceDeclaration assertDeclaration() { List<TypeDeclaration> decls = assertCompilationUnit().getTypes(); AbstractTestCase.assertEquals(1, decls.size()); TypeDeclaration decl = decls.get(0); return AbstractTestCase.assertInstanceOf(ClassOrInterfaceDeclaration.class, decl); }
@Override public void visit(final CompilationUnit n, final Object arg) { printJavaComment(n.getComment(), arg); if (n.getPackage() != null) { n.getPackage().accept(this, arg); } if (n.getImports() != null) { for (final ImportDeclaration i : n.getImports()) { i.accept(this, arg); } printer.printLn(); } if (n.getTypes() != null) { for (final Iterator<TypeDeclaration> i = n.getTypes().iterator(); i.hasNext();) { i.next().accept(this, arg); printer.printLn(); if (i.hasNext()) { printer.printLn(); } } } printOrphanCommentsEnding(n); }
public ClassOrInterfaceDeclaration assertDeclaration() { List<TypeDeclaration> decls = assertCompilationUnit().getTypes(); AbstractTestCase.assertEquals(1, decls.size()); TypeDeclaration decl = decls.get(0); return AbstractTestCase.assertInstanceOf(ClassOrInterfaceDeclaration.class, decl); }
@Override public void visit(final CompilationUnit n, final A arg) { visitComment(n.getComment(), arg); if (n.getPackage() != null) { n.getPackage().accept(this, arg); } if (n.getImports() != null) { for (final ImportDeclaration i : n.getImports()) { i.accept(this, arg); } } if (n.getTypes() != null) { for (final TypeDeclaration typeDeclaration : n.getTypes()) { typeDeclaration.accept(this, arg); } } }
@Override public Boolean visit(final CompilationUnit n1, final Node arg) { final CompilationUnit n2 = (CompilationUnit) arg; if (!nodeEquals(n1.getPackage(), n2.getPackage())) { return Boolean.FALSE; } if (!nodesEquals(n1.getImports(), n2.getImports())) { return Boolean.FALSE; } if (!nodesEquals(n1.getTypes(), n2.getTypes())) { return Boolean.FALSE; } if (!nodesEquals(n1.getComments(), n2.getComments())) { return Boolean.FALSE; } return Boolean.TRUE; }
final TypeDeclaration parent = unit.getTypes().get(0);
@Override public Node visit(CompilationUnit _n, Object _arg) { PackageDeclaration package_ = cloneNodes(_n.getPackage(), _arg); List<ImportDeclaration> imports = visit(_n.getImports(), _arg); List<TypeDeclaration> types = visit(_n.getTypes(), _arg); return new CompilationUnit( _n.getBeginLine(), _n.getBeginColumn(), _n.getEndLine(), _n.getEndColumn(), package_, imports, types ); }