690 shaares
3 liens privés
3 liens privés
Pour avoir la description d'une méthode version bytecode :
import java.lang.reflect.Constructor;
import java.lang.reflect.Executable;
import java.lang.reflect.Method;
import java.util.concurrent.ThreadPoolExecutor;
public class ByteCodeDescriptor {
public static void main(String... args) {
Class clazz = ThreadPoolExecutor.class;
String result= ByteCodeDescriptor.getDescriptorForClass(clazz);
System.out.println("class = " + result);
for (Method m : ThreadPoolExecutor.class.getMethods()) {
String resultM= ByteCodeDescriptor.getDescriptor(m);
System.out.println("method = " + m);
System.out.println("method result = " + m.getName() + resultM);
}
for (Constructor c : ThreadPoolExecutor.class.getConstructors()) {
String resultM= ByteCodeDescriptor.getDescriptor(c);
System.out.println("constructor = " + c);
System.out.println("constructor result = " + resultM);
}
}
static String getDescriptorForClass(final Class c)
{
if(c.isPrimitive())
{
if(c==byte.class)
return "B";
if(c==char.class)
return "C";
if(c==double.class)
return "D";
if(c==float.class)
return "F";
if(c==int.class)
return "I";
if(c==long.class)
return "J";
if(c==short.class)
return "S";
if(c==boolean.class)
return "Z";
if(c==void.class)
return "V";
throw new RuntimeException("Unrecognized primitive "+c);
}
if(c.isArray()) return c.getName().replace('.', '/');
return ('L'+c.getName()+';').replace('.', '/');
}
static String getDescriptor(Executable e) {
String s = "(";
for (final Class c : e.getParameterTypes())
s += getDescriptorForClass(c);
s += ')';
if (e instanceof Method) {
Method m = (Method) e;
return m.getName() + s + getDescriptorForClass(m.getReturnType());
} else if (e instanceof Constructor) {
Constructor c = (Constructor) e;
return "<init>" + s + getDescriptorForClass(void.class);
}
throw new RuntimeException("Unrecognized primitive "+e);
}
}