Search in sources :

Example 1 with SymbolMetadata

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

the class NullableAnnotationUtils method nonNullAnnotation.

@CheckForNull
public static String nonNullAnnotation(Symbol symbol) {
    SymbolMetadata metadata = symbol.metadata();
    if (isNullableThroughNonNull(symbol)) {
        return null;
    }
    Optional<String> result = NONNULL_ANNOTATIONS.stream().filter(metadata::isAnnotatedWith).findFirst();
    if (result.isPresent()) {
        return result.get();
    }
    if (isMethodAnnotatedWithEclipseNonNullReturnType(symbol)) {
        return ORG_ECLIPSE_JDT_ANNOTATION_NON_NULL_BY_DEFAULT;
    }
    return null;
}
Also used : SymbolMetadata(org.sonar.plugins.java.api.semantic.SymbolMetadata) CheckForNull(javax.annotation.CheckForNull)

Example 2 with SymbolMetadata

use of org.sonar.plugins.java.api.semantic.SymbolMetadata 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"));
}
Also used : SymbolMetadata(org.sonar.plugins.java.api.semantic.SymbolMetadata) TypeJavaSymbol(org.sonar.java.resolve.JavaSymbol.TypeJavaSymbol) TypeJavaSymbol(org.sonar.java.resolve.JavaSymbol.TypeJavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) TypeJavaSymbol(org.sonar.java.resolve.JavaSymbol.TypeJavaSymbol) Test(org.junit.Test)

Example 3 with SymbolMetadata

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

the class SpringComponentWithWrongScopeCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    ClassTree clazzTree = (ClassTree) tree;
    SymbolMetadata clazzMeta = clazzTree.symbol().metadata();
    if (isSpringComponent(clazzMeta) && clazzMeta.isAnnotatedWith(SCOPE_ANNOTATION_FQN) && !isScopeSingleton(clazzMeta)) {
        checkScopeAnnotation(clazzTree);
    }
}
Also used : SymbolMetadata(org.sonar.plugins.java.api.semantic.SymbolMetadata) ClassTree(org.sonar.plugins.java.api.tree.ClassTree)

Example 4 with SymbolMetadata

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

the class NoTestInTestClassCheck method isJUnitTestableMetaAnnotated.

private boolean isJUnitTestableMetaAnnotated(Type type) {
    if (seenAnnotations.contains(type.fullyQualifiedName())) {
        return false;
    }
    seenAnnotations.add(type.fullyQualifiedName());
    SymbolMetadata metadata = type.symbol().metadata();
    if (metadata.isAnnotatedWith("org.junit.platform.commons.annotation.Testable")) {
        testAnnotations.add(type.fullyQualifiedName());
        return true;
    }
    for (SymbolMetadata.AnnotationInstance annotation : metadata.annotations()) {
        if (isJUnitTestableMetaAnnotated(annotation.symbol().type())) {
            testAnnotations.add(type.fullyQualifiedName());
            return true;
        }
    }
    return false;
}
Also used : SymbolMetadata(org.sonar.plugins.java.api.semantic.SymbolMetadata)

Example 5 with SymbolMetadata

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

the class SpringComponentWithNonAutowiredMembersCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    ClassTree clazzTree = (ClassTree) tree;
    SymbolMetadata clazzMeta = clazzTree.symbol().metadata();
    if (isSpringComponent(clazzMeta) && !hasUniqueConstructor(clazzTree)) {
        clazzTree.members().stream().filter(v -> v.is(Kind.VARIABLE)).map(m -> (VariableTree) m).filter(v -> !v.symbol().isStatic()).filter(v -> !isSpringInjectionAnnotated(v.symbol().metadata())).forEach(v -> reportIssue(v.simpleName(), "Annotate this member with \"@Autowired\", \"@Resource\", \"@Inject\", or \"@Value\", or remove it."));
    }
}
Also used : VariableTree(org.sonar.plugins.java.api.tree.VariableTree) List(java.util.List) Kind(org.sonar.plugins.java.api.tree.Tree.Kind) ImmutableList(com.google.common.collect.ImmutableList) SymbolMetadata(org.sonar.plugins.java.api.semantic.SymbolMetadata) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) Rule(org.sonar.check.Rule) Tree(org.sonar.plugins.java.api.tree.Tree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) SymbolMetadata(org.sonar.plugins.java.api.semantic.SymbolMetadata) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree)

Aggregations

SymbolMetadata (org.sonar.plugins.java.api.semantic.SymbolMetadata)5 Symbol (org.sonar.plugins.java.api.semantic.Symbol)2 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)2 ImmutableList (com.google.common.collect.ImmutableList)1 List (java.util.List)1 CheckForNull (javax.annotation.CheckForNull)1 Test (org.junit.Test)1 Rule (org.sonar.check.Rule)1 TypeJavaSymbol (org.sonar.java.resolve.JavaSymbol.TypeJavaSymbol)1 IssuableSubscriptionVisitor (org.sonar.plugins.java.api.IssuableSubscriptionVisitor)1 Tree (org.sonar.plugins.java.api.tree.Tree)1 Kind (org.sonar.plugins.java.api.tree.Tree.Kind)1 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)1