how to get only the specific return type and method names without fully qualified return types and method names in java

huangapple go评论81阅读模式
英文:

how to get only the specific return type and method names without fully qualified return types and method names in java

问题

通过以下方式,

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(")");
英文:

By using the following way,

Class c = Class.forName("java.lang.Double");
Method[] m = c.getDeclaredMethods();
for(Method m1 : m)
System.out.println(m1);

the output is:

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()
-----------------------------------------
-----------------------------------------
-----------------------------------------
-----------------------------------------

Where I need to make changes to get return type and method names without package name to get the desired output as:

public boolean equals(Object)
public static String toString(double)
public String toString()
public int hashCode()
--------------------------------------------
--------------------------------------------
--------------------------------------------

Got the solution, by breaking the whole method into several parts, taking modifier, return type, method name and parameter name separately.

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(")");

答案1

得分: 2

没有为使用简单名称的方法提供内置的 toString 方法。最接近的是 MethodType.toString()

> 返回方法类型的字符串表示,格式为 "(PT0,PT1...)RT"。方法类型的字符串表示是括号括起来的,逗号分隔的类型名称列表,紧接着是返回类型。
>
> 每个类型由其简单名称表示。

因此,这仍然需要适应以用于方法描述。

注意,您应该使用 Modifier.toString(m1.getModifiers() & Modifier.methodModifiers()),如 [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+")"));
}

我们可以通过利用开头提到的 MethodTypetoString() 方法来实现相同的效果:

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+")"));
}
英文:

There’s no built-in toString method for methods using simple names. The closest, I could find, is 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.

So, this would still need adaptation to be used for a method description.

Note you should use Modifier.toString(m1.getModifiers() & Modifier.methodModifiers()) as suggested in the documentation of Modifier.toString(…), to filter out inappropriate bits.

Further, limit the scope of variables as far as possible, i.e. don’t declare the variables param and paramLen outside the loop but at the place of their first use. But you can also simplify the code using 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+")"));
}

We can achieve the same but utilizing the toString() method of MethodType mentioned at the beginning:

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);
}

Both variants only print type names, just as Method’s toString() method does. If you want to print parameter names, as your variant does, the first of this answer’s variants can be adapted:

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+")"));
}

huangapple
  • 本文由 发表于 2020年10月20日 17:15:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64442117.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定