问题
I need to create new instances of many classes. I'm using reflection but when I make c.newInstance(), the classes without 'empty parameter constructor' throws an java.lang.InstantiationException.
Now, how can i do to create instances of every classes ?
I know that i can use c.getConstructor(Class).newinstance(params) to create instances of classes that doesn't have 'empty parameter constructor', but i do not know the params of each classes.
One more thing, all those classes extend from another class called ParentClass, so one workaround that i could use is to include some code in the ParentClass that force the child classes to implement an 'empty parameter constructor', but don't know how to do this.
Thanks in advance !
回答1:
You can call Class.getConstructors() to get all the constructors for a given class.
On each Constructor, you can call Constructor.getGenericParameterTypes() to learn which parameters it expects.
- JavaDoc for Class
- JavaDoc for Constructor
回答2:
You can't. There's no way to "require" a parameterless constructor and have it enforced by the compiler, and by definition to create an instance of a class you must provide it with the necessary parameters, or otherwise you'll wind up with an object that violates the class contract (because it is not initialized properly).
The proper way to enforce this at a code level is with an object factory and interfaces. I'm presuming that you're having to use reflection because you don't know about the types at compile time; In this case, there should also be a "Factory" which knows how to produce instances of each type. This factory should be built/compiled with the type in question, so that it is aware of and can invoke the proper constructor. The Factory then implements an interface which your code is aware of, such as "ObjectFactory", that allows you to delegate to the factory for instantiating objects. You would then have some method which an object factory can use to register as being responsible for whatever types it can instantiate.
In the code that ships with the classes you're trying to create:
static {
FactoryRegistry.register(TypeA.class, new TypeAFactory());
}
And in your code:
Class<?> unknownClass = ...;
Object obj = FactoryRegistry.getFactory(unknownClass).newInstance();
(where you have a Factory
interface that TypeAFactory
implements and specifies the newInstance
method)
You don't know what unknownClass
is or how to instantiate it, but if the code that came with that class registered a factory, you can query for that factory and ask it to create the object for you. If unknownClass
is really TypeA.class
, then the registry will return the TypeAFactory
that was registered to create objects.
Alternatively, you can just require that the authors of any code your framework is loading dynamically include an argument-less constructor. It's not rigidly enforced, but can be easier for authors to implement.
回答3:
There are two reflective methods for creating instances of classes: java.lang.reflect.Constructor.newInstance()
and Class.newInstance()
Class.newInstance() can only invoke the zero-argument constructor, while
Constructor.newInstance() may invoke any constructor, regardless of the number of parameters.
来源:https://stackoverflow.com/questions/13769994/java-create-new-instance-using-reflection-without-knowing-constructor-params