Skip to content

Commit f9ba694

Browse files
committedDec 5, 2018
#953 committing missing changes
1 parent dba1385 commit f9ba694

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed
 

‎qulice-checkstyle/src/main/java/com/qulice/checkstyle/EmptyLinesCheck.java

+29-5
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
3333
import com.puppycrawl.tools.checkstyle.api.DetailAST;
3434
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
35+
import java.util.Comparator;
3536
import java.util.regex.Pattern;
37+
import java.util.stream.StreamSupport;
3638

3739
/**
3840
* Check for empty lines inside methods and constructors.
@@ -86,6 +88,7 @@ public int[] getRequiredTokens() {
8688

8789
@Override
8890
public void visitToken(final DetailAST ast) {
91+
this.getLine(ast.getLastChild().getLine());
8992
if (ast.getType() == TokenTypes.OBJBLOCK
9093
&& ast.getParent() != null
9194
&& ast.getParent().getType() == TokenTypes.LITERAL_NEW) {
@@ -114,8 +117,8 @@ public void finishTree(final DetailAST root) {
114117
final String[] lines = this.getLines();
115118
for (int line = 0; line < lines.length; ++line) {
116119
if (this.methods.inRange(line + 1)
117-
&& this.validInnerClassMethod(line + 1)
118-
&& EmptyLinesCheck.PATTERN.matcher(lines[line]).find()) {
120+
&& EmptyLinesCheck.PATTERN.matcher(lines[line]).find()
121+
&& this.insideMethod(line + 1)) {
119122
this.log(line + 1, "Empty line inside method");
120123
}
121124
}
@@ -132,8 +135,29 @@ public void finishTree(final DetailAST root) {
132135
* @param line The line to check if it is within a method or not.
133136
* @return True if the line is directly inside of a method.
134137
*/
135-
private boolean validInnerClassMethod(final int line) {
136-
return !this.anons.inRange(line)
137-
|| this.methods.within(this.anons).inRange(line);
138+
private boolean insideMethod(final int line) {
139+
final int method = EmptyLinesCheck.linesBetweenBraces(
140+
line, this.methods::iterator, Integer.MIN_VALUE
141+
);
142+
final int clazz = EmptyLinesCheck.linesBetweenBraces(
143+
line, this.anons::iterator, Integer.MAX_VALUE
144+
);
145+
return method < clazz;
146+
}
147+
148+
/**
149+
* Find number of lines between braces that contain a given line.
150+
* @param line Line to check
151+
* @param iterator Iterable of line ranges
152+
* @param def Default value if line is not within ranges
153+
* @return Number of lines between braces
154+
*/
155+
private static int linesBetweenBraces(final int line,
156+
final Iterable<LineRange> iterator, final int def) {
157+
return StreamSupport.stream(iterator.spliterator(), false)
158+
.filter(r -> r.within(line))
159+
.min(Comparator.comparingInt(r -> r.last() - r.first()))
160+
.map(r -> r.last() - r.first())
161+
.orElse(def);
138162
}
139163
}

‎qulice-checkstyle/src/test/resources/com/qulice/checkstyle/ChecksTest/EmptyLinesCheck/Valid.java

+15-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,22 @@
22
* This is not a real Java class. It won't be compiled ever. It is used
33
* only as a text resource in integration.ChecksIT.
44
*/
5-
public final class Invalid {
5+
public final class Valid {
66
public void main() {
77
int works = true;
8-
// everything is fine here
8+
new Take() {
9+
public Response act(final Request req) {
10+
ref.set(req);
11+
return new Response() {
12+
public Iterable<String> head() throws IOException {
13+
return Collections.singletonList("HTTP/1.1 200 OK");
14+
}
15+
16+
public InputStream body() throws IOException {
17+
return req.body();
18+
}
19+
};
20+
}
21+
}
922
}
1023
}

0 commit comments

Comments
 (0)
Please sign in to comment.