Here is a map from letter to type signataure.
Type Signature -> Java Type
Z -> boolean
B -> byte
C -> char
S -> short
I -> int
J -> long
F -> float
D -> double
Lfully-qualified-class; -> fully-qualified-class
[ type -> type[]
(arg-types)ret-type -> method type
// Holds a mapping from Java type names to native type codes.
private static final Map<Class<?>, String> PRIMITIVE_TO_SIGNATURE;
static {
PRIMITIVE_TO_SIGNATURE = new HashMap<Class<?>, String>(9);
PRIMITIVE_TO_SIGNATURE.put(byte.class, "B");
PRIMITIVE_TO_SIGNATURE.put(char.class, "C");
PRIMITIVE_TO_SIGNATURE.put(short.class, "S");
PRIMITIVE_TO_SIGNATURE.put(int.class, "I");
PRIMITIVE_TO_SIGNATURE.put(long.class, "J");
PRIMITIVE_TO_SIGNATURE.put(float.class, "F");
PRIMITIVE_TO_SIGNATURE.put(double.class, "D");
PRIMITIVE_TO_SIGNATURE.put(void.class, "V");
PRIMITIVE_TO_SIGNATURE.put(boolean.class, "Z");
}
/**
* Returns the internal name of {@code clazz} (also known as the descriptor).
*/
public static String getSignature(Class<?> clazz) {
String primitiveSignature = PRIMITIVE_TO_SIGNATURE.get(clazz);
if (primitiveSignature != null) {
return primitiveSignature;
} else if (clazz.isArray()) {
return "[" + getSignature(clazz.getComponentType());
} else {
return "L" + clazz.getName().replace('.', '/') + ";";
}
}
then, we get signature from java.lang.reflect.Method like this:
public static String getSignature(Method method) {
StringBuilder result = new StringBuilder();
result.append('(');
Class<?>[] parameterTypes = method.getParameterTypes();
for (Class<?> parameterType : parameterTypes) {
result.append(getSignature(parameterType));
}
result.append(')');
result.append(getSignature(method.getReturnType()));
return result.toString();
}
we print java.lang.Object class all methods' signature:
Method[] methods = Object.class.getDeclaredMethods();
for(Method method: methods) {
String signature = getSignature(method);
System.out.println(method.getName() + " " + signature);
}
and it prints
finalize ()V
wait (J)V
wait (JI)V
wait ()V
equals (Ljava/lang/Object;)Z
toString ()Ljava/lang/String;
hashCode ()I
getClass ()Ljava/lang/Class;
clone ()Ljava/lang/Object;
notify ()V
notifyAll ()V
registerNativces ()V
Above are some Java codes. we can also get signature from Java builtin command javap, -s for signature info. For example, we can get java.lang.Object class with this command javap -s java.lang.Object
:
Compiled from "Object.java"
public class java.lang.Object {
public java.lang.Object();
descriptor: ()V
public final native java.lang.Class<?> getClass();
descriptor: ()Ljava/lang/Class;
public native int hashCode();
descriptor: ()I
public boolean equals(java.lang.Object);
descriptor: (Ljava/lang/Object;)Z
protected native java.lang.Object clone() throws java.lang.CloneNotSupportedException;
descriptor: ()Ljava/lang/Object;
public java.lang.String toString();
descriptor: ()Ljava/lang/String;
public final native void notify();
descriptor: ()V
public final native void notifyAll();
descriptor: ()V
public final void wait() throws java.lang.InterruptedException;
descriptor: ()V
public final native void wait(long) throws java.lang.InterruptedException;
descriptor: (J)V
public final void wait(long, int) throws java.lang.InterruptedException;
descriptor: (JI)V
protected void finalize() throws java.lang.Throwable;
descriptor: ()V
static {};
descriptor: ()V
}