2016-05-01 2 views
1

ich die folgende Zuordnung gegeben wurde:Java Reflection Output

Write a method displayMethodInfo (as a method of a class of your choosing), with the following signature: static void displayMethodInfo(Object obj); The method writes, to the standard output, the type of the methods of obj. Please format the method type according to the example below. Let obj be an instance of the following class:

class A { 
    void foo(T1, T2) { ... } 
    int bar(T1, T2, T3) { ... } 
    static double doo() { ... } 
} 

The output of displayMethodInfo(obj) should be as follows:

foo (A, T1, T2) -> void 
bar (A, T1, T2, T3) -> int 
doo() -> double 

As you can see, the receiver type should be the first argument type. Methods declared static do not have a receiver, and should thus not display the class type as the first argument type.

Mein Arbeits Code für diese Aufgabe ist:

import java.lang.Class; 
import java.lang.reflect.*; 

class Main3 { 

    public static class A { 
     void foo(int T1, double T2) { } 
     int bar(int T1, double T2, char T3) { return 1; } 
     static double doo() { return 1; } 
    } 

    static void displayMethodInfo(Object obj) 
    { 
     Method methodsList[] = obj.getClass().getDeclaredMethods(); 
     for (Method y : methodsList) 
     { 
      System.out.print(y.getName() + "(" + y.getDeclaringClass().getSimpleName()); 
      Type[] typesList = y.getGenericParameterTypes(); 
      for (Type z : typesList) 
       System.out.print(", " + z.toString()); 
      System.out.println(") -> " + y.getGenericReturnType().toString()); 
     } 
    } 

    public static void main(String args[]) 
    { 
     A a = new A(); 
     displayMethodInfo(a); 
    } 
} 

Dies funktioniert, aber meine Ausgabe sieht wie folgt aus:

foo(A, int, double) -> void 
bar(A, int, double, char) -> int 
doo(A) -> double 

Wie kann ich dies ändern, damit die Ausgabe so aussieht, wie sie verlangt wird?

+0

Warum hast du beschließen, die Parameter der Methoden in 'A' als' int', 'double' und' char' zu deklarieren? Offensichtlich sollten die "T1", "T2" und "T3" der Zuweisung die * Typen *, nicht * Namen * der Parameter sein. Wenn Sie die Voraussetzungen falsch bekommen, ist es nicht verwunderlich, dass das Ergebnis auch falsch ist ... – Holger

+0

Übrigens, es ist nicht notwendig, ein 'import' für' java.lang.Class' hinzuzufügen ... – Holger

Antwort

1

Wenn ich Sie richtig verstehe, ist Ihr einziges Problem, den Klassentyp als erster Parameter in der statischen doo() -Methode zu haben.

können Sie verwenden, um die Modifier.isStatic() Methode, um dies zu überprüfen:

 boolean isStatic = Modifier.isStatic(y.getModifiers()); 
    System.out.print(y.getName() + "(" 
         + (isStatic ? "" : y.getDeclaringClass().getSimpleName())); 

Sie werden dann loszuwerden, die zusätzliche Komma bekommen müssen, aber dies sollte nicht zu schwer sein;)

+0

Wenn ich versuche, dies zu integrieren, Ich erhalte einen Fehler: String kann in dieser Zeile nicht in Boolean konvertiert werden: System.out.print (y.getName() + "(" + (isStatic? "": Y.getDeclaringClass(). GetSimpleName())); – JMV12

+0

Ich habe 2 Klammern vergessen - meine Antwort wurde mit einer getesteten Version aktualisiert. –