Example usage for org.eclipse.jdt.core.dom PackageDeclaration getName

List of usage examples for org.eclipse.jdt.core.dom PackageDeclaration getName

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom PackageDeclaration getName.

Prototype

public Name getName() 

Source Link

Document

Returns the package name of this package declaration.

Usage

From source file:ast.StructureParse.java

public void setCode(JTextPane textCode) {
    ASTParser parser = ASTParser.newParser(AST.JLS2);
    parser.setSource(textCode.getText().toCharArray());
    //parser.setSource("/*abc*/".toCharArray());
    parser.setKind(ASTParser.K_COMPILATION_UNIT);

    final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
    _package = new DefaultMutableTreeNode("root");

    codeTree.setRootVisible(false);/*from  w w w. j a v  a 2s . c  o  m*/

    cu.accept(new ASTVisitor() {
        Set names = new HashSet();

        /*
         * Package declaration
        */
        @Override
        public boolean visit(PackageDeclaration node) {
            _package = new DefaultMutableTreeNode(node.getName().toString());
            codeTree.setRootVisible(true);
            System.out.println("Package : " + node.getName().toString());
            return false;
        }

        /*
         * Class Declaration.
         * sub node from package
        */
        @Override
        public boolean visit(TypeDeclaration node) {
            String type;
            if (node.isInterface())
                type = "interface ";
            else
                type = "class ";
            _class = new DefaultMutableTreeNode(type + node.getName());
            System.out.println(type + node.getName());

            _package.add(_class);

            String superClass = node.getSuperclass() + "";
            if (!(superClass.equals("null") || (superClass.equals("")))) {
                _class.add(new DefaultMutableTreeNode("extends " + superClass));
                System.out.println("Extends : " + superClass);
            }

            for (Object getInterface : node.superInterfaces()) {
                _class.add(new DefaultMutableTreeNode("implements " + getInterface));
                System.out.println("Implements : " + getInterface);
            }
            return true;
        }

        /*
         * method declaration
         * sub node from class
        */
        @Override
        public boolean visit(MethodDeclaration node) {
            _method = new DefaultMutableTreeNode(node.getName());
            _class.add(_method);
            System.out.println("MethodDeclaration : " + node.getName());
            return true;
        }

        /*
         * field declaration
         * sub node from class
        */
        //            @Override
        //            public boolean visit(FieldDeclaration node)
        //            {
        //                _field=new DefaultMutableTreeNode(node.toString());
        //                _class.add(_field);
        //                Object o = node.fragments().get(0);
        //                if(o instanceof VariableDeclarationFragment){
        //                    _method.add(new DefaultMutableTreeNode(o.toString()));
        //                }
        //                return false;
        //            }

        /*
         * Variable declaration fragment AST node type
         * used in field declarations
         * local variable declarations
         * sub node from method
        */
        @Override
        public boolean visit(VariableDeclarationFragment node) {
            if (node.getParent() instanceof FieldDeclaration) {
                FieldDeclaration declaration = ((FieldDeclaration) node.getParent());
                _class.add(new DefaultMutableTreeNode(declaration.getType().toString()));
                System.out.println("FieldDeclaration : " + declaration.getType().toString());
            } else {
                _method.add(new DefaultMutableTreeNode(node.toString()));
                System.out.println("VariableDeclarationFragment : " + node.toString());
            }
            return false; // do not continue to avoid usage info
        }

        @Override
        public boolean visit(VariableDeclarationStatement node) {
            _method.add(new DefaultMutableTreeNode(node.toString()));
            System.out.println("VariableDeclarationStatement : " + node.toString());
            return false;
        }

        /*
         * Simple Name
         * A simple name is an identifier other than a keyword, boolean literal ("true", "false") or null literal ("null"). 
        */
        @Override
        public boolean visit(SimpleName node) {
            if (this.names.contains(node.getIdentifier())) {
                _method.add(new DefaultMutableTreeNode(node.toString()));
                System.out.println("SimpleNode Identifier : " + node.toString());
            }
            return false;
        }

        /*
         * Alternate constructor invocation (calling constructor) statement AST node type
         * For JLS2: 
        */
        @Override
        public boolean visit(SuperConstructorInvocation node) {
            _constructorCall = new DefaultMutableTreeNode(node);
            _method.add(_constructorCall);
            System.out.println("SuperConstructorInvocation : " + node);
            return false;
        }

        /*
         * method call
        */
        @Override
        public boolean visit(MethodInvocation node) {
            _methodCall = new DefaultMutableTreeNode(node);
            _method.add(_methodCall);
            System.out.println("MethodInvocation : " + node);
            return false;
        }

        @Override
        public boolean visit(SuperMethodInvocation node) {
            _methodCall = new DefaultMutableTreeNode(node);
            _method.add(_methodCall);
            System.out.println("SuperMethodInvocation : " + node);
            return false;
        }

        @Override
        public boolean visit(ReturnStatement node) {
            _method.add(new DefaultMutableTreeNode(node.toString()));
            System.out.println("ReturnStatement : " + node.toString());
            return false;
        }

        @Override
        public boolean visit(IfStatement node) {
            String elseExist = "";
            _if = new DefaultMutableTreeNode("if (" + node.getExpression() + ")");
            _method.add(_if);
            System.out.println("if : " + node.getExpression());
            enclose(node.getThenStatement().toString(), _if);
            elseExist = node.getElseStatement() + "";
            if (!(elseExist.equals("") || elseExist.equals("null"))) {
                _else = new DefaultMutableTreeNode("else");
                _method.add(_else);
                System.out.println("else : " + node.getElseStatement().toString());
                enclose(node.getElseStatement().toString(), _else);
            }
            return false;
        }

        @Override
        public boolean visit(EnhancedForStatement node) {
            _for = new DefaultMutableTreeNode(
                    "for (" + node.getParameter() + " : " + node.getExpression() + ")");
            _method.add(_for);
            System.out.println("EnchancedFor : (" + node.getParameter() + " : " + node.getExpression() + ")");
            enclose(node.getBody() + "", _for);
            return false;
        }

        @Override
        public boolean visit(ForStatement node) {
            /*
             * because initial may more than 1.
            */
            String initial = "";
            for (int i = 0; i < node.initializers().size(); i++) {
                initial += node.initializers().get(i);
                if (node.initializers().size() - 1 != i)
                    initial += ", ";
            }

            /*
             * because increment may more than 1
            */
            String inc = "";
            for (int i = 0; i < node.updaters().size(); i++) {
                inc += node.updaters().get(i);
                if (node.updaters().size() - 1 != i)
                    inc += ", ";
            }

            _for = new DefaultMutableTreeNode(
                    "for (" + initial + "; " + node.getExpression() + "; " + inc + ")");
            _method.add(_for);
            System.out.println("for (" + initial + "; " + node.getExpression() + "; " + inc + ")");
            enclose(node.getBody().toString(), _for);
            return false;
        }

        @Override
        public boolean visit(WhileStatement node) {
            _while = new DefaultMutableTreeNode("while " + node.getExpression());
            _method.add(_while);
            System.out.println("WhileStatement : " + node.getExpression());
            enclose(node.getBody().toString(), _while);
            return false;
        }

        @Override
        public boolean visit(DoStatement node) {
            _do = new DefaultMutableTreeNode("do");
            _method.add(_do);
            System.out.println("Do");
            enclose(node.getBody().toString(), _do);
            _while = new DefaultMutableTreeNode("while(" + node.getExpression() + ")");
            _method.add(_while);
            System.out.println("WhileDo : " + node.getExpression());
            return false;
        }

        @Override
        public boolean visit(TryStatement node) {
            String ada = "";
            _try = new DefaultMutableTreeNode("try");
            _method.add(_try);
            System.out.println("try");
            enclose(node.getBody().toString(), _try);
            ada = node.getFinally() + "";
            if (!(ada.equals("") || ada.equals("null"))) {
                _final = new DefaultMutableTreeNode("finally");
                _method.add(_final);
                System.out.println("finall");
                enclose(node.getFinally().toString(), _final);
            }
            return false;
        }

        @Override
        public boolean visit(CatchClause node) {
            _catch = new DefaultMutableTreeNode("catch (" + node.getException() + ")");
            _method.add(_catch);
            System.out.println("catch : " + node.getException());
            enclose(node.getBody().toString(), _catch);
            return false;
        }

        @Override
        public boolean visit(Assignment node) {
            _assignment = new DefaultMutableTreeNode(node.toString());
            _method.add(_assignment);
            System.out.println("Assignment : " + node.toString());
            return false;
        }

        @Override
        public boolean visit(ConstructorInvocation node) {
            _constructorCall = new DefaultMutableTreeNode(node.toString());
            _method.add(_constructorCall);
            System.out.println(node.toString());
            return false;
        }

        @Override
        public boolean visit(AnonymousClassDeclaration node) {
            _constructorCall = new DefaultMutableTreeNode(node.toString());
            _method.add(_constructorCall);
            System.out.println("AnonymousClassDeclaration : " + node.toString());
            return false;
        }

        @Override
        public boolean visit(ArrayAccess node) {
            _class = new DefaultMutableTreeNode(node.toString());
            _method.add(_class);
            System.out.println("AbstrackTypeDeclaration : " + node.toString());
            return false;
        }

        @Override
        public boolean visit(ArrayCreation node) {
            _array = new DefaultMutableTreeNode(node.toString());
            _method.add(_array);
            System.out.println("ArrayCreation : " + node.toString());
            return false;
        }

        @Override
        public boolean visit(ArrayInitializer node) {
            _array = new DefaultMutableTreeNode(node.toString());
            System.out.println("ArrayInitialize : " + node.toString());
            _method.add(_array);
            return false;
        }

        @Override
        public boolean visit(AssertStatement node) {
            _statement = new DefaultMutableTreeNode(node.toString());
            System.out.println("AssertStatement : " + node.toString());
            _method.add(_statement);
            return false;
        }

        @Override
        public boolean visit(ContinueStatement node) {
            _statement = new DefaultMutableTreeNode(node.toString());
            System.out.println("ContinueStatement : " + node.toString());
            _method.add(_statement);
            return false;
        }

        @Override
        public boolean visit(SwitchStatement node) {
            _switch = new DefaultMutableTreeNode("switch (" + node.getExpression() + ")");
            System.out.println("switch (" + node.getExpression() + ")");
            _method.add(_switch);
            List getStatement = node.statements();
            for (Object st : getStatement) {
                Matcher _caseMatch = Pattern.compile("^case\\s+.+\\:").matcher(st.toString());
                if (_caseMatch.find()) {
                    _case = new DefaultMutableTreeNode(_caseMatch.group());
                    _switch.add(_case);
                }

                enclose(st.toString(), _case);

                Matcher _breakMatch = Pattern.compile("^break\\s*.*;").matcher(st.toString());
                if (_breakMatch.find()) {
                    _break = new DefaultMutableTreeNode(_breakMatch.group());
                    _case.add(_break);
                }
            }
            return false;
        }

        @Override
        public boolean visit(ClassInstanceCreation node) {
            _constructorCall = new DefaultMutableTreeNode(node.toString());
            System.out.println("ClassInstanceCreation : " + node.toString());
            _method.add(_constructorCall);
            return false;
        }
    });

    //        model = new DefaultTreeModel(_package);
    //        model.reload();
    //        codeTree.setModel(model);
    codeTree.setModel(new DefaultTreeModel(_package) {
        public void reload(TreeNode node) {
            if (node != null) {
                fireTreeStructureChanged(this, getPathToRoot(node), null, null);
            }
        }
    });
    codeTree.setCellRenderer(new TreeRender());
    //        ((DefaultTreeModel)codeTree.getModel()).reload();
    for (int i = 0; i < codeTree.getRowCount(); i++)
        codeTree.expandRow(i);
}

