Class DefaultConstructorStrategy
java.lang.Object
ch.obermuhlner.scriptengine.java.constructor.DefaultConstructorStrategy
- All Implemented Interfaces:
ConstructorStrategy
The default
ConstructorStrategy
implementation.
This implementation has three static constructor methods to define the constructor that should be called:
byDefaultConstructor()
to call the public default no-argument constructor.byArgumentTypes(Class[], Object...)
to call the public constructor with the specified argument types and pass it the specified arguments.byMatchingArguments(Object...)
to call a public constructor that matches the specified arguments.
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
DefaultConstructorStrategy
(Class<?>[] argumentTypes, Object[] arguments) -
Method Summary
Modifier and TypeMethodDescriptionstatic DefaultConstructorStrategy
byArgumentTypes
(Class<?>[] argumentTypes, Object... arguments) Creates aDefaultConstructorStrategy
that will call the public constructor with the specified argument types and passes the specified argument list.static DefaultConstructorStrategy
Creates aDefaultConstructorStrategy
that will call the public default no-argument constructor.static DefaultConstructorStrategy
byMatchingArguments
(Object... arguments) Creates aDefaultConstructorStrategy
that will call a public constructor that matches the specified arguments.Constructs an instance of aClass
.private Constructor
<?> findConstructor
(Class<?> clazz, Class<?>[] argumentTypes, Object[] arguments)
-
Field Details
-
argumentTypes
-
arguments
-
-
Constructor Details
-
DefaultConstructorStrategy
-
-
Method Details
-
construct
Description copied from interface:ConstructorStrategy
Constructs an instance of aClass
.- Specified by:
construct
in interfaceConstructorStrategy
- Parameters:
clazz
- theClass
- Returns:
- the constructed instance or
null
- Throws:
ScriptException
- if the instance could not be constructed
-
byDefaultConstructor
Creates aDefaultConstructorStrategy
that will call the public default no-argument constructor.- Returns:
- the created
DefaultConstructorStrategy
-
byArgumentTypes
public static DefaultConstructorStrategy byArgumentTypes(Class<?>[] argumentTypes, Object... arguments) Creates aDefaultConstructorStrategy
that will call the public constructor with the specified argument types and passes the specified argument list.- Parameters:
argumentTypes
- the argument types defining the constructor to callarguments
- the arguments to pass to the constructor (may containnull
)- Returns:
- the created
DefaultConstructorStrategy
-
byMatchingArguments
Creates aDefaultConstructorStrategy
that will call a public constructor that matches the specified arguments. A constructor must match all specified arguments, exceptnull
values which match any non-primitive type. The conversion from object types into corresponding primitive types (for exampleInteger
intoint
) is handled automatically. If multiple public constructors match the specified arguments theconstruct(Class)
method will throw aScriptException
.- Parameters:
arguments
- the arguments to pass to the constructor (may containnull
)- Returns:
- the created
DefaultConstructorStrategy
-
findConstructor
private Constructor<?> findConstructor(Class<?> clazz, Class<?>[] argumentTypes, Object[] arguments) throws NoSuchMethodException, ScriptException - Throws:
NoSuchMethodException
ScriptException
-