Top Description Constructors Methods
org.apache.tools.ant.util

public Class ReflectUtil

extends Object
Class Inheritance
Imports
java.lang.reflect.Constructor, .Field, .InvocationTargetException, .Method, java.util.function.Predicate, java.util.stream.Stream, org.apache.tools.ant.BuildException

Utility class to handle reflection on java objects. The class contains static methods to call reflection methods, catch any exceptions, converting them to BuildExceptions.

Constructor Summary

AccessConstructor and Description
private
ReflectUtil()

private constructor

Method Summary

Modifier and TypeMethod and Description
public static <
desired type
T
>
T

Returns:

the value of the field.
getField
(Object
the object to look at.
obj
,
String
the name of the field in the object.
fieldName
)

Get the value of a field in an object.

public static <
desired type
T
>
T

Returns:

the object returned by the method
invoke
(Object
the object to invoke the method on.
obj
,
String
the name of the method to call
methodName
)

Call a method on the object with no parameters.

public static <
desired type
T
>
T

Returns:

the object returned by the method
invoke
(Object
the object to invoke the method on.
obj
,
String
the name of the method to call
methodName
,
Class<?>
the type of argument.
argType
,
Object
the value of the argument.
arg
)

Call a method on the object with one argument.

public static <
desired type
T
>
T

Returns:

the object returned by the method
invoke
(Object
the object to invoke the method on.
obj
,
String
the name of the method to call
methodName
,
Class<?>
the type of the first argument.
argType1
,
Object
the value of the first argument.
arg1
,
Class<?>
the type of the second argument.
argType2
,
Object
the value of the second argument.
arg2
)

Call a method on the object with two argument.

public static <
desired type
T
>
T

Returns:

the object returned by the method
invokeStatic
(Object
the object to invoke the method on.
obj
,
String
the name of the method to call
methodName
)

Call a method on the object with no parameters.

public static <
desired type
T
>
T

Returns:

class instance
newInstance
(Class<T>
Class<T>
ofClass
,
Class<?>[]
Class<?>[]
argTypes
,
Object[]
Object[]
args
)

Create an instance of a class using the constructor matching the given arguments.

public static boolean

Returns:

true if the object has the method.
respondsTo
(Object
the object
o
,
String
the method to check for
methodName
)

A method to test if an object responds to a given message (method call)

public static void
throwBuildException(Exception
the invocation target exception.
t
)

A method to convert an invocationTargetException to a BuildException and throw it.

public static BuildException

Returns:

the converted exception.
toBuildException
(Exception
the invocation target exception.
t
)

A method to convert an invocationTargetException to a BuildException.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

ReflectUtilback to summary
private ReflectUtil()

private constructor

Method Detail

getFieldback to summary
public static <T> T getField(Object obj, String fieldName) throws BuildException

Get the value of a field in an object.

Parameters
<T>
desired type
obj:Object

the object to look at.

fieldName:String

the name of the field in the object.

Returns:T

the value of the field.

Annotations
@SuppressWarnings:unchecked
Exceptions
BuildException:
if there is an error.
invokeback to summary
public static <T> T invoke(Object obj, String methodName)

Call a method on the object with no parameters.

Parameters
<T>
desired type
obj:Object

the object to invoke the method on.

methodName:String

the name of the method to call

Returns:T

the object returned by the method

Annotations
@SuppressWarnings:unchecked
invokeback to summary
public static <T> T invoke(Object obj, String methodName, Class<?> argType, Object arg)

Call a method on the object with one argument.

Parameters
<T>
desired type
obj:Object

the object to invoke the method on.

methodName:String

the name of the method to call

argType:Class<?>

the type of argument.

arg:Object

the value of the argument.

Returns:T

the object returned by the method

Annotations
@SuppressWarnings:unchecked
invokeback to summary
public static <T> T invoke(Object obj, String methodName, Class<?> argType1, Object arg1, Class<?> argType2, Object arg2)

Call a method on the object with two argument.

Parameters
<T>
desired type
obj:Object

the object to invoke the method on.

methodName:String

the name of the method to call

argType1:Class<?>

the type of the first argument.

arg1:Object

the value of the first argument.

argType2:Class<?>

the type of the second argument.

arg2:Object

the value of the second argument.

Returns:T

the object returned by the method

Annotations
@SuppressWarnings:unchecked
invokeStaticback to summary
public static <T> T invokeStatic(Object obj, String methodName)

Call a method on the object with no parameters.

Note

Unlike the invoke method above, this calls class or static methods, not instance methods.

Parameters
<T>
desired type
obj:Object

the object to invoke the method on.

methodName:String

the name of the method to call

Returns:T

the object returned by the method

Annotations
@SuppressWarnings:unchecked
newInstanceback to summary
public static <T> T newInstance(Class<T> ofClass, Class<?>[] argTypes, Object[] args)

Create an instance of a class using the constructor matching the given arguments.

Parameters
<T>
desired type
ofClass:Class<T>

Class<T>

argTypes:Class<?>[]

Class<?>[]

args:Object[]

Object[]

Returns:T

class instance

Since
Ant 1.8.0
respondsToback to summary
public static boolean respondsTo(Object o, String methodName) throws BuildException

A method to test if an object responds to a given message (method call)

Parameters
o:Object

the object

methodName:String

the method to check for

Returns:boolean

true if the object has the method.

Exceptions
BuildException:
if there is a problem.
throwBuildExceptionback to summary
public static void throwBuildException(Exception t) throws BuildException

A method to convert an invocationTargetException to a BuildException and throw it.

Parameters
t:Exception

the invocation target exception.

Exceptions
BuildException:
the converted exception.
toBuildExceptionback to summary
public static BuildException toBuildException(Exception t)

A method to convert an invocationTargetException to a BuildException.

Parameters
t:Exception

the invocation target exception.

Returns:BuildException

the converted exception.

Since
ant 1.7.1