Raaga SRI

Greenhorn
+ Follow
since Dec 02, 2006
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Raaga SRI

public interface ES
{

Factory Factory = (Factory) MyFactory.INSTANCE;

interface Factory
{
ES create(String a, String b);
}
}

public class MyFactory implements ES.Factory
{

public static MyFactory INSTANCE = new MyFactory();

public ES create(String a, String b)
{
return create(a, b);
}

public ES create(a, b){
System.out.println("&&&&&&&&&&&&&&&&& do something");
}

I want to invoke :

ES es = ES.Factory.create(a,b);

using reflection.

I tried two ways:

First Way: Get the field and try to invoke the method on the filed like following:
String className = "EC";
Class classDefinition = Class.forName(className);
Field factory = classDefinition.getField("Factory");
Class factoryCls = factory.getType();
Class[] par=new Class[2];
par[0]=java.lang.String.class;
par[1]=String.class;
Method method = factoryCls.getMethod("create",par);
ES es = (ES)method.invoke(factoryCls,input);

Second Way:

Class factoryClsImpl = Class.forName("MyFactory");
Class[] par=new Class[2];
par[0]=java.lang.String.class;
par[1]=String.class;
Method method2 = factoryClsImpl.getMethod("create",par);
String[] input2={componentKindQName,componentQName};
es = (ES)method2.invoke(factoryClsImpl,input2);
11 years ago