Skip to content

Commit 000ef03

Browse files
Luolcrnveach
authored andcommitted
Issue #37: extract program
1 parent b7c183f commit 000ef03

File tree

3 files changed

+208
-0
lines changed

3 files changed

+208
-0
lines changed

.travis.yml

+14
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,20 @@ matrix:
4646
- DESC="findbugs and pmd"
4747
- CMD="mvn clean compile pmd:check findbugs:check"
4848

49+
# checkstyle injection compile no error (oraclejdk8)
50+
- jdk: oraclejdk8
51+
env:
52+
- DESC="checkstyle injection compile no error"
53+
- CMD1="git clone https://github.com/checkstyle/checkstyle &&"
54+
- CMD2="cp src/main/resources/com/github/checkstyle/regression/extract/ExtractInfoGeneratorTest.java "
55+
- CMD3="checkstyle/src/test/java/com/puppycrawl/tools/checkstyle/ExtractInfoGeneratorTest.java &&"
56+
- CMD4="cp src/main/resources/com/github/checkstyle/regression/extract/JsonUtil.java "
57+
- CMD5="checkstyle/src/test/java/com/puppycrawl/tools/checkstyle/JsonUtil.java &&"
58+
- CMD6="cd checkstyle &&"
59+
- CMD7="mvn clean compile &&"
60+
- CMD8="mvn test -Dtest=ExtractInfoGeneratorTest#generateExtractInfoFile"
61+
- CMD="$CMD1$CMD2$CMD3$CMD4$CMD5$CMD6$CMD7$CMD8"
62+
4963
script: eval $CMD
5064

