Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Java source stats. See http://habrahabr.ru/post/258641/
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 java.util.stream.Collectors.joining;
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;
}
@Override
public void accept(T t) {
max.add(t);
if (max.size() > n) {
max.pollFirst();
}
int m = metric.applyAsInt(t);
stats.accept(m);
while (counts.size() <= m)
counts.add(0);
counts.set(m, counts.get(m) + 1);
}
@Override
public String toString() {
String s = String.format("Average: %.3f", stats.getAverage()) + "<br><br>";
s += "<table><tr><th>Count</th><th>Member</th></tr>";
s += max.stream()
.map(e -> "<tr><td>" + metric.applyAsInt(e) + "</td><td>" + e.getSignature() + "</td></tr>")
.collect(joining(""));
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.io.FileNotFoundException;
import java.util.List;
import java.util.function.ToIntFunction;
public class SpoonAnalysisTest {
@Test
public void analyzeSpoon() {
Launcher spoon = new Launcher();
SpoonCompiler comp = spoon.createCompiler();
try {
comp.addInputSources(SpoonResourceHelper.resources(
"C:\\Program Files\\Java\\jdk1.7\\src\\java",
"C:\\Program Files\\Java\\jdk1.7\\src\\javax",
"C:\\Program Files\\Java\\jdk1.7\\src\\org"));
spoon.getEnvironment().setNoClasspath(true);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
comp.build();
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()));
return elems.stream();
}).forEach(stats);
System.out.println("<h2>" + type.getSimpleName() + " " + statName + " stats</h2>");
System.out.println(stats);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.