@Deprecated public int getArrayDepth() { if (jjtGetNumChildren() != 0 && (jjtGetChild(0) instanceof ASTReferenceType || jjtGetChild(0) instanceof ASTPrimitiveType)) { return ((Dimensionable) jjtGetChild(0)).getArrayDepth(); } return 0; // this is not an array }
private static List<String> getMethodDeclaratorParameterTypes(Node methodOrConstructorDeclarator) { List<ASTFormalParameter> parameters = methodOrConstructorDeclarator .findDescendantsOfType(ASTFormalParameter.class); List<String> parameterTypes = new ArrayList<>(); if (parameters != null) { for (ASTFormalParameter p : parameters) { ASTType type = p.getFirstChildOfType(ASTType.class); if (type.jjtGetChild(0) instanceof ASTPrimitiveType) { parameterTypes.add(type.jjtGetChild(0).getImage()); } else if (type.jjtGetChild(0) instanceof ASTReferenceType) { parameterTypes.add("ref"); } else { parameterTypes.add("<unkown>"); } } } return parameterTypes; }
/** * Returns the first child of the node returned by {@link #getTypeNode()}. * The image of that node can usually be interpreted as the image of the * type. */ // TODO unreliable, not typesafe and not useful, should be deprecated public Node getTypeNameNode() { ASTType type = getTypeNode(); return type == null ? null : getTypeNode().jjtGetChild(0); }
/** * Note that an array of primitive types (int[]) is a reference type. */ public boolean isReferenceType() { return !isTypeInferred() && getAccessNodeParent().getFirstChildOfType(ASTType.class).jjtGetChild(0) instanceof ASTReferenceType; }
public boolean isPrimitiveType() { return !isTypeInferred() && getAccessNodeParent().getFirstChildOfType(ASTType.class).jjtGetChild(0) instanceof ASTPrimitiveType; }
private TypeNode getTypeNode() { if (isPrimitiveType()) { return (TypeNode) getAccessNodeParent().getFirstChildOfType(ASTType.class).jjtGetChild(0); } if (!isTypeInferred()) { return (TypeNode) getAccessNodeParent().getFirstChildOfType(ASTType.class).jjtGetChild(0).jjtGetChild(0); } return null; }
@Override public Object visit(ASTFieldDeclaration node, Object data) { for (int x = 0; x < node.jjtGetNumChildren(); ++x) { Node firstStmt = node.jjtGetChild(x); if (firstStmt instanceof ASTType) { ASTType tp = (ASTType) firstStmt; Node nd = tp.jjtGetChild(0); checkVariableType(nd, nd.getImage()); } } 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 int hashCode() { int hash = node.getImage().hashCode() * 31 + ((ASTMethodDeclarator) node).getParameterCount(); ASTFormalParameters myParams = (ASTFormalParameters) node.jjtGetChild(0); for (int i = 0; i < ((ASTMethodDeclarator) node).getParameterCount(); i++) { ASTFormalParameter myParam = (ASTFormalParameter) myParams.jjtGetChild(i); Node myTypeNode = myParam.getTypeNode().jjtGetChild(0); String myTypeImg; if (myTypeNode instanceof ASTPrimitiveType) { myTypeImg = myTypeNode.getImage(); } else { myTypeImg = myTypeNode.jjtGetChild(0).getImage(); } hash = hash * 31 + myTypeImg.hashCode(); } return hash; }
if (typeNode.jjtGetChild(0) instanceof ASTReferenceType) { return ((TypeNode) typeNode.jjtGetChild(0)).getTypeDefinition(); } else { // primitive type return JavaTypeDefinition.forClass(typeNode.getType());
if (typeNode.jjtGetNumChildren() == 0 || !(typeNode.jjtGetChild(0) instanceof ASTReferenceType)) { 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); }
Node myTypeNode = myParam.getTypeNode().jjtGetChild(0); Node otherTypeNode = otherParam.getTypeNode().jjtGetChild(0);
ASTReferenceType ref = (ASTReferenceType) type.jjtGetChild(0); ASTClassOrInterfaceType clazz = (ASTClassOrInterfaceType) ref.jjtGetChild(0); addViolation(data, id, clazz.getImage());
ASTType argType = declaration.getNode().jjtGetParent().jjtGetParent() .getFirstDescendantOfType(ASTType.class); if (argType != null && argType.jjtGetChild(0) instanceof ASTReferenceType && ((ASTReferenceType) argType.jjtGetChild(0)).isArray()) { return super.visit(node, data);
@Deprecated public int getArrayDepth() { if (jjtGetNumChildren() != 0 && (jjtGetChild(0) instanceof ASTReferenceType || jjtGetChild(0) instanceof ASTPrimitiveType)) { return ((Dimensionable) jjtGetChild(0)).getArrayDepth(); } return 0; // this is not an array }
/** * Returns the first child of the node returned by {@link #getTypeNode()}. * The image of that node can usually be interpreted as the image of the * type. */ // TODO unreliable, not typesafe and not useful, should be deprecated public Node getTypeNameNode() { ASTType type = getTypeNode(); return type == null ? null : getTypeNode().jjtGetChild(0); }
/** * Note that an array of primitive types (int[]) is a reference type. */ public boolean isReferenceType() { return !isTypeInferred() && getAccessNodeParent().getFirstChildOfType(ASTType.class).jjtGetChild(0) instanceof ASTReferenceType; }
private TypeNode getTypeNode() { if (isPrimitiveType()) { return (TypeNode) getAccessNodeParent().getFirstChildOfType(ASTType.class).jjtGetChild(0); } if (!isTypeInferred()) { return (TypeNode) getAccessNodeParent().getFirstChildOfType(ASTType.class).jjtGetChild(0).jjtGetChild(0); } return null; }
@Override public Object visit(ASTFieldDeclaration node, Object data) { for (int x = 0; x < node.jjtGetNumChildren(); ++x) { Node firstStmt = node.jjtGetChild(x); if (firstStmt instanceof ASTType) { ASTType tp = (ASTType) firstStmt; Node nd = tp.jjtGetChild(0); checkVariableType(nd, nd.getImage()); } } return super.visit(node, data); }