Skip to content

Instantly share code, notes, and snippets.

@paulo-raca
Created May 23, 2017 23:44
Show Gist options
  • Save paulo-raca/42ea00bbcda4c14d345c43de6c35bb84 to your computer and use it in GitHub Desktop.
Save paulo-raca/42ea00bbcda4c14d345c43de6c35bb84 to your computer and use it in GitHub Desktop.
package test;
class Outer {
public class A1 {}
protected class A2 {}
private class A3 {}
/*package*/ class A4 {}
public static class B1 {}
protected static class B2 {}
private static class B3 {}
/*package*/ static class B4 {}
public abstract class C1 {}
protected abstract class C2 {}
private abstract class C3 {}
/*package*/ abstract class C4 {}
public final class D1 {}
protected final class D2 {}
private final class D3 {}
/*package*/ final class D4 {}
}
test.Outer$A1: public
test.Outer$A2: public
test.Outer$A3: /*package*/
test.Outer$A4: /*package*/
test.Outer$B1: public static
test.Outer$B2: public static
test.Outer$B3: /*package*/ static
test.Outer$B4: /*package*/ static
test.Outer$C1: public abstract
test.Outer$C2: public abstract
test.Outer$C3: /*package*/ abstract
test.Outer$C4: /*package*/ abstract
test.Outer$D1: public final
test.Outer$D2: public final
test.Outer$D3: /*package*/ final
test.Outer$D4: /*package*/ final
test.Outer: /*package*/
import com.google.common.base.Joiner;
import com.google.common.collect.Sets;
import com.iperlane.reflectionacessor.MakePublicTransform;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.Modifier;
import javassist.build.IClassTransformer;
public class Test {
private File classesDir;
private ClassPool classPool;
public Test(File classesDir) throws Exception {
this.classesDir = classesDir;
this.classPool = new ClassPool();
this.classPool.appendClassPath(classesDir.getAbsolutePath());
this.classPool.appendSystemPath();
}
public void go() throws Exception {
go(classesDir);
}
private void go(File f) throws Exception {
if (f.isDirectory()) {
File[] children = f.listFiles();
Arrays.sort(children);
for (File c : children) {
go(c);
}
} else if (f.isFile() && f.getName().endsWith(".class")) {
String className = f.getAbsolutePath().substring(classesDir.getAbsolutePath().length()); //Relative path
className = className.substring(1, className.length() - 6); //Remove .class
className = className.replaceAll("/", "."); //Use '.' for package delimiter
CtClass cls = classPool.get(className);
go(cls);
}
}
private void go(CtClass cls) throws Exception {
System.out.println(cls.getName() + ": " + modifiersToString(cls.getModifiers()));
}
private static String modifiersToString(int modifiers) {
String ret = "";
if (Modifier.isPublic(modifiers)) {
ret = "public";
} else if (Modifier.isPrivate(modifiers)) {
ret = "private";
} else if (Modifier.isProtected(modifiers)) {
ret = "protected";
} else if (Modifier.isPackage(modifiers)) {
ret = "/*package*/";
}
if (Modifier.isStatic(modifiers)) {
ret += " static";
}
if (Modifier.isFinal(modifiers)) {
ret += " final";
}
if (Modifier.isAbstract(modifiers)) {
ret += " abstract";
}
return ret;
}
public static void main(String[] args) throws Exception {
new Test(new File("/home/paulo/testClasses/")).go();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment