@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 ); }
/** * This method will add methods, fields and import statement to existing java file * * @throws IOException * @throws ParseException */ public void insertCode() throws IOException, ParseException { CompilationUnit cuResult = JavaParser.parse(baseFile); if (cuResult.getImports() != null) { List<ImportDeclaration> importsFromBaseFile = cuResult.getImports(); for (ImportDeclaration eachImport : importsFromExtendedFile) { if (!importAlreadyPresent(importsFromBaseFile, eachImport)) { importsFromBaseFile.add(eachImport); } } cuResult.setImports(importsFromBaseFile); } String code = cuResult.toString(); BufferedWriter b = new BufferedWriter(new FileWriter(baseFile)); b.write(code); b.close(); }
public CompilationUnit(PackageDeclaration pakage, List<ImportDeclaration> imports, List<TypeDeclaration> types) { setPackage(pakage); setImports(imports); setTypes(types); }
/** * 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); }
@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; }
@Override public R visit(final CompilationUnit n, final A arg) { if (n.getPackage() != null) { R result = n.getPackage().accept(this, arg); if (result != null) { return result; if (n.getImports() != null) { for (final ImportDeclaration i : n.getImports()) { if (n.getTypes() != null) { for (final TypeDeclaration typeDeclaration : n.getTypes()) {
@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; }
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 String packageNameFromCompilationUnit() { PackageDeclaration packageDeclaration = compilationUnit.getPackage(); if (packageDeclaration == null) { throw new IllegalArgumentException("Source file does not declare a package. Default package is unsupported."); } return packageDeclaration.getName().toString(); } }
final List<ImportDeclaration> imports = unit.getImports() != null ? unit.getImports() : new ArrayList<ImportDeclaration>(); final boolean foundAnnImport = imported(imports, fullyQualifiedName); unit.setImports(imports); final TypeDeclaration parent = unit.getTypes().get(0);
final String enclosingName = n.getPackage().getName() + "." + n.getTypes().get(0).getName();
public void build() { UnitChromatticVisitor unitChromatticVisitor = new UnitChromatticVisitor(); compilationUnit.getPackage().getName().setName(compilationUnit.getPackage().getName().getName() + ".groovy"); unitChromatticVisitor.visit(compilationUnit, null); sb.append(compilationUnit.toString(new GroovyCompatibilityFactory())); }
public void build(DumpVisitorFactory factory, List<String> excludedMethods) { UnitTestVisitor unitTestVisitor = new UnitTestVisitor(name); // Iterator<ImportDeclaration> itImports = compilationUnit.getImports().iterator(); while (itImports.hasNext()) { if (GroovyTestGeneration.class.getName().equals(itImports.next().getName().toString())) { itImports.remove(); } } // for (String dep : deps) { int i = dep.lastIndexOf("."); String depPackage = dep.substring(0, i) + ".groovy"; String depImport = depPackage + dep.substring(i); compilationUnit.getImports().add(new ImportDeclaration(new NameExpr(depImport), false, false)); } // unitTestVisitor.visit(compilationUnit, excludedMethods); sb.append(compilationUnit.toString(factory)); }
/** * Comments are attributed to the thing the comment and are removed from * allComments. */ private static void insertCommentsInCu(CompilationUnit cu, CommentsCollection commentsCollection){ if (commentsCollection.size()==0) return; // I should sort all the direct children and the comments, if a comment is the first thing then it // a comment to the CompilationUnit // FIXME if there is no package it could be also a comment to the following class... // so I could use some heuristics in these cases to distinguish the two cases List<Comment> comments = commentsCollection.getAll(); sortByBeginPosition(comments); List<Node> children = cu.getChildrenNodes(); sortByBeginPosition(children); if (cu.getPackage()!=null && (children.size()==0 || areInOrder(comments.get(0), children.get(0)))){ cu.setComment(comments.get(0)); comments.remove(0); } insertCommentsInNode(cu,comments); }
private void initialize() throws IOException, ParseException { CompilationUnit cu = JavaParser.parse(file); if (cu.getImports() != null) { imports = cu.getImports(); } initializeFieldDetails(cu); initializeMethodDetails(cu); }
public void assertSave() { assertSave(cu.toString()); }
throw new ParseException(); CompilationUnit tmp = new CompilationUnit(line == -1 ? 0 : line, column, token.endLine, token.endColumn,pakage, imports, types);
@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 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(); } } } }