Search in sources :

Example 6 with ClassInstanceCreation

use of org.eclipse.jdt.core.dom.ClassInstanceCreation in project che by eclipse.

the class SemanticHighlightings method getBinding.

/**
     * Extracts the binding from the token's simple name.
     * Works around bug 62605 to return the correct constructor binding in a ClassInstanceCreation.
     *
     * @param token
     *         the token to extract the binding from
     * @return the token's binding, or <code>null</code>
     */
private static IBinding getBinding(SemanticToken token) {
    ASTNode node = token.getNode();
    ASTNode normalized = ASTNodes.getNormalizedNode(node);
    if (normalized.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) {
        // work around: https://bugs.eclipse.org/bugs/show_bug.cgi?id=62605
        return ((ClassInstanceCreation) normalized.getParent()).resolveConstructorBinding();
    }
    return token.getBinding();
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Example 7 with ClassInstanceCreation

use of org.eclipse.jdt.core.dom.ClassInstanceCreation in project che by eclipse.

the class SourceProvider method updateImplicitReceivers.

private void updateImplicitReceivers(ASTRewrite rewriter, CallContext context) {
    if (context.receiver == null)
        return;
    List<Expression> implicitReceivers = fAnalyzer.getImplicitReceivers();
    for (Iterator<Expression> iter = implicitReceivers.iterator(); iter.hasNext(); ) {
        ASTNode node = iter.next();
        ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(node, context.importer);
        if (node instanceof MethodInvocation) {
            final MethodInvocation inv = (MethodInvocation) node;
            rewriter.set(inv, MethodInvocation.EXPRESSION_PROPERTY, createReceiver(rewriter, context, (IMethodBinding) inv.getName().resolveBinding(), importRewriteContext), null);
        } else if (node instanceof ClassInstanceCreation) {
            final ClassInstanceCreation inst = (ClassInstanceCreation) node;
            rewriter.set(inst, ClassInstanceCreation.EXPRESSION_PROPERTY, createReceiver(rewriter, context, inst.resolveConstructorBinding(), importRewriteContext), null);
        } else if (node instanceof ThisExpression) {
            rewriter.replace(node, rewriter.createStringPlaceholder(context.receiver, ASTNode.METHOD_INVOCATION), null);
        } else if (node instanceof FieldAccess) {
            final FieldAccess access = (FieldAccess) node;
            rewriter.set(access, FieldAccess.EXPRESSION_PROPERTY, createReceiver(rewriter, context, access.resolveFieldBinding(), importRewriteContext), null);
        } else if (node instanceof SimpleName && ((SimpleName) node).resolveBinding() instanceof IVariableBinding) {
            IVariableBinding vb = (IVariableBinding) ((SimpleName) node).resolveBinding();
            if (vb.isField()) {
                Expression receiver = createReceiver(rewriter, context, vb, importRewriteContext);
                if (receiver != null) {
                    FieldAccess access = node.getAST().newFieldAccess();
                    ASTNode target = rewriter.createMoveTarget(node);
                    access.setName((SimpleName) target);
                    access.setExpression(receiver);
                    rewriter.replace(node, access, null);
                }
            }
        }
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) SimpleName(org.eclipse.jdt.core.dom.SimpleName) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess)

Example 8 with ClassInstanceCreation

use of org.eclipse.jdt.core.dom.ClassInstanceCreation in project che by eclipse.

the class PotentialProgrammingProblemsFix method getDeclarationNode.

/**
	 * Returns the declaration node for the originally selected node.
	 * @param name the name of the node
	 *
	 * @return the declaration node
	 */
private static ASTNode getDeclarationNode(SimpleName name) {
    ASTNode parent = name.getParent();
    if (!(parent instanceof AbstractTypeDeclaration)) {
        parent = parent.getParent();
        if (parent instanceof ParameterizedType || parent instanceof Type)
            parent = parent.getParent();
        if (parent instanceof ClassInstanceCreation) {
            final ClassInstanceCreation creation = (ClassInstanceCreation) parent;
            parent = creation.getAnonymousClassDeclaration();
        }
    }
    return parent;
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) IType(org.eclipse.jdt.core.IType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 9 with ClassInstanceCreation

use of org.eclipse.jdt.core.dom.ClassInstanceCreation in project che by eclipse.

the class ASTNodeDeleteUtil method getNodesToDelete.

private static ASTNode[] getNodesToDelete(IJavaElement element, CompilationUnit cuNode) throws JavaModelException {
    // fields are different because you don't delete the whole declaration but only a fragment of it
    if (element.getElementType() == IJavaElement.FIELD) {
        if (JdtFlags.isEnum((IField) element))
            return new ASTNode[] { ASTNodeSearchUtil.getEnumConstantDeclaration((IField) element, cuNode) };
        else
            return new ASTNode[] { ASTNodeSearchUtil.getFieldDeclarationFragmentNode((IField) element, cuNode) };
    }
    if (element.getElementType() == IJavaElement.TYPE && ((IType) element).isLocal()) {
        IType type = (IType) element;
        if (type.isAnonymous()) {
            if (type.getParent().getElementType() == IJavaElement.FIELD) {
                EnumConstantDeclaration enumDecl = ASTNodeSearchUtil.getEnumConstantDeclaration((IField) element.getParent(), cuNode);
                if (enumDecl != null && enumDecl.getAnonymousClassDeclaration() != null) {
                    return new ASTNode[] { enumDecl.getAnonymousClassDeclaration() };
                }
            }
            ClassInstanceCreation creation = ASTNodeSearchUtil.getClassInstanceCreationNode(type, cuNode);
            if (creation != null) {
                if (creation.getLocationInParent() == ExpressionStatement.EXPRESSION_PROPERTY) {
                    return new ASTNode[] { creation.getParent() };
                } else if (creation.getLocationInParent() == VariableDeclarationFragment.INITIALIZER_PROPERTY) {
                    return new ASTNode[] { creation };
                }
                return new ASTNode[] { creation.getAnonymousClassDeclaration() };
            }
            return new ASTNode[0];
        } else {
            ASTNode[] nodes = ASTNodeSearchUtil.getDeclarationNodes(element, cuNode);
            // we have to delete the TypeDeclarationStatement
            nodes[0] = nodes[0].getParent();
            return nodes;
        }
    }
    return ASTNodeSearchUtil.getDeclarationNodes(element, cuNode);
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) EnumConstantDeclaration(org.eclipse.jdt.core.dom.EnumConstantDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) IField(org.eclipse.jdt.core.IField) IType(org.eclipse.jdt.core.IType)

Example 10 with ClassInstanceCreation

use of org.eclipse.jdt.core.dom.ClassInstanceCreation in project che by eclipse.

the class JavadocFinder method resolveBinding.

private static IBinding resolveBinding(ASTNode node) {
    if (node instanceof SimpleName) {
        SimpleName simpleName = (SimpleName) node;
        // workaround for https://bugs.eclipse.org/62605 (constructor name resolves to type, not method)
        ASTNode normalized = ASTNodes.getNormalizedNode(simpleName);
        if (normalized.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) {
            ClassInstanceCreation cic = (ClassInstanceCreation) normalized.getParent();
            IMethodBinding constructorBinding = cic.resolveConstructorBinding();
            if (constructorBinding == null)
                return null;
            ITypeBinding declaringClass = constructorBinding.getDeclaringClass();
            if (!declaringClass.isAnonymous())
                return constructorBinding;
            ITypeBinding superTypeDeclaration = declaringClass.getSuperclass().getTypeDeclaration();
            return resolveSuperclassConstructor(superTypeDeclaration, constructorBinding);
        }
        return simpleName.resolveBinding();
    } else if (node instanceof SuperConstructorInvocation) {
        return ((SuperConstructorInvocation) node).resolveConstructorBinding();
    } else if (node instanceof ConstructorInvocation) {
        return ((ConstructorInvocation) node).resolveConstructorBinding();
    } else {
        return null;
    }
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ConstructorInvocation(org.eclipse.jdt.core.dom.ConstructorInvocation) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation)

Aggregations

ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)32 ASTNode (org.eclipse.jdt.core.dom.ASTNode)18 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)16 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)12 SimpleName (org.eclipse.jdt.core.dom.SimpleName)12 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)9 Type (org.eclipse.jdt.core.dom.Type)9 Expression (org.eclipse.jdt.core.dom.Expression)8 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)8 ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)8 ArrayList (java.util.ArrayList)6 IType (org.eclipse.jdt.core.IType)6 SuperConstructorInvocation (org.eclipse.jdt.core.dom.SuperConstructorInvocation)6 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)5 AST (org.eclipse.jdt.core.dom.AST)5 ArrayType (org.eclipse.jdt.core.dom.ArrayType)5 CastExpression (org.eclipse.jdt.core.dom.CastExpression)5 SimpleType (org.eclipse.jdt.core.dom.SimpleType)5 VariableDeclaration (org.eclipse.jdt.core.dom.VariableDeclaration)5 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)4