From source file:at.bestsolution.fxide.jdt.corext.dom.ASTFlattener.java

License:Open Source License

@Override
public boolean visit(PackageDeclaration node) {
    if (node.getAST().apiLevel() >= JLS3) {
        if (node.getJavadoc() != null) {
            node.getJavadoc().accept(this);
        }//  w w w .j a  va2 s .  c  o m
        for (Iterator<Annotation> it = node.annotations().iterator(); it.hasNext();) {
            Annotation p = it.next();
            p.accept(this);
            this.fBuffer.append(" ");//$NON-NLS-1$
        }
    }
    this.fBuffer.append("package ");//$NON-NLS-1$
    node.getName().accept(this);
    this.fBuffer.append(";");//$NON-NLS-1$
    return false;
}

From source file:boa.datagen.util.Java7Visitor.java

License:Apache License

@Override
public boolean visit(CompilationUnit node) {
    //      b.setPosition(pos.build());
    PackageDeclaration pkg = node.getPackage();
    if (pkg == null) {
        b.setName("");
    } else {/* ww  w.  j a va2 s  .c  o  m*/
        b.setName(pkg.getName().getFullyQualifiedName());
        for (Object a : pkg.annotations()) {
            ((Annotation) a).accept(this);
            b.addModifiers(modifiers.pop());
        }
    }
    for (Object i : node.imports()) {
        ImportDeclaration id = (ImportDeclaration) i;
        String imp = "";
        if (id.isStatic())
            imp += "static ";
        imp += id.getName().getFullyQualifiedName();
        if (id.isOnDemand())
            imp += ".*";
        imports.add(imp);
    }
    for (Object t : node.types()) {
        declarations.push(new ArrayList<boa.types.Ast.Declaration>());
        ((AbstractTypeDeclaration) t).accept(this);
        for (boa.types.Ast.Declaration d : declarations.pop())
            b.addDeclarations(d);
    }
    for (Object c : node.getCommentList())
        ((org.eclipse.jdt.core.dom.Comment) c).accept(this);
    return false;
}

