Search in sources :

Example 16 with SimpleType

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

the class InferTypeArgumentsRefactoring method rewriteTypeVariable.

private static ParameterizedType rewriteTypeVariable(TypeVariable2 typeCv, CompilationUnitRewrite rewrite, InferTypeArgumentsTCModel tCModel, boolean leaveUnconstraindRaw, SimpleType[] types) {
    ASTNode node = typeCv.getRange().getNode(rewrite.getRoot());
    if (node instanceof Name && node.getParent() instanceof Type) {
        Type originalType = (Type) node.getParent();
        if (types != null && !has(types, originalType))
            return null;
        // Must rewrite all type arguments in one batch. Do the rewrite when the first one is encountered; skip the others.
        Object rewritten = originalType.getProperty(REWRITTEN);
        if (rewritten == REWRITTEN)
            return null;
        originalType.setProperty(REWRITTEN, REWRITTEN);
        ArrayList<CollectionElementVariable2> typeArgumentCvs = getTypeArgumentCvs(typeCv, tCModel);
        Type[] typeArguments = getTypeArguments(originalType, typeArgumentCvs, rewrite, tCModel, leaveUnconstraindRaw);
        if (typeArguments == null)
            return null;
        Type movingType = (Type) rewrite.getASTRewrite().createMoveTarget(originalType);
        ParameterizedType newType = rewrite.getAST().newParameterizedType(movingType);
        for (int i = 0; i < typeArguments.length; i++) {
            newType.typeArguments().add(typeArguments[i]);
        }
        rewrite.getASTRewrite().replace(originalType, newType, rewrite.createGroupDescription(RefactoringCoreMessages.InferTypeArgumentsRefactoring_addTypeArguments));
        return newType;
    } else {
        //TODO: other node types?
        return null;
    }
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) TType(org.eclipse.jdt.internal.corext.refactoring.typeconstraints.types.TType) CollectionElementVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CollectionElementVariable2) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Name(org.eclipse.jdt.core.dom.Name)

Example 17 with SimpleType

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

the class Java50Fix method getRawReference.

private static SimpleType getRawReference(MethodInvocation invocation, CompilationUnit compilationUnit) {
    Name name1 = (Name) invocation.getStructuralProperty(MethodInvocation.NAME_PROPERTY);
    if (name1 instanceof SimpleName) {
        SimpleType rawReference = getRawReference((SimpleName) name1, compilationUnit);
        if (rawReference != null) {
            return rawReference;
        }
    }
    Expression expr = (Expression) invocation.getStructuralProperty(MethodInvocation.EXPRESSION_PROPERTY);
    if (expr instanceof SimpleName) {
        SimpleType rawReference = getRawReference((SimpleName) expr, compilationUnit);
        if (rawReference != null) {
            return rawReference;
        }
    } else if (expr instanceof QualifiedName) {
        Name name = (Name) expr;
        while (name instanceof QualifiedName) {
            SimpleName simpleName = (SimpleName) name.getStructuralProperty(QualifiedName.NAME_PROPERTY);
            SimpleType rawReference = getRawReference(simpleName, compilationUnit);
            if (rawReference != null) {
                return rawReference;
            }
            name = (Name) name.getStructuralProperty(QualifiedName.QUALIFIER_PROPERTY);
        }
        if (name instanceof SimpleName) {
            SimpleType rawReference = getRawReference((SimpleName) name, compilationUnit);
            if (rawReference != null) {
                return rawReference;
            }
        }
    } else if (expr instanceof MethodInvocation) {
        SimpleType rawReference = getRawReference((MethodInvocation) expr, compilationUnit);
        if (rawReference != null) {
            return rawReference;
        }
    }
    return null;
}
Also used : SimpleType(org.eclipse.jdt.core.dom.SimpleType) Expression(org.eclipse.jdt.core.dom.Expression) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name)

Example 18 with SimpleType

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

the class Java50Fix method isRawTypeReference.

