Top Description Constructors Methods
org.eclipse.xtext.xbase.lib.util

public Class ReflectExtensions

extends Object
Class Inheritance
Annotations
@Beta
Imports
java.lang.reflect.Field, .InvocationTargetException, .Method, .Modifier, com.google.common.annotations.Beta, com.google.common.base.Preconditions

Extension methods to simplify reflective invocation of methods and fields.
Author
Sven Efftinge - Initial contribution and API
Since
2.3

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public <T> T

Returns:

the value of the field
get
(Object
the container of the field, not null
receiver
,
String
the field's name, not null
fieldName
)

Retrieves the value of the given accessible field of the given receiver.

private Field
getDeclaredField(Class<?> clazz, String name)

public Object

Returns:

the result of the method invocation. null if the method was of type void.
invoke
(Object
the method call receiver, not null
receiver
,
String
the method name, not null
methodName
,
Object...
the arguments for the method invocation
args
)

Invokes the first accessible method defined on the receiver'c class with the given name and a parameter list compatible to the given arguments.

private boolean
isCompatible(Method candidate, String featureName, Object... args)

public void
set(Object
the receiver, never null
receiver
,
String
the field's name, never null
fieldName
,
Object
the value to set
value
)

Sets the given value on an the receivers's accessible field with the given name.

private Class<?>
wrapperTypeFor(Class<?> primitive)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

ReflectExtensionsback to summary
public ReflectExtensions()

Method Detail

getback to summary
public <T> T get(Object receiver, String fieldName) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException

Retrieves the value of the given accessible field of the given receiver.

Parameters
receiver:Object

the container of the field, not null

fieldName:String

the field's name, not null

Returns:T

the value of the field

Annotations
@SuppressWarnings:unchecked
Exceptions
SecurityException:
see Class#getField(String)
NoSuchFieldException:
see Class#getField(String)
IllegalArgumentException:
see Field#get(Object)
IllegalAccessException:
see Field#get(Object)
getDeclaredFieldback to summary
private Field getDeclaredField(Class<?> clazz, String name) throws NoSuchFieldException
invokeback to summary
public Object invoke(Object receiver, String methodName, Object... args) throws SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException

Invokes the first accessible method defined on the receiver'c class with the given name and a parameter list compatible to the given arguments.

Parameters
receiver:Object

the method call receiver, not null

methodName:String

the method name, not null

args:Object[]

the arguments for the method invocation

Returns:Object

the result of the method invocation. null if the method was of type void.

Exceptions
SecurityException:
see Class#getMethod(String, Class...)
IllegalArgumentException:
see Method#invoke(Object, Object...)
IllegalAccessException:
see Method#invoke(Object, Object...)
InvocationTargetException:
see Method#invoke(Object, Object...)
NoSuchMethodException:
see Class#getMethod(String, Class...)
isCompatibleback to summary
private boolean isCompatible(Method candidate, String featureName, Object... args)
setback to summary
public void set(Object receiver, String fieldName, Object value) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException

Sets the given value on an the receivers's accessible field with the given name.

Parameters
receiver:Object

the receiver, never null

fieldName:String

the field's name, never null

value:Object

the value to set

Exceptions
SecurityException:
see Class#getField(String)
NoSuchFieldException:
see Class#getField(String)
IllegalArgumentException:
see Field#set(Object, Object)
IllegalAccessException:
see Field#set(Object, Object)
wrapperTypeForback to summary
private Class<?> wrapperTypeFor(Class<?> primitive)