Search in sources :

Example 31 with ArrayType

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

the class ASTResolving method getPossibleReferenceBinding.

private static ITypeBinding getPossibleReferenceBinding(ASTNode node) {
    ASTNode parent = node.getParent();
    switch(parent.getNodeType()) {
        case ASTNode.ASSIGNMENT:
            Assignment assignment = (Assignment) parent;
            if (node.equals(assignment.getLeftHandSide())) {
                // field write access: xx= expression
                return assignment.getRightHandSide().resolveTypeBinding();
            }
            // read access
            return assignment.getLeftHandSide().resolveTypeBinding();
        case ASTNode.INFIX_EXPRESSION:
            InfixExpression infix = (InfixExpression) parent;
            InfixExpression.Operator op = infix.getOperator();
            if (op == InfixExpression.Operator.CONDITIONAL_AND || op == InfixExpression.Operator.CONDITIONAL_OR) {
                // $NON-NLS-1$
                return infix.getAST().resolveWellKnownType("boolean");
            } else if (op == InfixExpression.Operator.LEFT_SHIFT || op == InfixExpression.Operator.RIGHT_SHIFT_UNSIGNED || op == InfixExpression.Operator.RIGHT_SHIFT_SIGNED) {
                // $NON-NLS-1$
                return infix.getAST().resolveWellKnownType("int");
            }
            if (node.equals(infix.getLeftOperand())) {
                // xx operation expression
                ITypeBinding rigthHandBinding = infix.getRightOperand().resolveTypeBinding();
                if (rigthHandBinding != null) {
                    return rigthHandBinding;
                }
            } else {
                // expression operation xx
                ITypeBinding leftHandBinding = infix.getLeftOperand().resolveTypeBinding();
                if (leftHandBinding != null) {
                    return leftHandBinding;
                }
            }
            if (op != InfixExpression.Operator.EQUALS && op != InfixExpression.Operator.NOT_EQUALS) {
                // $NON-NLS-1$
                return infix.getAST().resolveWellKnownType("int");
            }
            break;
        case ASTNode.INSTANCEOF_EXPRESSION:
            InstanceofExpression instanceofExpression = (InstanceofExpression) parent;
            return instanceofExpression.getRightOperand().resolveBinding();
        case ASTNode.VARIABLE_DECLARATION_FRAGMENT:
            VariableDeclarationFragment frag = (VariableDeclarationFragment) parent;
            if (frag.getInitializer().equals(node)) {
                return frag.getName().resolveTypeBinding();
            }
            break;
        case ASTNode.SUPER_METHOD_INVOCATION:
            SuperMethodInvocation superMethodInvocation = (SuperMethodInvocation) parent;
            IMethodBinding superMethodBinding = ASTNodes.getMethodBinding(superMethodInvocation.getName());
            if (superMethodBinding != null) {
                return getParameterTypeBinding(node, superMethodInvocation.arguments(), superMethodBinding);
            }
            break;
        case ASTNode.METHOD_INVOCATION:
            MethodInvocation methodInvocation = (MethodInvocation) parent;
            IMethodBinding methodBinding = methodInvocation.resolveMethodBinding();
            if (methodBinding != null) {
                return getParameterTypeBinding(node, methodInvocation.arguments(), methodBinding);
            }
            break;
        case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
            {
                SuperConstructorInvocation superInvocation = (SuperConstructorInvocation) parent;
                IMethodBinding superBinding = superInvocation.resolveConstructorBinding();
                if (superBinding != null) {
                    return getParameterTypeBinding(node, superInvocation.arguments(), superBinding);
                }
                break;
            }
        case ASTNode.CONSTRUCTOR_INVOCATION:
            {
                ConstructorInvocation constrInvocation = (ConstructorInvocation) parent;
                IMethodBinding constrBinding = constrInvocation.resolveConstructorBinding();
                if (constrBinding != null) {
                    return getParameterTypeBinding(node, constrInvocation.arguments(), constrBinding);
                }
                break;
            }
        case ASTNode.CLASS_INSTANCE_CREATION:
            {
                ClassInstanceCreation creation = (ClassInstanceCreation) parent;
                IMethodBinding creationBinding = creation.resolveConstructorBinding();
                if (creationBinding != null) {
                    return getParameterTypeBinding(node, creation.arguments(), creationBinding);
                }
                break;
            }
        case ASTNode.PARENTHESIZED_EXPRESSION:
            return guessBindingForReference(parent);
        case ASTNode.ARRAY_ACCESS:
            if (((ArrayAccess) parent).getIndex().equals(node)) {
                // $NON-NLS-1$
                return parent.getAST().resolveWellKnownType("int");
            } else {
                ITypeBinding parentBinding = getPossibleReferenceBinding(parent);
                if (parentBinding == null) {
                    // $NON-NLS-1$
                    parentBinding = parent.getAST().resolveWellKnownType("java.lang.Object");
                }
                return parentBinding.createArrayType(1);
            }
        case ASTNode.ARRAY_CREATION:
            if (((ArrayCreation) parent).dimensions().contains(node)) {
                // $NON-NLS-1$
                return parent.getAST().resolveWellKnownType("int");
            }
            break;
        case ASTNode.ARRAY_INITIALIZER:
            ASTNode initializerParent = parent.getParent();
            int dim = 1;
            while (initializerParent instanceof ArrayInitializer) {
                initializerParent = initializerParent.getParent();
                dim++;
            }
            Type creationType = null;
            if (initializerParent instanceof ArrayCreation) {
                creationType = ((ArrayCreation) initializerParent).getType();
            } else if (initializerParent instanceof VariableDeclaration) {
                VariableDeclaration varDecl = (VariableDeclaration) initializerParent;
                creationType = ASTNodes.getType(varDecl);
                dim -= varDecl.getExtraDimensions();
            } else if (initializerParent instanceof MemberValuePair) {
                String name = ((MemberValuePair) initializerParent).getName().getIdentifier();
                IMethodBinding annotMember = findAnnotationMember((Annotation) initializerParent.getParent(), name);
                if (annotMember != null) {
                    return getReducedDimensionBinding(annotMember.getReturnType(), dim);
                }
            }
            if (creationType instanceof ArrayType) {
                ITypeBinding creationTypeBinding = ((ArrayType) creationType).resolveBinding();
                if (creationTypeBinding != null) {
                    return Bindings.getComponentType(creationTypeBinding, dim);
                }
            }
            break;
        case ASTNode.CONDITIONAL_EXPRESSION:
            ConditionalExpression expression = (ConditionalExpression) parent;
            if (node.equals(expression.getExpression())) {
                // $NON-NLS-1$
                return parent.getAST().resolveWellKnownType("boolean");
            }
            if (node.equals(expression.getElseExpression())) {
                return expression.getThenExpression().resolveTypeBinding();
            }
            return expression.getElseExpression().resolveTypeBinding();
        case ASTNode.POSTFIX_EXPRESSION:
            // $NON-NLS-1$
            return parent.getAST().resolveWellKnownType("int");
        case ASTNode.PREFIX_EXPRESSION:
            if (((PrefixExpression) parent).getOperator() == PrefixExpression.Operator.NOT) {
                // $NON-NLS-1$
                return parent.getAST().resolveWellKnownType("boolean");
            }
            // $NON-NLS-1$
            return parent.getAST().resolveWellKnownType("int");
        case ASTNode.IF_STATEMENT:
        case ASTNode.WHILE_STATEMENT:
        case ASTNode.DO_STATEMENT:
            if (node instanceof Expression) {
                // $NON-NLS-1$
                return parent.getAST().resolveWellKnownType("boolean");
            }
            break;
        case ASTNode.SWITCH_STATEMENT:
            if (((SwitchStatement) parent).getExpression().equals(node)) {
                // $NON-NLS-1$
                return parent.getAST().resolveWellKnownType("int");
            }
            break;
        case ASTNode.RETURN_STATEMENT:
            MethodDeclaration decl = ASTResolving.findParentMethodDeclaration(parent);
            if (decl != null && !decl.isConstructor()) {
                return decl.getReturnType2().resolveBinding();
            }
            LambdaExpression lambdaExpr = ASTResolving.findEnclosingLambdaExpression(parent);
            if (lambdaExpr != null) {
                IMethodBinding lambdaMethodBinding = lambdaExpr.resolveMethodBinding();
                if (lambdaMethodBinding != null && lambdaMethodBinding.getReturnType() != null) {
                    return lambdaMethodBinding.getReturnType();
                }
            }
            break;
        case ASTNode.CAST_EXPRESSION:
            return ((CastExpression) parent).getType().resolveBinding();
        case ASTNode.THROW_STATEMENT:
        case ASTNode.CATCH_CLAUSE:
            // $NON-NLS-1$
            return parent.getAST().resolveWellKnownType("java.lang.Exception");
        case ASTNode.FIELD_ACCESS:
            if (node.equals(((FieldAccess) parent).getName())) {
                return getPossibleReferenceBinding(parent);
            }
            break;
        case ASTNode.SUPER_FIELD_ACCESS:
            return getPossibleReferenceBinding(parent);
        case ASTNode.QUALIFIED_NAME:
            if (node.equals(((QualifiedName) parent).getName())) {
                return getPossibleReferenceBinding(parent);
            }
            break;
        case ASTNode.SWITCH_CASE:
            if (node.equals(((SwitchCase) parent).getExpression()) && parent.getParent() instanceof SwitchStatement) {
                return ((SwitchStatement) parent.getParent()).getExpression().resolveTypeBinding();
            }
            break;
        case ASTNode.ASSERT_STATEMENT:
            if (node.getLocationInParent() == AssertStatement.EXPRESSION_PROPERTY) {
                // $NON-NLS-1$
                return parent.getAST().resolveWellKnownType("boolean");
            }
            // $NON-NLS-1$
            return parent.getAST().resolveWellKnownType("java.lang.String");
        case ASTNode.SINGLE_MEMBER_ANNOTATION:
            {
                // $NON-NLS-1$
                IMethodBinding annotMember = findAnnotationMember((Annotation) parent, "value");
                if (annotMember != null) {
                    return annotMember.getReturnType();
                }
                break;
            }
        case ASTNode.MEMBER_VALUE_PAIR:
            {
                String name = ((MemberValuePair) parent).getName().getIdentifier();
                IMethodBinding annotMember = findAnnotationMember((Annotation) parent.getParent(), name);
                if (annotMember != null) {
                    return annotMember.getReturnType();
                }
                break;
            }
        default:
    }
    return null;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) Assignment(org.eclipse.jdt.core.dom.Assignment) ArrayType(org.eclipse.jdt.core.dom.ArrayType) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) ConstructorInvocation(org.eclipse.jdt.core.dom.ConstructorInvocation) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) MemberValuePair(org.eclipse.jdt.core.dom.MemberValuePair) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) Annotation(org.eclipse.jdt.core.dom.Annotation) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) 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) WildcardType(org.eclipse.jdt.core.dom.WildcardType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) 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) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ArrayCreation(org.eclipse.jdt.core.dom.ArrayCreation) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) ArrayInitializer(org.eclipse.jdt.core.dom.ArrayInitializer)

