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

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

Introduction

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

Prototype

public final AST getAST() 

Source Link

Document

Returns this node's AST.

Usage

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;//from   w ww  . j a  va2 s . com
    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.objectteams.otdt.internal.ui.text.correction.TypeProposalSubProcessor.java

License:Open Source License

/**
 * Change a simple type role reference to a an anchored type reference.
 * Infer possible anchors from the context.  
 * //from w ww  .  j a  v  a  2  s .c  o  m
 * @param cu               where everything happens 
 * @param fullName         the qualified name of a role type
 * @param type               the node specifying the role type (to be replaced)
 * @param enclosingTeamName the team containing the role type to change.
 * 
 * all parameters: non-null!
 * @return the proposal (always)
 */
@SuppressWarnings("unchecked")
public static LinkedCorrectionProposal changeTypeToAnchored(ICompilationUnit cu, String fullName, Name type,
        String enclosingTeamName) {
    AST ast = type.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);

    // search candidates for an anchor:      
    String[] variables = matchingVariables(type, enclosingTeamName);
    String firstAnchor = (variables.length > 0) ? variables[0] : "aTeam"; //$NON-NLS-1$ 

    // construct and replace 'Type<@anchor>'
    SimpleName anchorName = (SimpleName) rewrite.createStringPlaceholder(firstAnchor, ASTNode.SIMPLE_NAME);
    TypeAnchor anchor = ast.newTypeAnchor(anchorName);
    Name newTypeName = type;
    if (type.isQualifiedName())
        newTypeName = ((QualifiedName) type).getName();
    newTypeName = (Name) ASTNode.copySubtree(ast, newTypeName);
    ParameterizedType newType = ast.newParameterizedType(ast.newSimpleType(newTypeName));
    newType.typeArguments().add(anchor);
    rewrite.replace(type, newType, null);

    // assemble a proposal:
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    int relevance = 13; // FIXME
    String label = CorrectionMessages.OTQuickFix_Type_change_type_to_anchored;
    LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, cu, rewrite, relevance, image);

    // setup linked mode:
    proposal.addLinkedPosition(rewrite.track(anchorName), true, ANCHOR_GROUP_ID);
    if (variables.length > 1) // should we present choices?
        for (int i = 0; i < variables.length; i++)
            proposal.addLinkedPositionProposal(ANCHOR_GROUP_ID, variables[i], null);
    return proposal;
}

From source file:org.spoofax.interpreter.adapter.ecj.ECJFactory.java

License:LGPL

private Name asName(IStrategoTerm term) {
    Name x = ((WrappedName) term).getWrappee();
    return x.getParent() == null && x.getAST() == ast ? x : (Name) ASTNode.copySubtree(ast, x);
}

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./*from w ww.  ja  va2  s.co  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);
        }
    }
}

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

License:Open Source License

/** Add a prefix to the AST name node, and return the new AST name node.
 *
 *  @param name The AST node of the name.
 *  @param prefix The prefix to be added to the beginning of the name (not
 *   including "." between the prefix and the name).
 *  @return The new AST name node./*from  w  w  w  .  j av  a 2s  . c  om*/
 */
private Name _addPrefix(Name name, String prefix) {
    AST ast = name.getAST();
    Name newName = null;

    // Coverity scan suggests that name cannot be null.
    while (name instanceof QualifiedName && !(((QualifiedName) name).getQualifier() instanceof SimpleName)) {
        name = ((QualifiedName) name).getQualifier();
    }

    int lastPosition = prefix.length() - 1;

    while (lastPosition >= 0) {
        int dotPosition = prefix.lastIndexOf('.', lastPosition);
        String part = (dotPosition == -1) ? prefix.substring(0, lastPosition + 1)
                : prefix.substring(dotPosition + 1, lastPosition + 1);
        lastPosition = dotPosition - 1;

        if (name == null) {
            name = ast.newSimpleName(part);
            newName = name;
        } else if (name instanceof SimpleName) {
            name = ast.newQualifiedName(ast.newSimpleName(part),
                    ast.newSimpleName(((SimpleName) name).getIdentifier()));
            newName = name;
        } else {
            QualifiedName qualifiedName = (QualifiedName) name;
            SimpleName leftPart = (SimpleName) qualifiedName.getQualifier();
            qualifiedName.setQualifier(
                    ast.newQualifiedName(ast.newSimpleName(part), ast.newSimpleName(leftPart.getIdentifier())));
            name = qualifiedName.getQualifier();
        }
    }

    return newName;
}