Problem with Class.getConstructor()
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   AAH_BNM
Posted On:   Wednesday, October 17, 2001 06:00 AM

For example I have a class A: public class A { public A(I i){ } } interface I{ } public class B implements I{ } and I want ta make an factory public class MyFactory{ public static Object createInstance(Strng className, Object[] constructorAttributes){ Object myObject = null; try{ Class[] parameterTypes = new Class[constructorAttributes.length]; for(int i = 0; i < constructorAttributes.length; i++){ parameterTypes[i] = constructorAttributes[i].getClass(); } Class objectClass = Class.forName(className); //Here I got a problem Constructor constructor = objectC   More>>

For example I have a class A:

			
public class A {
public A(I i){
}
}

interface I{
}

public class B implements I{
}



and I want ta make an factory
			
public class MyFactory{
public static Object createInstance(Strng className, Object[] constructorAttributes){
Object myObject = null;
try{
Class[] parameterTypes = new Class[constructorAttributes.length];
for(int i = 0; i < constructorAttributes.length; i++){
parameterTypes[i] = constructorAttributes[i].getClass();
}
Class objectClass = Class.forName(className);
//Here I got a problem
Constructor constructor = objectClass.getConstructor(parameterTypes);
Object myObject = constructor.newInstance(constructorAttributes);
}
catch(Exception ex){
System.out.println("Failure -> "+className+"
"+ex);
myObject = null;
}
return myObject;
}
}



And if I call :


MyFactory.createInstance("A", new Object[] {new B() } );


I get an NoSuchMethodException, it don't recognize that B is an implementation of I,


may be someone have any sugestion how to solve this problem other then to declare:
			
public class A {
//direct to put class B as a constructor parameters
public A(B b){
}
}



thank all in advance.    <<Less
About | Sitemap | Contact