From source file:br.uff.ic.mergeguider.javaparser.DepVisitor.java

@Override
public boolean visit(TypeDeclaration node) {

    //Treating class name
    String className;/* w  w  w  .  j  ava2s.  c o  m*/
    PackageDeclaration aPackage = cu.getPackage();
    if (aPackage != null) {
        String packageName = aPackage.getName().getFullyQualifiedName();
        className = packageName + "." + node.getName().getIdentifier();
    } else {
        className = null;
    }

    classLanguageContructs = new ClassLanguageContructs(className, path);

    classLanguageConstructsList.add(classLanguageContructs);

    simpleNames = new ArrayList<>();
    simpleNamesList.add(simpleNames);

    //Treating location and language construct
    Location location;

    int elementLineBegin = cu.getLineNumber(node.getStartPosition());
    int elementLineEnd = cu.getLineNumber(node.getStartPosition() + node.getLength());
    int elementColumnBegin = cu.getColumnNumber(node.getStartPosition());
    int elementColumnEnd = cu.getColumnNumber(node.getStartPosition() + node.getLength());

    location = new Location(elementLineBegin, elementLineEnd, elementColumnBegin, elementColumnEnd);

    MyTypeDeclaration typeDeclaration = new MyTypeDeclaration(node, location);

    if (!classLanguageConstructsList.isEmpty()) {
        classLanguageConstructsList.get(classLanguageConstructsList.size() - 1).getTypeDeclarations()
                .add(typeDeclaration);
    }

    return true;
}

