Search in sources :

Example 51 with ParameterizedType

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

the class AbstractClassSubstituteCleanUp method substituteType.

/**
 * Returns the substitute type or null if the class should be the same.
 * @param origType               The original type
 * @param originalExpression     The original expression
 * @param classesToUseWithImport The classes that should be used with simple
 *                               name.
 * @param importsToAdd           The imports that need to be added during this
 *                               cleanup.
 *
 * @return the substitute type or null if the class should be the same.
 */
protected Type substituteType(final Type origType, final ASTNode originalExpression, final Set<String> classesToUseWithImport, final Set<String> importsToAdd) {
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    ITypeBinding origTypeBinding = origType.resolveBinding();
    if (origTypeBinding == null) {
        return null;
    }
    String origRawType = origTypeBinding.getErasure().getQualifiedName();
    String substitutingClassName = getSubstitutingClassName(origRawType);
    if (substitutingClassName != null) {
        if (classesToUseWithImport.contains(substitutingClassName)) {
            importsToAdd.add(substitutingClassName);
            substitutingClassName = getSimpleName(substitutingClassName);
        }
        TypeNameDecider typeNameDecider = new TypeNameDecider(originalExpression);
        if (origTypeBinding.isParameterizedType()) {
            ITypeBinding[] origTypeArgs = origTypeBinding.getTypeArguments();
            Type[] newTypes;
            if (((ParameterizedType) origType).typeArguments().isEmpty()) {
                newTypes = new Type[0];
            } else {
                newTypes = new Type[origTypeArgs.length];
                for (int i = 0; i < origTypeArgs.length; i++) {
                    newTypes[i] = ast.toType(origTypeArgs[i], typeNameDecider);
                }
            }
            return ast.newParameterizedType(substitutingClassName, newTypes);
        }
        return ast.type(substitutingClassName);
    }
    return null;
}
Also used : Type(org.eclipse.jdt.core.dom.Type) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) TypeNameDecider(org.autorefactor.jdt.internal.corext.dom.TypeNameDecider)

Example 52 with ParameterizedType

use of org.eclipse.jdt.core.dom.ParameterizedType 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)

Example 53 with ParameterizedType

use of org.eclipse.jdt.core.dom.ParameterizedType in project evosuite by EvoSuite.

the class TestExtractingVisitor method retrieveTypeClass.

/**
 * <p>
 * retrieveTypeClass
 * </p>
 *
 * @param argument
 *            a {@link java.lang.Object} object.
 * @return a {@link java.lang.Class} object.
 */
protected Class<?> retrieveTypeClass(Object argument) {
    assert argument != null;
    if (argument instanceof SimpleType) {
        SimpleType simpleType = (SimpleType) argument;
        return retrieveTypeClass(simpleType);
    }
    if (argument instanceof ITypeBinding) {
        ITypeBinding binding = (ITypeBinding) argument;
        return retrieveTypeClass(binding);
    }
    if (argument instanceof IVariableBinding) {
        IVariableBinding variableBinding = (IVariableBinding) argument;
        return retrieveTypeClass(variableBinding.getType());
    }
    if (argument instanceof SimpleName) {
        SimpleName simpleName = (SimpleName) argument;
        return retrieveTypeClass(simpleName.resolveBinding());
    }
    if (argument instanceof StringLiteral) {
        return String.class;
    }
    if (argument instanceof NumberLiteral) {
        return retrieveTypeClass((NumberLiteral) argument);
    }
    if (argument instanceof PrimitiveType) {
        PrimitiveType primitiveType = (PrimitiveType) argument;
        String typeCode = primitiveType.getPrimitiveTypeCode().toString();
        Class<?> result = PRIMITIVE_TYPECODE_MAPPING.get(typeCode);
        assert result != null : "Could not resolve typecode " + typeCode + ".";
        return result;
    }
    if (argument instanceof ArrayType) {
        ArrayType arrayType = (ArrayType) argument;
        return retrieveTypeClass(arrayType);
    }
    if (argument instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) argument;
        return retrieveTypeClass(parameterizedType.getType());
    }
    if (argument instanceof VariableDeclarationFragment) {
        VariableDeclarationFragment varDeclFrgmnt = (VariableDeclarationFragment) argument;
        return retrieveTypeClass(varDeclFrgmnt.resolveBinding());
    }
    if (argument instanceof InfixExpression) {
        InfixExpression infixExpr = (InfixExpression) argument;
        ITypeBinding refTypeBinding = infixExpr.resolveTypeBinding();
        if (refTypeBinding != null) {
            return retrieveTypeClass(refTypeBinding);
        } else {
            throw new RuntimeException("Could not determine type class of infix expression '" + infixExpr + "'.");
        }
    }
    if (argument instanceof MethodInvocation) {
        MethodInvocation methodInvocation = (MethodInvocation) argument;
        ITypeBinding typeBinding = methodInvocation.resolveTypeBinding();
        if (typeBinding != null) {
            return retrieveTypeClass(typeBinding);
        }
        Expression typeExpression = methodInvocation.getExpression();
        if (typeExpression instanceof MethodInvocation) {
            MethodInvocation parentMethodInvocation = (MethodInvocation) typeExpression;
            IMethodBinding parentMethodBinding = parentMethodInvocation.resolveMethodBinding();
            return retrieveTypeClass(parentMethodBinding.getDeclaringClass());
        } else {
            return retrieveTypeClass(typeExpression);
        }
    }
    if (argument instanceof ArrayAccess) {
        ArrayAccess arrayAccess = (ArrayAccess) argument;
        return retrieveTypeClass(arrayAccess.getArray());
    }
    if (argument instanceof Class<?>) {
        return (Class<?>) argument;
    }
    if (argument instanceof ClassInstanceCreation) {
        return retrieveTypeClass(((ClassInstanceCreation) argument).resolveTypeBinding());
    }
    if (argument instanceof BooleanLiteral) {
        return Boolean.TYPE;
    }
    throw new UnsupportedOperationException("Retrieval of type " + argument.getClass() + " not implemented yet!");
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) BooleanLiteral(org.eclipse.jdt.core.dom.BooleanLiteral) SimpleName(org.eclipse.jdt.core.dom.SimpleName) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) PrimitiveExpression(org.evosuite.testcase.PrimitiveExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) GenericClass(org.evosuite.utils.GenericClass) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral)

