Skip to content

Commit 2272664

Browse files
committed
*: add SourceException, SourceFormat
0 parents  commit 2272664

File tree

7 files changed

+282
-0
lines changed

7 files changed

+282
-0
lines changed

.gitignore

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
2+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
3+
4+
# User-specific stuff
5+
.idea/**/workspace.xml
6+
.idea/**/tasks.xml
7+
.idea/**/usage.statistics.xml
8+
.idea/**/dictionaries
9+
.idea/**/shelf
10+
11+
# Generated files
12+
.idea/**/contentModel.xml
13+
14+
# Sensitive or high-churn files
15+
.idea/**/dataSources/
16+
.idea/**/dataSources.ids
17+
.idea/**/dataSources.local.xml
18+
.idea/**/sqlDataSources.xml
19+
.idea/**/dynamic.xml
20+
.idea/**/uiDesigner.xml
21+
.idea/**/dbnavigator.xml
22+
23+
# Gradle
24+
.idea/**/gradle.xml
25+
.idea/**/libraries
26+
27+
# Gradle and Maven with auto-import
28+
# When using Gradle or Maven with auto-import, you should exclude module files,
29+
# since they will be recreated, and may cause churn. Uncomment if using
30+
# auto-import.
31+
# .idea/modules.xml
32+
# .idea/*.iml
33+
# .idea/modules
34+
35+
# CMake
36+
cmake-build-*/
37+
38+
# Mongo Explorer plugin
39+
.idea/**/mongoSettings.xml
40+
41+
# File-based project format
42+
*.iws
43+
44+
# IntelliJ
45+
out/
46+
47+
# mpeltonen/sbt-idea plugin
48+
.idea_modules/
49+
50+
# JIRA plugin
51+
atlassian-ide-plugin.xml
52+
53+
# Cursive Clojure plugin
54+
.idea/replstate.xml
55+
56+
# Crashlytics plugin (for Android Studio and IntelliJ)
57+
com_crashlytics_export_strings.xml
58+
crashlytics.properties
59+
crashlytics-build.properties
60+
fabric.properties
61+
62+
# Editor-based Rest Client
63+
.idea/httpRequests
64+
65+
# Android studio 3.1+ serialized cache file
66+
.idea/caches/build_file_checksums.ser
67+
68+
# Compiled class file
69+
*.class
70+
71+
# Log file
72+
*.log
73+
74+
# BlueJ files
75+
*.ctxt
76+
77+
# Mobile Tools for Java (J2ME)
78+
.mtj.tmp/
79+
80+
# Package Files #
81+
*.jar
82+
*.war
83+
*.nar
84+
*.ear
85+
*.zip
86+
*.tar.gz
87+
*.rar
88+
89+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
90+
hs_err_pid*
91+
92+
# for myself
93+
94+
target/
95+
96+
.idea/

JavaIntelligence.iml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4" />

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# JavaIntelligence

