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(); } }
@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 PackageDeclaration assertPackage() { return assertCompilationUnit().getPackage(); }
final String enclosingName = n.getPackage().getName() + "." + n.getTypes().get(0).getName();
public PackageDeclaration assertPackage() { return assertCompilationUnit().getPackage(); }
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())); }
/** * 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); }
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(); } } } }
@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); }
@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 R visit(final CompilationUnit n, final A arg) { if (n.getPackage() != null) { R result = n.getPackage().accept(this, arg); if (result != null) { return result;
@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 final NodeData resolve(Node node, String mappedClass) { if (!(node instanceof FieldDeclaration)) { throw new IllegalArgumentException("this annotation belongs only on FieldDeclaration"); } final FieldDeclaration field = (FieldDeclaration) node; if (ResolverUtil.canFieldBeAnnotated(field)) { final TypeDeclaration dclr = (TypeDeclaration) node.getParentNode(); final String name = dclr.getName(); final String pckg = ((CompilationUnit) dclr.getParentNode()).getPackage().getName().toString(); final String fullyQualifiedClass = pckg + "." + name; final boolean mappedColumn = OjbUtil.isMappedColumn(mappedClass, ParserUtil.getFieldName(field), descriptorRepositories); if (mappedColumn) { return getAnnotationNodes(fullyQualifiedClass, ParserUtil.getFieldName(field), mappedClass); } } return null; }
@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 ); }
@Override public NodeData resolve(Node node, String mappedClass) { if (!(node instanceof ClassOrInterfaceDeclaration)) { throw new IllegalArgumentException("this annotation belongs only on ClassOrInterfaceDeclaration"); } final TypeDeclaration dclr = (TypeDeclaration) node; if (!(dclr.getParentNode() instanceof CompilationUnit)) { //handling nested classes return null; } final String name = dclr.getName(); final String pckg = ((CompilationUnit) dclr.getParentNode()).getPackage().getName().toString(); final String enclosingClass = pckg + "." + name; final Collection<String> customizedFieldsOnNode = getFieldsOnNode(dclr, getCustomizedFields(mappedClass)); if (customizedFieldsOnNode == null || customizedFieldsOnNode.isEmpty()) { LOG.info(ResolverUtil.logMsgForClass(enclosingClass, mappedClass) + " has no customized fields"); return null; } return new NodeData(new SingleMemberAnnotationExpr(new NameExpr(SIMPLE_NAME), new NameExpr("CreateCustomizerFor" + customizedFieldsOnNode.toString())), new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false)); }
@Override public NodeData resolve(Node node, String mappedClass) { if (!(node instanceof ClassOrInterfaceDeclaration)) { throw new IllegalArgumentException("this annotation belongs only on ClassOrInterfaceDeclaration"); } final TypeDeclaration dclr = (TypeDeclaration) node; if (!(dclr.getParentNode() instanceof CompilationUnit)) { //handling nested classes return null; } final String name = dclr.getName(); final String pckg = ((CompilationUnit) dclr.getParentNode()).getPackage().getName().toString(); final String enclosingClass = pckg + "." + name; if (!enclosingClass.equals(mappedClass)) { return new NodeData(new MarkerAnnotationExpr(new NameExpr(SIMPLE_NAME)), new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false)); } return null; } }
@Override public NodeData resolve(Node node, String mappedClass) { if (!(node instanceof ClassOrInterfaceDeclaration)) { throw new IllegalArgumentException("this annotation belongs only on ClassOrInterfaceDeclaration"); } final TypeDeclaration dclr = (TypeDeclaration) node; if (!(dclr.getParentNode() instanceof CompilationUnit)) { //handling nested classes return null; } final String name = dclr.getName(); final String pckg = ((CompilationUnit) dclr.getParentNode()).getPackage().getName().toString(); final String enclosingClass = pckg + "." + name; final ClassDescriptor cd = OjbUtil.findClassDescriptor(enclosingClass, descriptorRepositories); if (cd != null) { final String tableName = getMappedTable(enclosingClass); if (tableName == null) { LOG.error(ResolverUtil.logMsgForClass(enclosingClass, mappedClass) + " table could not be found"); return null; } return new NodeData(new NormalAnnotationExpr(new NameExpr(SIMPLE_NAME), Collections.singletonList(new MemberValuePair("name", new StringLiteralExpr(upperCaseTableName ? tableName.toUpperCase() : tableName)))), new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false)); } return null; }
@Override public NodeData resolve(Node node, String mappedClass) { if (!(node instanceof ClassOrInterfaceDeclaration)) { throw new IllegalArgumentException("this annotation belongs only on ClassOrInterfaceDeclaration"); } final TypeDeclaration dclr = (TypeDeclaration) node; if (!(dclr.getParentNode() instanceof CompilationUnit)) { //handling nested classes return null; } final String name = dclr.getName(); final String pckg = ((CompilationUnit) dclr.getParentNode()).getPackage().getName().toString(); final String enclosingClass = pckg + "." + name; final ClassDescriptor cd = OjbUtil.findClassDescriptor(enclosingClass, descriptorRepositories); if (cd != null) { return new NodeData(new MarkerAnnotationExpr(new NameExpr(SIMPLE_NAME)), new ImportDeclaration(new QualifiedNameExpr(new NameExpr(PACKAGE), SIMPLE_NAME), false, false)); } return null; } }
assertTrue(file.renameTo(tmp)); JavaFile javaFile = helper.assertJavaSource("metamodel.controller.sub.A"); javaFile.assertCompilationUnit().getPackage().setName(ASTHelper.createNameExpr("metamodel.controller.sub")); javaFile.assertSave();
assertTrue(file.renameTo(tmp)); JavaFile javaFile = helper.assertJavaSource("metamodel.controller.sub.A"); javaFile.assertCompilationUnit().getPackage().setName(ASTHelper.createNameExpr("metamodel.controller.sub")); javaFile.assertSave();