gpt4 book ai didi

java - java - 如何在没有完全限定的返回类型和方法名称的情况下仅获取特定的返回类型和方法名称

转载 作者:行者123 更新时间:2023-12-04 08:43:37 25 4
gpt4 key购买 nike

通过使用以下方式,

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.


因此,这仍然需要适应以用于方法描述。
请注意,您应该使用 Modifier.toString(m1.getModifiers() & Modifier.methodModifiers())正如 the documentation of Modifier.toString(…) 中所建议的, 过滤掉不合适的位。
此外,尽可能限制变量的范围,即不要声明变量 paramparamLen在循环之外,但在它们第一次使用的地方。但是您也可以使用 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+")"));
}
我们可以实现相同但使用 toString() MethodType的方法开头提到:
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);
}
两种变体都只打印类型名称,就像 MethodtoString()方法。如果您想像变体一样打印参数名称,则可以修改此答案的第一个变体:
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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com