Skip to content

Commit 6a60fb0

Browse files
committed
SourceProblem: init & add to SourceFormat
1 parent ee2d4ff commit 6a60fb0

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

src/main/java/org/hacksource/core/SourceFormat.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,42 @@
11
package org.hacksource.core;
22

33
import com.github.javaparser.Range;
4+
import com.github.javaparser.TokenRange;
45
import com.github.javaparser.ast.CompilationUnit;
56
import com.github.javaparser.ast.Node;
67
import com.github.javaparser.ast.NodeList;
78
import com.github.javaparser.ast.stmt.*;
89

910
import java.io.*;
1011
import java.nio.file.Paths;
12+
import java.util.ArrayList;
1113
import java.util.Arrays;
1214
import java.util.List;
1315

1416

1517
public class SourceFormat {
1618

17-
public static String format(CompilationUnit cu) {
19+
public static String format(CompilationUnit cu, List<SourceProblem> list) {
1820

1921
Arrays.stream(new Class[]{
2022
IfStmt.class, ForStmt.class, ForEachStmt.class, WhileStmt.class
21-
}).forEach(c -> addBracket(cu, c));
23+
}).forEach(c -> addStmtBracket(cu, c, list));
2224

2325
return cu.toString();
2426
}
2527

26-
private static <T extends Statement> void addBracket(CompilationUnit cu, Class<T> c) {
28+
private static <T extends Statement> void addStmtBracket(CompilationUnit cu, Class<T> c, List<SourceProblem> list) {
2729
cu.findAll(c).forEach(stmt -> {
2830
List<Node> children = stmt.getChildNodes();
31+
2932
if(children.size() != 0) {
3033
Statement block = (Statement)children.get(children.size() - 1);
34+
3135
if(!block.isBlockStmt()) {
32-
stmt.replace(block, new BlockStmt(new NodeList<Statement>(block)));
33-
Range range = stmt.getRange().orElseThrow(() -> new RuntimeException("cannot get range"));
34-
System.out.println(range.toString());
36+
stmt.replace(block, new BlockStmt(new NodeList<>(block)));
37+
38+
TokenRange tokenRange = stmt.getTokenRange().orElseThrow(() -> new RuntimeException("cannot get range"));
39+
list.add(new SourceProblem("s4.1.1-braces-always-used", tokenRange));
3540
}
3641
}
3742
});
@@ -42,7 +47,10 @@ public static void main(String[] args) throws IOException, SourceException {
4247
String path = SourceFormat.class.getResource("/example.java").getPath();
4348
path = path.substring(1);
4449

45-
String output = format(SourceParser.parseFile(Paths.get(path)));
50+
List<SourceProblem> problems = new ArrayList<>();
51+
String output = format(SourceParser.parseFile(Paths.get(path)), problems);
52+
53+
problems.forEach(p -> System.out.println(p));
4654

4755
FileWriter fileWriter = new FileWriter("generated/example.java");
4856
fileWriter.write(output);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.hacksource.core;
2+
3+
import com.github.javaparser.Range;
4+
import com.github.javaparser.TokenRange;
5+
6+
public class SourceProblem {
7+
8+
private String codeStyleTag;
9+
private TokenRange tokenRange;
10+
11+
SourceProblem(String tag, TokenRange range) {
12+
codeStyleTag = tag;
13+
tokenRange = range;
14+
}
15+
16+
public String getCodeStyleTag() {
17+
return codeStyleTag;
18+
}
19+
20+
public TokenRange getTokenRange() {
21+
return tokenRange;
22+
}
23+
24+
public Range getRange() {
25+
return getTokenRange().toRange().orElseThrow(() -> new RuntimeException("cannot get range"));
26+
}
27+
28+
public String toString() {
29+
return getRange().toString() + ":" + codeStyleTag;
30+
}
31+
}

0 commit comments

Comments
 (0)