Search in sources :

Example 71 with ITypeBinding

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

the class UnresolvedElementsSubProcessor method addMissingCastParentsProposal.

private static void addMissingCastParentsProposal(ICompilationUnit cu, MethodInvocation invocationNode, Collection<ICommandAccess> proposals) {
    Expression sender = invocationNode.getExpression();
    if (sender instanceof ThisExpression) {
        return;
    }
    ITypeBinding senderBinding = sender.resolveTypeBinding();
    if (senderBinding == null || Modifier.isFinal(senderBinding.getModifiers())) {
        return;
    }
    if (sender instanceof Name && ((Name) sender).resolveBinding() instanceof ITypeBinding) {
        // static access
        return;
    }
    ASTNode parent = invocationNode.getParent();
    while (parent instanceof Expression && parent.getNodeType() != ASTNode.CAST_EXPRESSION) {
        parent = parent.getParent();
    }
    boolean hasCastProposal = false;
    if (parent instanceof CastExpression) {
        //	(TestCase) x.getName() -> ((TestCase) x).getName
        hasCastProposal = useExistingParentCastProposal(cu, (CastExpression) parent, sender, invocationNode.getName(), getArgumentTypes(invocationNode.arguments()), proposals);
    }
    if (!hasCastProposal) {
        // x.getName() -> ((TestCase) x).getName
        Expression target = sender;
        while (target instanceof ParenthesizedExpression) {
            target = ((ParenthesizedExpression) target).getExpression();
        }
        String label;
        if (target.getNodeType() != ASTNode.CAST_EXPRESSION) {
            String targetName = null;
            if (target.getLength() <= 18) {
                targetName = ASTNodes.asString(target);
            }
            if (targetName == null) {
                label = CorrectionMessages.UnresolvedElementsSubProcessor_methodtargetcast_description;
            } else {
                label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_methodtargetcast2_description, BasicElementLabels.getJavaCodeString(targetName));
            }
        } else {
            String targetName = null;
            if (target.getLength() <= 18) {
                targetName = ASTNodes.asString(((CastExpression) target).getExpression());
            }
            if (targetName == null) {
                label = CorrectionMessages.UnresolvedElementsSubProcessor_changemethodtargetcast_description;
            } else {
                label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changemethodtargetcast2_description, BasicElementLabels.getJavaCodeString(targetName));
            }
        }
        proposals.add(new CastCorrectionProposal(label, cu, target, (ITypeBinding) null, IProposalRelevance.CHANGE_CAST));
    }
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) 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) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) CastExpression(org.eclipse.jdt.core.dom.CastExpression) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name) CastCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.CastCorrectionProposal)

Example 72 with ITypeBinding

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

the class UnresolvedElementsSubProcessor method hasMethodWithName.

private static boolean hasMethodWithName(ITypeBinding typeBinding, String name) {
    IVariableBinding[] fields = typeBinding.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
        if (fields[i].getName().equals(name)) {
            return true;
        }
    }
    ITypeBinding superclass = typeBinding.getSuperclass();
    if (superclass != null) {
        return hasMethodWithName(superclass, name);
    }
    return false;
}
Also used : ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 73 with ITypeBinding

use of org.eclipse.jdt.core.dom.ITypeBinding in project flux by eclipse.

the class ASTNodeFactory method newCreationType.

public static Type newCreationType(AST ast, ITypeBinding typeBinding, ImportRewrite importRewrite, ImportRewriteContext importContext) {
    if (typeBinding.isParameterizedType()) {
        Type baseType = newCreationType(ast, typeBinding.getTypeDeclaration(), importRewrite, importContext);
        ParameterizedType parameterizedType = ast.newParameterizedType(baseType);
        for (ITypeBinding typeArgument : typeBinding.getTypeArguments()) {
            parameterizedType.typeArguments().add(newCreationType(ast, typeArgument, importRewrite, importContext));
        }
        return parameterizedType;
    } else if (typeBinding.isParameterizedType()) {
        Type elementType = newCreationType(ast, typeBinding.getElementType(), importRewrite, importContext);
        ArrayType arrayType = ast.newArrayType(elementType, 0);
        while (typeBinding.isArray()) {
            Dimension dimension = ast.newDimension();
            IAnnotationBinding[] typeAnnotations = typeBinding.getTypeAnnotations();
            for (IAnnotationBinding typeAnnotation : typeAnnotations) {
                dimension.annotations().add(importRewrite.addAnnotation(typeAnnotation, ast, importContext));
            }
            arrayType.dimensions().add(dimension);
            typeBinding = typeBinding.getComponentType();
        }
        return arrayType;
    } else if (typeBinding.isWildcardType()) {
        ITypeBinding bound = typeBinding.getBound();
        typeBinding = (bound != null) ? bound : typeBinding.getErasure();
        return newCreationType(ast, typeBinding, importRewrite, importContext);
    } else {
        return importRewrite.addImport(typeBinding, ast, importContext);
    }
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) Type(org.eclipse.jdt.core.dom.Type) UnionType(org.eclipse.jdt.core.dom.UnionType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) IAnnotationBinding(org.eclipse.jdt.core.dom.IAnnotationBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) Dimension(org.eclipse.jdt.core.dom.Dimension)

