Skip to content

Commit ee2d4ff

Browse files
committed
SourceParser: init
1 parent 1470ce6 commit ee2d4ff

File tree

3 files changed

+49
-30
lines changed

3 files changed

+49
-30
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@
3535
<artifactId>javaparser-symbol-solver-core</artifactId>
3636
<version>3.7.1</version>
3737
</dependency>
38+
<dependency>
39+
<groupId>org.jetbrains</groupId>
40+
<artifactId>annotations</artifactId>
41+
<version>RELEASE</version>
42+
<scope>compile</scope>
43+
</dependency>
3844
</dependencies>
3945

4046
<properties>

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

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,20 @@
11
package org.hacksource.core;
22

3-
import com.github.javaparser.JavaParser;
4-
import com.github.javaparser.ParseResult;
5-
3+
import com.github.javaparser.Range;
64
import com.github.javaparser.ast.CompilationUnit;
75
import com.github.javaparser.ast.Node;
86
import com.github.javaparser.ast.NodeList;
97
import com.github.javaparser.ast.stmt.*;
108

11-
129
import java.io.*;
13-
import java.nio.charset.StandardCharsets;
14-
import java.nio.file.Files;
1510
import java.nio.file.Paths;
1611
import java.util.Arrays;
1712
import java.util.List;
18-
import java.util.stream.Stream;
1913

20-
import static com.github.javaparser.ParseStart.*;
21-
import static com.github.javaparser.Providers.provider;
2214

2315
public class SourceFormat {
2416

25-
public static String format(String source) throws SourceException {
26-
JavaParser javaParser = new JavaParser();
27-
ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider(source));
28-
29-
CompilationUnit cu = result.getResult().orElseThrow(() -> new SourceException(result.getProblems()));
17+
public static String format(CompilationUnit cu) {
3018

3119
Arrays.stream(new Class[]{
3220
IfStmt.class, ForStmt.class, ForEachStmt.class, WhileStmt.class
@@ -42,31 +30,19 @@ private static <T extends Statement> void addBracket(CompilationUnit cu, Class<T
4230
Statement block = (Statement)children.get(children.size() - 1);
4331
if(!block.isBlockStmt()) {
4432
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());
4535
}
4636
}
4737
});
4838
}
4939

50-
public static void main(String[] args) throws IOException {
40+
public static void main(String[] args) throws IOException, SourceException {
5141

5242
String path = SourceFormat.class.getResource("/example.java").getPath();
5343
path = path.substring(1);
5444

55-
StringBuilder contentBuilder = new StringBuilder();
56-
57-
try (Stream<String> stream = Files.lines(Paths.get(path), StandardCharsets.UTF_8)) {
58-
stream.forEach(s -> contentBuilder.append(s).append("\n"));
59-
}
60-
catch (IOException e) {
61-
e.printStackTrace();
62-
}
63-
64-
String output = "";
65-
try {
66-
output = format(contentBuilder.toString());
67-
} catch (SourceException e) {
68-
e.printStackTrace();
69-
}
45+
String output = format(SourceParser.parseFile(Paths.get(path)));
7046

7147
FileWriter fileWriter = new FileWriter("generated/example.java");
7248
fileWriter.write(output);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.hacksource.core;
2+
3+
import com.github.javaparser.JavaParser;
4+
import com.github.javaparser.ParseResult;
5+
import com.github.javaparser.ast.CompilationUnit;
6+
7+
import java.io.IOException;
8+
import java.nio.file.Files;
9+
import java.nio.file.Path;
10+
import java.util.stream.Stream;
11+
12+
import static com.github.javaparser.ParseStart.COMPILATION_UNIT;
13+
import static com.github.javaparser.Providers.provider;
14+
15+
public class SourceParser {
16+
17+
public static CompilationUnit parse(String source) throws SourceException {
18+
JavaParser javaParser = new JavaParser();
19+
ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider(source));
20+
21+
CompilationUnit cu = result.getResult().orElseThrow(() -> new SourceException(result.getProblems()));
22+
23+
return cu;
24+
25+
}
26+
27+
public static CompilationUnit parseFile(Path path) throws SourceException, IOException {
28+
StringBuilder contentBuilder = new StringBuilder();
29+
30+
Stream<String> stream = Files.lines(path);
31+
stream.forEach(s -> contentBuilder.append(s).append("\n"));
32+
33+
return SourceParser.parse(contentBuilder.toString());
34+
35+
}
36+
37+
}

0 commit comments

Comments
 (0)