private static boolean isRawTypeReference(ASTNode node) {
    if (!(node instanceof SimpleType))
        return false;
    ITypeBinding typeBinding = ((SimpleType) node).resolveBinding();
    if (typeBinding == null)
        return false;
    ITypeBinding binding = typeBinding.getTypeDeclaration();
    if (binding == null)
        return false;
    ITypeBinding[] parameters = binding.getTypeParameters();
    if (parameters.length == 0)
        return false;
    return true;
}
Also used : SimpleType(org.eclipse.jdt.core.dom.SimpleType) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Example 19 with SimpleType

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

the class Java50Fix method createRawTypeReferenceOperations.

private static SimpleType createRawTypeReferenceOperations(CompilationUnit compilationUnit, IProblemLocation[] locations, List<CompilationUnitRewriteOperation> operations) {
    if (hasFatalError(compilationUnit))
        return null;
    List<SimpleType> result = new ArrayList<SimpleType>();
    for (int i = 0; i < locations.length; i++) {
        IProblemLocation problem = locations[i];
        if (isRawTypeReferenceProblem(problem.getProblemId())) {
            ASTNode node = problem.getCoveredNode(compilationUnit);
            if (node instanceof ClassInstanceCreation) {
                Type rawReference = (Type) node.getStructuralProperty(ClassInstanceCreation.TYPE_PROPERTY);
                if (isRawTypeReference(rawReference)) {
                    result.add((SimpleType) rawReference);
                }
            } else if (node instanceof SimpleName) {
                ASTNode rawReference = node.getParent();
                if (isRawTypeReference(rawReference)) {
                    ASTNode parent = rawReference.getParent();
                    if (!(parent instanceof ArrayType || parent instanceof ParameterizedType))
                        result.add((SimpleType) rawReference);
                }
            } else if (node instanceof MethodInvocation) {
                MethodInvocation invocation = (MethodInvocation) node;
                SimpleType rawReference = getRawReference(invocation, compilationUnit);
                if (rawReference != null) {
                    result.add(rawReference);
                }
            }
        }
    }
    if (result.size() == 0)
        return null;
    SimpleType[] types = result.toArray(new SimpleType[result.size()]);
    operations.add(new AddTypeParametersOperation(types));
    return types[0];
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) IProblemLocation(org.eclipse.jdt.ui.text.java.IProblemLocation) 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) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Example 20 with SimpleType

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

the class PotentialProgrammingProblemsFix method getSelectedName.

private static SimpleName getSelectedName(CompilationUnit compilationUnit, IProblemLocation problem) {
    final ASTNode selection = problem.getCoveredNode(compilationUnit);
    if (selection == null)
        return null;
    Name name = null;
    if (selection instanceof SimpleType) {
        name = ((SimpleType) selection).getName();
    } else if (selection instanceof NameQualifiedType) {
        name = ((NameQualifiedType) selection).getName();
    } else if (selection instanceof QualifiedType) {
        name = ((QualifiedType) selection).getName();
    } else if (selection instanceof ParameterizedType) {
        final ParameterizedType type = (ParameterizedType) selection;
        final Type raw = type.getType();
        if (raw instanceof SimpleType)
            name = ((SimpleType) raw).getName();
        else if (raw instanceof NameQualifiedType)
            name = ((NameQualifiedType) raw).getName();
        else if (raw instanceof QualifiedType)
            name = ((QualifiedType) raw).getName();
    } else if (selection instanceof Name) {
        name = (Name) selection;
    }
    if (name == null)
        return null;
    if (name.isSimpleName()) {
        return (SimpleName) name;
    } else {
        return ((QualifiedName) name).getName();
    }
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) 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) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName)

Aggregations

SimpleType (org.eclipse.jdt.core.dom.SimpleType)23 ASTNode (org.eclipse.jdt.core.dom.ASTNode)13 SimpleName (org.eclipse.jdt.core.dom.SimpleName)13 Type (org.eclipse.jdt.core.dom.Type)10 ArrayType (org.eclipse.jdt.core.dom.ArrayType)9 ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)9 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)8 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)8 ArrayList (java.util.ArrayList)7 Name (org.eclipse.jdt.core.dom.Name)7 NameQualifiedType (org.eclipse.jdt.core.dom.NameQualifiedType)7 List (java.util.List)5 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)5 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)5 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)5 Expression (org.eclipse.jdt.core.dom.Expression)5 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)5 IType (org.eclipse.jdt.core.IType)4 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)4 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)4