java - Method is not working with reflection -
one of methods not working used both map , java reflection. not sure because of reflection or other reason working in other class didn't use reflection.
the method findaccessors()
should retrieve value map2
. method defined in class readedges
. method called method findmethod()
defined in class findmethod
.
whenever call method findaccessors()
method findmethod()
, returning empty linked list instead of returning value map2
. classes given below:
class 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 next edge string str = null; //a stringtokinizer stringtokenizer newnodes = null; //get edges , set edges 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 edge inputs not in format, throw exception if(!pattern.matches(edgepat, str.trim())) joptionpane.showmessagedialog(null,"an invalid input '" + str + "' edge. please read notes above forms. "); //use comma separate tokens newnodes = new stringtokenizer (str, ", "); //get value of source node of edge string src = newnodes.nexttoken(); //create source node , 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 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; } }
class 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 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 parameter name of method using refletion (i.e. (int)) public string getparameter(method method){ string str = ""; class<?>[] params = method.getparametertypes(); (int = 0; < 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 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 method @ runtime 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)"); } }
can please tell me why method findaccessors()
not working within method findmethod()
? or please give me solution of problem.
note: there class used in program loanapprovalsystem ()
. if need can give definition of class too.
it looks calling default constructor readedges:
'readedges r = new readedges();'
when need call constructor populates maps:
'readedges r = new readedges(edgeinput, transitionsinput, accessorinput);'
edit: function public void readedge(string edgeinput, string transitionsinput, string accessorinput);
never being called.
you need remove void
, add 's' readedge make constructor: public readedges(string edgeinput, string transitionsinput, string accessorinput);
then, when instantiate readedges in findmethod class, need supply arguments.
readedges r = new readedges();
should be: readedges r = new readedges(edgeinput, transitionsinput, accessorinput);
for more information, read 'constructor overloading' , 'method overloading'. http://beginnersbook.com/2013/05/constructor-overloading/
Comments
Post a Comment