Example 74 with ITypeBinding

use of org.eclipse.jdt.core.dom.ITypeBinding in project flux by eclipse.

the class ASTNodeFactory method newType.

/**
	 * Returns the new type node corresponding to the type of the given declaration
	 * including the extra dimensions. If the type is a {@link UnionType}, use the LUB type.
	 * If the <code>importRewrite</code> is <code>null</code>, the type may be fully-qualified.
	 * 
	 * @param ast The AST to create the resulting type with.
	 * @param declaration The variable declaration to get the type from
	 * @param importRewrite the import rewrite to use, or <code>null</code>
	 * @param context the import rewrite context, or <code>null</code>
	 * @return a new type node created with the given AST.
	 * 
	 * @since 3.7.1
	 */
public static Type newType(AST ast, VariableDeclaration declaration, ImportRewrite importRewrite, ImportRewriteContext context) {
    if (declaration instanceof VariableDeclarationFragment && declaration.getParent() instanceof LambdaExpression) {
        return newType((LambdaExpression) declaration.getParent(), (VariableDeclarationFragment) declaration, ast, importRewrite, context);
    }
    Type type = ASTNodes.getType(declaration);
    if (declaration instanceof SingleVariableDeclaration) {
        Type type2 = ((SingleVariableDeclaration) declaration).getType();
        if (type2 instanceof UnionType) {
            ITypeBinding typeBinding = type2.resolveBinding();
            if (typeBinding != null) {
                if (importRewrite != null) {
                    type = importRewrite.addImport(typeBinding, ast, context);
                    return type;
                } else {
                    String qualifiedName = typeBinding.getQualifiedName();
                    if (qualifiedName.length() > 0) {
                        type = ast.newSimpleType(ast.newName(qualifiedName));
                        return type;
                    }
                }
            }
            // XXX: fallback for intersection types or unresolved types: take first type of union
            type = (Type) ((UnionType) type2).types().get(0);
            return type;
        }
    }
    type = (Type) ASTNode.copySubtree(ast, type);
    List<Dimension> extraDimensions = declaration.extraDimensions();
    if (!extraDimensions.isEmpty()) {
        ArrayType arrayType;
        if (type instanceof ArrayType) {
            arrayType = (ArrayType) type;
        } else {
            arrayType = ast.newArrayType(type, 0);
            type = arrayType;
        }
        arrayType.dimensions().addAll(ASTNode.copySubtrees(ast, extraDimensions));
    }
    return type;
}
Also used : ArrayType(org.eclipse.jdt.core.dom.ArrayType) UnionType(org.eclipse.jdt.core.dom.UnionType) Type(org.eclipse.jdt.core.dom.Type) UnionType(org.eclipse.jdt.core.dom.UnionType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) Dimension(org.eclipse.jdt.core.dom.Dimension) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression)

Example 75 with ITypeBinding

use of org.eclipse.jdt.core.dom.ITypeBinding in project flux by eclipse.

the class ASTNodes method getReceiverTypeBinding.

/**
	 * Returns the receiver's type binding of the given method invocation.
	 *
	 * @param invocation method invocation to resolve type of
	 * @return the type binding of the receiver
	 */
public static ITypeBinding getReceiverTypeBinding(MethodInvocation invocation) {
    ITypeBinding result = null;
    Expression exp = invocation.getExpression();
    if (exp != null) {
        return exp.resolveTypeBinding();
    } else {
        AbstractTypeDeclaration type = (AbstractTypeDeclaration) getParent(invocation, AbstractTypeDeclaration.class);
        if (type != null)
            return type.resolveBinding();
    }
    return result;
}
Also used : ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Aggregations

ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)388 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)110 ASTNode (org.eclipse.jdt.core.dom.ASTNode)72 Expression (org.eclipse.jdt.core.dom.Expression)69 Type (org.eclipse.jdt.core.dom.Type)55 SimpleName (org.eclipse.jdt.core.dom.SimpleName)52 ArrayList (java.util.ArrayList)50 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)49 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)49 AST (org.eclipse.jdt.core.dom.AST)43 IBinding (org.eclipse.jdt.core.dom.IBinding)41 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)40 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)36 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)35 CastExpression (org.eclipse.jdt.core.dom.CastExpression)33 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)32 Name (org.eclipse.jdt.core.dom.Name)31 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)29 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)29 ContextSensitiveImportRewriteContext (org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext)26