2016-05-25 7 views
-1

Eine meiner Methoden funktioniert nicht, die ich sowohl mit Karte und Java-Reflexion verwendet. Ich bin mir nicht sicher, ob es wegen der Reflektion oder aus irgendeinem anderen Grund ist, aber es funktioniert in einer anderen Klasse, in der ich keine Reflexion verwendet habe. Die Methode findAccessors() sollte einen Wert von map2 abrufen. Die Methode ist in der Klasse ReadEdges definiert. Diese Methode wird mit einer anderen Methode findmethod() aufgerufen, die in der Klasse FindMethod definiert ist.Methode funktioniert nicht mit Reflexion

Immer wenn ich die Methode findAccessors() mit der Methode findmethod() aufrufen, wird eine leere verknüpfte Liste zurückgegeben, anstatt den Wert von map2 zurückzugeben. Die Klassen sind unten angegeben:

Klasse ReadEdges:

import java.io.BufferedReader; 
import java.io.CharArrayReader; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.LinkedHashSet; 
import java.util.LinkedList; 
import java.util.List; 
import java.util.Map; 
import java.util.StringTokenizer; 
import java.util.regex.Pattern; 

import javax.swing.JOptionPane; 

public class ReadEdges { 
    static DFSclass dfs = new DFSclass(); 
    List<String> sourcenodes=new ArrayList<String>(); // source node 
    List<String> destinationnodes=new ArrayList<String>(); // destination node 
    LinkedHashSet<String> findtransitions=new LinkedHashSet<String>(); 
    LoanApprovalSystem LS = new LoanApprovalSystem(); 
    TestdataGeneration testdata = new TestdataGeneration(); 

    private static final String edgePat = "([a-zA-Z]|[0-9])+(,|\\x20)([a-zA-Z]|[0-9])+"; 
    private static final String start=dfs.getstart(); 
    private static final String edge = dfs.getedge(); 
    private static final String transitions=dfs.gettransitions(); 
    public static String a; 
    public static String b; 
    public static String c; 
    public static String d; 
    private Map<String, LinkedHashSet<String>> map = new HashMap(); 
    private Map<String, LinkedHashSet<String>> map2 = new HashMap(); 

    public int getLineCount(String edge){ 
     int count = edge.split("[\n|\r]").length; 
     //System.out.println(count); 
      return count; 
     } 


    public void addEdge(String node1, String node2) throws IOException{ 

     LinkedHashSet<String> adjacent = map.get(node1); 
     { 
      if(adjacent==null) { 
       adjacent = new LinkedHashSet(); 
       map.put(node1, adjacent); 
      } 
      adjacent.add(node2);    
     } 

     } 

    public void addedgeandAccessor(String edge, String accessor) throws IOException{ 
     LinkedHashSet<String> adjacent2 = map2.get(edge); 
     { 
      if(adjacent2==null) { 
       adjacent2 = new LinkedHashSet(); 
       map2.put(edge, adjacent2); 
       //System.out.println(map2); 
      } 

      adjacent2.add(accessor); 
      //System.out.println(map2); 
     } 
    } 

    public void ReadEdge(String edgeinput,String transitionsinput,String accessorinput) throws InvalidInputException 
    { 
     char[] buf = edgeinput.toCharArray(); 
     BufferedReader br = new BufferedReader(new CharArrayReader(buf)); 

     char[] buf2 = transitionsinput.toCharArray(); 
     BufferedReader br2 = new BufferedReader(new CharArrayReader(buf2));  
     String str2 = null; 

     char[] buf3 = accessorinput.toCharArray(); 
     BufferedReader br3 = new BufferedReader(new CharArrayReader(buf3));  
     String str3 = null; 

     try 
     { 
      //a string for a next edge 
      String str = null; 
      //a StringTokinizer 
      StringTokenizer newNodes = null; 
      //get edges and set edges for the graph 
      while((((str = br.readLine()) != null) && (str2 = br2.readLine()) != null) && ((str3 = br3.readLine()) != null)) 
      { 
       c=str; 
       d=str2; 


       LinkedHashSet<String> adjacent = map.get(str); 
       if(adjacent==null) { 
        adjacent = new LinkedHashSet(); 
        map.put(str, adjacent); 
       } 
       adjacent.add(str2); 

       addedgeandAccessor(str,str3); 

       //if the edge inputs are not in good format, throw the exception 
       if(!Pattern.matches(edgePat, str.trim())) 
        JOptionPane.showMessageDialog(null,"An invalid input '" + str + "' for an edge. Please read the notes above the forms. "); 
       //use a comma to separate tokens 
       newNodes = new StringTokenizer (str, ", "); 
       //get the value of source node of an edge 
       String src = newNodes.nextToken(); 
       //create the source node and destination node 
       String srcNode = src; 
       String desNode = newNodes.nextToken(); 

       a=srcNode; 
       b=desNode; 


       addEdge(srcNode, desNode); 
       //System.out.println(adjacent);    
       //findTransition(a,b); 
       //findAccessors(a,b); 

      } 

      //System.out.println(listoftransitions); 
     } 
      catch (IOException e) { 
       JOptionPane.showMessageDialog(null, "Something is Wrong!"); 
       e.printStackTrace(); 
      } 
} 


