@Override public Object visit(ASTClassOrInterfaceType node, Object data) { // This name has no qualification, it can't be unnecessarily qualified if (node.getImage().indexOf('.') < 0) { return data; } checkImports(node, data); return data; }
/** * Gets the image of the first ASTClassOrInterfaceType child or * <code>null</code> if none is found. Note that when the statement is * something like throw new Exception, this method returns 'Exception' and * if the throw statement is like throw e: this method returns 'e'. A * special case of returning <code>null</code> is when the throws is like * throw this.e or throw this. * * <p>TODO - use symbol table (?)</p> * * @return the image of the first ASTClassOrInterfaceType node found or * <code>null</code> */ public final String getFirstClassOrInterfaceTypeImage() { final ASTClassOrInterfaceType t = getFirstDescendantOfType(ASTClassOrInterfaceType.class); return t == null ? null : t.getImage(); } }
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 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; }
public String getTypeImage() { ASTClassOrInterfaceType refType = getFirstDescendantOfType(ASTClassOrInterfaceType.class); if (refType != null) { return refType.getImage(); } return getFirstDescendantOfType(ASTPrimitiveType.class).getImage(); }
/** * Checks whether the type this node is referring to is declared within the * same compilation unit - either a class/interface or a enum type. You want * to check this, if {@link #getType()} is null. * * @return <code>true</code> if this node referencing a type in the same * compilation unit, <code>false</code> otherwise. */ public boolean isReferenceToClassSameCompilationUnit() { ASTCompilationUnit root = getFirstParentOfType(ASTCompilationUnit.class); for (ASTClassOrInterfaceDeclaration c : root.findDescendantsOfType(ASTClassOrInterfaceDeclaration.class, true)) { if (c.hasImageEqualTo(getImage())) { return true; } } for (ASTEnumDeclaration e : root.findDescendantsOfType(ASTEnumDeclaration.class, true)) { if (e.hasImageEqualTo(getImage())) { return true; } } return false; }
String name = ((ASTClassOrInterfaceType) child).getImage(); results.add(name);
private SimpleTypedNameDeclaration convertToSimpleType(ASTClassOrInterfaceType t) { String typeImage = t.getImage(); typeImage = qualifyTypeName(typeImage); Node declaringNode = getEnclosingScope(SourceFileScope.class).getQualifiedTypeNames().get(typeImage); return new SimpleTypedNameDeclaration(typeImage, this.getEnclosingScope(SourceFileScope.class).resolveType(typeImage), determineSuper(declaringNode)); }
@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 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(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; }
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); } }
private boolean isJUnit3Class(ASTCompilationUnit node) { ASTClassOrInterfaceDeclaration cid = node.getFirstDescendantOfType(ASTClassOrInterfaceDeclaration.class); if (cid == null) { return false; } if (node.getType() != null && TypeHelper.isA(node, JUNIT3_CLASS_NAME)) { return true; } else if (node.getType() == null) { ASTExtendsList extendsList = cid.getFirstChildOfType(ASTExtendsList.class); if (extendsList == null) { return false; } if (((ASTClassOrInterfaceType) extendsList.jjtGetChild(0)).getImage().endsWith("TestCase")) { return true; } String className = cid.getImage(); return className.endsWith("Test"); } else if (hasImports(node, JUNIT3_CLASS_NAME)) { return cid.getImage().endsWith("Test"); } return false; }
@Override public Object visit(ASTClassOrInterfaceType classType, Object data) { // Is extends/implements list using one of the previous match on import ? // FIXME: use type resolution framework to deal with star import ? for (String matchType : simpleClassname) { if (searchForAMatch(matchType, classType)) { addAMatch(classType, data); } } // TODO: implements the "operand" functionnality // Is there any names that actually match ? for (Pattern pattern : this.namesMatch) { if (RegexHelper.isMatch(pattern, classType.getImage())) { addAMatch(classType, data); } } return super.visit(classType, data); }
if ("Cloneable".equals(type.getImage())) { return true;
ASTClassOrInterfaceType param = formalParam.getFirstDescendantOfType(ASTClassOrInterfaceType.class); if (param != null) { paramName = param.getImage();
private Object process(Node node, Object data) { ASTClassOrInterfaceType cit = node.getFirstDescendantOfType(ASTClassOrInterfaceType.class); if (cit == null || !implClassNames.contains(cit.getImage())) { return data; } cit = cit.getFirstDescendantOfType(ASTClassOrInterfaceType.class); if (cit == null) { return data; } ASTVariableDeclaratorId decl = node.getFirstDescendantOfType(ASTVariableDeclaratorId.class); List<NameOccurrence> usages = decl.getUsages(); for (NameOccurrence no : usages) { ASTName name = (ASTName) no.getLocation(); Node n = name.jjtGetParent().jjtGetParent().jjtGetParent(); if (n instanceof ASTCastExpression) { addViolation(data, n); } } return null; } }
@Override public Object visit(ASTThrowStatement node, Object data) { ASTTryStatement parent = node.getFirstParentOfType(ASTTryStatement.class); if (parent == null) { return data; } for (parent = parent.getFirstParentOfType(ASTTryStatement.class); parent != null; parent = parent .getFirstParentOfType(ASTTryStatement.class)) { List<ASTCatchStatement> list = parent.findDescendantsOfType(ASTCatchStatement.class); for (ASTCatchStatement catchStmt : list) { ASTFormalParameter fp = (ASTFormalParameter) catchStmt.jjtGetChild(0); ASTType type = fp.getFirstDescendantOfType(ASTType.class); ASTClassOrInterfaceType name = type.getFirstDescendantOfType(ASTClassOrInterfaceType.class); if (node.getFirstClassOrInterfaceTypeImage() != null && node.getFirstClassOrInterfaceTypeImage().equals(name.getImage())) { addViolation(data, name); } } } return data; }
if (node.jjtGetChild(0) instanceof ASTClassOrInterfaceType) { // Ignore primitives final ASTClassOrInterfaceType type = (ASTClassOrInterfaceType) node.jjtGetChild(0); final List<ASTConstructorDeclaration> constructors = privateConstructors.get(type.getImage());
@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; }