Example usage for org.eclipse.jdt.core.dom QualifiedName resolveTypeBinding

List of usage examples for org.eclipse.jdt.core.dom QualifiedName resolveTypeBinding

Introduction

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

Prototype

public final ITypeBinding resolveTypeBinding() 

Source Link

Document

Resolves and returns the binding for the type of this expression.

Usage

From source file:cc.kave.eclipse.namefactory.NodeFactory.java

License:Apache License

private static Name createQualifiedName(ASTNode node) {
    StringBuilder sb = new StringBuilder();
    QualifiedName qualifiedNameNode = (QualifiedName) node;

    sb.append("[");
    String typeName = BindingFactory.getBindingName(qualifiedNameNode.resolveTypeBinding());
    sb.append(CsTypeName.newTypeName(typeName).getIdentifier());
    sb.append("] [");
    sb.append(getDeclaringType(qualifiedNameNode));
    sb.append("] ");
    sb.append(qualifiedNameNode.getName().getIdentifier());

    return CsFieldName.newFieldName(sb.toString());
}

From source file:org.eclipse.babel.tapiji.tools.java.ui.refactoring.Cal10nRefactoringVisitor.java

License:Open Source License

/**
 * Changes the argument of the IMessageConveyor#getMessage(...) call. The
 * new enum key replaces the old one.//  w ww  . java  2s  .  c om
 */
@Override
@SuppressWarnings("unchecked")
public boolean visit(MethodInvocation node) {

    boolean isCal10nCall = "ch.qos.cal10n.IMessageConveyor"
            .equals(node.resolveMethodBinding().getDeclaringClass().getQualifiedName());

    if (!isCal10nCall) {
        if (node.arguments().size() == 0) {
            return false;
        } else {
            return true; // because the Cal10n call may be an argument!
        }
    } else {
        QualifiedName qName = (QualifiedName) node.arguments().get(0);
        String fullPath = qName.resolveTypeBinding().getJavaElement().getResource().getFullPath()
                .toPortableString();
        if (fullPath.equals(enumPath) && qName.getName().toString().equals(oldKey)) {

            // ASTRewrite
            Expression exp = (Expression) rewriter.createCopyTarget(node.getExpression());

            MethodInvocation newMethod = ast.newMethodInvocation();
            newMethod.setExpression(exp);
            newMethod.setName(ast.newSimpleName(node.getName().getIdentifier()));

            SimpleName newSimpleName = ast.newSimpleName(newKey);
            Name newName = ast.newName(qName.getQualifier().getFullyQualifiedName());
            QualifiedName newQualifiedName = ast.newQualifiedName(newName, newSimpleName);

            newMethod.arguments().add(newQualifiedName);
            rewriter.replace(node, newMethod, null);

            // protocol
            int startPos = node.getStartPosition();
            ICompilationUnit icu = (ICompilationUnit) cu.getJavaElement();
            changeSet.add(icu.getPath().toPortableString() + ": line " + cu.getLineNumber(startPos));
        }
    }

    return false;
}

From source file:org.eclipse.objectteams.otdt.internal.ui.text.correction.TypeProposalSubProcessor.java

License:Open Source License

/**
 * Import a base class so that a qualified reference can be replaced by a simple one.
 * //from w  w w. j a va  2 s.  c  o  m
 * @param selectedNode the qualified reference
 * @param cu where everything happens
 * @return the proposal or null
 * @throws JavaModelException exception in one of the various model operations
 */
public static ICommandAccess getImportBaseclass(ASTNode selectedNode, ICompilationUnit cu)
        throws JavaModelException {
    if (selectedNode.getNodeType() != ASTNode.QUALIFIED_NAME)
        return null;

    QualifiedName typeRef = (QualifiedName) selectedNode;

    ITypeBinding type = typeRef.resolveTypeBinding();
    if (type == null)
        return null;

    int relevance = 1000; // TODO(SH): compute

    IType[] types = cu.getTypes();
    boolean isRoleFile = false;
    for (IType toplevelType : types) {
        IOTType ottype = OTModelManager.getOTElement(toplevelType);
        if (ottype != null && ottype.isRole()) {
            isRoleFile = true;
            break;
        }
    }
    // no import base in role file, create a new one in the team instead?
    final IJavaCompletionProposal importProposal = isRoleFile
            ? createImportInRoFisTeamProposal(cu, type.getQualifiedName(), typeRef, relevance, 1)
            : null;

    // create the base import
    ImportRewrite importRewrite = StubUtility.createImportRewrite(cu, true);
    if (!isRoleFile) {
        importRewrite.addImport(type);
        if (!importRewrite.setImportBase(type))
            return null; // failure
    }

    AST ast = selectedNode.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);

    // replace the type reference with a simple name:
    Name simpleTypeRef = ast.newName(typeRef.getName().getIdentifier());
    rewrite.replace(selectedNode, simpleTypeRef, null);

    // assemble the proposal:
    String simpleName = type.getName();
    String pack1 = type.getPackage().getNameComponents()[0];
    String displayName = pack1 + "..." + simpleName; //$NON-NLS-1$
    ASTRewriteCorrectionProposal rewriteProposal = new ASTRewriteCorrectionProposal(
            Messages.format(CorrectionMessages.OTQuickFix_Type_convert_fqn_to_importtobase_description,
                    new String[] { displayName }),
            cu, rewrite, relevance, JavaPluginImages.get(JavaPluginImages.IMG_OBJS_IMPDECL)) {
        @Override
        public void apply(IDocument document) {
            super.apply(document);
            // propagate to the import proposal which refers to a different CU (that's why be build two separate proposals):
            if (importProposal != null)
                importProposal.apply(document);
        }
    };
    rewriteProposal.setImportRewrite(importRewrite);
    return rewriteProposal;
}

