通过以下方式,
Class c = Class.forName("java.lang.Double");
Method[] m = c.getDeclaredMethods();
for(Method m1 : m)
System.out.println(m1);
输出是:
public boolean java.lang.Double.equals(java.lang.Object)
public static java.lang.String java.lang.Double.toString(double)
public java.lang.String java.lang.Double.toString()
public int java.lang.Double.hashCode()
-----------------------------------------
-----------------------------------------
-----------------------------------------
-----------------------------------------
我需要进行更改以获取没有包名称的返回类型和方法名称,以获得所需的输出:
public boolean equals(Object)
public static String toString(double)
public String toString()
public int hashCode()
--------------------------------------------
--------------------------------------------
--------------------------------------------
得到了解决方案,将整个方法分成几个部分,分别取修饰符、返回类型、方法名称和参数名称。
Method[] m = c.getDeclaredMethods();
Parameter[] param;
int paramLen;
for(Method m1 : m){
System.out.print(Modifier.toString(m1.getModifiers()) + " ");
System.out.print(m1.getReturnType().getSimpleName());
System.out.print(" ");
System.out.print(m1.getName() + "(");
param = m1.getParameters();
paramLen = param.length;
for(Parameter param1 : param){
System.out.print(param1.getType().getSimpleName() + " " + param1.getName());
if(--paramLen > 0)
System.out.print(", ");
}
System.out.println(")");
最佳答案
对于使用简单名称的方法,没有内置的 toString
方法。我能找到的最接近的是 MethodType.toString()
:
Returns a string representation of the method type, of the form
"(PT0,PT1...)RT"
. The string representation of a method type is a parenthesis enclosed, comma separated list of type names, followed immediately by the return type.Each type is represented by its simple name.
因此,这仍然需要调整才能用于方法描述。
请注意,您应该按照 the documentation of Modifier.toString(…)
中的建议使用 Modifier.toString(m1.getModifiers() & Modifier.methodModifiers())
,过滤掉不适当的位。
此外,尽可能限制变量的范围,即不要在循环之外但在第一次使用的地方声明变量 param
和 paramLen
。但您也可以使用 StringJoiner
来简化代码:
for(Method m: c.getDeclaredMethods()) {
StringJoiner j = new StringJoiner(" ");
String access = Modifier.toString(m.getModifiers() & Modifier.methodModifiers());
if(!access.isEmpty()) j.add(access);
j.add(m.getReturnType().getSimpleName());
StringJoiner argJ = new StringJoiner(", ");
for(Class<?> cl: m.getParameterTypes()) argJ.add(cl.getSimpleName());
System.out.println(j.add(m.getName()+"("+argJ+")"));
}
我们可以实现相同的目的,但利用开头提到的 MethodType
的 toString()
方法:
for(Method m: c.getDeclaredMethods()) {
StringJoiner j = new StringJoiner(" ");
String access = Modifier.toString(m.getModifiers() & Modifier.methodModifiers());
if(!access.isEmpty()) j.add(access);
String shortSig = MethodType.methodType(m.getReturnType(), m.getParameterTypes())
.toString();
int split = shortSig.lastIndexOf(')') + 1;
j.add(shortSig.substring(split)).add(m.getName()+shortSig.substring(0, split));
System.out.println(j);
}
这两个变体都只打印类型名称,就像 Method
的 toString()
方法一样。如果您想像您的变体一样打印参数名称,则可以调整此答案的第一个变体:
for(Method m: c.getDeclaredMethods()) {
StringJoiner j = new StringJoiner(" ");
String access = Modifier.toString(m.getModifiers() & Modifier.methodModifiers());
if(!access.isEmpty()) j.add(access);
j.add(m.getReturnType().getSimpleName());
StringJoiner argJ = new StringJoiner(", ");
for(Parameter p: m.getParameters())
argJ.add(p.getType().getSimpleName()+' '+p.getName());
System.out.println(j.add(m.getName()+"("+argJ+")"));
}
关于java - 如何在java中仅获取特定的返回类型和方法名称而没有完全限定的返回类型和方法名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64442117/