Skip to content

Commit

Permalink
Issue checkstyle#37: extract program
Browse files Browse the repository at this point in the history
  • Loading branch information
Luolc committed Jul 6, 2017
1 parent f924b17 commit e7f9d6a
Showing 1 changed file with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.puppycrawl.tools.checkstyle;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.Test;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.puppycrawl.tools.checkstyle.internal.CheckUtil;
import com.puppycrawl.tools.checkstyle.utils.ModuleReflectionUtils;

/**
* This file would be injected into checkstyle project and invoked by maven command
* to generate the module extract info file.
* @author LuoLiangchen
*/
public class ExtractInfoGeneratorTest {
/**
* Generates the extract info file named as "checkstyle_modules.json".
* @throws IOException failure when generating the file
*/
@Test
public void generateExtractInfoFile() throws IOException {
final Gson gson = new GsonBuilder().setPrettyPrinting().create();
final List<JsonObject> objects = CheckUtil.getCheckstyleModules().stream()
.map(ExtractInfoGeneratorTest::createJsonObjectFromModuleClass)
.collect(Collectors.toList());
objects.sort(Comparator.comparing(obj -> obj.get("name").getAsString()));
String jsonString = gson.toJson(objects);
final File output = new File("checkstyle_modules.json");
Files.write(output.toPath(), jsonString.getBytes(Charset.forName("UTF-8")),
StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
}

/**
* Creates Json object for a module from the module class.
* @param clazz the given module class
* @return the Json object describing the extract info of the module
*/
private static JsonObject createJsonObjectFromModuleClass(Class<?> clazz) {
final JsonObject object = new JsonObject();

final String name = clazz.getSimpleName();
final String parent;
if (ModuleReflectionUtils.isCheckstyleCheck(clazz)) {
parent = "TreeWalker";
}
else if ("Checker".equals(name)) {
parent = "";
}
else {
parent = "Checker";
}
object.addProperty("name", name);
object.addProperty("packageName", clazz.getPackage().getName());
object.addProperty("parent", parent);

final JsonArray interfaces = new JsonArray();
final JsonArray hierarchies = new JsonArray();
Arrays.stream(clazz.getInterfaces()).forEach(cls -> interfaces.add(cls.getCanonicalName()));
Class<?> superClass = clazz.getSuperclass();
while (!Object.class.equals(superClass)) {
hierarchies.add(superClass.getCanonicalName());
Arrays.stream(superClass.getInterfaces())
.forEach(cls -> interfaces.add(cls.getCanonicalName()));
superClass = superClass.getSuperclass();
}
object.add("interfaces", interfaces);
object.add("hierarchies", hierarchies);

return object;
}
}

0 comments on commit e7f9d6a

Please sign in to comment.