use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class CastArithmeticOperandCheck method checkMethodInvocationArgument.
private void checkMethodInvocationArgument(MethodInvocationTree mit) {
Symbol symbol = mit.symbol();
if (symbol.isMethodSymbol()) {
List<Type> parametersTypes = ((Symbol.MethodSymbol) symbol).parameterTypes();
if (mit.arguments().size() == parametersTypes.size()) {
int i = 0;
for (Type argType : parametersTypes) {
checkExpression(argType, mit.arguments().get(i));
i++;
}
}
}
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class AssertionsWithoutMessageCheck method onMethodInvocationFound.
@Override
protected void onMethodInvocationFound(MethodInvocationTree mit) {
Symbol symbol = mit.symbol();
if (symbol.owner().type().isSubtypeOf(GENERIC_ASSERT) && !FEST_AS_METHOD.matches(mit)) {
if (isConstructor(symbol)) {
return;
}
FestVisitor visitor = new FestVisitor();
mit.methodSelect().accept(visitor);
if (!visitor.useDescription) {
reportIssue(mit, "Add a message to this assertion.");
}
} else if (mit.arguments().isEmpty() || !isString(mit.arguments().get(0)) || isAssertingOnStringWithNoMessage(mit)) {
reportIssue(mit, "Add a message to this assertion.");
}
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class BytecodeCompleterTest method annotationArrayOfEnum.
@Test
public void annotationArrayOfEnum() {
JavaSymbol.TypeJavaSymbol classSymbol = bytecodeCompleter.getClassSymbol("org.sonar.java.resolve.targets.AnnotationSymbolMethod");
Symbol barMethod = classSymbol.memberSymbols().stream().filter(symbol -> "bar".equals(symbol.name())).findFirst().get();
SymbolMetadata barMetadata = barMethod.metadata();
assertThat(barMetadata.annotations()).hasSize(1);
List<SymbolMetadata.AnnotationValue> valuesForAnnotation = barMetadata.valuesForAnnotation("org.sonar.java.resolve.targets.ArrayEnumAnnotation");
assertThat(valuesForAnnotation).hasSize(1);
assertThat(valuesForAnnotation.get(0).name()).isEqualTo("value");
Object annotationValue = valuesForAnnotation.get(0).value();
assertThat(annotationValue).isInstanceOf(Object[].class);
assertThat((Object[]) annotationValue).hasSize(2);
assertThat(Arrays.stream((Object[]) annotationValue)).allMatch(o -> o instanceof Symbol && ((Symbol) o).type().is("org.sonar.java.resolve.targets.MyEnum"));
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class BytecodeCompleterTest method deprecated_classes_should_be_flagged.
@Test
public void deprecated_classes_should_be_flagged() throws Exception {
JavaSymbol.TypeJavaSymbol deprecatedClass = bytecodeCompleter.getClassSymbol("org.sonar.java.resolve.targets.DeprecatedClass");
assertThat(deprecatedClass.isDeprecated()).isTrue();
JavaSymbol.TypeJavaSymbol staticInnerClass = (JavaSymbol.TypeJavaSymbol) deprecatedClass.members().lookup("StaticInnerClass").get(0);
assertThat(staticInnerClass.isDeprecated()).isTrue();
JavaSymbol.TypeJavaSymbol innerClass = (JavaSymbol.TypeJavaSymbol) deprecatedClass.members().lookup("InnerClass").get(0);
assertThat(innerClass.isDeprecated()).isTrue();
JavaSymbol.TypeJavaSymbol deprecatedEnum = bytecodeCompleter.getClassSymbol("org.sonar.java.resolve.targets.DeprecatedEnum");
assertThat(deprecatedEnum.isDeprecated()).isTrue();
assertThat(deprecatedEnum.memberSymbols().stream().filter(Symbol::isVariableSymbol).filter(Symbol::isEnum).noneMatch(Symbol::isDeprecated)).isTrue();
JavaSymbol.TypeJavaSymbol partiallyDeprecatedEnum = bytecodeCompleter.getClassSymbol("org.sonar.java.resolve.targets.PartiallyDeprecatedEnum");
assertThat(partiallyDeprecatedEnum.isDeprecated()).isFalse();
List<Symbol> deprecatedEnumConstants = partiallyDeprecatedEnum.memberSymbols().stream().filter(Symbol::isVariableSymbol).filter(Symbol::isEnum).filter(Symbol::isDeprecated).collect(Collectors.toList());
assertThat(deprecatedEnumConstants).hasSize(1);
assertThat(deprecatedEnumConstants.get(0).name()).isEqualTo("C");
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class BytecodeCompleterTest method forward_type_parameter_in_methods.
@Test
public void forward_type_parameter_in_methods() throws Exception {
Symbol.TypeSymbol clazz = bytecodeCompleter.getClassSymbol("org.sonar.java.resolve.targets.ForwardParameterInMethod");
assertThat(clazz.type()).isNotNull();
Collection<Symbol> symbols = clazz.lookupSymbols("bar");
assertThat(symbols).hasSize(1);
Symbol method = symbols.iterator().next();
Collection<JavaSymbol> typeParameters = ((JavaSymbol.MethodJavaSymbol) method).typeParameters().scopeSymbols();
assertThat(typeParameters).hasSize(2);
JavaSymbol xSymbol = ((JavaSymbol.MethodJavaSymbol) method).typeParameters().lookup("X").iterator().next();
JavaSymbol ySymbol = ((JavaSymbol.MethodJavaSymbol) method).typeParameters().lookup("Y").iterator().next();
assertThat(((TypeVariableJavaType) xSymbol.type).bounds).hasSize(1);
JavaType bound = ((TypeVariableJavaType) xSymbol.type).bounds.get(0);
assertThat(((ParametrizedTypeJavaType) bound).typeParameters()).hasSize(1);
assertThat(((ParametrizedTypeJavaType) bound).substitution(((ParametrizedTypeJavaType) bound).typeParameters().get(0))).isSameAs(ySymbol.type);
}
Aggregations