Skip to content

Instantly share code, notes, and snippets.

Forked from leventov/
Last active August 29, 2015 14:21
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ozodrukh/45cce9000316f1636e46 to your computer and use it in GitHub Desktop.
package spoon.test;
import spoon.reflect.declaration.*;
import java.util.ArrayList;
import java.util.IntSummaryStatistics;
import java.util.TreeSet;
import java.util.function.Consumer;
import java.util.function.ToIntFunction;
import static java.util.Comparator.comparingInt;
import static;
public class AdvancedIntStatistics<T extends CtTypeMember & CtNamedElement & CtElement> implements Consumer<T> {
TreeSet<T> max;
private int n;
private ToIntFunction<T> metric;
IntSummaryStatistics stats = new IntSummaryStatistics();
ArrayList<Integer> counts = new ArrayList<>();
public AdvancedIntStatistics(int n, ToIntFunction<T> metric) {
this.n = n;
max = new TreeSet<>(comparingInt(metric)
.thenComparing(e -> (!(e instanceof CtType) ? e.getParent() : e).getSignature()));
this.metric = metric;
public void accept(T t) {
if (max.size() > n) {
int m = metric.applyAsInt(t);
while (counts.size() <= m)
counts.set(m, counts.get(m) + 1);
public String toString() {
String s = String.format("Average: %.3f", stats.getAverage()) + "<br><br>";
s += "<table><tr><th>Count</th><th>Member</th></tr>";
s +=
.map(e -> "<tr><td>" + metric.applyAsInt(e) + "</td><td>" + e.getSignature() + "</td></tr>")
s += "</table>";
return s;
package spoon.test;
import org.junit.Test;
import spoon.Launcher;
import spoon.compiler.SpoonCompiler;
import spoon.compiler.SpoonResourceHelper;
import spoon.reflect.declaration.*;
import java.util.List;
import java.util.function.ToIntFunction;
public class SpoonAnalysisTest {
public void analyzeSpoon() {
Launcher spoon = new Launcher();
SpoonCompiler comp = spoon.createCompiler();
try {
"C:\\Program Files\\Java\\jdk1.7\\src\\java",
"C:\\Program Files\\Java\\jdk1.7\\src\\javax",
"C:\\Program Files\\Java\\jdk1.7\\src\\org"));
} catch (FileNotFoundException e) {
stats(comp, CtMethod.class, "param", m -> m.getParameters().size());
stats(comp, CtMethod.class, "type param", m -> m.getFormalTypeParameters().size());
stats(comp, CtConstructor.class, "param", m -> m.getParameters().size());
stats(comp, CtConstructor.class, "type param", m -> m.getFormalTypeParameters().size());
stats(comp, CtClass.class, "type param", c -> c.getFormalTypeParameters().size());
stats(comp, CtClass.class, "super-interface", c -> c.getSuperInterfaces().size());
stats(comp, CtClass.class, "anon-exec", c -> c.getAnonymousExecutables().size());
stats(comp, CtClass.class, "field", c -> c.getFields().size());
stats(comp, CtClass.class, "constructor", c -> c.getConstructors().size());
stats(comp, CtClass.class, "method", c -> c.getMethods().size());
stats(comp, CtClass.class, "nested types", c -> c.getNestedTypes().size());
private static <T extends CtTypeMember & CtNamedElement> void stats(
SpoonCompiler comp, Class<T> type, String statName, ToIntFunction<T> metric) {
AdvancedIntStatistics<T> stats = new AdvancedIntStatistics<>(10, metric);
comp.getFactory().Package().getAllRoots().stream().flatMap(p -> {
List<T> elems = p.getElements(e -> type.isAssignableFrom(e.getClass()));
System.out.println("<h2>" + type.getSimpleName() + " " + statName + " stats</h2>");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment