private boolean isStringType(ASTName name) { ASTType type = getTypeNode(name); if (type != null) { List<ASTClassOrInterfaceType> types = type.findDescendantsOfType(ASTClassOrInterfaceType.class); if (!types.isEmpty()) { ASTClassOrInterfaceType typeDeclaration = types.get(0); if (String.class == typeDeclaration.getType() || "String".equals(typeDeclaration.getImage())) { return true; } } } return false; }
private boolean isNewThrowable(ASTPrimaryExpression last) { // in case a new exception is created or the exception class is // mentioned. ASTClassOrInterfaceType classOrInterface = last.getFirstDescendantOfType(ASTClassOrInterfaceType.class); return classOrInterface != null && classOrInterface.getType() != null && TypeHelper.isA(classOrInterface, Throwable.class); }
private boolean isJUnitTest(ASTClassOrInterfaceType type) { Class<?> clazz = type.getType(); if (clazz == null) { if ("junit.framework.Test".equals(type.getImage())) { return true; } } else if (isJUnitTest(clazz)) { return true; } else { while (clazz != null && !Object.class.equals(clazz)) { for (Class<?> intf : clazz.getInterfaces()) { if (isJUnitTest(intf)) { return true; } } clazz = clazz.getSuperclass(); } } return false; }
private Class<?> resolveGenericType(String typeImage, List<ASTTypeParameter> types) { for (ASTTypeParameter type : types) { if (typeImage.equals(type.getImage())) { ASTClassOrInterfaceType bound = type.getFirstDescendantOfType(ASTClassOrInterfaceType.class); if (bound != null) { if (bound.getType() != null) { return bound.getType(); } else { return this.getEnclosingScope(SourceFileScope.class).resolveType(bound.getImage()); } } else { // type parameter found, but no binding. return Object.class; } } } return null; }
@Override public Object visit(ASTLocalVariableDeclaration node, Object data) { ASTClassOrInterfaceType type = null; if (!node.isTypeInferred()) { type = node.getFirstChildOfType(ASTType.class).getFirstDescendantOfType(ASTClassOrInterfaceType.class); } if (type != null && (type.getType() != null && "java.sql.ResultSet".equals(type.getType().getName()) || "ResultSet".equals(type.getImage()))) { ASTVariableDeclarator declarator = node.getFirstChildOfType(ASTVariableDeclarator.class); if (declarator != null) { ASTName name = declarator.getFirstDescendantOfType(ASTName.class); if (type.getType() != null || name != null && name.getImage().endsWith("executeQuery")) { ASTVariableDeclaratorId id = declarator.getFirstChildOfType(ASTVariableDeclaratorId.class); resultSetVariables.put(id.getImage(), node); } } } return super.visit(node, data); }
private void checkForResources(Node node, Object data) { List<ASTLocalVariableDeclaration> vars = node.findDescendantsOfType(ASTLocalVariableDeclaration.class); List<ASTVariableDeclaratorId> ids = new ArrayList<>(); // find all variable references to Connection objects for (ASTLocalVariableDeclaration var : vars) { ASTType type = var.getTypeNode(); if (type != null && type.jjtGetChild(0) instanceof ASTReferenceType) { ASTReferenceType ref = (ASTReferenceType) type.jjtGetChild(0); if (ref.jjtGetChild(0) instanceof ASTClassOrInterfaceType) { ASTClassOrInterfaceType clazz = (ASTClassOrInterfaceType) ref.jjtGetChild(0); if (clazz.getType() != null && types.contains(clazz.getType().getName()) || clazz.getType() == null && simpleTypes.contains(toSimpleType(clazz.getImage())) && !clazz.isReferenceToClassSameCompilationUnit() || types.contains(clazz.getImage()) && !clazz.isReferenceToClassSameCompilationUnit()) { ASTVariableDeclaratorId id = var.getFirstDescendantOfType(ASTVariableDeclaratorId.class); ids.add(id); } } } } // if there are connections, ensure each is closed. for (ASTVariableDeclaratorId x : ids) { ensureClosed((ASTLocalVariableDeclaration) x.jjtGetParent().jjtGetParent(), x, data); } }
@Override public Object visit(ASTClassOrInterfaceType node, Object data) { if (methodHasOverride(node)) { return data; } Node parent = node.getNthParent(3); Class<?> clazzType = node.getType(); boolean isType = CollectionUtil.isCollectionType(clazzType, false); if (isType && (parent instanceof ASTFieldDeclaration || parent instanceof ASTFormalParameter || parent instanceof ASTResultType)) { addViolation(data, node, node.getImage()); } return data; }
@Override public Object visit(ASTPrimarySuffix node, Object data) { if (node.getImage() != null && node.getImage().endsWith("size")) { ASTClassOrInterfaceType type = getTypeOfPrimaryPrefix(node); if (type == null) { type = getTypeOfMethodCall(node); } if (type != null && CollectionUtil.isCollectionType(type.getType(), true)) { Node expr = node.jjtGetParent().jjtGetParent(); checkNodeAndReport(data, node, expr); } } return data; }
final ASTBlockStatement block = blocks.get(0); final ASTClassOrInterfaceType type = block.getFirstDescendantOfType(ASTClassOrInterfaceType.class); if (type != null && type.getType() != null && type.getNthParent(9).equals(node) && type.getType().equals(CloneNotSupportedException.class)) { return data; } else if (type != null && type.getType() == null && "CloneNotSupportedException".equals(type.getImage())) { return data;
@Override public Object visit(ASTImplementsList node, Object data) { for (int ix = 0; ix < node.jjtGetNumChildren(); ix++) { if (node.jjtGetChild(ix) instanceof ASTClassOrInterfaceType) { ASTClassOrInterfaceType cit = (ASTClassOrInterfaceType) node.jjtGetChild(ix); Class<?> clazz = cit.getType(); if (clazz != null && node.jjtGetChild(ix).hasImageEqualTo("Comparable")) { implementsComparable = true; return data; } } } return super.visit(node, data); }
@Override public Object visit(ASTVariableDeclarator node, Object data) { if (count > 1) { return super.visit(node, data); } ASTType type = node.jjtGetParent().getFirstChildOfType(ASTType.class); if (type != null) { Node reftypeNode = type.jjtGetChild(0); if (reftypeNode instanceof ASTReferenceType) { Node classOrIntType = reftypeNode.jjtGetChild(0); if (classOrIntType instanceof ASTClassOrInterfaceType) { Class<?> clazzType = ((ASTClassOrInterfaceType) classOrIntType).getType(); if (clazzType != null && (TypeHelper.isA((ASTClassOrInterfaceType) classOrIntType, LOG4J_LOGGER_NAME) || TypeHelper.isA((ASTClassOrInterfaceType) classOrIntType, JAVA_LOGGER_NAME) || TypeHelper.isA((ASTClassOrInterfaceType) classOrIntType, SLF4J_LOGGER_NAME)) || clazzType == null && "Logger".equals(classOrIntType.getImage())) { ++count; } } } } return super.visit(node, data); }
@Override public Object visit(ASTClassOrInterfaceType node, Object data) { super.visit(node, data); String typeName = node.getImage(); if (node.isAnonymousClass()) { QualifiableNode parent = node.getFirstParentOfAnyType(ASTAllocationExpression.class, ASTEnumConstant.class); if (parent != null) { typeName = parent.getQualifiedName().toString(); } } // FIXME, we should discard the array depth on this node, it should only be known to ASTReferenceType (#910) populateType(node, typeName, node.getArrayDepth()); ASTTypeArguments typeArguments = node.getFirstChildOfType(ASTTypeArguments.class); if (typeArguments != null) { final JavaTypeDefinition[] boundGenerics = new JavaTypeDefinition[typeArguments.jjtGetNumChildren()]; for (int i = 0; i < typeArguments.jjtGetNumChildren(); ++i) { boundGenerics[i] = ((TypeNode) typeArguments.jjtGetChild(i)).getTypeDefinition(); } node.setTypeDefinition(JavaTypeDefinition.forClass(node.getType(), boundGenerics)); } return data; }
private boolean hasThread(ASTExtendsList extendsList) { List<ASTClassOrInterfaceType> typeList = extendsList.findChildrenOfType(ASTClassOrInterfaceType.class); if (typeList == null || typeList.isEmpty()) { return false; } for (ASTClassOrInterfaceType type : typeList) { if (type.getType() == Thread.class) { return true; } } return false; } }
private boolean isStringType(ASTName name) { ASTType type = getTypeNode(name); if (type != null) { List<ASTClassOrInterfaceType> types = type.findDescendantsOfType(ASTClassOrInterfaceType.class); if (!types.isEmpty()) { ASTClassOrInterfaceType typeDeclaration = types.get(0); if (String.class == typeDeclaration.getType() || "String".equals(typeDeclaration.getImage())) { return true; } } } return false; }
private boolean isNewThrowable(ASTPrimaryExpression last) { // in case a new exception is created or the exception class is // mentioned. ASTClassOrInterfaceType classOrInterface = last.getFirstDescendantOfType(ASTClassOrInterfaceType.class); return classOrInterface != null && classOrInterface.getType() != null && TypeHelper.isA(classOrInterface, Throwable.class); }
@Override public Object visit(ASTClassOrInterfaceType node, Object data) { if (methodHasOverride(node)) { return data; } Node parent = node.getNthParent(3); Class<?> clazzType = node.getType(); boolean isType = CollectionUtil.isCollectionType(clazzType, false); if (isType && (parent instanceof ASTFieldDeclaration || parent instanceof ASTFormalParameter || parent instanceof ASTResultType)) { addViolation(data, node, node.getImage()); } return data; }
@Override public Object visit(final ASTPrimarySuffix node, final Object data) { if (node.getImage() != null && node.getImage().endsWith("length")) { ASTClassOrInterfaceType type = getTypeOfPrimaryPrefix(node); if (type == null) { type = getTypeOfMethodCall(node); } if (type != null && this.appliesToClassName(type.getType().getSimpleName()) ) { checkNodeAndReport( data, node, node.jjtGetParent().jjtGetParent() ); } } return data; }
@Override public Object visit(ASTPrimarySuffix node, Object data) { if (node.getImage() != null && node.getImage().endsWith("size")) { ASTClassOrInterfaceType type = getTypeOfPrimaryPrefix(node); if (type == null) { type = getTypeOfMethodCall(node); } if (type != null && CollectionUtil.isCollectionType(type.getType(), true)) { Node expr = node.jjtGetParent().jjtGetParent(); checkNodeAndReport(data, node, expr); } } return data; }
@Override public Object visit(ASTImplementsList node, Object data) { for (int ix = 0; ix < node.jjtGetNumChildren(); ix++) { if (node.jjtGetChild(ix) instanceof ASTClassOrInterfaceType) { ASTClassOrInterfaceType cit = (ASTClassOrInterfaceType) node.jjtGetChild(ix); Class<?> clazz = cit.getType(); if (clazz != null && node.jjtGetChild(ix).hasImageEqualTo("Comparable")) { implementsComparable = true; return data; } } } return super.visit(node, data); }