views:

314

answers:

3

I was looking for some utility class/code that would take a java bean and initialize all its values to random values. It could be done via reflection as some libraries already create the toString() or equals() methods. Is is useful while developing the UI to have some data for example.

Other possible nice to haves:

  1. recursively initialize non primitive or simple (string, date) members too
  2. initialize a collection of beans
  3. maybe give some way to limit the values generated, for example for numbers we could give ranges, for strings regexps or wildcards...

somebody knows something like this? thanks

EDIT: resolving...Got Apocalisp's sample working and definitively is what I was looking for. It has some drawbacks IMHO:

  • The library has a much larger scope than that use, but this is not a problem for me
  • It's quite complicated to understand how to build the Arbitrary for your objects unless you invest some time to study the whole thing. This is a drawback.
  • And it could be more succint I guess, but that is fine too.

thanks!

A: 

I don't have a library for you but if you get a random generator, then maybe this class can come in handy:

public class BeanMethodIterator implements Iterable<Method> {

    private static final int MODIFIER_FILTER = (Modifier.PUBLIC | Modifier.STATIC);
    private static final int MODIFIER_EXPECTED = Modifier.PUBLIC;

    /**
     * Indicator to filter getter or setter methods. 
     */
    public enum Filter {

        /** Only getter methods. */
        GETTERS(new Transform<Method, Boolean>(){
            public Boolean transform(Method input) {
                return (input.getName().startsWith("get") || 
                        input.getName().startsWith("is")) 
                    &&  input.getParameterTypes().length == 0;
            };
        }),

        /** Only setter methods. */
        SETTTERS(new Transform<Method, Boolean>(){
            public Boolean transform(Method input) {
                return input.getName().startsWith("set") && 
                    input.getParameterTypes().length == 1;
            };
        }),

        /** Getter and setter methods. */
        BOTH(new Transform<Method, Boolean>(){
            public Boolean transform(Method input) {
                return Filter.SETTTERS.condition.transform(input) || 
                    Filter.GETTERS.condition.transform(input);
            };
        });

        private Transform<Method, Boolean> condition;

        private Filter(Transform<Method, Boolean> condition) {
            this.condition = condition;
        }

    };

    /**
     * Iterate parent methods also?
     */
    public enum Scope {
        PARENTS_ALSO() {
            @Override
            protected Method[] getMethods(Class<?> c) {
                return c.getMethods();
            };
        },
        THIS_CLASS_ONLY() {
            @Override
            protected Method[] getMethods(Class<?> c) {
                return c.getDeclaredMethods();
            }
        };

        protected abstract Method[] getMethods(Class<?> c);
    }

    private final Filter filter;
    private final Scope scope;
    private final Class<?> theClass;

    /**
     * Constructor. 
     *
     * @param theClass
     * @param what
     */
    public BeanMethodIterator(Class<?> theClass, Filter what, Scope scope) {
        this.filter = what;
        this.theClass = theClass;
        this.scope = scope;
    }

    /**
     * Constructor. 
     */
    public BeanMethodIterator(Class<?> theClass) {
        this(theClass, Filter.BOTH, Scope.PARENTS_ALSO);
    }

    /**
     * Tells if a method is public
     * @param method
     * @return
     */
    private static boolean isPublic(Method method) {
        return (method.getModifiers() & MODIFIER_FILTER) == MODIFIER_EXPECTED; 
    }

    /**
     * {@inheritDoc}
     * @see java.lang.Iterable#iterator()
     */
    public Iterator<Method> iterator() {
        final Method[] methods = this.scope.getMethods(this.theClass);        

        return new Iterator<Method>() {
            int index = 0;

            public boolean hasNext() {
                while (index < methods.length) {
                    if (isPublic(methods[index]) && filter.condition.transform(methods[index]))
                        return true;
                    index++;
                }
                return false;
            }

            public Method next() {
                if (!hasNext())
                    throw new NoSuchElementException();
                return methods[index++];
            }

            public void remove() {
                throw new UnsupportedOperationException();
            }

        };
    }

