My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
Limits  
usage limits and a little detail into how it works
Updated Aug 10, 2010 by codeth...@gmail.com

Like all things this library has some limits. In fact this library has some very serious limits, this is mainly due to how it records a script of method calls and then replays the method calls on each element in the iterables.

  • Unable to proxy final classes
  • Unable to proxy classes without a default constructor

These are mainly, and not unreasonable limits of the cglib class proxying system. In practice it means you have to be a little bit careful with what methods you call and more importantly what types they return.

For example:

select(people, where(Person.class).getFirstName(), equalTo("jon"));

Is pretty reasonable where(Person.class).getFirstName() is where the magic is happening. Lets break it down:

First the where(Class.class) returns a proxy instance of the Person.class

Person p = where(Person.class) ;

The method call on the proxy (p) and records call. The return type for getFirstName() is java.lang.String this is of course a final class, it can not be proxied.

String value = p.getFirstName() ;

When called the getFirstName() method will actually return null, it doesn't have a suitable value and it can't create a proxy so null seems like a good idea. This means that the following is actually going to throw a NullPointerException

// attempt to find all the people with beginning with 's'...is going die in a big bang
select(people, where(Person.class).getFirstName().substring(0,1), equalTo("s"));

// in order to overcome this just use a different matcher
select(people, where(Person.class).getFirstName(), startsWidth("s"));
Comment by nat.pr...@gmail.com, Oct 15, 2008

You can use Objenesis to get round the need for a default constructor.


Sign in to add a comment
Powered by Google Project Hosting