pom.xml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.hacksource</groupId>
8+
<artifactId>JavaIntelligence</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<developers>
12+
<developer>
13+
<name>Pragma Twice</name>
14+
<email>i@twice.moe</email>
15+
<url>https://github.com/PragmaTwice</url>
16+
<id>PragmaTwice</id>
17+
</developer>
18+
</developers>
19+
<build>
20+
<plugins>
21+
<plugin>
22+
<groupId>org.apache.maven.plugins</groupId>
23+
<artifactId>maven-compiler-plugin</artifactId>
24+
<configuration>
25+
<source>8</source>
26+
<target>8</target>
27+
</configuration>
28+
</plugin>
29+
</plugins>
30+
</build>
31+
32+
<dependencies>
33+
<dependency>
34+
<groupId>com.github.javaparser</groupId>
35+
<artifactId>javaparser-symbol-solver-core</artifactId>
36+
<version>3.7.1</version>
37+
</dependency>
38+
</dependencies>
39+
40+
<properties>
41+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
42+
</properties>
43+
44+
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.hacksource.core;
2+
3+
import com.github.javaparser.Problem;
4+
5+
import java.util.List;
6+
7+
public class SourceException extends Exception {
8+
9+
private List<Problem> problems;
10+
11+
SourceException(List<Problem> problem) {
12+
problems = problem;
13+
}
14+
15+
public List<Problem> getProblems() {
16+
return problems;
17+
}
18+
19+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package org.hacksource.core;
2+
3+
import com.github.javaparser.JavaParser;
4+
import com.github.javaparser.ParseResult;
5+
6+
import com.github.javaparser.ast.CompilationUnit;
7+
import com.github.javaparser.ast.Node;
8+
import com.github.javaparser.ast.NodeList;
9+
import com.github.javaparser.ast.stmt.*;
10+
11+
12+
import java.io.BufferedReader;
13+
import java.io.IOException;
14+
import java.nio.file.Files;
15+
import java.nio.file.Paths;
16+
import java.util.Arrays;
17+
import java.util.List;
18+
19+
import static com.github.javaparser.ParseStart.*;
20+
import static com.github.javaparser.Providers.provider;
21+
22+
public class SourceFormat {
23+
24+
public static String format(String source) throws SourceException {
25+
JavaParser javaParser = new JavaParser();
26+
ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider(source));
27+
28+
CompilationUnit cu = result.getResult().orElseThrow(() -> new SourceException(result.getProblems()));
29+
30+
Arrays.stream(new Class[]{
31+
IfStmt.class, ForStmt.class, ForEachStmt.class, WhileStmt.class
32+
}).forEach(c -> addBracket(cu, c));
33+
34+
return cu.toString();
35+
}
36+
37+
private static <T extends Statement> void addBracket(CompilationUnit cu, Class<T> c) {
38+
cu.findAll(c).forEach(stmt -> {
39+
List<Node> children = stmt.getChildNodes();
40+
if(children.size() != 0) {
41+
Statement block = (Statement)children.get(children.size() - 1);
42+
if(!block.isBlockStmt()) {
43+
stmt.replace(block, new BlockStmt(new NodeList<Statement>(block)));
44+
}
45+
}
46+
});
47+
}
48+
49+
public static void main(String[] args) throws IOException {
50+
51+
StringBuilder sb = new StringBuilder();
52+
53+
String path = SourceFormat.class.getResource("/example.java").getPath();
54+
path = path.substring(1);
55+
56+
try (BufferedReader br = Files.newBufferedReader(Paths.get(path))) {
57+
String line;
58+
while ((line = br.readLine()) != null) {
59+
sb.append(line).append("\n");
60+
}
61+
62+
} catch (IOException e) {
63+
e.printStackTrace();
64+
}
65+
66+
try {
67+
System.out.println(format(sb.toString()));
68+
} catch (SourceException e) {
69+
e.printStackTrace();
70+
}
71+
}
72+
}

src/main/resources/example.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package Graph;
2+
3+
import java.util.LinkedList;
4+
5+
public class Graph {
6+
7+
private final int V;private int E; private LinkedList<Integer>[] adj;
8+
9+
public Graph(int V)
10+
{
11+
this.V=V;
12+
this.E=0;
13+
adj = (LinkedList<Integer>[]) new LinkedList[V];
14+
for (int v = 0; v < V; v++)
15+
adj[v] = new LinkedList<>();
16+
}
17+
18+
public int V()
19+
{
20+
21+
22+
23+
return V;
24+
}
25+
26+
public
27+
int E() {
28+
return E;
29+
}
30+
31+
public void addEdge(int v, int w) {
32+
adj[v].add(w);
33+
adj[w].add(v);
34+
E++;
35+
}
36+
37+
public LinkedList<Integer> adj(int v) {
38+
return adj[v];
39+
}
40+
41+
public int degree(int v,Graph g){
42+
int count = 0;
43+
for(int s : adj(v))
44+
count++;
45+
return count;
46+
}
47+
48+
}

0 commit comments

Comments
 (0)