Example 32 with ArrayType

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

the class ASTNodeFactory method newArrayType.

/**
 * Returns an {@link ArrayType} that adds one dimension to the given type node.
 * If the given node is already an ArrayType, then a new {@link Dimension}
 * without annotations is inserted at the first position.
 *
 * @param type the type to be wrapped
 * @return the array type
 * @since 3.10
 */
public static ArrayType newArrayType(Type type) {
    if (type instanceof ArrayType) {
        Dimension dimension = type.getAST().newDimension();
        ArrayType arrayType = (ArrayType) type;
        // first dimension is outermost
        arrayType.dimensions().add(0, dimension);
        return arrayType;
    } else {
        return type.getAST().newArrayType(type);
    }
}
Also used : ArrayType(org.eclipse.jdt.core.dom.ArrayType) Dimension(org.eclipse.jdt.core.dom.Dimension)

Example 33 with ArrayType

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

the class ASTNodes method getQualifiedTypeName.

/**
 * Returns the (potentially qualified) name of a type, followed by array dimensions.
 * Skips type arguments and type annotations.
 *
 * @param type a type that has a name
 * @return the name, followed by array dimensions
 * @since 3.10
 */
public static String getQualifiedTypeName(Type type) {
    final StringBuffer buffer = new StringBuffer();
    ASTVisitor visitor = new ASTVisitor() {

        @Override
        public boolean visit(SimpleType node) {
            buffer.append(node.getName().getFullyQualifiedName());
            return false;
        }

        @Override
        public boolean visit(QualifiedType node) {
            node.getQualifier().accept(this);
            buffer.append('.');
            buffer.append(node.getName().getIdentifier());
            return false;
        }

        @Override
        public boolean visit(NameQualifiedType node) {
            buffer.append(node.getQualifier().getFullyQualifiedName());
            buffer.append('.');
            buffer.append(node.getName().getIdentifier());
            return false;
        }

        @Override
        public boolean visit(ParameterizedType node) {
            node.getType().accept(this);
            return false;
        }

        @Override
        public void endVisit(ArrayType node) {
            for (int i = 0; i < node.dimensions().size(); i++) {
                // $NON-NLS-1$
                buffer.append("[]");
            }
        }
    };
    type.accept(visitor);
    return buffer.toString();
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor)

