• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reflection on self  RSS feed

 
Jay Richards
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I need to call a method from the class it self i.e.

public class Demo{

//call a method with parameter
public void execute(String methodName,HashMap params){
//example methodName=sampleMethod1
//i need to call the sampleMethod1 method from this class
}

//call a method without a parameter
public void execute(String methodName){
//example methodName=sampleMethod2
//i need to call the sampleMethod2 method from this class
}

//method to be called
public void sampleMethod1(HashMap params){
}

//method to be called2
public void sampleMethod2(){
}
}

Hope someone could show me. thanks a lot in advance.
 
Brandon Broschinsky
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can just call method directly. see below

public class Demo{
//call a method with parameter
public void execute(String methodName,HashMap params){
//i need to call the sampleMethod1 method from this class

sampleMethod1(params); //this will call the method, as it doesn't
//return a value that is all you need
}

//call a method without a parameter
public void execute(String methodName){
//i need to call the sampleMethod2 method from this class

sampleMethod2(); //without params
}

//method to be called
public void sampleMethod1(HashMap params){
}

//method to be called2
public void sampleMethod2(){
}
}
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Sun tutorial page on reflection actually does a very good job at teaching you the library. It also has many nice examples.

Henry
 
Jay Richards
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will call this class, via another class.

actually Demo will implement an Interface.

public interface DemoInterface{
/call a method with parameter
public void execute(String methodName,HashMap params);

//call a method without a parameter
public void execute(String methodName);
}

so i would need to know only the execute method to call the other method from an implementation class.

any ideas will be very much appreciated. thanks
 
Jay Richards
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a more concrete example of what i am trying to accomplish


public interface DemoInterface{
/call a method with parameter
public void execute(String methodName,HashMap params);

//call a method without a parameter
public void execute(String methodName);
}


public class Demo implements DemoInterface{

//call a method with parameter
public void execute(String methodName,HashMap params){
//example methodName=sampleMethod1
//i need to call the sampleMethod1 method from this class
???
???
}

//call a method without a parameter
public void execute(String methodName){
//example methodName=sampleMethod2
//i need to call the sampleMethod2 method from this class
???
???
}

//method to be called
public void sampleMethod1(HashMap params){
}

//method to be called2
public void sampleMethod2(){
}
}


public TestDemo(){
public static void main(String[] args){
DemoInterface demo = new Demo();
HashMap params = new HashMap();

//i need to call methods from the class.
demo.execute("sampleMethod1",params);
demo.execute("sampleMethod2");
}
}


Thanks for any help in this matter.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure why you mention reflection in the thread title, since it seem smuch easier to do this without reflection. Is using reflection a requirement? Is this a homework assignment perhaps?
 
Jay Richards
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it should be reflection because i must not call the methods directly since in the main method, it will be just a DemoInterface that will be used. so only the excute method will be available.

public TestDemo(){
public static void main(String[] args){
DemoInterface demo = new Demo();
HashMap params = new HashMap();

//i need to call methods from the class.
demo.execute("sampleMethod1",params);
demo.execute("sampleMethod2");
}
}

i have read the reflection notes and tutorials, but all of then creates a new instance, what i need is to execute the methods from within the class instance itself i.e. Demo (extends DemoInterface) class.

any idea how? can it be done?

thanks.
 
Adam Vinueza
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't tried this myself, but won't this.getClass() get the class you want? Then you can call getMethod() to get the right method, and call invoke() on the Method to call it. That'd my strategy for implementing execute(). Something a bit like this:

I don't know if it will work--again, I haven't tried it--but that's the basic idea.
 
Jay Richards
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have done and tested this. Kind of solved what i need. so i would like to share it just in case you need to do something like it. enjoy...


public class Demo implements DemoInterface {


public void execute(String methodName){
try{
java.lang.reflect.Method m = this.getClass().getMethod(methodName, null);
m.invoke(this,new Object[]{});
}catch(Exception e){
System.out.println("Demo::execute("+methodName+")-"+e.getMessage());
}
}

public void execute(String methodName,Map params){
try{
Class[] paramType = {Map.class};
java.lang.reflect.Method m = this.getClass().getMethod(methodName, paramType);
Object[] paramData = new Object[1];
paramData[0] = params;
m.invoke(this,paramData);
}catch(Exception e){
System.out.println("Demo::execute("+methodName+")-"+e.getMessage());
e.printStackTrace(System.err);
}
}

public void sampleMethod(){
System.out.println("Demo::sampleMethod() was called.");
}

public void sampleMethod2(Map params){
System.out.println("Demo::sampleMethod2() was called.");
Collection c = params.values();
int mapsize = params.size();

Iterator keyValuePairs1 = params.entrySet().iterator();
for (int i = 0; i < mapsize; i++)
{
Map.Entry entry = (Map.Entry) keyValuePairs1.next();
String key = (String) entry.getKey();
String value = (String) entry.getValue();
System.out.println(key+":"+value);
}
}

public static void main(String[] args){
DemoInterface demo = new Demo();
demo.execute("sampleMethod");
Map sample = new HashMap();
sample.put("param1","this is param1");
sample.put("param2","this is param2");
sample.put("param3","this is param3");
demo.execute("sampleMethod2",sample);
}
}
[ May 02, 2005: Message edited by: Jay Richards ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!