Search in sources :

Example 1 with MethodMatchers

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

the class PrimitiveTypeBoxingWithToStringCheck method isValueOfInvocation.

private static boolean isValueOfInvocation(ExpressionTree abstractTypedTree) {
    if (!abstractTypedTree.is(Kind.METHOD_INVOCATION)) {
        return false;
    }
    Type type = abstractTypedTree.symbolType();
    MethodMatchers valueOfMatcher = MethodMatchers.create().ofTypes(type.fullyQualifiedName()).names("valueOf").addParametersMatcher(JUtils.primitiveType(type).fullyQualifiedName()).build();
    return valueOfMatcher.matches((MethodInvocationTree) abstractTypedTree);
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) MethodMatchers(org.sonar.plugins.java.api.semantic.MethodMatchers)

Example 2 with MethodMatchers

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

the class SerializableContract method hasSpecialHandlingSerializationMethods.

public static boolean hasSpecialHandlingSerializationMethods(ClassTree classTree) {
    boolean hasWriteObject = false;
    boolean hasReadObject = false;
    String classFullyQualifiedName = classTree.symbol().type().fullyQualifiedName();
    for (Tree member : classTree.members()) {
        MethodMatchers writeObjectMatcher = writeObjectMatcher(classFullyQualifiedName);
        MethodMatchers readObjectMatcher = readObjectMatcher(classFullyQualifiedName);
        if (member.is(Tree.Kind.METHOD)) {
            MethodTree methodTree = (MethodTree) member;
            if (ModifiersUtils.hasModifier(methodTree.modifiers(), Modifier.PRIVATE)) {
                hasWriteObject |= writeObjectMatcher.matches(methodTree) && methodThrows(methodTree, "java.io.IOException");
                hasReadObject |= readObjectMatcher.matches(methodTree) && methodThrows(methodTree, "java.io.IOException", "java.lang.ClassNotFoundException");
            }
        }
    }
    return hasReadObject && hasWriteObject;
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) MethodMatchers(org.sonar.plugins.java.api.semantic.MethodMatchers)

Example 3 with MethodMatchers

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

the class WriteObjectTheOnlySynchronizedMethodCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    MethodTree methodTree = (MethodTree) tree;
    Symbol.TypeSymbol enclosingClass = methodTree.symbol().enclosingClass();
    String className = enclosingClass.type().fullyQualifiedName();
    MethodMatchers writeObjectMatcher = SerializableContract.writeObjectMatcher(className);
    if (writeObjectMatcher.matches(methodTree) && hasModifier(methodTree.modifiers(), SYNCHRONIZED)) {
        SynchronizationVisitor visitor = new SynchronizationVisitor(methodTree);
        enclosingClass.declaration().accept(visitor);
        if (!visitor.moreThanSingleLock) {
            reportIssue(ModifiersUtils.getModifier(methodTree.modifiers(), SYNCHRONIZED), "Remove this \"synchronized\" keyword.");
        }
    }
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodMatchers(org.sonar.plugins.java.api.semantic.MethodMatchers)

Example 4 with MethodMatchers

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

the class MethodMatcherFactory method constructorMatcher.

public static MethodMatchers constructorMatcher(String descriptor) {
    Matcher matcher = CLASS_PATTERN.matcher(descriptor);
    if (!matcher.find()) {
        throw new IllegalArgumentException("Illegal constructor specification: " + descriptor);
    }
    MethodMatchers.ParametersBuilder constructorMatcher = MethodMatchers.create().ofTypes(matcher.group(1)).constructor();
    return collectArguments(descriptor, matcher, 2, constructorMatcher);
}
Also used : Matcher(java.util.regex.Matcher) MethodMatchers(org.sonar.plugins.java.api.semantic.MethodMatchers)

Example 5 with MethodMatchers

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

the class MethodMatcherFactory method methodMatchers.

public static MethodMatchers methodMatchers(String descriptor) {
    Matcher matcher = METHOD_PATTERN.matcher(descriptor);
    if (!matcher.find()) {
        throw new IllegalArgumentException("Illegal method specification: " + descriptor);
    }
    MethodMatchers.ParametersBuilder methodMatcher = MethodMatchers.create().ofTypes(matcher.group(1)).names(matcher.group(2));
    return collectArguments(descriptor, matcher, 3, methodMatcher);
}
Also used : Matcher(java.util.regex.Matcher) MethodMatchers(org.sonar.plugins.java.api.semantic.MethodMatchers)

Aggregations

MethodMatchers (org.sonar.plugins.java.api.semantic.MethodMatchers)10 Test (org.junit.jupiter.api.Test)4 Matcher (java.util.regex.Matcher)2 InputFile (org.sonar.api.batch.fs.InputFile)2 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)2 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)2 ArrayList (java.util.ArrayList)1 Symbol (org.sonar.plugins.java.api.semantic.Symbol)1 Type (org.sonar.plugins.java.api.semantic.Type)1 BaseTreeVisitor (org.sonar.plugins.java.api.tree.BaseTreeVisitor)1 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)1 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)1 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)1 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)1 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)1 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)1 Tree (org.sonar.plugins.java.api.tree.Tree)1