Search in sources :

Example 1 with MethodMatcher

use of org.sonar.java.matcher.MethodMatcher in project sonar-java by SonarSource.

the class DisallowedConstructorCheck method getMethodInvocationMatchers.

@Override
protected List<MethodMatcher> getMethodInvocationMatchers() {
    MethodMatcher invocationMatcher = MethodMatcher.create().name("<init>");
    if (StringUtils.isEmpty(className)) {
        return Collections.emptyList();
    }
    invocationMatcher.typeDefinition(className);
    if (allOverloads) {
        invocationMatcher.withAnyParameters();
    } else {
        String[] args = StringUtils.split(argumentTypes, ",");
        if (args.length == 0) {
            invocationMatcher.withoutParameter();
        } else {
            for (String arg : args) {
                invocationMatcher.addParameter(StringUtils.trim(arg));
            }
        }
    }
    return Collections.singletonList(invocationMatcher);
}
Also used : MethodMatcher(org.sonar.java.matcher.MethodMatcher)

Example 2 with MethodMatcher

use of org.sonar.java.matcher.MethodMatcher in project sonar-java by SonarSource.

the class WriteObjectTheOnlySynchronizedMethodCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    MethodTree methodTree = (MethodTree) tree;
    Symbol.TypeSymbol enclosingClass = methodTree.symbol().enclosingClass();
    String className = enclosingClass.type().fullyQualifiedName();
    MethodMatcher 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) MethodMatcher(org.sonar.java.matcher.MethodMatcher)

Example 3 with MethodMatcher

use of org.sonar.java.matcher.MethodMatcher 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()) {
        MethodMatcher writeObjectMatcher = writeObjectMatcher(classFullyQualifiedName);
        MethodMatcher 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) MethodMatcher(org.sonar.java.matcher.MethodMatcher)

Example 4 with MethodMatcher

use of org.sonar.java.matcher.MethodMatcher 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();
    MethodMatcher valueOfMatcher = MethodMatcher.create().typeDefinition(type.fullyQualifiedName()).name("valueOf").addParameter(((JavaType) type).primitiveType().fullyQualifiedName());
    return valueOfMatcher.matches((MethodInvocationTree) abstractTypedTree);
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) JavaType(org.sonar.java.resolve.JavaType) JavaType(org.sonar.java.resolve.JavaType) MethodMatcher(org.sonar.java.matcher.MethodMatcher)

Example 5 with MethodMatcher

use of org.sonar.java.matcher.MethodMatcher in project sonar-java by SonarSource.

the class InvalidDateValuesCheck method getThresholdToCheck.

@CheckForNull
private static String getThresholdToCheck(ExpressionTree tree) {
    if (tree.is(Tree.Kind.METHOD_INVOCATION)) {
        MethodInvocationTree mit = (MethodInvocationTree) tree;
        String name = getMethodName(mit);
        for (MethodMatcher methodInvocationMatcher : DATE_METHODS_COMPARISON) {
            if (methodInvocationMatcher.matches(mit)) {
                return getName(mit, name);
            }
        }
    }
    return null;
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) MethodMatcher(org.sonar.java.matcher.MethodMatcher) CheckForNull(javax.annotation.CheckForNull)

Aggregations

MethodMatcher (org.sonar.java.matcher.MethodMatcher)6 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)2 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)2 CheckForNull (javax.annotation.CheckForNull)1 JavaType (org.sonar.java.resolve.JavaType)1 Symbol (org.sonar.plugins.java.api.semantic.Symbol)1 Type (org.sonar.plugins.java.api.semantic.Type)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 Tree (org.sonar.plugins.java.api.tree.Tree)1