/** * Returns true if the result type of this method is {@code void}. */ public boolean isVoid() { return getResultType().isVoid(); }
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; }
@Override public Object visit(ASTMethodDeclaration node, Object data) { if (node.getResultType().isVoid()) { super.visit(node, data); } return data; }
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; } }
private void checkPrefixedTransformMethods(ASTMethodDeclaration node, Object data, String nameOfMethod) { ASTResultType resultType = node.getResultType(); List<String> prefixes = getProperty(TRANSFORM_METHOD_NAMES_PROPERTY); String[] splitMethodName = StringUtils.splitByCharacterTypeCamelCase(nameOfMethod); if (resultType.isVoid() && splitMethodName.length > 0 && prefixes.contains(splitMethodName[0].toLowerCase(Locale.ROOT))) { // "To" or any other configured prefix found addViolationWithMessage(data, node, "Linguistics Antipattern - The transform method ''{0}'' should not return void linguistically", new Object[] { nameOfMethod }); } }
private void checkSetters(ASTMethodDeclaration node, Object data, String nameOfMethod) { ASTResultType resultType = node.getResultType(); if (hasPrefix(nameOfMethod, "set") && !resultType.isVoid()) { addViolationWithMessage(data, node, "Linguistics Antipattern - The setter ''{0}'' should not return any type except void linguistically", new Object[] { nameOfMethod }); } }
private void checkGetters(ASTMethodDeclaration node, Object data, String nameOfMethod) { ASTResultType resultType = node.getResultType(); if (hasPrefix(nameOfMethod, "get") && resultType.isVoid()) { addViolationWithMessage(data, node, "Linguistics Antipattern - The getter ''{0}'' should not return void linguistically", new Object[] { nameOfMethod }); } }
private void checkTransformMethods(ASTMethodDeclaration node, Object data, String nameOfMethod) { ASTResultType resultType = node.getResultType(); List<String> infixes = getProperty(TRANSFORM_METHOD_NAMES_PROPERTY); for (String infix : infixes) { if (resultType.isVoid() && containsWord(nameOfMethod, StringUtils.capitalize(infix))) { // "To" or any other configured infix in the middle somewhere addViolationWithMessage(data, node, "Linguistics Antipattern - The transform method ''{0}'' should not return void linguistically", new Object[] { nameOfMethod }); // the first violation is sufficient - it is still the same method we are analyzing here break; } } }
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 boolean isMainMethod(ASTAnyTypeBodyDeclaration bodyDeclaration) { if (DeclarationKind.METHOD != bodyDeclaration.getKind()) { return false; } ASTMethodDeclaration decl = (ASTMethodDeclaration) bodyDeclaration.getDeclarationNode(); return decl.isStatic() && "main".equals(decl.getMethodName()) && decl.getResultType().isVoid() && decl.getFormalParameters().getParameterCount() == 1 && String[].class.equals(decl.getFormalParameters().iterator().next().getType()); }
@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); }
@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; }
String returnVariableName = null; if (node.getResultType().isVoid()) { return super.visit(node, data);
@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);
@Override public Object visit(ASTMethodDeclaration method, Object data) { if (!method.getResultType().returnsArray() || method.isPrivate()) { return data;
ASTResultType res = m.getResultType(); ASTClassOrInterfaceType c = res.getFirstDescendantOfType(ASTClassOrInterfaceType.class); if (c != null && c.hasImageEqualTo("Test")) {
private void checkPrefixedTransformMethods(ASTMethodDeclaration node, Object data, String nameOfMethod) { ASTResultType resultType = node.getResultType(); List<String> prefixes = getProperty(TRANSFORM_METHOD_NAMES_PROPERTY); String[] splitMethodName = StringUtils.splitByCharacterTypeCamelCase(nameOfMethod); if (resultType.isVoid() && splitMethodName.length > 0 && prefixes.contains(splitMethodName[0].toLowerCase(Locale.ROOT))) { // "To" or any other configured prefix found addViolationWithMessage(data, node, "Linguistics Antipattern - The transform method ''{0}'' should not return void linguistically", new Object[] { nameOfMethod }); } }
private void checkSetters(ASTMethodDeclaration node, Object data, String nameOfMethod) { ASTResultType resultType = node.getResultType(); if (hasPrefix(nameOfMethod, "set") && !resultType.isVoid()) { addViolationWithMessage(data, node, "Linguistics Antipattern - The setter ''{0}'' should not return any type except void linguistically", new Object[] { nameOfMethod }); } }
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 boolean isMainMethod(ASTAnyTypeBodyDeclaration bodyDeclaration) { if (DeclarationKind.METHOD != bodyDeclaration.getKind()) { return false; } ASTMethodDeclaration decl = (ASTMethodDeclaration) bodyDeclaration.getDeclarationNode(); return decl.isStatic() && "main".equals(decl.getMethodName()) && decl.getResultType().isVoid() && decl.getFormalParameters().getParameterCount() == 1 && String[].class.equals(decl.getFormalParameters().iterator().next().getType()); }