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