Note that we compiled following class with -parameters option of the compiler to include the method parameter names in source class (more info)
package com.logicbig.example.method;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
public class GetParametersExample {
private static class Test {
private <T extends Serializable> void process(int processorId, String processName,
Map<Byte, T> processParams) {}
}
public static void main(String... args) throws Exception {
Method[] m = Test.class.getDeclaredMethods();
for (Method method : m) {
System.out.println("-- " + method + " --");
Parameter[] parameters = method.getParameters();
System.out.println("-- " + Arrays.toString(parameters) + " --");
for (Parameter parameter : parameters) {
System.out.println("-- " + parameter + " --");
printParameter(parameter);
}
}
}
private static void printParameter(Parameter p) throws Exception {
for (Method method : Parameter.class.getDeclaredMethods()) {
String methodName = method.getName();
if (methodName.startsWith("get")) {
if (method.getParameterCount() > 0) {
continue;
}
if (method.canAccess(p)) {
if (!method.trySetAccessible()) {
System.err.println("trySEtAccessible fails " + method);
continue;
}
}
Object o = null;
try {
o = method.invoke(p);
} catch (Exception e) {
//System.err.println(e + " - " + method);
}
print("Parameter#" + methodName, o);
}
}
}
private static void print(String msg, Object val) {
if (val != null && val.getClass().isArray()) {
val = Arrays.toString((Object[]) val);
} else {
val = Objects.toString(val);
}
System.out.printf(" %s = %s%n", msg, val);
}
}
Output
-- private void com.logicbig.example.method.GetParametersExample$Test.process(int,java.lang.String,java.util.Map) --
-- [int processorId, java.lang.String processName, java.util.Map<java.lang.Byte, T> processParams] --
-- int processorId --
Parameter#getModifiers = 0
Parameter#getName = processorId
Parameter#getAnnotations = []
Parameter#getDeclaredAnnotations = []
Parameter#getType = int
Parameter#getAnnotatedType = sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@6b601a48
Parameter#getDeclaringExecutable = private void com.logicbig.example.method.GetParametersExample$Test.process(int,java.lang.String,java.util.Map)
Parameter#getRealName = null
Parameter#getParameterizedType = int
-- java.lang.String processName --
Parameter#getModifiers = 0
Parameter#getName = processName
Parameter#getAnnotations = []
Parameter#getDeclaredAnnotations = []
Parameter#getType = class java.lang.String
Parameter#getAnnotatedType = sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@1f985ed5
Parameter#getDeclaringExecutable = private void com.logicbig.example.method.GetParametersExample$Test.process(int,java.lang.String,java.util.Map)
Parameter#getRealName = null
Parameter#getParameterizedType = class java.lang.String
-- java.util.Map<java.lang.Byte, T> processParams --
Parameter#getModifiers = 0
Parameter#getName = processParams
Parameter#getAnnotations = []
Parameter#getDeclaredAnnotations = []
Parameter#getType = interface java.util.Map
Parameter#getAnnotatedType = sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedParameterizedTypeImpl@79369677
Parameter#getDeclaringExecutable = private void com.logicbig.example.method.GetParametersExample$Test.process(int,java.lang.String,java.util.Map)
Parameter#getRealName = null
Parameter#getParameterizedType = java.util.Map<java.lang.Byte, T>