Search in sources :

Example 86 with Type

use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.

the class TypeAndReferenceSolver method visitUnionType.

@Override
public void visitUnionType(UnionTypeTree tree) {
    resolveAs((List<TypeTree>) tree.typeAlternatives(), JavaSymbol.TYP);
    ImmutableSet.Builder<Type> uniontype = ImmutableSet.builder();
    for (TypeTree typeTree : tree.typeAlternatives()) {
        uniontype.add(typeTree.symbolType());
    }
    registerType(tree, (JavaType) resolve.leastUpperBound(uniontype.build()));
}
Also used : UnionTypeTree(org.sonar.plugins.java.api.tree.UnionTypeTree) ArrayTypeTree(org.sonar.plugins.java.api.tree.ArrayTypeTree) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) PrimitiveTypeTree(org.sonar.plugins.java.api.tree.PrimitiveTypeTree) Type(org.sonar.plugins.java.api.semantic.Type) ImmutableSet(com.google.common.collect.ImmutableSet)

Example 87 with Type

use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.

the class TypeAndReferenceSolver method inferArgumentTypes.

private void inferArgumentTypes(List<JavaType> argTypes, Resolve.Resolution resolution) {
    Type formal = Symbols.unknownType;
    for (int i = 0; i < argTypes.size(); i++) {
        JavaType arg = argTypes.get(i);
        if (resolution.symbol().isMethodSymbol()) {
            List<JavaType> resolvedFormals = ((MethodJavaType) resolution.type()).argTypes;
            int size = resolvedFormals.size();
            formal = resolvedFormals.get((i < size) ? i : (size - 1));
        }
        if (arg.isTagged(JavaType.DEFERRED)) {
            setInferedType(formal, (DeferredType) arg);
        }
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type)

Example 88 with Type

use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.

the class TypeAndReferenceSolver method setInferedType.

private void setInferedType(Type infered, DeferredType deferredType) {
    AbstractTypedTree inferedExpression = deferredType.tree();
    Type newType = infered;
    if (inferedExpression.is(Tree.Kind.NEW_CLASS)) {
        Type newClassType = ((NewClassTree) inferedExpression).identifier().symbolType();
        if (((JavaType) newClassType).isParameterized()) {
            newType = resolve.resolveTypeSubstitutionWithDiamondOperator((ParametrizedTypeJavaType) newClassType, (JavaType) infered);
        }
    }
    inferedExpression.setInferedType(newType);
    inferedExpression.accept(this);
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) AbstractTypedTree(org.sonar.java.model.AbstractTypedTree)

Example 89 with Type

use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.

the class LeastUpperBound method cachedLeastUpperBound.

private Type cachedLeastUpperBound(Set<Type> types) {
    Preconditions.checkArgument(!types.isEmpty());
    Iterator<Type> iterator = types.iterator();
    Type first = iterator.next();
    // lub(U) = U
    if (types.size() == 1) {
        return first;
    }
    Set<Type> newTypes = primitiveWrappers(types);
    List<Set<Type>> supertypes = supertypes(newTypes);
    List<Set<Type>> erasedSupertypes = erased(supertypes);
    List<Type> erasedCandidates = intersection(erasedSupertypes);
    List<Type> minimalErasedCandidates = minimalCandidates(erasedCandidates);
    if (minimalErasedCandidates.isEmpty()) {
        return Symbols.unknownType;
    }
    Multimap<Type, Type> relevantParameterizations = relevantParameterizations(minimalErasedCandidates, supertypes);
    Type erasedBest = best(minimalErasedCandidates);
    Collection<Type> erasedTypeParameterizations = relevantParameterizations.get(erasedBest);
    if (erasedTypeParameterizations != null && !erasedTypeParameterizations.contains(erasedBest)) {
        Set<Type> searchedTypes = new HashSet<>(types);
        // we interrupt calculation and use the erasure of the parameterized type instead
        if (!lubCache.contains(searchedTypes)) {
            lubCache.add(searchedTypes);
            return leastContainingParameterization(new ArrayList<>(erasedTypeParameterizations));
        }
    }
    return erasedBest;
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 90 with Type

use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.

the class LeastUpperBound method leastContainingParameterization.

/**
 * Let the "candidate" parameterization of G, Candidate(G), be the most specific parameterization of the generic type G that contains all
 * the relevant parameterizations of G: Candidate(G) = lcp(Relevant(G)), where lcp() is the least containing parameterization.
 * @param types
 * @return
 */
private Type leastContainingParameterization(List<Type> types) {
    if (types.size() == 1) {
        return types.get(0);
    }
    JavaType type1 = (JavaType) types.get(0);
    JavaType type2 = (JavaType) types.get(1);
    Type reduction = leastContainingTypeArgument(type1, type2);
    List<Type> reducedList = Lists.newArrayList(reduction);
    reducedList.addAll(types.subList(2, types.size()));
    return leastContainingParameterization(reducedList);
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type)

Aggregations

Type (org.sonar.plugins.java.api.semantic.Type)164 Test (org.junit.Test)67 Symbol (org.sonar.plugins.java.api.semantic.Symbol)23 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)23 MethodJavaSymbol (org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol)18 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)18 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)17 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)17 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)17 JavaType (org.sonar.java.resolve.JavaType)16 Tree (org.sonar.plugins.java.api.tree.Tree)15 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)13 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)12 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)11 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)10 ArrayList (java.util.ArrayList)9 TypeTree (org.sonar.plugins.java.api.tree.TypeTree)9 VisibleForTesting (com.google.common.annotations.VisibleForTesting)8 RelationalSymbolicValue (org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)8 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)8