Skip to content

Commit

Permalink
Consider outer interface visibility
Browse files Browse the repository at this point in the history
Update `SpringMethodVisibilityCheck` to consider interface definitions
when checking method visibility.

Closes gh-140
  • Loading branch information
philwebb committed Dec 3, 2019
1 parent b2961fa commit 17c3d0b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ private void visitPublicMethod(DetailAST modifiers, DetailAST method) {
|| classModifiers.findFirstToken(TokenTypes.LITERAL_PROTECTED) != null) {
return;
}
DetailAST interfaceDef = getInterfaceDef(classDef.getParent());
if (interfaceDef != null) {
DetailAST interfaceModifiers = interfaceDef.findFirstToken(TokenTypes.MODIFIERS);
if (interfaceModifiers.findFirstToken(TokenTypes.LITERAL_PUBLIC) != null
|| interfaceModifiers.findFirstToken(TokenTypes.LITERAL_PROTECTED) != null) {
return;
}
}
DetailAST ident = method.findFirstToken(TokenTypes.IDENT);
log(ident.getLineNo(), ident.getColumnNo(), "methodvisibility.publicMethod", ident.getText());
}
Expand All @@ -73,8 +81,16 @@ private boolean hasOverrideAnnotation(DetailAST modifiers) {
}

private DetailAST getClassDef(DetailAST ast) {
return findParent(ast, TokenTypes.CLASS_DEF);
}

private DetailAST getInterfaceDef(DetailAST ast) {
return findParent(ast, TokenTypes.INTERFACE_DEF);
}

private DetailAST findParent(DetailAST ast, int classDef) {
while (ast != null) {
if (ast.getType() == TokenTypes.CLASS_DEF) {
if (ast.getType() == classDef) {
return ast;
}
ast = ast.getParent();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
+0 errors
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2017-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.spring.javaformat.checkstyle;

/**
* Interface with implicit public elements.
*
* @author Phillip Webb
*/
public interface NestedInterfaceItems {

/**
* A nested class class.
*/
class NestedClass {

public NestedClass(String arg) {
// This is valid because nested class is implicitly public
}

public void thisIsFine() {
}

}

}

0 comments on commit 17c3d0b

Please sign in to comment.