From source file:org.eclipse.virgo.ide.bundlor.jdt.core.ArtifactAnalyserTypeVisitor.java

License:Open Source License

/**
 * {@inheritDoc}/*from  ww  w.  j av a  2 s.  c o  m*/
 */
@Override
public boolean visit(QualifiedName node) {
    IBinding binding = node.resolveBinding();

    if (isValid(binding)) {
        // Record the declared type.
        recordTypeBinding(node.resolveTypeBinding());

        // Record the declaring type.
        if (binding.getKind() == IBinding.VARIABLE) {
            recordTypeBinding(((IVariableBinding) binding).getDeclaringClass());
        }
    }
    return true;
}

From source file:org.eclipse.wb.android.internal.model.widgets.AndroidRootProcessor.java

License:Open Source License

/**
 * Adds dependencies for given {@link ICompilationUnit}.
 *//* ww w.ja  va 2  s . com*/
private static void addDependencies(final Map<IResource, Long> dependencies, final Set<String> checkedTypes,
        final ICompilationUnit modelUnit, final int level) throws Exception {
    if (level < 5 && dependencies.size() < 100) {
        final IJavaProject javaProject = modelUnit.getJavaProject();
        // add current resource
        {
            IResource resource = modelUnit.getResource();
            dependencies.put(resource, resource.getModificationStamp());
        }
        // add references
        CompilationUnit astUnit = CodeUtils.parseCompilationUnit(modelUnit);
        astUnit.accept(new ASTVisitor() {
            @Override
            public void endVisit(QualifiedName node) {
                addNewType(node.resolveTypeBinding());
            }

            @Override
            public void endVisit(SimpleName node) {
                addNewType(node.resolveTypeBinding());
            }

            private void addNewType(final ITypeBinding binding) {
                if (binding == null) {
                    return;
                }
                ExecutionUtils.runIgnore(new RunnableEx() {
                    public void run() throws Exception {
                        String typeName = AstNodeUtils.getFullyQualifiedName(binding, false);
                        if (typeName.indexOf('.') != -1 && !checkedTypes.contains(typeName)) {
                            checkedTypes.add(typeName);
                            IType type = javaProject.findType(typeName);
                            if (type != null && !type.isBinary()) {
                                addDependencies(dependencies, checkedTypes, type.getCompilationUnit(),
                                        level + 1);
                            }
                        }
                    }
                });
            }
        });
    }
}

From source file:org.evosuite.junit.TestExtractingVisitor.java

License:Open Source License

private VariableReference retrieveVariableReference(QualifiedName qualifiedName) {
    try {// w w  w .j  a v  a 2s  . co  m
        Class<?> referencedClass = retrieveTypeClass(qualifiedName.getQualifier().resolveTypeBinding());
        Field field = referencedClass.getField(qualifiedName.getName().getIdentifier());
        FieldReference fieldReference = new FieldReference(testCase.getReference(),
                new GenericField(field, referencedClass));
        Class<?> resultClass = retrieveTypeClass(qualifiedName.resolveTypeBinding());
        FieldStatement fieldStatement = new FieldStatement(testCase.getReference(),
                new GenericField(field, resultClass), fieldReference);
        testCase.addStatement(fieldStatement);
        return fieldStatement.getReturnValue();
    } catch (Exception exc) {
        throw new RuntimeException(exc);
    }
}

From source file:sharpen.core.CSharpBuilder.java

License:Open Source License

public boolean visit(QualifiedName node) {
    if (isTypeReference(node)) {
        pushTypeReference(node.resolveTypeBinding());
    } else {/*from w w  w.ja v  a2s  .c o  m*/
        String primitiveTypeRef = checkForPrimitiveTypeReference(node);
        if (primitiveTypeRef != null) {
            pushTypeOfExpression(new CSTypeReference(primitiveTypeRef));
        } else {
            handleRegularQualifiedName(node);
        }
    }
    return false;
}