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; }
final public void ClassOrInterfaceType() throws ParseException { ASTClassOrInterfaceType jjtn000 = new ASTClassOrInterfaceType(this, JJTCLASSORINTERFACETYPE); boolean jjtc000 = true; jjtree.openNodeScope(jjtn000); jjtn000.jjtSetFirstToken(getToken(1));StringBuilder s = new StringBuilder(); Token t; try { jjtn000.jjtSetLastToken(getToken(0)); jjtn000.setImage(s.toString()); } catch (Throwable jjte000) { if (jjtc000) { if (jjtc000) { jjtree.closeNodeScope(jjtn000, true); jjtn000.jjtSetLastToken(getToken(0));
@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; }
@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 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) { // This name has no qualification, it can't be unnecessarily qualified if (node.getImage().indexOf('.') < 0) { return data; } checkImports(node, data); 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 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; } }
/** * 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; }
ASTClassOrInterfaceType classOrInterfaceType = new ASTClassOrInterfaceType( JavaParserTreeConstants.JJTCLASSORINTERFACETYPE); classOrInterfaceType.setImage(parameterTypes[i]); referenceType.jjtAddChild(classOrInterfaceType, 0); classOrInterfaceType.jjtSetParent(referenceType);
private void checkForBadAnonymousDiamondUsage() { if (jdkVersion < 9) { ASTAllocationExpression node = (ASTAllocationExpression)jjtree.peekNode(); ASTTypeArguments types = node.getFirstChildOfType(ASTClassOrInterfaceType.class).getFirstChildOfType(ASTTypeArguments.class); if (node.isAnonymousClass() && types != null && types.isDiamond()) { throwParseException("Cannot use '<>' with anonymous inner classes when running in JDK inferior to 9 mode!"); } } } /**
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; final List<ASTClassOrInterfaceType> types = implementsList.findChildrenOfType(ASTClassOrInterfaceType.class); for (final ASTClassOrInterfaceType t : types) { if (classesNames.contains(t.getImage())) { return data; if (extendsList != null) { final ASTClassOrInterfaceType type = extendsList.getFirstChildOfType(ASTClassOrInterfaceType.class); if (classesNames.contains(type.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; }
/** * 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 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 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; } }
/** * 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; }
ASTClassOrInterfaceType classOrInterfaceType = new ASTClassOrInterfaceType( JavaParserTreeConstants.JJTCLASSORINTERFACETYPE); classOrInterfaceType.setImage(parameterTypes[i]); referenceType.jjtAddChild(classOrInterfaceType, 0); classOrInterfaceType.jjtSetParent(referenceType);
private void checkForBadAnonymousDiamondUsage() { if (jdkVersion < 9) { ASTAllocationExpression node = (ASTAllocationExpression)jjtree.peekNode(); ASTTypeArguments types = node.getFirstChildOfType(ASTClassOrInterfaceType.class).getFirstChildOfType(ASTTypeArguments.class); if (node.isAnonymousClass() && types != null && types.isDiamond()) { throwParseException("Cannot use '<>' with anonymous inner classes when running in JDK inferior to 9 mode!"); } } } /**
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; }