| 1 |
<?xml version="1.0" encoding="UTF-8" ?> |
| 2 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 3 |
<html xmlns="http://www.w3.org/1999/xhtml"> |
| 4 |
<head> |
| 5 |
<!-- |
| 6 |
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 7 |
in compliance with the License. You may obtain a copy of the License at |
| 8 |
|
| 9 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
|
| 11 |
Unless required by applicable law or agreed to in writing, software distributed under the License |
| 12 |
is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 13 |
or implied. See the License for the specific language governing permissions and limitations under |
| 14 |
the License. |
| 15 |
|
| 16 |
Copyright @2007-2009 the original author or authors. |
| 17 |
--> |
| 18 |
</head> |
| 19 |
<body bgcolor="white"> |
| 20 |
<p> |
| 21 |
Provides a "fluent" API that |
| 22 |
makes usage of the Java Reflection API easier and improves code readability. |
| 23 |
</p> |
| 24 |
<p> |
| 25 |
Here are some examples: |
| 26 |
<pre> |
| 27 |
// import static {@link org.fest.reflect.core.Reflection org.fest.reflect.core.Reflection}.*; |
| 28 |
|
| 29 |
// Loads the class 'org.republic.Jedi' |
| 30 |
Class<?> jediType = {@link org.fest.reflect.core.Reflection#type(String) type}("org.republic.Jedi").{@link org.fest.reflect.type.Type#load() load}(); |
| 31 |
|
| 32 |
// Loads the class 'org.republic.Jedi' as 'org.republic.Person' (Jedi extends Person) |
| 33 |
Class<Person> jediType = {@link org.fest.reflect.core.Reflection#type(String) type}("org.republic.Jedi").{@link org.fest.reflect.type.Type#loadAs(Class) loadAs}(Person.class); |
| 34 |
|
| 35 |
// Loads the class 'org.republic.Jedi' using a custom class loader |
| 36 |
Class<?> jediType = {@link org.fest.reflect.core.Reflection#type(String) type}("org.republic.Jedi").{@link org.fest.reflect.type.Type#withClassLoader(ClassLoader) withClassLoader}(myClassLoader).{@link org.fest.reflect.type.TypeLoader#load() load}(); |
| 37 |
|
| 38 |
// Gets the inner class 'Master' in the declaring class 'Jedi': |
| 39 |
Class<?> masterClass = {@link org.fest.reflect.core.Reflection#staticInnerClass(String) staticInnerClass}("Master").{@link org.fest.reflect.innerclass.StaticInnerClassName#in(Class) in}(Jedi.class).{@link org.fest.reflect.innerclass.Invoker#get() get}(); |
| 40 |
|
| 41 |
// Equivalent to call 'new Person()' |
| 42 |
Person p = {@link org.fest.reflect.core.Reflection#constructor() constructor}().{@link org.fest.reflect.constructor.TargetType#in in}(Person.class).{@link org.fest.reflect.constructor.Invoker#newInstance(Object...) newInstance}(); |
| 43 |
|
| 44 |
// Equivalent to call 'new Person("Yoda")' |
| 45 |
Person p = {@link org.fest.reflect.core.Reflection#constructor() constructor}().{@link org.fest.reflect.constructor.TargetType#withParameterTypes(Class...) withParameterTypes}(String.class).{@link org.fest.reflect.constructor.ParameterTypes#in(Class) in}(Person.class).{@link org.fest.reflect.constructor.Invoker#newInstance(Object...) newInstance}("Yoda"); |
| 46 |
|
| 47 |
// Retrieves the value of the field "name" |
| 48 |
String name = {@link org.fest.reflect.core.Reflection#field(String) field}("name").{@link org.fest.reflect.field.FieldName#ofType(Class) ofType}(String.class).{@link org.fest.reflect.field.FieldType#in(Object) in}(person).{@link org.fest.reflect.field.Invoker#get() get}(); |
| 49 |
|
| 50 |
// Sets the value of the field 'name' to "Yoda" |
| 51 |
{@link org.fest.reflect.core.Reflection#field(String) field}("name").{@link org.fest.reflect.field.FieldName#ofType(Class) ofType}(String.class).{@link org.fest.reflect.field.FieldType#in(Object) in}(person).{@link org.fest.reflect.field.Invoker#set(Object) set}("Yoda"); |
| 52 |
|
| 53 |
// Equivalent to call 'jedi.getPowers()' |
| 54 |
List<String> powers = {@link org.fest.reflect.core.Reflection#method(String) method}("getPowers").{@link org.fest.reflect.method.MethodName#withReturnType(org.fest.reflect.reference.TypeRef) withReturnType}(new {@link org.fest.reflect.reference.TypeRef TypeRef}<List<String>>() {}) |
| 55 |
.{@link org.fest.reflect.method.MethodReturnTypeRef#in(Object) in}(person) |
| 56 |
.{@link org.fest.reflect.method.Invoker#invoke(Object...) invoke}(); |
| 57 |
|
| 58 |
// Equivalent to call 'person.setName("Luke")' |
| 59 |
{@link org.fest.reflect.core.Reflection#method(String) method}("setName").{@link org.fest.reflect.method.MethodName#withParameterTypes(Class...) withParameterTypes}(String.class) |
| 60 |
.{@link org.fest.reflect.method.MethodParameterTypes#in(Object) in}(person) |
| 61 |
.{@link org.fest.reflect.method.Invoker#invoke(Object...) invoke}("Luke"); |
| 62 |
|
| 63 |
// Equivalent to call 'person.concentrate()' |
| 64 |
{@link org.fest.reflect.core.Reflection#method(String) method}("concentrate").{@link org.fest.reflect.method.MethodName#in(Object) in}(person).{@link org.fest.reflect.method.Invoker#invoke(Object...) invoke}(); |
| 65 |
|
| 66 |
// Equivalent to call 'person.getName()' |
| 67 |
String name = {@link org.fest.reflect.core.Reflection#method(String) method}("getName").{@link org.fest.reflect.method.MethodName#withReturnType(Class) withReturnType}(String.class) |
| 68 |
.{@link org.fest.reflect.method.MethodReturnType#in(Object) in}(person) |
| 69 |
.{@link org.fest.reflect.method.Invoker#invoke(Object...) invoke}(); |
| 70 |
|
| 71 |
// Equivalent to call 'Jedi.getCommonPowers()' |
| 72 |
List<String> powers = {@link org.fest.reflect.core.Reflection#staticMethod(String) staticMethod}("getCommonPowers").{@link org.fest.reflect.method.StaticMethodName#withReturnType(org.fest.reflect.reference.TypeRef) withReturnType}(new {@link org.fest.reflect.reference.TypeRef TypeRef}<List<String>>() {}) |
| 73 |
.{@link org.fest.reflect.method.StaticMethodReturnTypeRef#in(Class) in}(Jedi.class) |
| 74 |
.{@link org.fest.reflect.method.Invoker#invoke(Object...) invoke}(); |
| 75 |
</pre> |
| 76 |
</p> |
| 77 |
</body> |
| 78 |
</html> |