Search in sources :

Example 76 with ITypeBinding

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

the class ASTNodes method getDimensions.

public static int getDimensions(VariableDeclaration declaration) {
    int dim = declaration.getExtraDimensions();
    if (declaration instanceof VariableDeclarationFragment && declaration.getParent() instanceof LambdaExpression) {
        LambdaExpression lambda = (LambdaExpression) declaration.getParent();
        IMethodBinding methodBinding = lambda.resolveMethodBinding();
        if (methodBinding != null) {
            ITypeBinding[] parameterTypes = methodBinding.getParameterTypes();
            int index = lambda.parameters().indexOf(declaration);
            ITypeBinding typeBinding = parameterTypes[index];
            return typeBinding.getDimensions();
        }
    } else {
        Type type = getType(declaration);
        if (type instanceof ArrayType) {
            dim += ((ArrayType) type).getDimensions();
        }
    }
    return dim;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ArrayType(org.eclipse.jdt.core.dom.ArrayType) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) IType(org.eclipse.jdt.core.IType) UnionType(org.eclipse.jdt.core.dom.UnionType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression)

Example 77 with ITypeBinding

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

the class ScopeAnalyzer method isVisible.

/**
	 * Evaluates if the declaration is visible in a certain context.
	 * @param binding The binding of the declaration to examine
	 * @param context The context to test in
	 * @return Returns
	 */
public static boolean isVisible(IBinding binding, ITypeBinding context) {
    if (binding.getKind() == IBinding.VARIABLE && !((IVariableBinding) binding).isField()) {
        // all local variables found are visible
        return true;
    }
    ITypeBinding declaring = getDeclaringType(binding);
    if (declaring == null) {
        return false;
    }
    declaring = declaring.getTypeDeclaration();
    int modifiers = binding.getModifiers();
    if (Modifier.isPublic(modifiers) || declaring.isInterface()) {
        return true;
    } else if (Modifier.isProtected(modifiers) || !Modifier.isPrivate(modifiers)) {
        if (declaring.getPackage() == context.getPackage()) {
            return true;
        }
        return isTypeInScope(declaring, context, Modifier.isProtected(modifiers));
    }
    // private visibility
    return isTypeInScope(declaring, context, false);
}
Also used : ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 78 with ITypeBinding

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

the class JdtFlags method isDefaultMethod.

public static boolean isDefaultMethod(IMethodBinding method) {
    int modifiers = method.getModifiers();
    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=405517#c7
    ITypeBinding declaringClass = method.getDeclaringClass();
    if (declaringClass.isInterface()) {
        return !Modifier.isAbstract(modifiers) && !Modifier.isStatic(modifiers);
    }
    return false;
}
Also used : ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Example 79 with ITypeBinding

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

the class NecessaryParenthesesChecker method needsParenthesesInInfixExpression.

private static boolean needsParenthesesInInfixExpression(Expression expression, InfixExpression parentInfix, StructuralPropertyDescriptor locationInParent, ITypeBinding leftOperandType) {
    InfixExpression.Operator parentInfixOperator = parentInfix.getOperator();
    ITypeBinding rightOperandType;
    ITypeBinding parentInfixExprType;
    if (leftOperandType == null) {
        // parentInfix has bindings
        leftOperandType = parentInfix.getLeftOperand().resolveTypeBinding();
        rightOperandType = parentInfix.getRightOperand().resolveTypeBinding();
        parentInfixExprType = parentInfix.resolveTypeBinding();
    } else {
        rightOperandType = expression.resolveTypeBinding();
        parentInfixExprType = getInfixExpressionType(parentInfixOperator, leftOperandType, rightOperandType);
    }
    boolean isAllOperandsHaveSameType = isAllOperandsHaveSameType(parentInfix, leftOperandType, rightOperandType);
    if (locationInParent == InfixExpression.LEFT_OPERAND_PROPERTY) {
        //infix expressions are evaluated from left to right -> parentheses not needed
        return false;
    } else if (isAssociative(parentInfixOperator, parentInfixExprType, isAllOperandsHaveSameType)) {
        //left op (right) == (right) op left == right op left
        if (expression instanceof InfixExpression) {
            InfixExpression infixExpression = (InfixExpression) expression;
            Operator operator = infixExpression.getOperator();
            if (isStringType(parentInfixExprType)) {
                if (parentInfixOperator == InfixExpression.Operator.PLUS && operator == InfixExpression.Operator.PLUS && isStringType(infixExpression.resolveTypeBinding())) {
                    // "" + (2 + "") == "" + 2 + ""
                    return !isStringType(infixExpression.getLeftOperand().resolveTypeBinding()) && !isStringType(leftOperandType);
                }
                //"" + (1 + 2), "" + (1 - 2) etc
                return true;
            }
            if (parentInfixOperator != InfixExpression.Operator.TIMES)
                return false;
            if (operator == InfixExpression.Operator.TIMES)
                // x * (y * z) == x * y * z
                return false;
            if (operator == InfixExpression.Operator.REMAINDER || operator == InfixExpression.Operator.DIVIDE)
                // x * (y % z) != x * y % z , x * (y / z) == x * y / z rounding involved
                return true;
            return false;
        }
        return false;
    } else {
        return true;
    }
}
Also used : Operator(org.eclipse.jdt.core.dom.InfixExpression.Operator) Operator(org.eclipse.jdt.core.dom.InfixExpression.Operator) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression)

Example 80 with ITypeBinding

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

the class NecessaryParenthesesChecker method isAllOperandsHaveSameType.

/*
	 * Do all operands in expression have same type
	 */
private static boolean isAllOperandsHaveSameType(InfixExpression expression, ITypeBinding leftOperandType, ITypeBinding rightOperandType) {
    ITypeBinding binding = leftOperandType;
    if (binding == null)
        return false;
    ITypeBinding current = rightOperandType;
    if (binding != current)
        return false;
    for (Iterator<Expression> iterator = expression.extendedOperands().iterator(); iterator.hasNext(); ) {
        Expression operand = iterator.next();
        current = operand.resolveTypeBinding();
        if (binding != current)
            return false;
    }
    return true;
}
Also used : ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

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