5165
after_success:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.puppycrawl.tools.checkstyle;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.nio.charset.Charset;
6+
import java.nio.file.Files;
7+
import java.nio.file.StandardOpenOption;
8+
import java.util.ArrayList;
9+
import java.util.Arrays;
10+
import java.util.Comparator;
11+
import java.util.List;
12+
13+
import org.junit.Test;
14+
15+
import com.puppycrawl.tools.checkstyle.internal.CheckUtil;
16+
import com.puppycrawl.tools.checkstyle.utils.ModuleReflectionUtils;
17+
18+
/**
19+
* This file would be injected into checkstyle project and invoked by maven command
20+
* to generate the module extract info file.
21+
* @author LuoLiangchen
22+
*/
23+
public class ExtractInfoGeneratorTest {
24+
/**
25+
* Generates the extract info file named as "checkstyle_modules.json".
26+
* @throws IOException failure when generating the file
27+
*/
28+
@Test
29+
public void generateExtractInfoFile() throws IOException {
30+
final List<Class<?>> modules = new ArrayList<>(CheckUtil.getCheckstyleModules());
31+
modules.sort(Comparator.comparing(Class::getSimpleName));
32+
final JsonUtil.JsonArray moduleJsonArray = new JsonUtil.JsonArray();
33+
for (Class<?> module : modules) {
34+
moduleJsonArray.add(createJsonObjectFromModuleClass(module));
35+
}
36+
String jsonString = moduleJsonArray.toString();
37+
final File output = new File("checkstyle_modules.json");
38+
Files.write(output.toPath(), jsonString.getBytes(Charset.forName("UTF-8")),
39+
StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
40+
}
41+
42+
/**
43+
* Creates Json object for a module from the module class.
44+
* @param clazz the given module class
45+
* @return the Json object describing the extract info of the module
46+
*/
47+
private static JsonUtil.JsonObject createJsonObjectFromModuleClass(Class<?> clazz) {
48+
final JsonUtil.JsonObject object = new JsonUtil.JsonObject();
49+
50+
final String name = clazz.getSimpleName();
51+
final String parent;
52+
if (ModuleReflectionUtils.isCheckstyleCheck(clazz)) {
53+
parent = "TreeWalker";
54+
}
55+
else if (ModuleReflectionUtils.isRootModule(clazz)) {
56+
parent = "";
57+
}
58+
else {
59+
parent = "Checker";
60+
}
61+
object.addProperty("name", name);
62+
object.addProperty("packageName", clazz.getPackage().getName());
63+
object.addProperty("parent", parent);
64+
65+
final JsonUtil.JsonArray interfaces = new JsonUtil.JsonArray();
66+
final JsonUtil.JsonArray hierarchies = new JsonUtil.JsonArray();
67+
Arrays.stream(clazz.getInterfaces()).forEach(cls -> interfaces.add(cls.getCanonicalName()));
68+
Class<?> superClass = clazz.getSuperclass();
69+
while (!Object.class.equals(superClass)) {
70+
hierarchies.add(superClass.getCanonicalName());
71+
Arrays.stream(superClass.getInterfaces())
72+
.forEach(cls -> interfaces.add(cls.getCanonicalName()));
73+
superClass = superClass.getSuperclass();
74+
}
75+
object.add("interfaces", interfaces);
76+
object.add("hierarchies", hierarchies);
77+
78+
return object;
79+
}
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.puppycrawl.tools.checkstyle;
2+
3+
import java.util.Arrays;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
8+
/**
9+
* Our own Json processing utility.
10+
* @author LuoLiangchen
11+
*/
12+
class JsonUtil {
13+
/**
14+
* Adds indent of the given text.
15+
* @param text the text to add indent
16+
* @return the result text with indent
17+
*/
18+
private static String addIndent(String text) {
19+
return Arrays.stream(text.split("\n"))
20+
.map(line -> " " + line)
21+
.collect(Collectors.joining("\n"));
22+
}
23+
24+
/**
25+
* Formats the Json object.
26+
* @param object the object to format
27+
* @return the format result
28+
*/
29+
private static String format(Object object) {
30+
if (object instanceof String) {
31+
return "\"" + object + "\"";
32+
} else {
33+
return object.toString();
34+
}
35+
}
36+
37+
/** Represents an object type in Json. */
38+
public static final class JsonObject {
39+
/** Fields of this Json object. */
40+
private final List<KeyValue> members = new LinkedList<>();
41+
42+
/**
43+
* Adds a member.
44+
* @param key the key of the field
45+
* @param value the value of the field
46+
*/
47+
void addProperty(String key, Object value) {
48+
add(key, value);
49+
}
50+
51+
/**
52+
* Adds a member.
53+
* @param key the key of the field
54+
* @param value the value of the field
55+
*/
56+
void add(String key, Object value) {
57+
members.add(new KeyValue(key, value));
58+
}
59+
60+
@Override
61+
public String toString() {
62+
final String keyValueLines = members.stream()
63+
.map(Object::toString)
64+
.collect(Collectors.joining(",\n"));
65+
return "{\n" + addIndent(keyValueLines) + "\n}";
66+
}
67+
}
68+
69+
/** Represents an array type in Json. */
70+
public static final class JsonArray {
71+
/** Items of this Json array. */
72+
private final List<Object> members = new LinkedList<>();
73+
74+
/**
75+
* Adds a member.
76+
* @param object the member to add
77+
*/
78+
void add(Object object) {
79+
members.add(object);
80+
}
81+
82+
@Override
83+
public String toString() {
84+
final String membersLines = members.stream()
85+
.map(JsonUtil::format)
86+
.collect(Collectors.joining(",\n"));
87+
return "[\n" + addIndent(membersLines) + "\n]";
88+
}
89+
}
90+
91+
/** Represents a key-value pair in Json object. */
92+
private static final class KeyValue {
93+
/** The key of the field. */
94+
private final String key;
95+
96+
/** The value of the field. */
97+
private final Object value;
98+
99+
/**
100+
* Creates a new instance of KeyValue.
101+
* @param key the key of the field
102+
* @param value the value of the field
103+
*/
104+
KeyValue(String key, Object value) {
105+
this.key = key;
106+
this.value = value;
107+
}
108+
109+
@Override
110+
public String toString() {
111+
return "\"" + key + "\": " + format(value);
112+
}
113+
}
114+
}

0 commit comments

Comments
 (0)