use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.
the class BadConstantNameCheck method visitNode.
@Override
public void visitNode(Tree tree) {
ClassTree classTree = (ClassTree) tree;
for (Tree member : classTree.members()) {
if (member.is(Tree.Kind.VARIABLE) && hasSemantic()) {
VariableTree variableTree = (VariableTree) member;
Type symbolType = variableTree.type().symbolType();
if (isConstantType(symbolType) && (classTree.is(Tree.Kind.INTERFACE, Tree.Kind.ANNOTATION_TYPE) || isStaticFinal(variableTree))) {
checkName(variableTree);
}
} else if (member.is(Tree.Kind.ENUM_CONSTANT)) {
checkName((VariableTree) member);
}
}
}
use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.
the class ExplodedGraphWalker method executeTypeCast.
private void executeTypeCast(TypeCastTree typeCast) {
Type type = typeCast.type().symbolType();
if (type.isPrimitive()) {
JavaType expType = (JavaType) typeCast.expression().symbolType();
// create SV to consume factory if any
SymbolicValue castSV = constraintManager.createSymbolicValue(typeCast);
// if exp type is a primitive and subtype of cast type, we can reuse the same symbolic value
if (!expType.isPrimitive() || !new Types().isSubtype(expType, (JavaType) type)) {
ProgramState.Pop unstack = programState.unstackValue(1);
programState = unstack.state;
programState = programState.stackValue(castSV);
}
}
}
use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.
the class ExplodedGraphWalker method addExceptionalYield.
public void addExceptionalYield(SymbolicValue target, ProgramState exceptionalState, String exceptionFullyQualifiedName, SECheck check) {
// in order to create such Exceptional Yield, a parameter of the method has to be the cause of the exception
if (methodBehavior != null && methodBehavior.parameters().contains(target)) {
Type exceptionType = semanticModel.getClassType(exceptionFullyQualifiedName);
ProgramState newExceptionalState = exceptionalState.clearStack().stackValue(constraintManager.createExceptionalSymbolicValue(exceptionType));
ExplodedGraph.Node exitNode = explodedGraph.node(node.programPoint, newExceptionalState);
methodBehavior.createExceptionalCheckBasedYield(target, exitNode, exceptionFullyQualifiedName, check);
exitNode.addParent(node, null);
}
}
use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.
the class ExplodedGraphWalker method handleCatchVariable.
private SymbolicValue handleCatchVariable(Type caughtType) {
SymbolicValue peekValue = programState.peekValue();
SymbolicValue.ExceptionalSymbolicValue sv = null;
Type exceptionType = null;
// FIXME SONARJAVA-2069 every path conducting to a catch block should have an exceptional symbolic value on top of the stack
if (peekValue instanceof SymbolicValue.ExceptionalSymbolicValue) {
sv = (SymbolicValue.ExceptionalSymbolicValue) peekValue;
exceptionType = sv.exceptionType();
}
if (exceptionType == null || exceptionType.isUnknown()) {
// unknown exception, create an exception of the adequate type
sv = constraintManager.createExceptionalSymbolicValue(caughtType);
}
// use a dedicated SV encapsulating the caught exception
return constraintManager.createCaughtExceptionSymbolicValue(sv);
}
use of org.sonar.plugins.java.api.semantic.Type in project sonar-java by SonarSource.
the class LeastUpperBound method supertypes.
@VisibleForTesting
Set<Type> supertypes(JavaType type) {
List<Type> result = new ArrayList<>();
result.add(type);
Symbol.TypeSymbol symbol = type.symbol();
TypeSubstitution substitution = getTypeSubstitution(type);
if (substitution.size() == 0 && !((JavaSymbol.TypeJavaSymbol) symbol).typeVariableTypes.isEmpty()) {
// raw type : let's create a substitution based on erasures
TypeSubstitution ts = new TypeSubstitution();
((JavaSymbol.TypeJavaSymbol) symbol).typeVariableTypes.forEach(t -> ts.add(t, t.erasure()));
substitution = ts;
}
result.addAll(interfacesWithSubstitution(symbol, substitution));
Type superClass = symbol.superClass();
while (superClass != null) {
JavaType substitutedSuperClass = applySubstitution(superClass, substitution);
result.add(substitutedSuperClass);
substitution = getTypeSubstitution(substitutedSuperClass);
JavaSymbol.TypeJavaSymbol superClassSymbol = substitutedSuperClass.getSymbol();
result.addAll(interfacesWithSubstitution(superClassSymbol, substitution));
superClass = superClassSymbol.superClass();
}
return new LinkedHashSet<>(result);
}
Aggregations