org.eclipse.xtext.xbase.lib.util
Class ReflectExtensions

java.lang.Object
  extended by org.eclipse.xtext.xbase.lib.util.ReflectExtensions

@Beta
@GwtCompatible
public class ReflectExtensions
extends Object

Extension methods to simplify reflective invocation of methods and fields.

Since:
2.3
Author:
Sven Efftinge - Initial contribution and API

Constructor Summary
ReflectExtensions()
           
 
Method Summary
<T> T
get(Object receiver, String fieldName)
          Retrieves the value of the given accessible field of the given receiver.
 Object invoke(Object receiver, String methodName, Object... 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.
 void set(Object receiver, String fieldName, Object value)
          Sets the given value on an the receivers's accessible field with the given name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReflectExtensions

public ReflectExtensions()
Method Detail

set

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 - the receiver, never null
fieldName - the field's name, never null
value - the value to set
Throws:
NoSuchFieldException - see Class.getField(String)
SecurityException - see Class.getField(String)
IllegalAccessException - see Field.set(Object, Object)
IllegalArgumentException - see Field.set(Object, Object)

get

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 - the container of the field, not null
fieldName - the field's name, not null
Returns:
the value of the field
Throws:
NoSuchFieldException - see Class.getField(String)
SecurityException - see Class.getField(String)
IllegalAccessException - see Field.get(Object)
IllegalArgumentException - see Field.get(Object)

invoke

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 - the method call receiver, not null
methodName - the method name, not null
args - the arguments for the method invocation
Returns:
the result of the method invocation. null if the method was of type void.
Throws:
SecurityException - see Class.getMethod(String, Class...)
NoSuchMethodException - see Class.getMethod(String, Class...)
IllegalAccessException - see Method.invoke(Object, Object...)
IllegalArgumentException - see Method.invoke(Object, Object...)
InvocationTargetException - see Method.invoke(Object, Object...)


Copyright © 2013. All Rights Reserved.