    public LinkedList<String> adjacentNodes(String last) { 
     LinkedHashSet<String> adjacent = map.get(last); 
     if(adjacent==null) { 
      return new LinkedList(); 
     } 
     return new LinkedList<String>(adjacent); 

    } 

    public LinkedList<String> findTransition(String node1, String node2) throws IOException{ 

      LinkedHashSet<String> adjacent = map.get(node1+" "+node2); 
      if(adjacent==null) { 
       return new LinkedList(); 
      } 
      findtransitions = adjacent; 
     return new LinkedList<String>(findtransitions); 

    } 

    public LinkedList<String> findAccessors(String node1, String node2) { 
     LinkedHashSet<String> adjacent = map2.get(node1+" "+node2); 
     if(adjacent==null) { 
      return new LinkedList(); 
     } 
     System.out.println(adjacent); 
     return new LinkedList<String>(adjacent); 

    } 

public String getsrcNode(){ 
    return a; 
} 

public String getedgeline(){ 
    return c; 
} 

public String gettransitionline(){ 
    return d; 
} 

} 

Klasse FindMethod:

import java.util.ArrayList; 
import java.util.LinkedList; 
import java.lang.reflect.*; 

public class FindMethod { 

    ReadEdges r = new ReadEdges(); 
    LoanApprovalSystem LS = new LoanApprovalSystem(); 
    TestdataGeneration testdata = new TestdataGeneration(); 


    int method1; 
    String method2; 
    boolean method3; 
    boolean method4; 
    String method5; 
    String m; 


    //returns the method name using refletion 
    public String getmethod(Method method){ 

     FindMethod fm = new FindMethod(); 
      m = method.getName();   
      String str = "";   

       str += m+"(" +fm.getparameter(method)+ ")"; 
       // System.out.println(str); 

      return str; 
    } 


    //returns the parameter name of the method using refletion (i.e. (int)) 
    public String getparameter(Method method){ 

     String str = ""; 
     Class<?>[] params = method.getParameterTypes(); 
     for (int i = 0; i < params.length; i++) { 
      if (i > 0) { 
       str += ", "; 
      } 
      str += (params[i].getSimpleName()); 
     } 
     return str; 
    } 

public void findmethod(String s,String t,String transition) throws InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException{ 

     FindMethod fm = new FindMethod(); 


     LoanApprovalSystem cls = new LoanApprovalSystem(); 
     Class<? extends LoanApprovalSystem> c = cls.getClass(); 
     Object obj = c.newInstance(); 
     Method[] methods = LoanApprovalSystem.class.getMethods(); 


      for(Method method : methods) 
      { 
       //returns the method name (i.e. Receive or Asses) 
       m = method.getName(); 
       fm.getmethod(method); 


     if(transition.equals(fm.getmethod(method)) && (transition.equals("Receive(int)"))) 
     { 
      if(fm.getparameter(method).equals("int")) 
      { 
       //LS.Receive(testdata.TestData(s,t)); 
       //invoking the method at runtime where m="Receive". 

       method = c.getMethod(m, int.class); 
       method.invoke(obj,testdata.TestData(s,t)); 

       LinkedList<String> accessors= r.findAccessors(s,t); 
       System.out.println("A:"+accessors); 

       method1=LS.getamount(); 

       System.out.println(m+"("+method1+")"); 
       System.out.println("Amount: "+method1); 
      } 
     } 

} 
    } 

    public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException 
    { 
     FindMethod fm = new FindMethod(); 
     fm.findmethod("1","2","Receive(int)"); 
    } 
} 

Kann jemand mir bitte erklären, warum meine Methode findAccessors() nicht innerhalb der Methode findmethod() zu arbeiten? Oder bitte geben Sie mir eine Lösung für dieses Problem.

Hinweis: In diesem Programm wird eine andere Klasse verwendet LoanApprovalSystem(). Wenn jemand etwas braucht, kann ich auch die Definition dieser Klasse angeben.

Antwort

0

Es sieht aus wie Sie den Standard-Konstruktor für ReadEdges anrufen:

'ReadEdges r = new ReadEdges();'

Wenn Sie Ihre Konstruktor aufrufen, die die Karten auffüllt:

'ReadEdges r = new ReadEdges (edgeinput, transitionsinput, accessorinput);'

EDIT: Die Funktion public void ReadEdge(String edgeinput, String transitionsinput, String accessorinput); wird nie aufgerufen.

Sie müssen void entfernen und als ‚s‘ zu ReadEdge fügen Sie es einen Konstruktor zu machen: public ReadEdges(String edgeinput, String transitionsinput, String accessorinput);

Dann, wenn Sie ReadEdges in der FindMethod Klasse instanziiert, müssen Sie die Argumente liefern.

ReadEdges r = new ReadEdges(); sollte sein: ReadEdges r = new ReadEdges(edgeinput, transitionsinput, accessorinput);

Weitere Informationen lesen Sie über 'Konstruktor Überlastung' und 'Methode Überlastung'. http://beginnersbook.com/2013/05/constructor-overloading/

+0

können Sie genauer? Wie mache ich das oder wo soll ich das nennen ?? Kannst du mir ein Beispiel geben? – Hasan

+0

Ich aktualisierte meine Antwort mit mehr Informationen. Sie müssen sicherstellen, dass Sie die Funktion "ReadEdge" aufrufen, die map2 auffüllt. – Echo

Verwandte Themen