/** * Whether we should check this node. * * @param ast a given node. * @param nodeScope the scope of the node. * @return whether we should check a given node. */ private boolean shouldCheck(final DetailAST ast, final Scope nodeScope) { final Scope surroundingScope = ScopeUtil.getSurroundingScope(ast); return (excludeScope == null || nodeScope != excludeScope && surroundingScope != excludeScope) && nodeScope.isIn(scope) && surroundingScope.isIn(scope); }
/** * Whether we should check this node. * @param ast a given node. * @return whether we should check a given node. */ private boolean shouldCheck(final DetailAST ast) { final Scope customScope; if (ScopeUtil.isInInterfaceOrAnnotationBlock(ast)) { customScope = Scope.PUBLIC; } else { final DetailAST mods = ast.findFirstToken(TokenTypes.MODIFIERS); customScope = ScopeUtil.getScopeFromMods(mods); } final Scope surroundingScope = ScopeUtil.getSurroundingScope(ast); return customScope.isIn(scope) && (surroundingScope == null || surroundingScope.isIn(scope)) && (excludeScope == null || !customScope.isIn(excludeScope) || surroundingScope != null && !surroundingScope.isIn(excludeScope)) && !AnnotationUtil.containsAnnotation(ast, allowedAnnotations); }
/** * Whether we should check this node. * @param ast a given node. * @return whether we should check a given node. */ private boolean shouldCheck(final DetailAST ast) { boolean result = false; if (!ScopeUtil.isInCodeBlock(ast) && !isIgnored(ast)) { Scope customScope = Scope.PUBLIC; if (ast.getType() != TokenTypes.ENUM_CONSTANT_DEF && !ScopeUtil.isInInterfaceOrAnnotationBlock(ast)) { final DetailAST mods = ast.findFirstToken(TokenTypes.MODIFIERS); customScope = ScopeUtil.getScopeFromMods(mods); } final Scope surroundingScope = ScopeUtil.getSurroundingScope(ast); result = customScope.isIn(scope) && surroundingScope.isIn(scope) && (excludeScope == null || !customScope.isIn(excludeScope) || !surroundingScope.isIn(excludeScope)); } return result; }
@Test public void testIsInNothing() { assertTrue("Invalid subscope", Scope.NOTHING.isIn(Scope.NOTHING)); assertFalse("Invalid subscope", Scope.PUBLIC.isIn(Scope.NOTHING)); assertFalse("Invalid subscope", Scope.PROTECTED.isIn(Scope.NOTHING)); assertFalse("Invalid subscope", Scope.PACKAGE.isIn(Scope.NOTHING)); assertFalse("Invalid subscope", Scope.PRIVATE.isIn(Scope.NOTHING)); assertFalse("Invalid subscope", Scope.ANONINNER.isIn(Scope.NOTHING)); }
@Test public void testIsInPrivate() { assertTrue("Invalid subscope", Scope.NOTHING.isIn(Scope.PRIVATE)); assertTrue("Invalid subscope", Scope.PUBLIC.isIn(Scope.PRIVATE)); assertTrue("Invalid subscope", Scope.PROTECTED.isIn(Scope.PRIVATE)); assertTrue("Invalid subscope", Scope.PACKAGE.isIn(Scope.PRIVATE)); assertTrue("Invalid subscope", Scope.PRIVATE.isIn(Scope.PRIVATE)); assertFalse("Invalid subscope", Scope.ANONINNER.isIn(Scope.PRIVATE)); }
@Test public void testIsInPackage() { assertTrue("Invalid subscope", Scope.NOTHING.isIn(Scope.PACKAGE)); assertTrue("Invalid subscope", Scope.PUBLIC.isIn(Scope.PACKAGE)); assertTrue("Invalid subscope", Scope.PROTECTED.isIn(Scope.PACKAGE)); assertTrue("Invalid subscope", Scope.PACKAGE.isIn(Scope.PACKAGE)); assertFalse("Invalid subscope", Scope.PRIVATE.isIn(Scope.PACKAGE)); assertFalse("Invalid subscope", Scope.ANONINNER.isIn(Scope.PACKAGE)); }
@Test public void testIsInProtected() { assertTrue("Invalid subscope", Scope.NOTHING.isIn(Scope.PROTECTED)); assertTrue("Invalid subscope", Scope.PUBLIC.isIn(Scope.PROTECTED)); assertTrue("Invalid subscope", Scope.PROTECTED.isIn(Scope.PROTECTED)); assertFalse("Invalid subscope", Scope.PACKAGE.isIn(Scope.PROTECTED)); assertFalse("Invalid subscope", Scope.PRIVATE.isIn(Scope.PROTECTED)); assertFalse("Invalid subscope", Scope.ANONINNER.isIn(Scope.PROTECTED)); }
@Test public void testIsInAnonInner() { assertTrue("Invalid subscope", Scope.NOTHING.isIn(Scope.ANONINNER)); assertTrue("Invalid subscope", Scope.PUBLIC.isIn(Scope.ANONINNER)); assertTrue("Invalid subscope", Scope.PROTECTED.isIn(Scope.ANONINNER)); assertTrue("Invalid subscope", Scope.PACKAGE.isIn(Scope.ANONINNER)); assertTrue("Invalid subscope", Scope.PRIVATE.isIn(Scope.ANONINNER)); assertTrue("Invalid subscope", Scope.ANONINNER.isIn(Scope.ANONINNER)); }
@Test public void testIsInPublic() { assertTrue("Invalid subscope", Scope.NOTHING.isIn(Scope.PUBLIC)); assertTrue("Invalid subscope", Scope.PUBLIC.isIn(Scope.PUBLIC)); assertFalse("Invalid subscope", Scope.PROTECTED.isIn(Scope.PUBLIC)); assertFalse("Invalid subscope", Scope.PACKAGE.isIn(Scope.PUBLIC)); assertFalse("Invalid subscope", Scope.PRIVATE.isIn(Scope.PUBLIC)); assertFalse("Invalid subscope", Scope.ANONINNER.isIn(Scope.PUBLIC)); }
/** * Whether we should check this node. * @param ast a given node. * @return whether we should check a given node. */ private boolean shouldCheck(final DetailAST ast) { boolean check = false; if (ast.getType() == TokenTypes.PACKAGE_DEF) { check = getFileContents().inPackageInfo(); } else if (!ScopeUtil.isInCodeBlock(ast)) { final Scope customScope; if (ScopeUtil.isInInterfaceOrAnnotationBlock(ast) || ast.getType() == TokenTypes.ENUM_CONSTANT_DEF) { customScope = Scope.PUBLIC; } else { customScope = ScopeUtil.getScopeFromMods(ast.findFirstToken(TokenTypes.MODIFIERS)); } final Scope surroundingScope = ScopeUtil.getSurroundingScope(ast); check = customScope.isIn(scope) && (surroundingScope == null || surroundingScope.isIn(scope)) && (excludeScope == null || !customScope.isIn(excludeScope) || surroundingScope != null && !surroundingScope.isIn(excludeScope)); } return check; }
/** * Returns the scope of the surrounding "block". * @param node the node to return the scope for * @return the Scope of the surrounding block */ public static Scope getSurroundingScope(DetailAST node) { Scope returnValue = null; for (DetailAST token = node.getParent(); token != null; token = token.getParent()) { final int type = token.getType(); if (type == TokenTypes.CLASS_DEF || type == TokenTypes.INTERFACE_DEF || type == TokenTypes.ANNOTATION_DEF || type == TokenTypes.ENUM_DEF) { final DetailAST mods = token.findFirstToken(TokenTypes.MODIFIERS); final Scope modScope = getScopeFromMods(mods); if (returnValue == null || returnValue.isIn(modScope)) { returnValue = modScope; } } else if (type == TokenTypes.LITERAL_NEW) { returnValue = Scope.ANONINNER; // because Scope.ANONINNER is not in any other Scope break; } } return returnValue; }
/** * Checks whether a method can be overridden. * Method can be overridden if it is not private, abstract, final or static. * Note that the check has nothing to do for interfaces. * @param methodDef method definition token. * @return true if a method can be overridden in a subclass. */ private static boolean canBeOverridden(DetailAST methodDef) { final DetailAST modifiers = methodDef.findFirstToken(TokenTypes.MODIFIERS); return ScopeUtil.getSurroundingScope(methodDef).isIn(Scope.PROTECTED) && !ScopeUtil.isInInterfaceOrAnnotationBlock(methodDef) && modifiers.findFirstToken(TokenTypes.LITERAL_PRIVATE) == null && modifiers.findFirstToken(TokenTypes.ABSTRACT) == null && modifiers.findFirstToken(TokenTypes.FINAL) == null && modifiers.findFirstToken(TokenTypes.LITERAL_STATIC) == null; }
/** * Whether we should check this node. * * @param ast a given node. * @param nodeScope the scope of the node. * @return whether we should check a given node. */ private boolean shouldCheck(final DetailAST ast, final Scope nodeScope) { final Scope surroundingScope = ScopeUtil.getSurroundingScope(ast); return (excludeScope == null || nodeScope != excludeScope && surroundingScope != excludeScope) && nodeScope.isIn(scope) && surroundingScope.isIn(scope); }
private boolean shouldCheck(final DetailAST ast) { final DetailAST mods = ast.findFirstToken(TokenTypes.MODIFIERS); final Scope declaredScope = ScopeUtils.getScopeFromMods(mods); final Scope customScope; if (ScopeUtils.isInInterfaceOrAnnotationBlock(ast)) { customScope = Scope.PUBLIC; } else { customScope = declaredScope; } final Scope surroundingScope = ScopeUtils.getSurroundingScope(ast); return customScope.isIn(scope) && (surroundingScope == null || surroundingScope.isIn(scope)) && (excludeScope == null || !customScope.isIn(excludeScope) || surroundingScope != null && !surroundingScope.isIn(excludeScope)); }
/** * Whether we should check this node. * @param ast a given node. * @return whether we should check a given node. */ private boolean shouldCheck(final DetailAST ast) { final Scope customScope; if (ScopeUtil.isInInterfaceOrAnnotationBlock(ast)) { customScope = Scope.PUBLIC; } else { final DetailAST mods = ast.findFirstToken(TokenTypes.MODIFIERS); customScope = ScopeUtil.getScopeFromMods(mods); } final Scope surroundingScope = ScopeUtil.getSurroundingScope(ast); return customScope.isIn(scope) && (surroundingScope == null || surroundingScope.isIn(scope)) && (excludeScope == null || !customScope.isIn(excludeScope) || surroundingScope != null && !surroundingScope.isIn(excludeScope)) && !AnnotationUtil.containsAnnotation(ast, allowedAnnotations); }
/** * Whether we should check this node. * @param ast a given node. * @return whether we should check a given node. */ private boolean shouldCheck(final DetailAST ast) { boolean result = false; if (!ScopeUtil.isInCodeBlock(ast) && !isIgnored(ast)) { Scope customScope = Scope.PUBLIC; if (ast.getType() != TokenTypes.ENUM_CONSTANT_DEF && !ScopeUtil.isInInterfaceOrAnnotationBlock(ast)) { final DetailAST mods = ast.findFirstToken(TokenTypes.MODIFIERS); customScope = ScopeUtil.getScopeFromMods(mods); } final Scope surroundingScope = ScopeUtil.getSurroundingScope(ast); result = customScope.isIn(scope) && surroundingScope.isIn(scope) && (excludeScope == null || !customScope.isIn(excludeScope) || !surroundingScope.isIn(excludeScope)); } return result; }
/** * Whether we should check this node. * @param ast a given node. * @return whether we should check a given node. */ private boolean shouldCheck(final DetailAST ast) { boolean check = false; if (ast.getType() == TokenTypes.PACKAGE_DEF) { check = getFileContents().inPackageInfo(); } else if (!ScopeUtil.isInCodeBlock(ast)) { final Scope customScope; if (ScopeUtil.isInInterfaceOrAnnotationBlock(ast) || ast.getType() == TokenTypes.ENUM_CONSTANT_DEF) { customScope = Scope.PUBLIC; } else { customScope = ScopeUtil.getScopeFromMods(ast.findFirstToken(TokenTypes.MODIFIERS)); } final Scope surroundingScope = ScopeUtil.getSurroundingScope(ast); check = customScope.isIn(scope) && (surroundingScope == null || surroundingScope.isIn(scope)) && (excludeScope == null || !customScope.isIn(excludeScope) || surroundingScope != null && !surroundingScope.isIn(excludeScope)); } return check; }
/** * Returns the scope of the surrounding "block". * @param node the node to return the scope for * @return the Scope of the surrounding block */ public static Scope getSurroundingScope(DetailAST node) { Scope returnValue = null; for (DetailAST token = node.getParent(); token != null; token = token.getParent()) { final int type = token.getType(); if (type == TokenTypes.CLASS_DEF || type == TokenTypes.INTERFACE_DEF || type == TokenTypes.ANNOTATION_DEF || type == TokenTypes.ENUM_DEF) { final DetailAST mods = token.findFirstToken(TokenTypes.MODIFIERS); final Scope modScope = getScopeFromMods(mods); if (returnValue == null || returnValue.isIn(modScope)) { returnValue = modScope; } } else if (type == TokenTypes.LITERAL_NEW) { returnValue = Scope.ANONINNER; // because Scope.ANONINNER is not in any other Scope break; } } return returnValue; }
/** * Checks whether a method can be overridden. * Method can be overridden if it is not private, abstract, final or static. * Note that the check has nothing to do for interfaces. * @param methodDef method definition token. * @return true if a method can be overridden in a subclass. */ private static boolean canBeOverridden(DetailAST methodDef) { final DetailAST modifiers = methodDef.findFirstToken(TokenTypes.MODIFIERS); return ScopeUtil.getSurroundingScope(methodDef).isIn(Scope.PROTECTED) && !ScopeUtil.isInInterfaceOrAnnotationBlock(methodDef) && modifiers.findFirstToken(TokenTypes.LITERAL_PRIVATE) == null && modifiers.findFirstToken(TokenTypes.ABSTRACT) == null && modifiers.findFirstToken(TokenTypes.FINAL) == null && modifiers.findFirstToken(TokenTypes.LITERAL_STATIC) == null; }