Example 34 with ArrayType

use of org.eclipse.jdt.core.dom.ArrayType in project AutoRefactor by JnRouvignac.

the class ASTNodeFactory method copyType.

private Type copyType(final Type type) {
    switch(type.getNodeType()) {
        case ASTNode.ARRAY_TYPE:
            ArrayType arrayType = (ArrayType) type;
            return ast.newArrayType(copyType(arrayType.getElementType()), arrayType.getDimensions());
        case ASTNode.PRIMITIVE_TYPE:
            Code code = ((PrimitiveType) type).getPrimitiveTypeCode();
            return ast.newPrimitiveType(code);
        case ASTNode.QUALIFIED_TYPE:
            ITypeBinding typeBinding = type.resolveBinding();
            if (typeBinding == null) {
                return null;
            }
            return type(typeBinding.getQualifiedName());
        case ASTNode.SIMPLE_TYPE:
            SimpleType sType = (SimpleType) type;
            return ast.newSimpleType(createCopyTarget(sType.getName()));
        case ASTNode.PARAMETERIZED_TYPE:
            ParameterizedType pType = (ParameterizedType) type;
            ParameterizedType copyOfType = ast.newParameterizedType(createCopyTarget(pType.getType()));
            List<Type> newTypeArgs = copyOfType.typeArguments();
            for (Object typeArg : pType.typeArguments()) {
                if (((Type) typeArg).isWildcardType()) {
                    newTypeArgs.add(ast.newWildcardType());
                } else {
                    newTypeArgs.add(createCopyTarget((Type) typeArg));
                }
            }
            return copyOfType;
    }
    // $NON-NLS-1$
    throw new NotImplementedException(null, "Unknown type for type " + type);
}
Also used : ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) 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) WildcardType(org.eclipse.jdt.core.dom.WildcardType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) NotImplementedException(org.autorefactor.util.NotImplementedException) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) Code(org.eclipse.jdt.core.dom.PrimitiveType.Code)

