private boolean isNotcatched(Type type) { for (List<Type> tryCatch : tryCatches) { for (Type tryCatchType : tryCatch) { if (type.isSubtypeOf(tryCatchType)) { return false; } } } return true; }
private static boolean isAcceptedType(Type type, Set<String> accepted) { for (String acceptedType : accepted) { if (type.isSubtypeOf(acceptedType)) { return true; } } return false; }
private boolean handledByFramework(MethodInvocationTree mit) { // according to spring documentation, no leak is expected: // "Implementations do not need to concern themselves with SQLExceptions that may be thrown from operations // they attempt. The JdbcTemplate class will catch and handle SQLExceptions appropriately." return JDBC_RESOURCE_CREATIONS.anyMatch(mit) && (visitedMethodOwnerType.isSubtypeOf("org.springframework.jdbc.core.PreparedStatementCreator") || visitedMethodOwnerType.isSubtypeOf("org.springframework.jdbc.core.CallableStatementCreator")); }
public void addCatch(Type type, Block catchBlock) { if (type.is("java.lang.Exception") || type.is("java.lang.Throwable") || type.isSubtypeOf("java.lang.Error") || type.isSubtypeOf("java.lang.RuntimeException") || !type.isSubtypeOf("java.lang.Exception")) { runtimeCatches.add(catchBlock); } catches.put(type, catchBlock); } }
public boolean exitingOnRuntimeException() { if (exitSymbolicValue instanceof SymbolicValue.ExceptionalSymbolicValue) { Type exceptionType = ((SymbolicValue.ExceptionalSymbolicValue) exitSymbolicValue).exceptionType(); return exceptionType == null || exceptionType.isSubtypeOf("java.lang.RuntimeException"); } return false; }
public static boolean isUncheckedException(@Nullable Type exceptionType) { if (exceptionType == null) { return false; } return exceptionType.isSubtypeOf("java.lang.RuntimeException") || exceptionType.isSubtypeOf("java.lang.Error") || exceptionType.is("java.lang.Exception") || exceptionType.is("java.lang.Throwable"); }
private static boolean isCompatibleWithThrows(Type exceptionType, List<TypeTree> throwsClauses) { for (TypeTree typeTree : throwsClauses) { if (exceptionType.isSubtypeOf(typeTree.symbolType())) { return true; } } return false; }
private void buildTypeCast(Tree tree) { enclosingTry.peek().catches.entrySet().stream() .filter(e -> e.getKey().isSubtypeOf("java.lang.ClassCastException")) .findFirst() .ifPresent(e -> { currentBlock = createBlock(currentBlock); currentBlock.successors.add(e.getValue()); }); currentBlock.elements.add(tree); TypeCastTree typeCastTree = (TypeCastTree) tree; build(typeCastTree.expression()); }
private boolean isCatchExhaustive(Type exceptionType, List<BytecodeCFG.Block> blocksCatchingException) { return blocksCatchingException.stream() .filter(BytecodeCFG.Block::isCatchBlock) .anyMatch(b -> b.isUncaughtException() || exceptionType.isSubtypeOf(b.getExceptionType(semanticModel))); }
private static boolean isUnnecessaryLambdaCast(Type childType, Type parentType) { if (parentType.isSubtypeOf(childType)) { return true; } // intersection type on lambda should not raise an issue : required to make lambda serializable for instance if (((JavaType) childType).isTagged(JavaType.INTERSECTION)) { return false; } List<MethodJavaSymbol> childMethods = getMethodSymbolsOf(childType).collect(Collectors.toList()); return childMethods.isEmpty() || (childMethods.size() == 1 && isSingleAbstractMethodOverride(childMethods.get(0), parentType)); }
private static boolean firstArgumentIsCookie(Arguments arguments) { if (arguments.isEmpty()) { return false; } ExpressionTree firstArgument = arguments.get(0); return COOKIE_ARGUMENT_TYPES.stream().anyMatch(type -> firstArgument.symbolType().isSubtypeOf(type)); }
private static boolean isJUnitSoftAssertions(MethodInvocationTree mit) { ExpressionTree expressionTree = mit.methodSelect(); if (expressionTree.is(Tree.Kind.MEMBER_SELECT)) { Type type = ((MemberSelectExpressionTree) expressionTree).expression().symbolType(); return type.isSubtypeOf("org.assertj.core.api.JUnitSoftAssertions") || type.isSubtypeOf("org.assertj.core.api.Java6JUnitSoftAssertions"); } return false; }
private int fromInstance(Symbol symbol, Symbol owner) { int i = -1; Type ownerType = owner.type(); for (Symbol outerClass : outerClasses) { i++; if (symbol.equals(outerClass) || (ownerType != null && owner.isTypeSymbol() && outerClass.type().isSubtypeOf(ownerType))) { return i; } } return -1; }
private static boolean isAcceptableType(org.sonar.plugins.java.api.semantic.Type argType) { return argType.isSubtypeOf("java.io.Serializable") || argType.is("java.lang.Object") || argType.isPrimitive() || !argType.isClass(); }
private static boolean isJavaSecurityMessageDigestSubClass(ClassTree tree) { Symbol.TypeSymbol classSymbol = tree.symbol(); // Corner case: A type is a subtype of itself return classSymbol != null && !classSymbol.type().is(MESSAGE_DIGEST_QUALIFIED_NAME) && classSymbol.type().isSubtypeOf(MESSAGE_DIGEST_QUALIFIED_NAME); } }
@Override public void visitIdentifier(IdentifierTree tree) { Type type = tree.symbolType(); if (type instanceof MethodJavaType) { type = ((MethodJavaType) type).resultType(); } if (!sameErasure(type) && type.isSubtypeOf(classType.erasure())) { reportIssue(tree, String.format("Remove this reference to \"%s\".", type.symbol().name())); } }
@Override public void visitClass(ClassTree tree) { for (Tree member : tree.members()) { if (!tree.symbol().type().isSubtypeOf(JAVA_UTIL_COLLECTION) || !member.is(Tree.Kind.METHOD)) { scan(member); } } }
@Override public void visitNode(Tree tree) { InstanceOfTree instanceOfTree = (InstanceOfTree) tree; Type expressionType = instanceOfTree.expression().symbolType(); Type instanceOf = instanceOfTree.type().symbolType(); if (expressionType.isSubtypeOf(instanceOf) && !instanceOfTree.expression().is(Tree.Kind.NULL_LITERAL)) { reportIssue(instanceOfTree.instanceofKeyword(), "Remove this useless \"instanceof\" operator; it will always return \"true\". "); } } }
@Override public void visitMethodInvocation(MethodInvocationTree syntaxNode) { if (syntaxNode.methodSelect().is(Tree.Kind.MEMBER_SELECT)) { MemberSelectExpressionTree memberSelect = (MemberSelectExpressionTree) syntaxNode.methodSelect(); final ExpressionTree expression = memberSelect.expression(); if (expression.is(Tree.Kind.IDENTIFIER) && expression.symbolType().isSubtypeOf(LOCK)) { final String methodName = memberSelect.identifier().name(); visitMethodInvocationWithIdentifierTarget(methodName, (IdentifierTree) expression); } } }
@Override public void visitVariable(VariableTree tree) { if (tree.type().symbolType().isSubtypeOf(JAVA_UTIL_MAP)) { ExpressionTree initializer = tree.initializer(); if (initializer != null && !usesNullKey(tree.symbol())) { checkNewMap(initializer, hasEnumKey(tree.type().symbolType())); } } else { super.visitVariable(tree); } }