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);
}
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;
}
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.");
}
}
}
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);
}
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);
}
Aggregations