Example 35 with ArrayType

use of org.eclipse.jdt.core.dom.ArrayType in project flow by vaadin.

the class CodeTest method gwtGenerics.

private static void gwtGenerics(File file) throws IOException {
    ASTParser parser = ASTParser.newParser(AST.JLS8);
    String value = FileUtils.readFileToString(file, UTF_8);
    parser.setSource(value.toCharArray());
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
    cu.accept(new ASTVisitor() {

        Set<String> imports = new HashSet<>();

        String packageName;

        @Override
        public boolean visit(PackageDeclaration node) {
            packageName = node.getName().toString();
            return false;
        }

        @Override
        public boolean visit(ImportDeclaration node) {
            imports.add(node.getName().toString());
            return false;
        }

        @Override
        public boolean visit(VariableDeclarationStatement node) {
            for (Object frament : node.fragments()) {
                if (frament instanceof VariableDeclarationFragment) {
                    VariableDeclarationFragment variableDeclaration = (VariableDeclarationFragment) frament;
                    Expression expression = variableDeclaration.getInitializer();
                    if (expression instanceof ClassInstanceCreation) {
                        ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation) expression;
                        Class<?> typeClass = getClass(node.getType());
                        Class<?> instanceClass = getClass(classInstanceCreation.getType());
                        if (typeClass != instanceClass && typeClass.isAssignableFrom(instanceClass)) {
                            fail("Variable type must be the specific implementation in " + node + " in " + file.getName());
                        }
                    }
                }
            }
            return false;
        }

        private Class<?> getClass(Type type) {
            if (type instanceof ArrayType) {
                type = ((ArrayType) type).getElementType();
            }
            if (type instanceof ParameterizedType) {
                type = ((ParameterizedType) type).getType();
            }
            String className = type.toString();
            if (className.indexOf('.') == -1) {
                String dotPrefix = '.' + className;
                for (String i : imports) {
                    if (i.endsWith(dotPrefix)) {
                        className = i;
                        break;
                    }
                }
            }
            Class<?> clas = getClass(className);
            if (clas != null) {
                return clas;
            }
            clas = getClass("java.lang." + className);
            if (clas != null) {
                return clas;
            }
            try {
                String fileName = file.getName();
                fileName = fileName.substring(0, fileName.lastIndexOf('.'));
                if (fileName.equals(className)) {
                    return Class.forName(packageName + '.' + fileName);
                }
                clas = getClass(packageName + '.' + className);
                if (clas != null) {
                    return clas;
                }
                return Class.forName(packageName + '.' + fileName + '$' + className);
            } catch (ClassNotFoundException e) {
                fail("Could not load class " + e);
                return null;
            }
        }

        private Class<?> getClass(String className) {
            try {
                return ClassUtils.getClass(className);
            } catch (ClassNotFoundException e) {
                return null;
            }
        }
    });
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) Type(org.eclipse.jdt.core.dom.Type) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ImportDeclaration(org.eclipse.jdt.core.dom.ImportDeclaration) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ASTParser(org.eclipse.jdt.core.dom.ASTParser) PackageDeclaration(org.eclipse.jdt.core.dom.PackageDeclaration) HashSet(java.util.HashSet)

Aggregations

ArrayType (org.eclipse.jdt.core.dom.ArrayType)39 Type (org.eclipse.jdt.core.dom.Type)27 ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)21 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)18 SimpleType (org.eclipse.jdt.core.dom.SimpleType)18 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)15 Dimension (org.eclipse.jdt.core.dom.Dimension)10 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)10 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)10 QualifiedType (org.eclipse.jdt.core.dom.QualifiedType)8 SimpleName (org.eclipse.jdt.core.dom.SimpleName)8 List (java.util.List)7 ASTNode (org.eclipse.jdt.core.dom.ASTNode)7 NameQualifiedType (org.eclipse.jdt.core.dom.NameQualifiedType)7 UnionType (org.eclipse.jdt.core.dom.UnionType)7 IType (org.eclipse.jdt.core.IType)6 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)6 Expression (org.eclipse.jdt.core.dom.Expression)6 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)6 ArrayList (java.util.ArrayList)5