From source file:br.uff.ic.mergeguider.javaparser.DepVisitor.java

@Override
public boolean visit(EnumDeclaration node) {

    String className;/*www.  j  a  v a 2  s  .  c  o m*/

    PackageDeclaration aPackage = cu.getPackage();
    if (aPackage != null) {
        String packageName = aPackage.getName().getFullyQualifiedName();
        className = packageName + "." + node.getName().getIdentifier();
    } else {
        className = null;
    }

    classLanguageContructs = new ClassLanguageContructs(className, path);
    classLanguageConstructsList.add(classLanguageContructs);

    simpleNames = new ArrayList<>();
    simpleNamesList.add(simpleNames);

    return true;
}

From source file:br.uff.ic.mergeguider.javaparser.DepVisitor.java

@Override
public boolean visit(AnnotationTypeDeclaration node) {

    String className;//from   ww w .  j  a  va 2  s  .co m

    PackageDeclaration aPackage = cu.getPackage();
    if (aPackage != null) {
        String packageName = aPackage.getName().getFullyQualifiedName();
        className = packageName + "." + node.getName().getIdentifier();
    } else {
        className = null;
    }

    classLanguageContructs = new ClassLanguageContructs(className, path);
    classLanguageConstructsList.add(classLanguageContructs);

    simpleNames = new ArrayList<>();
    simpleNamesList.add(simpleNames);

    //Treating location and language construct
    Location location;

    int elementLineBegin = cu.getLineNumber(node.getStartPosition());
    int elementLineEnd = cu.getLineNumber(node.getStartPosition() + node.getLength());
    int elementColumnBegin = cu.getColumnNumber(node.getStartPosition());
    int elementColumnEnd = cu.getColumnNumber(node.getStartPosition() + node.getLength());

    location = new Location(elementLineBegin, elementLineEnd, elementColumnBegin, elementColumnEnd);

    MyAnnotationDeclaration annotationDeclaration = new MyAnnotationDeclaration(node, location);

    if (!classLanguageConstructsList.isEmpty()) {
        classLanguageConstructsList.get(classLanguageConstructsList.size() - 1).getAnnotationDeclarations()
                .add(annotationDeclaration);
    }

    return true;
}

