Skip to content

Commit 17c3d0b

Browse files
committed
Consider outer interface visibility
Update `SpringMethodVisibilityCheck` to consider interface definitions when checking method visibility. Closes gh-140
1 parent b2961fa commit 17c3d0b

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

spring-javaformat/spring-javaformat-checkstyle/src/main/java/io/spring/javaformat/checkstyle/check/SpringMethodVisibilityCheck.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ private void visitPublicMethod(DetailAST modifiers, DetailAST method) {
5353
|| classModifiers.findFirstToken(TokenTypes.LITERAL_PROTECTED) != null) {
5454
return;
5555
}
56+
DetailAST interfaceDef = getInterfaceDef(classDef.getParent());
57+
if (interfaceDef != null) {
58+
DetailAST interfaceModifiers = interfaceDef.findFirstToken(TokenTypes.MODIFIERS);
59+
if (interfaceModifiers.findFirstToken(TokenTypes.LITERAL_PUBLIC) != null
60+
|| interfaceModifiers.findFirstToken(TokenTypes.LITERAL_PROTECTED) != null) {
61+
return;
62+
}
63+
}
5664
DetailAST ident = method.findFirstToken(TokenTypes.IDENT);
5765
log(ident.getLineNo(), ident.getColumnNo(), "methodvisibility.publicMethod", ident.getText());
5866
}
@@ -73,8 +81,16 @@ private boolean hasOverrideAnnotation(DetailAST modifiers) {
7381
}
7482

7583
private DetailAST getClassDef(DetailAST ast) {
84+
return findParent(ast, TokenTypes.CLASS_DEF);
85+
}
86+
87+
private DetailAST getInterfaceDef(DetailAST ast) {
88+
return findParent(ast, TokenTypes.INTERFACE_DEF);
89+
}
90+
91+
private DetailAST findParent(DetailAST ast, int classDef) {
7692
while (ast != null) {
77-
if (ast.getType() == TokenTypes.CLASS_DEF) {
93+
if (ast.getType() == classDef) {
7894
return ast;
7995
}
8096
ast = ast.getParent();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
+0 errors
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2017-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.spring.javaformat.checkstyle;
18+
19+
/**
20+
* Interface with implicit public elements.
21+
*
22+
* @author Phillip Webb
23+
*/
24+
public interface NestedInterfaceItems {
25+
26+
/**
27+
* A nested class class.
28+
*/
29+
class NestedClass {
30+
31+
public NestedClass(String arg) {
32+
// This is valid because nested class is implicitly public
33+
}
34+
35+
public void thisIsFine() {
36+
}
37+
38+
}
39+
40+
}

0 commit comments

Comments
 (0)