@@ -0,0 +1,3204 @@
+package com.yo1000.daisychain;%0A%0Aimport org.objectweb.asm.ClassReader;%0Aimport org.objectweb.asm.util.TraceClassVisitor;%0A%0Aimport java.io.*;%0Aimport java.net.URI;%0Aimport java.nio.file.Files;%0Aimport java.nio.file.Path;%0Aimport java.nio.file.Paths;%0Aimport java.util.Map;%0Aimport java.util.TreeMap;%0Aimport java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;%0A%0A/**%0A * Created by yoichi.kikuchi on 2016/01/12.%0A */%0Apublic class ReferenceCounter %7B%0A    protected static final Pattern CLASS_NAME_PATTERN = Pattern.compile(%22%5B() %5DL(%5Ba-zA-Z0-9_%5C%5C$/%5D+);%22);%0A%0A    public static void main(String%5B%5D args) throws IOException %7B%0A        String directoryPath = args.length %3E= 1 ? %22file:%22 + args%5B0%5D : null;%0A        String filterPackage = args.length %3E= 2 ? args%5B1%5D : %22%22;%0A%0A        new ReferenceCounter().count(Paths.get(URI.create(directoryPath))).entrySet().stream()%0A                .filter(entry -%3E entry.getKey().startsWith(filterPackage))%0A                .sorted((x, y) -%3E y.getValue() - x.getValue())%0A                .forEach(entry -%3E System.out.printf(%22%25s : %25d%5Cn%22, entry.getKey(), entry.getValue()));%0A    %7D%0A%0A    public Map%3CString, Integer%3E count(Path directoryPath) throws IOException %7B%0A        Map%3CString, Integer%3E dependencies = new TreeMap%3C%3E();%0A%0A        Files.list(directoryPath).forEach(path -%3E %7B%0A            File file = path.toFile();%0A%0A            if (file.isFile() && file.getName().toLowerCase().endsWith(%22.class%22)) %7B%0A                try %7B%0A                    this.count(file).forEach((key, val) -%3E %7B%0A                        Integer count = dependencies.get(key);%0A                        dependencies.put(key, count != null ? count + val : val);%0A                    %7D);%0A                %7D catch (IOException e) %7B%0A                    throw new IllegalStateException(e);%0A                %7D%0A                return;%0A            %7D%0A%0A            if (file.isDirectory()) %7B%0A                try %7B%0A                    this.count(path).forEach((key, val) -%3E %7B%0A                        Integer count = dependencies.get(key);%0A                        dependencies.put(key, count != null ? count + val : val);%0A                    %7D);%0A                %7D catch (IOException e) %7B%0A                    throw new IllegalStateException(e);%0A                %7D%0A                return;%0A            %7D%0A        %7D);%0A%0A        return dependencies;%0A    %7D%0A%0A    public Map%3CString, Integer%3E count(File classFile) throws IOException %7B%0A        try (FileInputStream stream = new FileInputStream(classFile)) %7B%0A            ClassReader reader = new ClassReader(stream);%0A            StringWriter writer = new StringWriter();%0A%0A            TraceClassVisitor visitor = new TraceClassVisitor(new PrintWriter(writer));%0A            reader.accept(visitor, ClassReader.SKIP_DEBUG);%0A%0A            String trace = writer.toString();%0A            Matcher matcher = CLASS_NAME_PATTERN.matcher(trace);%0A%0A            Map%3CString, Integer%3E dependencies = new TreeMap%3C%3E();%0A%0A            while (matcher.find()) %7B%0A                String className = matcher.group(1);%0A                int count = dependencies.containsKey(className) ? dependencies.get(className) : 0;%0A                dependencies.put(className, count + 1);%0A            %7D%0A%0A            return dependencies;%0A        %7D%0A    %7D%0A%7D%0A

Update class name