Example 54 with ParameterizedType

use of org.eclipse.jdt.core.dom.ParameterizedType in project bndtools by bndtools.

the class NewTypeWizardPage method superInterfacesChanged.

/**
 * Hook method that gets called when the list of super interface has changed. The method validates the super
 * interfaces and returns the status of the validation.
 * <p>
 * Subclasses may extend this method to perform their own validation.
 * </p>
 *
 * @return the status of the validation
 */
protected IStatus superInterfacesChanged() {
    StatusInfo status = new StatusInfo();
    IPackageFragmentRoot root = getPackageFragmentRoot();
    fSuperInterfacesDialogField.enableButton(0, root != null);
    if (root != null) {
        List<InterfaceWrapper> elements = fSuperInterfacesDialogField.getElements();
        int nElements = elements.size();
        for (int i = 0; i < nElements; i++) {
            String intfname = elements.get(i).interfaceName;
            Type type = TypeContextChecker.parseSuperInterface(intfname);
            if (type == null) {
                status.setError(Messages.format(NewWizardMessages.NewTypeWizardPage_error_InvalidSuperInterfaceName, BasicElementLabels.getJavaElementName(intfname)));
                return status;
            }
            if (type instanceof ParameterizedType && !JavaModelUtil.is50OrHigher(root.getJavaProject())) {
                status.setError(Messages.format(NewWizardMessages.NewTypeWizardPage_error_SuperInterfaceNotParameterized, BasicElementLabels.getJavaElementName(intfname)));
                return status;
            }
        }
    }
    return status;
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) IType(org.eclipse.jdt.core.IType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) Type(org.eclipse.jdt.core.dom.Type) StatusInfo(org.eclipse.jdt.internal.ui.dialogs.StatusInfo) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 55 with ParameterizedType

use of org.eclipse.jdt.core.dom.ParameterizedType in project bndtools by bndtools.

the class NewTypeWizardPage method superClassChanged.

/**
 * Hook method that gets called when the superclass name has changed. The method validates the superclass name and
 * returns the status of the validation.
 * <p>
 * Subclasses may extend this method to perform their own validation.
 * </p>
 *
 * @return the status of the validation
 */
protected IStatus superClassChanged() {
    StatusInfo status = new StatusInfo();
    IPackageFragmentRoot root = getPackageFragmentRoot();
    fSuperClassDialogField.enableButton(root != null);
    fSuperClassStubTypeContext = null;
    String sclassName = getSuperClass();
    if (sclassName.length() == 0) {
        // accept the empty field (stands for java.lang.Object)
        return status;
    }
    if (root != null) {
        Type type = TypeContextChecker.parseSuperClass(sclassName);
        if (type == null) {
            status.setError(NewWizardMessages.NewTypeWizardPage_error_InvalidSuperClassName);
            return status;
        }
        if (type instanceof ParameterizedType && !JavaModelUtil.is50OrHigher(root.getJavaProject())) {
            status.setError(NewWizardMessages.NewTypeWizardPage_error_SuperClassNotParameterized);
            return status;
        }
    } else {
        // $NON-NLS-1$
        status.setError("");
    }
    return status;
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) IType(org.eclipse.jdt.core.IType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) Type(org.eclipse.jdt.core.dom.Type) StatusInfo(org.eclipse.jdt.internal.ui.dialogs.StatusInfo) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Aggregations

ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)57 Type (org.eclipse.jdt.core.dom.Type)38 SimpleType (org.eclipse.jdt.core.dom.SimpleType)23 ArrayType (org.eclipse.jdt.core.dom.ArrayType)22 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)18 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)14 ASTNode (org.eclipse.jdt.core.dom.ASTNode)13 IType (org.eclipse.jdt.core.IType)12 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)10 NameQualifiedType (org.eclipse.jdt.core.dom.NameQualifiedType)10 QualifiedType (org.eclipse.jdt.core.dom.QualifiedType)10 SimpleName (org.eclipse.jdt.core.dom.SimpleName)9 WildcardType (java.lang.reflect.WildcardType)8 org.eclipse.jdt.core.dom (org.eclipse.jdt.core.dom)8 WildcardType (org.eclipse.jdt.core.dom.WildcardType)8 Name (org.eclipse.jdt.core.dom.Name)7 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)7 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)6 UnionType (org.eclipse.jdt.core.dom.UnionType)6 AST (org.eclipse.jdt.core.dom.AST)5