@Test public void testMisc() { final Scope scope = Scope.getInstance("public"); assertNotNull("Scope must not be null", scope); assertEquals("Invalid scope toString", "public", scope.toString()); assertEquals("Invalid scope name", "public", scope.getName()); try { Scope.getInstance("unknown"); fail("exception expected"); } catch (IllegalArgumentException ex) { assertEquals("Invalid error message", "No enum constant com.puppycrawl.tools.checkstyle.api.Scope.UNKNOWN", ex.getMessage()); } }
/** * 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); }
/** * Scope factory method. * * @param scopeName scope name, such as "nothing", "public", etc. * @return the {@code Scope} associated with {@code scopeName} */ public static Scope getInstance(String scopeName) { return valueOf(Scope.class, scopeName.trim().toUpperCase(Locale.ENGLISH)); }
@Override public String toString() { return getName(); }
@SuppressWarnings({"unchecked", "rawtypes"}) @Override public Object convert(Class type, Object value) { return Scope.getInstance(value.toString()); }
/** * Checks if this scope is a subscope of another scope. * Example: PUBLIC is a subscope of PRIVATE. * * @param scope a {@code Scope} value * @return if {@code this} is a subscope of {@code scope}. */ public boolean isIn(Scope scope) { return compareTo(scope) <= 0; }
/** * Returns name of severity level. * @return the name of this severity level. */ public String getName() { return name().toLowerCase(Locale.ENGLISH); }
/** * @param classAst : required to be CLASS_DEF type * @param scope : modifier of returned variables * @return variable asts that in given scope */ public static List<DetailAST> getVariables(DetailAST classAst, Scope scope) { return getVariables(classAst).stream() .filter(variableAst -> scope.equals(getScopeOf(variableAst))) .collect(Collectors.toList()); }
@Test public void testScopeAnonInnerPrivate() throws Exception { final DefaultConfiguration checkConfig = createModuleConfig(JavadocMethodCheck.class); checkConfig.addAttribute("scope", Scope.PRIVATE.getName()); final String[] expected = CommonUtil.EMPTY_STRING_ARRAY; verify(checkConfig, getPath("InputJavadocMethodScopeAnonInner.java"), expected); }
@Test public void testMixedCaseSpaces() { assertEquals("Invalid scope", Scope.NOTHING, Scope.getInstance("NothinG ")); assertEquals("Invalid scope", Scope.PUBLIC, Scope.getInstance(" PuBlic")); assertEquals("Invalid scope", Scope.PROTECTED, Scope.getInstance(" ProteCted")); assertEquals("Invalid scope", Scope.PACKAGE, Scope.getInstance(" PackAge ")); assertEquals("Invalid scope", Scope.PRIVATE, Scope.getInstance("privaTe ")); assertEquals("Invalid scope", Scope.ANONINNER, Scope.getInstance("AnonInner")); }
/** * Checks if given modifiers are valid in substate of given * state({@code Scope}), if it is it updates substate or else it * logs violation. * @param modifiersAst modifiers to process * @param state current state * @param isStateValid is main state for given modifiers is valid */ private void processModifiersSubState(DetailAST modifiersAst, ScopeState state, boolean isStateValid) { final Scope access = ScopeUtil.getScopeFromMods(modifiersAst); if (state.declarationAccess.compareTo(access) > 0) { if (isStateValid && !ignoreModifiers && !isForwardReference(modifiersAst.getParent())) { log(modifiersAst, MSG_ACCESS); } } else { state.declarationAccess = access; } }
/** * Returns name of severity level. * @return the name of this severity level. */ public String getName() { return name().toLowerCase(Locale.ENGLISH); }
/** * 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); }
@Test public void testAnother2() throws Exception { final DefaultConfiguration checkConfig = createModuleConfig(JavadocVariableCheck.class); checkConfig.addAttribute("scope", Scope.PUBLIC.getName()); final String[] expected = CommonUtil.EMPTY_STRING_ARRAY; verify(checkConfig, getPath("InputJavadocVariableInner.java"), expected); }
@Test public void testMixedCaseSpacesWithDifferentLocales() { final Locale[] differentLocales = {new Locale("TR", "tr") }; final Locale defaultLocale = Locale.getDefault(); try { for (Locale differentLocale : differentLocales) { Locale.setDefault(differentLocale); assertEquals("Invalid scope", Scope.NOTHING, Scope.getInstance("NothinG ")); assertEquals("Invalid scope", Scope.PUBLIC, Scope.getInstance(" PuBlic")); assertEquals("Invalid scope", Scope.PROTECTED, Scope.getInstance(" ProteCted")); assertEquals("Invalid scope", Scope.PACKAGE, Scope.getInstance(" PackAge ")); assertEquals("Invalid scope", Scope.PRIVATE, Scope.getInstance("privaTe ")); assertEquals("Invalid scope", Scope.ANONINNER, Scope.getInstance("AnonInner")); } } finally { Locale.setDefault(defaultLocale); } }
@Test public void testScopeValueOf() { final Scope scope = Scope.valueOf("PRIVATE"); assertEquals("Invalid scope", Scope.PRIVATE, scope); }
/** * Checks if this scope is a subscope of another scope. * Example: PUBLIC is a subscope of PRIVATE. * * @param scope a {@code Scope} value * @return if {@code this} is a subscope of {@code scope}. */ public boolean isIn(Scope scope) { return compareTo(scope) <= 0; }
/** * 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 testNoJavadoc() throws Exception { final DefaultConfiguration checkConfig = createModuleConfig(JavadocMethodCheck.class); checkConfig.addAttribute("scope", Scope.NOTHING.getName()); final String[] expected = CommonUtil.EMPTY_STRING_ARRAY; verify(checkConfig, getPath("InputJavadocMethodPublicOnly.java"), expected); }
/** * Sets the scope to check. * * @param from string to set scope from */ public void setScope(String from) { scope = Scope.getInstance(from); }