    public static void main(String[] args) {
        for (Method m: new BeanMethodIterator(Date.class, Filter.GETTERS, Scope.THIS_CLASS_ONLY)) {
            System.out.println(m.getName());
        }
    }

}


/**
 * Represents a function that takes one input and returns a transformation of that input value i.e.
 * a transformation. The name Transform is used because it is shorter.
 * 
 * @author Hannes de Jager
 * @since 01 Sep 2008
 */
interface Transform<I, O> {

    /**
     * Invokes the function, performing the transformation, to produce an interpreted value.
     * 
     * @param input the input value.
     * @return The computed result.
     */
    public O transform(I input);
}
Hannes de Jager
btw: use it as follows:for(Method m: new BeanMethodIterator(bean.getClass(), BeanMethodIterator.Filter.SETTTERS, BeanMethodIterator.Scope.THIS_CLASS_ONLY)) { // Your code here}
Hannes de Jager
I have to admit after having a fast look at your code I still don't see clearly what it does. Haven't run it yet either. So I'll take some time to play with it.
raticulin
I've updated the code, I see I had some code in there that depended on external code, I've removed the dependencies. I've also added a main method that illustrates the use of the class.
Hannes de Jager
thanks, I have been looking at it. As far as I can see, it serves to iterate over the methods of a given class (with some variations, setters, its own, etc etc). But how is it related to my question? That is, generating random values for the members of a bean.
raticulin
+1  A: 

Take a look at the Gen class from the Reductio library. This is part of a highly configurable framework for generating arbitrary values of more or less any type. Generators for primitive types and most of the Java collections classes are provided. You should be able to create Arbitrary instances for your classes fairly easily.

EDIT Here's the example, corrected:

import static fj.test.Arbitrary.*;
import static fj.Function.*;

static final Arbitrary<Person> personArbitrary =
  arbitrary(arbInteger.gen.bind(arbString.gen, arbBoolean.gen,
      curry(new F3<Integer, String, Boolean, Person>() {
        public Person f(final Integer age, final String name, final Boolean male)
          {return new Person(age, name, male);}})));

Then generate an arbitrary Person of "size 100" like so. I.e. it will have a name of 100 characters at most.

Person p = personArbitrary.gen.gen(100, Rand.standard);
Apocalisp
something like that is exactly what I was looking for, but the syntax to call the example is a bit scary...damm. Let me try it.
raticulin
It's unfortunately verbose, but it's Java, so what can you do. Note that "new F<Integer, F<String, F<Boolean, Person>>>(){...}" can also be written "curry(new F3<Integer, String, Boolean, Person>(){...})". That should be considerably shorter.
Apocalisp
@raticulin If this was the answer could you flag it as such? Then we all know it worked!
extraneon
extraneon: I want to test first, then for sure I will
raticulin
I cannot get this to compile, cannot find any method arbInteger() even in the library source...And wondering wheter it could be further shorthened by using reflection somehow.
raticulin
Hmm, the example is wrong. arbInteger is a static field on the Arbitrary class, not a method.
Apocalisp
Reflection is a cop-out. You lose all static guarantees and you may as well be writing Ruby or Lisp.
Apocalisp
where does Random.standard?? does not compile that part. Is it Rand.standard ? But then I get yet another compile error:The method gen(int, Rand) is undefined for the type Arbitrary<Person>
raticulin
Yes, Rand, sorry. And gen is a method on Gen. You need to get the Gen from the Arbitrary. See revised example.
Apocalisp
A: 

Apache Commons BeanUtils (http://commons.apache.org/beanutils) might be useful for you. There is not any utility you could use out-of-the-box, but I guess all the building blocks are there i.e. access to properties, random generators.

pregzt
I guess you meant 'there is not any utility' right?
raticulin
Yep. You are right :) There is no utility out of the box.
pregzt
I am already using commons-beanutils, but I was looking for something more specific. BTW I use RamdomUtils etc from commons-lang didnt know beanutils had ramdom stuff
raticulin