Reputation: 1091
Lets say I have class Foo:
public class Foo {
// ...
}
I want to get its constant pool as a byte array from another class. i.e:
public class Bar {
void butts() {
byte[] fooConstantPool = Foo.class.getConstantPool();
// ...
}
}
Class#getConstantPool
is not part of the standard API. Is there any consistent way I can get another class' constant pool?
Upvotes: 0
Views: 855
Reputation: 1110
You can scan your class loader to find the class's location on disk, then use a framework like BCEL to analyze it.
I have a website demonstrating this, but StackOverflow says I am not allowed to post links to it.
Upvotes: 0
Reputation: 2415
You can get it as sun.reflect.ConstantPool
object via reflection like this:
import sun.reflect.ConstantPool;
public class Bar {
private static final Method getConstantPool;
static {
try {
getConstantPool = Class.class.getDeclaredMethod("getConstantPool");
getConstantPool.setAccessible(true);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
void butts() {
ConstantPool constantPool = (ConstantPool) getConstantPool.invoke(Foo.class);
// ...
}
}
but I'm not sure how to get it as byte array. You can try to serialize it :)
Upvotes: 2