Reflection Method.getParameterTypes() - 对象数组参数

时间:2012-11-14 13:16:54

标签: java reflection

我使用反射来获取所有方法参数名称。

问题是当其中一个参数类型为:the.package.myobject [](array)

String name = method.getParameterTypes()‬[0].getName()

我得到:[the.package.myobject;] //字母L和符号;

我怎样才能获得纯类型名称? (没有子串)

2 个答案:

答案 0 :(得分:8)

您需要检查type.isArray(),如果是,请使用getComponentType()

final Class<?> c = method.getParameterTypes()[0];
final String name = (c.isArray()? c.getComponentType() : c).getName();

答案 1 :(得分:1)

最有可能的是

Class firstType = method.getParameterTypes()‬[0];

// will be null if not an array.
Class firstComponentType = firstType.getComponentType();
相关问题