private void checkBooleanMethods(ASTMethodDeclaration node, Object data, String nameOfMethod) { ASTResultType resultType = node.getResultType(); ASTType t = node.getResultType().getFirstChildOfType(ASTType.class); if (!resultType.isVoid() && t != null) { for (String prefix : getProperty(BOOLEAN_METHOD_PREFIXES_PROPERTY)) { if (hasPrefix(nameOfMethod, prefix) && !isBooleanType(t)) { addViolationWithMessage(data, node, "Linguistics Antipattern - The method ''{0}'' indicates linguistically it returns a boolean, but it returns ''{1}''", new Object[] { nameOfMethod, t.getTypeImage() }); } } } }
private ASTClassOrInterfaceType getTypeOfMethodCall(ASTPrimarySuffix node) { ASTClassOrInterfaceType type = null; ASTName methodName = node.jjtGetParent().getFirstChildOfType(ASTPrimaryPrefix.class) .getFirstChildOfType(ASTName.class); if (methodName != null) { ClassScope classScope = node.getScope().getEnclosingScope(ClassScope.class); Map<MethodNameDeclaration, List<NameOccurrence>> methods = classScope.getMethodDeclarations(); for (Map.Entry<MethodNameDeclaration, List<NameOccurrence>> e : methods.entrySet()) { if (e.getKey().getName().equals(methodName.getImage())) { type = e.getKey().getNode().getFirstParentOfType(ASTMethodDeclaration.class) .getFirstChildOfType(ASTResultType.class) .getFirstDescendantOfType(ASTClassOrInterfaceType.class); break; } } } return type; }
final public void ResultType() throws ParseException { ASTResultType jjtn000 = new ASTResultType(this, JJTRESULTTYPE); boolean jjtc000 = true; jjtree.openNodeScope(jjtn000); jjtn000.jjtSetFirstToken(getToken(1)); try { switch (jj_nt.kind) { if (jjtc000) { jjtree.closeNodeScope(jjtn000, true); jjtn000.jjtSetLastToken(getToken(0));
public boolean returnsArray() { return !isVoid() && ((ASTType) jjtGetChild(0)).isArray(); }
@Override public Object visit(ASTResultType node, Object data) { for (int x = 0; x < node.jjtGetNumChildren(); x++) { Node tNode = node.jjtGetChild(x); if (tNode instanceof ASTType) { Node reftypeNode = tNode.jjtGetChild(0); if (reftypeNode instanceof ASTReferenceType) { Node classOrIntType = reftypeNode.jjtGetChild(0); if (classOrIntType instanceof ASTClassOrInterfaceType) { Node nameNode = classOrIntType; this.checkVariableType(nameNode, nameNode.getImage()); } } } } return super.visit(node, data); }
/** * Returns true if the result type of this method is {@code void}. */ public boolean isVoid() { return getResultType().isVoid(); }
if (((ASTResultType) node).isVoid()) { extras.add("void"); if (((ASTResultType) node).returnsArray()) { extras.add("returns array");
private boolean returnThread(ASTMethodDeclaration methodDeclaration) { ASTResultType resultType = methodDeclaration.getFirstChildOfType(ASTResultType.class); ASTType type = resultType.getFirstChildOfType(ASTType.class); return type != null && type.getType() == Thread.class; }
private boolean isMethodType(ASTMethodDeclaration node, String methodType) { boolean result = false; ASTResultType type = node.getResultType(); if (type != null) { result = type.hasDescendantMatchingXPath( "./Type/ReferenceType/ClassOrInterfaceType[@Image = '" + methodType + "']"); } return result; }
public boolean isVoid() { return jjtGetNumChildren() == 0; }
private boolean isBeanAccessor(ASTMethodDeclarator meth) { String methodName = meth.getImage(); if (methodName.startsWith("get") || methodName.startsWith("set")) { return true; } if (methodName.startsWith("is")) { ASTResultType ret = ((ASTMethodDeclaration) meth.jjtGetParent()).getResultType(); List<ASTPrimitiveType> primitives = ret.findDescendantsOfType(ASTPrimitiveType.class); if (!primitives.isEmpty() && primitives.get(0).isBoolean()) { return true; } } return false; } }
@Override public Object visit(ASTMethodDeclaration node, Object data) { // only boolean methods should be inspected ASTResultType r = node.getResultType(); if (!r.isVoid()) { Node t = r.jjtGetChild(0); if (t.jjtGetNumChildren() == 1) { t = t.jjtGetChild(0); if (t instanceof ASTPrimitiveType && ((ASTPrimitiveType) t).isBoolean()) { return super.visit(node, data); } } } // skip method return data; }
@Override public Object visit(ASTMethodDeclaration node, Object data) { if (node.getResultType().isVoid()) { super.visit(node, data); } return data; }
@Override public Object visit(ASTMethodDeclaration node, Object data) { /* * original XPath rule was //MethodDeclaration [ResultType * //PrimitiveType [@Image='int'] [//MethodDeclarator [@Image='hashcode' * or @Image='HashCode' or @Image='Hashcode'] * [not(FormalParameters/*)]]] */ ASTResultType type = node.getResultType(); ASTMethodDeclarator decl = node.getFirstChildOfType(ASTMethodDeclarator.class); String name = decl.getImage(); if ("hashcode".equalsIgnoreCase(name) && !"hashCode".equals(name) && decl.jjtGetChild(0).jjtGetNumChildren() == 0 && type.jjtGetNumChildren() != 0) { Node t = type.jjtGetChild(0).jjtGetChild(0); if (t instanceof ASTPrimitiveType && "int".equals(t.getImage())) { addViolation(data, node); return data; } } return super.visit(node, data); }
if (((ASTResultType) node).isVoid()) { extras.add("void"); if (((ASTResultType) node).returnsArray()) { extras.add("returns array");
private boolean isMethodType(ASTMethodDeclaration node, String methodType) { boolean result = false; ASTResultType type = node.getResultType(); if (type != null) { result = type.hasDescendantMatchingXPath( "./Type/ReferenceType/ClassOrInterfaceType[@Image = '" + methodType + "']"); } return result; }
public boolean isVoid() { return jjtGetNumChildren() == 0; }
private boolean isBeanAccessor(ASTMethodDeclarator meth) { String methodName = meth.getImage(); if (methodName.startsWith("get") || methodName.startsWith("set")) { return true; } if (methodName.startsWith("is")) { ASTResultType ret = ((ASTMethodDeclaration) meth.jjtGetParent()).getResultType(); List<ASTPrimitiveType> primitives = ret.findDescendantsOfType(ASTPrimitiveType.class); if (!primitives.isEmpty() && primitives.get(0).isBoolean()) { return true; } } return false; } }
@Override public Object visit(ASTMethodDeclaration node, Object data) { if (node.getResultType().isVoid()) { return super.visit(node, data); ASTType typeNode = (ASTType) node.getResultType().jjtGetChild(0); if (typeNode.jjtGetNumChildren() == 0 || !(typeNode.jjtGetChild(0) instanceof ASTReferenceType)) { return super.visit(node, data);
ASTClassOrInterfaceType c = res.getFirstDescendantOfType(ASTClassOrInterfaceType.class); if (c != null && c.hasImageEqualTo("Test")) { isOK = true;