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()));
}
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);
}
}
}
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);
}
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;
}
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);
}
Aggregations