public final class Reflection extends Object
The following is an example of proper usage of the classes in this package:
// Loads the class 'org.republic.Jedi' Class<?> jediType =type
("org.republic.Jedi").load
(); // Loads the class 'org.republic.Jedi' as 'org.republic.Person' (Jedi extends Person) Class<Person> jediType =type
("org.republic.Jedi").loadAs
(Person.class); // Loads the class 'org.republic.Jedi' using a custom class loader Class<?> jediType =type
("org.republic.Jedi").withClassLoader
(myClassLoader).load
(); // Gets the inner class 'Master' in the declaring class 'Jedi': Class<?> masterClass =staticInnerClass
("Master").in
(Jedi.class).get
(); // Equivalent to call 'new Person()' Person p =constructor
().in
(Person.class).newInstance
(); // Equivalent to call 'new Person("Yoda")' Person p =constructor
().withParameterTypes
(String.class).in
(Person.class).newInstance
("Yoda"); // Retrieves the value of the field "name" String name =field
("name").ofType
(String.class).in
(person).get
(); // Sets the value of the field "name" to "Yoda"field
("name").ofType
(String.class).in
(person).set
("Yoda"); // Retrieves the value of the field "powers" List<String> powers =field
("powers").ofType
(newTypeRef
<List<String>>() {}).in
(jedi).get
(); // Equivalent to call 'person.setName("Luke")'method
("setName").withParameterTypes
(String.class) .in
(person) .invoke
("Luke"); // Equivalent to call 'jedi.getPowers()' List<String> powers =method
("getPowers").withReturnType
(newTypeRef
<List<String>>() {}) .in
(person) .invoke
(); // Retrieves the value of the static field "count" in Person.class int count =staticField
("count").ofType
(int.class).in
(Person.class).get
(); // Sets the value of the static field "count" to 3 in Person.classstaticField
("count").ofType
(int.class).in
(Person.class).set
(3); // Retrieves the value of the static field "commonPowers" in Jedi.class List<String> commmonPowers =staticField
("commonPowers").ofType
(newTypeRef
<List<String>>() {}).in
(Jedi.class).get
(); // Equivalent to call 'person.concentrate()'method
("concentrate").in
(person).invoke
(); // Equivalent to call 'person.getName()' String name =method
("getName").withReturnType
(String.class) .in
(person) .invoke
(); // Equivalent to call 'Jedi.setCommonPower("Jump")'staticMethod
("setCommonPower").withParameterTypes
(String.class) .in
(Jedi.class) .invoke
("Jump"); // Equivalent to call 'Jedi.addPadawan()'staticMethod
("addPadawan").in
(Jedi.class).invoke
(); // Equivalent to call 'Jedi.commonPowerCount()' String name =staticMethod
("commonPowerCount").withReturnType
(String.class) .in
(Jedi.class) .invoke
(); // Equivalent to call 'Jedi.getCommonPowers()' List<String> powers =staticMethod
("getCommonPowers").withReturnType
(newTypeRef
<List<String>>() {}) .in
(Jedi.class) .invoke
(); // Retrieves the value of the property "name" String name =property
("name").ofType
(String.class).in
(person).get
(); // Sets the value of the property "name" to "Yoda"property
("name").ofType
(String.class).in
(person).set
("Yoda");
Modifier and Type | Method and Description |
---|---|
static TargetType |
constructor()
Starting point of the fluent interface for invoking constructors via reflection.
|
static FieldName |
field(String name)
Starting point of the fluent interface for accessing fields via reflection.
Nested field are supported with dot notation, e.g. |
static MethodName |
method(String name)
Starting point of the fluent interface for invoking methods via reflection.
|
static PropertyName |
property(String name)
Starting point of the fluent interface for accessing properties via Bean Introspection.
|
static StaticFieldName |
staticField(String name)
Starting point of the fluent interface for accessing static fields via reflection.
|
static StaticInnerClassName |
staticInnerClass(String name)
Starting point of the fluent interface for accessing static inner class via reflection.
|
static StaticMethodName |
staticMethod(String name)
Starting point of the fluent interface for invoking static methods via reflection.
|
static Type |
type(String name)
Starting point of the fluent interface for loading a class dynamically.
|
public static Type type(String name)
name
- the name of the class to load.NullPointerException
- if the given name is null
.IllegalArgumentException
- if the given name is empty.public static StaticInnerClassName staticInnerClass(String name)
name
- the name of the static inner class to access.NullPointerException
- if the given name is null
.IllegalArgumentException
- if the given name is empty.public static FieldName field(String name)
"person.address.street"
.
Let's look how it works on an example :
Let's say we have the following simple service: public class BusinessService { private NotificationService notificationService = new NotificationService(); //... logic goes here } Where NotificationService is defined as follows: public class NotificationService { private Logger logger = new Logger(); private IClientStatusDao clientStatusDao = new ClientStatusDao(); //... logic goes here } And our ClientStatusDao looks like: public class ClientStatusDao implements IClientStatusDao { private final Session session = new SessionImpl(); //... logic goes here } Let's say we want to change thelogger
field ofNotificationService
within our instance ofBusinessService
, we can do this: field("notificationService.logger").ofType(Logger.class).in(businessService).set(loggerMock); You can also set the deeply nestedsession
field withinClientStatusDao
like this: field("notificationService.clientStatusDao.session").ofType(Session.class).in(businessService).set(sessionMock);
name
- the name of the field to access.NullPointerException
- if the given name is null
.IllegalArgumentException
- if the given name is empty.public static StaticFieldName staticField(String name)
name
- the name of the static field to access.NullPointerException
- if the given name is null
.IllegalArgumentException
- if the given name is empty.public static MethodName method(String name)
name
- the name of the method to invoke.NullPointerException
- if the given name is null
.IllegalArgumentException
- if the given name is empty.public static StaticMethodName staticMethod(String name)
name
- the name of the static method to invoke.NullPointerException
- if the given name is null
.IllegalArgumentException
- if the given name is empty.public static TargetType constructor()
public static PropertyName property(String name)
name
- the name of the property to access.NullPointerException
- if the given name is null
.IllegalArgumentException
- if the given name is empty.Copyright © 2007–2016. All rights reserved.