1

I want to write a method just like spring's bean instantiation method.

context.getBean("beanobjectname", Type);  

I have multiple interfaces which will be implemented by developers and developers will put the class name in a property file from where I will read the class name in the form of string just like in bean.xml . I want to write a generic method to instantiate the object of class implementing these interfaces by just passing the classname in the form of string and the type where i will pass type of interface. Please help.

EDIT 1

Expecting the content of method like the following. But this is showing compile errors.

public <T> T getObject(String className, Class<T> type) throw ClassNotFoundException{
    Class<?> clazz = Class.forName(className);
    T object = (T)clazz.newInstance();
    if(clazz instanceof type){
        return (type)clazz;
    }
    return null;
}
Shubham Kadlag
  • 2,248
  • 1
  • 13
  • 32
Deepak Gangore
  • 343
  • 1
  • 13
  • Show your error information at console. – Vy Do May 03 '18 at 11:24
  • $ javac ObjectInstantiate.java ObjectInstantiate.java:6: error: cannot find symbol if(clazz instanceof type){ ^ symbol: class type location: class ObjectInstantiate ObjectInstantiate.java:7: error: cannot find symbol return (type)clazz; ^ symbol: class type location: class ObjectInstantiate Note: ObjectInstantiate.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 2 errors – Deepak Gangore May 03 '18 at 11:47

1 Answers1

-1

instanceof cannot be used with name of class dynamically generated at runtime .You would have to use isAssignableFrom() method of Class.

Refer: What is the difference between instanceof and Class.isAssignableFrom(...)?

  public Object getObject(String className, Class<?> theInterface) throws Exception{
    Class<?> clazz = Class.forName(className);

    if(theInterface.isAssignableFrom(clazz.getClass()))
    return clazz.newInstance();

    else throw new Exception("Not of same type exception");    

}

Hope this helps:)

You Should follow the Abstract Factory Design pattern.

A Factory Pattern or Factory Method Pattern says that just define an interface or abstract class for creating an object but let the subclasses decide which class to instantiate.

Abstract Factory patterns work around a super-factory which creates other factories. This factory is also called as factory of factories.

Refer here:

https://www.tutorialspoint.com/design_pattern/factory_pattern.htm

https://www.tutorialspoint.com/design_pattern/abstract_factory_pattern.htm

Shubham Kadlag
  • 2,248
  • 1
  • 13
  • 32
  • I know the factory pattern. All I need to know the content of method. It should show something like this. public T getObject(String className, Class type){ //Need to know what to code here. } – Deepak Gangore May 03 '18 at 10:15
  • I mean you should not do it the way you are doing.https://stackoverflow.com/questions/812415/why-is-springs-applicationcontext-getbean-considered-bad – Shubham Kadlag May 03 '18 at 10:19
  • You need to code something like { if (classname=="a") return new a(); if(classname=="b") return new b(); } – Shubham Kadlag May 03 '18 at 10:25
  • No @Shubham. You are not getting me. Here I am messing with java reflection. You please first read how spring instantiate beans name of which we pass in the form of string. It uses reflection during runtime to get the object. – Deepak Gangore May 03 '18 at 10:36
  • Okay, now I get it. You should clarify that in the question itself. – Shubham Kadlag May 03 '18 at 10:37
  • Thats why i added EDIT 1 section. :) – Deepak Gangore May 03 '18 at 10:38
  • @DeepakGangore Didn't my updated answer solve the issue? Please self answer the question if you found a different solution. – Shubham Kadlag Jun 05 '19 at 10:19