From source file:ca.mcgill.cs.swevo.ppa.PPAASTUtil.java

License:Open Source License

public static String getPackageName(CompilationUnit cu) {
    String packageName = null;//from   w  w  w. j a  v a2 s.  co  m
    PackageDeclaration pDeclaration = cu.getPackage();

    if (pDeclaration != null) {
        packageName = pDeclaration.getName().getFullyQualifiedName();
    }

    return packageName;
}

From source file:ca.mcgill.cs.swevo.ppa.ui.PPAUtil.java

License:Open Source License

public static String getPackageFromFile(File file) throws IOException {
    String packageName = "";
    PPAASTParser parser2 = new PPAASTParser(AST.JLS3);
    parser2.setStatementsRecovery(true);
    parser2.setResolveBindings(true);/*  w w  w . jav  a2  s  .  co  m*/
    parser2.setSource(PPAResourceUtil.getContent(file).toCharArray());
    CompilationUnit cu = (CompilationUnit) parser2.createAST(null);
    PackageDeclaration pDec = cu.getPackage();
    if (pDec != null) {
        packageName = pDec.getName().getFullyQualifiedName();
    }
    return packageName;
}

From source file:changetypes.ASTVisitorAtomicChange.java

License:Open Source License

public boolean visit(PackageDeclaration node) {
    try {/*ww w  .  j  a  va 2 s . co  m*/
        this.facts.add(Fact.makePackageFact(node.getName().toString()));
    } catch (Exception localException) {
        System.err.println("Cannot resolve bindings for package " + node.getName().toString());
    }
    return false;
}

From source file:coloredide.utils.CopiedNaiveASTFlattener.java

License:Open Source License

public boolean visit(PackageDeclaration node) {
    if (node.getAST().apiLevel() >= AST.JLS3) {
        if (node.getJavadoc() != null) {
            node.getJavadoc().accept(this);
        }/*from   ww w.  ja va 2s  .com*/
        for (Iterator it = node.annotations().iterator(); it.hasNext();) {
            Annotation p = (Annotation) it.next();
            p.accept(this);
            this.buffer.append(" ");//$NON-NLS-1$
        }
    }
    printIndent();
    this.buffer.append("package ");//$NON-NLS-1$
    node.getName().accept(this);
    this.buffer.append(";\n");//$NON-NLS-1$
    return false;
}