Giter Club home page Giter Club logo

fest-reflect's Introduction

FEST-Reflect provides an intuitive, compact and type-safe fluent API that makes Java reflection tremendously easy to use: no more casting, checked exceptions, PriviledgedActions or calls to setAccessible. FEST’s reflection module can even overcome the limitations of generics and type erasure.

Example:

// import static org.fest.reflect.core.Reflection.*;

// 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 = innerClass("Master").in(Jedi.class).get();

// Equivalent to invoking 'new Person()'
Person p = constructor().in(Person.class).newInstance();

// Equivalent to invoking '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(new TypeRef<List<String>>() {}).in(jedi).get();

// Equivalent to invoking the method 'person.setName("Luke")'
method("setName").withParameterTypes(String.class)
                 .in(person)
                 .invoke("Luke");

// Equivalent to invoking the method 'jedi.getPowers()'
List<String> powers = method("getPowers").withReturnType(new TypeRef<List<String>>() {})
                                         .in(person)
                                         .invoke();

// Retrieves the value of the static field "count" in Person.class
int count = field("count").ofType(int.class).in(Person.class).get();

// Sets the value of the static field "count" to 3 in Person.class
field("count").ofType(int.class).in(Person.class).set(3);

// Retrieves the value of the static field "commonPowers" in Jedi.class
List<String> commmonPowers = field("commonPowers").ofType(new TypeRef<List<String>>() {}).in(Jedi.class).get();

// Equivalent to invoking the method 'person.concentrate()'
method("concentrate").in(person).invoke();

// Equivalent to invoking the method 'person.getName()'
String name = method("getName").withReturnType(String.class)
                               .in(person)
                               .invoke();

// Equivalent to invoking the static method 'Jedi.setCommonPower("Jump")'
method("setCommonPower").withParameterTypes(String.class)
                        .in(Jedi.class)
                        .invoke("Jump");

// Equivalent to invoking the static method 'Jedi.addPadawan()'
method("addPadawan").in(Jedi.class).invoke();

// Equivalent to invoking the static method 'Jedi.commonPowerCount()'
String name = method("commonPowerCount").withReturnType(String.class)
                                        .in(Jedi.class)
                                        .invoke();

// Equivalent to invoking the static method 'Jedi.getCommonPowers()'
List<String> powers = method("getCommonPowers").withReturnType(new TypeRef<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");

fest-reflect's People

Contributors

alexruiz avatar ihr avatar joel-costigliola avatar thomedw avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.