Class DefaultConstructorStrategy
- java.lang.Object
-
- ch.obermuhlner.scriptengine.java.constructor.DefaultConstructorStrategy
-
- All Implemented Interfaces:
ConstructorStrategy
public class DefaultConstructorStrategy extends java.lang.Object implements ConstructorStrategy
The defaultConstructorStrategy
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 Modifier and Type Field Description private java.lang.Object[]
arguments
private java.lang.Class<?>[]
argumentTypes
-
Constructor Summary
Constructors Modifier Constructor Description private
DefaultConstructorStrategy(java.lang.Class<?>[] argumentTypes, java.lang.Object[] arguments)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static DefaultConstructorStrategy
byArgumentTypes(java.lang.Class<?>[] argumentTypes, java.lang.Object... arguments)
Creates aDefaultConstructorStrategy
that will call the public constructor with the specified argument types and passes the specified argument list.static DefaultConstructorStrategy
byDefaultConstructor()
Creates aDefaultConstructorStrategy
that will call the public default no-argument constructor.static DefaultConstructorStrategy
byMatchingArguments(java.lang.Object... arguments)
Creates aDefaultConstructorStrategy
that will call a public constructor that matches the specified arguments.java.lang.Object
construct(java.lang.Class<?> clazz)
Constructs an instance of aClass
.private java.lang.reflect.Constructor<?>
findConstructor(java.lang.Class<?> clazz, java.lang.Class<?>[] argumentTypes, java.lang.Object[] arguments)
-
-
-
Method Detail
-
construct
public java.lang.Object construct(java.lang.Class<?> clazz) throws javax.script.ScriptException
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:
javax.script.ScriptException
- if the instance could not be constructed
-
byDefaultConstructor
public static DefaultConstructorStrategy byDefaultConstructor()
Creates aDefaultConstructorStrategy
that will call the public default no-argument constructor.- Returns:
- the created
DefaultConstructorStrategy
-
byArgumentTypes
public static DefaultConstructorStrategy byArgumentTypes(java.lang.Class<?>[] argumentTypes, java.lang.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
public static DefaultConstructorStrategy byMatchingArguments(java.lang.Object... arguments)
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 java.lang.reflect.Constructor<?> findConstructor(java.lang.Class<?> clazz, java.lang.Class<?>[] argumentTypes, java.lang.Object[] arguments) throws java.lang.NoSuchMethodException, javax.script.ScriptException
- Throws:
java.lang.NoSuchMethodException
javax.script.ScriptException
-
-