Example usage for org.eclipse.jdt.core.dom Name getRoot

List of usage examples for org.eclipse.jdt.core.dom Name getRoot

Introduction

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

Prototype

public final ASTNode getRoot() 

Source Link

Document

Returns the root node at or above this node; returns this node if it is a root.

Usage

From source file:com.intel.ide.eclipse.mpt.ast.UnresolvedElementsSubProcessor.java

License:Open Source License

static CompilationUnitChange createAddImportChange(ICompilationUnit cu, Name name, String fullyQualifiedName)
        throws CoreException {
    String[] args = { BasicElementLabels.getJavaElementName(Signature.getSimpleName(fullyQualifiedName)),
            BasicElementLabels.getJavaElementName(Signature.getQualifier(fullyQualifiedName)) };
    String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_importtype_description,
            args);//from  www. j  a  v a 2  s .c  o  m

    CompilationUnitChange cuChange = new CompilationUnitChange(label, cu);
    ImportRewrite importRewrite = StubUtility.createImportRewrite((CompilationUnit) name.getRoot(), true);
    importRewrite.addImport(fullyQualifiedName);
    cuChange.setEdit(importRewrite.rewriteImports(null));
    return cuChange;
}

From source file:org.eclipse.ajdt.internal.ui.editor.quickfix.UnresolvedElementsSubProcessor.java

License:Open Source License

private static CUCorrectionProposal createTypeRefChangeProposal(ICompilationUnit cu, String fullName, Name node,
        int relevance, int maxProposals) throws CoreException {
    ImportRewrite importRewrite = null;// w  w w.  j  a  va2s .c  om
    String simpleName = fullName;
    String packName = Signature.getQualifier(fullName);
    if (packName.length() > 0) { // no imports for primitive types, type variables
        importRewrite = StubUtility.createImportRewrite((CompilationUnit) node.getRoot(), true);
        simpleName = importRewrite.addImport(fullName);
    }

    if (!isLikelyTypeName(simpleName)) {
        relevance -= 2;
    }

    ASTRewriteCorrectionProposal proposal;
    if (importRewrite != null && node.isSimpleName()
            && simpleName.equals(((SimpleName) node).getIdentifier())) { // import only
        // import only
        String[] arg = { simpleName, packName };
        String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_importtype_description,
                arg);
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_OBJS_IMPDECL);
        int boost = QualifiedTypeNameHistory.getBoost(fullName, 0, maxProposals);
        proposal = new AddImportCorrectionProposal(label, cu, relevance + 100 + boost, image, packName,
                simpleName, (SimpleName) node);
        proposal.setCommandId(ADD_IMPORT_ID);
    } else {
        String label;
        if (packName.length() == 0) {
            label = Messages.format(
                    CorrectionMessages.UnresolvedElementsSubProcessor_changetype_nopack_description,
                    simpleName);
        } else {
            String[] arg = { simpleName, packName };
            label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changetype_description,
                    arg);
        }
        ASTRewrite rewrite = ASTRewrite.create(node.getAST());
        rewrite.replace(node, rewrite.createStringPlaceholder(simpleName, ASTNode.SIMPLE_TYPE), null);
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, relevance, image);
    }
    if (importRewrite != null) {
        proposal.setImportRewrite(importRewrite);
    }
    return proposal;
}

From source file:org.eclipse.ajdt.internal.ui.editor.quickfix.UnresolvedElementsSubProcessor.java

License:Open Source License

private static void addNewTypeProposals(ICompilationUnit cu, Name refNode, int kind, int relevance,
        Collection proposals) throws JavaModelException {
    Name node = refNode;/* w  w  w .  ja  v a 2  s. c om*/
    do {
        String typeName = ASTNodes.getSimpleNameIdentifier(node);
        Name qualifier = null;
        // only propose to create types for qualifiers when the name starts with upper case
        boolean isPossibleName = isLikelyTypeName(typeName) || (node == refNode);
        if (isPossibleName) {
            IPackageFragment enclosingPackage = null;
            IType enclosingType = null;
            if (node.isSimpleName()) {
                enclosingPackage = (IPackageFragment) cu.getParent();
                // don't suggest member type, user can select it in wizard
            } else {
                Name qualifierName = ((QualifiedName) node).getQualifier();
                IBinding binding = qualifierName.resolveBinding();
                if (binding instanceof ITypeBinding) {
                    enclosingType = (IType) binding.getJavaElement();
                } else if (binding instanceof IPackageBinding) {
                    qualifier = qualifierName;
                    enclosingPackage = (IPackageFragment) binding.getJavaElement();
                } else {
                    IJavaElement[] res = cu.codeSelect(qualifierName.getStartPosition(),
                            qualifierName.getLength());
                    if (res != null && res.length > 0 && res[0] instanceof IType) {
                        enclosingType = (IType) res[0];
                    } else {
                        qualifier = qualifierName;
                        enclosingPackage = JavaModelUtil.getPackageFragmentRoot(cu)
                                .getPackageFragment(ASTResolving.getFullName(qualifierName));
                    }
                }
            }
            int rel = relevance;
            if (enclosingPackage != null && isLikelyPackageName(enclosingPackage.getElementName())) {
                rel += 3;
            }

            if ((enclosingPackage != null && !enclosingPackage
                    .getCompilationUnit(typeName + JavaModelUtil.DEFAULT_CU_SUFFIX).exists()) // new top level type
                    || (enclosingType != null && !enclosingType.isReadOnly()
                            && !enclosingType.getType(typeName).exists())) { // new member type
                IJavaElement enclosing = enclosingPackage != null ? (IJavaElement) enclosingPackage
                        : enclosingType;

                if ((kind & SimilarElementsRequestor.CLASSES) != 0) {
                    proposals.add(new NewCUUsingWizardProposal(cu, node, NewCUUsingWizardProposal.K_CLASS,
                            enclosing, rel + 2));
                }
                if ((kind & SimilarElementsRequestor.INTERFACES) != 0) {
                    proposals.add(new NewCUUsingWizardProposal(cu, node, NewCUUsingWizardProposal.K_INTERFACE,
                            enclosing, rel + 1));
                }
                if ((kind & SimilarElementsRequestor.ENUMS) != 0) {
                    proposals.add(new NewCUUsingWizardProposal(cu, node, NewCUUsingWizardProposal.K_ENUM,
                            enclosing, rel));
                }
                if (kind == SimilarElementsRequestor.ANNOTATIONS) { // only when in annotation
                    proposals.add(new NewCUUsingWizardProposal(cu, node, NewCUUsingWizardProposal.K_ANNOTATION,
                            enclosing, rel + 4));
                }
            }
        }
        node = qualifier;
    } while (node != null);

    // type parameter proposals
    if (refNode.isSimpleName() && ((kind & SimilarElementsRequestor.VARIABLES) != 0)) {
        CompilationUnit root = (CompilationUnit) refNode.getRoot();
        String name = ((SimpleName) refNode).getIdentifier();
        BodyDeclaration declaration = ASTResolving.findParentBodyDeclaration(refNode);
        int baseRel = relevance;
        if (isLikelyTypeParameterName(name)) {
            baseRel += 4;
        }
        while (declaration != null) {
            IBinding binding = null;
            int rel = baseRel;
            if (declaration instanceof MethodDeclaration) {
                binding = ((MethodDeclaration) declaration).resolveBinding();
            } else if (declaration instanceof TypeDeclaration) {
                binding = ((TypeDeclaration) declaration).resolveBinding();
                rel++;
            }
            if (binding != null) {
                AddTypeParameterProposal proposal = new AddTypeParameterProposal(cu, binding, root, name, null,
                        rel);
                proposals.add(proposal);
            }
            if (!Modifier.isStatic(declaration.getModifiers())) {
                declaration = ASTResolving.findParentBodyDeclaration(declaration.getParent());
            } else {
                declaration = null;
            }
        }
    }
}

From source file:ptolemy.backtrack.eclipse.ast.transform.PackageRule.java

License:Open Source License

/** Add an importation declaration for a class in the old package. That
 *  class is implicitly imported by the class being handled. When the class
 *  being handled is moved to a new package, the implicitly imported class
 *  must then be explicitly imported./*  w  w  w .  ja  v a  2  s  .  c  o  m*/
 *
 *  @param loader The class loader to be used to load the imported class.
 *  @param name The AST node of the name of the imported class.
 *  @param oldPackageName The name of the old package.
 */
private void _addImport(LocalClassLoader loader, Name name, String oldPackageName) {
    CompilationUnit root = (CompilationUnit) name.getRoot();
    AST ast = name.getAST();
    String className = name.toString();
    String fullName = oldPackageName + "." + className;

    boolean transform = true;

    // Try to load the class within the package.
    try {
        loader.loadClass(fullName);
    } catch (ClassNotFoundException e) {
        transform = false;
    } catch (NoClassDefFoundError e) {
        transform = false;
    }

    // Check conflict.
    if (transform) {
        Iterator classesIter = loader.getImportedClasses().iterator();

        while (classesIter.hasNext()) {
            LocalClassLoader.ClassImport classImport = (LocalClassLoader.ClassImport) classesIter.next();

            if (classImport.getClassName().equals(className)) {
                transform = false;
                break;
            }
        }

        if (transform) {
            ImportDeclaration importDeclaration = ast.newImportDeclaration();
            importDeclaration.setName(AbstractTransformer.createName(ast, fullName));
            root.imports().add(importDeclaration);
            loader.importClass(fullName);
        }
    }
}