Monday, February 16, 2009

Spring Modules Valang

I've been working on adding enhancements to Spring Modules Valang. I've added a number of new features so far and I have two versions uploaded to the Spring by Example Maven Repository (org.springbyexample.validation:sbe-validation). I have it under a different group id and artifact name so there aren't' any collisions in the future with Spring Modules releases.

I'm also working on documentation, but that's going to take a little longer to finish.

Version 0.91

  • Bytecode generation added to DefaultVisitor as a replacement for reflection accessing simple properties (BeanPropertyFunction) for a significant performance improvement.
  • Basic enum comparison support. In the expression below the personType is an enum and the value STUDENT will be convereted to an enum for comparison. The value must match an enum value on the type being compared or an exception will be thrown.

    personType EQUALS ['STUDENT']
    For better performance the full class name can be specified so the enum can be retrieved during parsing. The first example is for standard enum and the second one is for an inner enum class .

    personType EQUALS ['org.springmodules.validation.example.PersonType.STUDENT']

    personType EQUALS ['org.springmodules.validation.example.Person$PersonType.STUDENT']
  • Where clause support. In the expression below, the part of the expression price will only be evaluated if the personType is 'STUDENT'. Otherwise the validation will be skipped.

    price < 100 WHERE personType EQUALS ['STUDENT']
  • Improved performance of 'IN'/'NOT IN' if comparing a value to a java.util.Set it will use Set.contains(value). Static lists of Strings (ex: 'A', 'B', 'C') are now stored in a Set instead of an ArrayList.
  • Functions can be configured in Spring, but need to have their scope set as prototype and use a FunctionWrapper that is also a prototype bean with set on it.
  • Removed servlet dependency from Valang project except for the custom JSP tag ValangValidateTag needing it, but running Valang no longer requires it. This involved removing ServletContextAware from it's custom dependency injection. If someone was using this in a custom function, the function can now be configured directly in Spring and Spring can inject any "aware" values.

Version 0.92

  • Removed custom dependency injection since functions can be configured in Spring.
  • Added auto-discovery of FunctionWrapper beans from the Spring context to go with existing auto-discovery of FunctionDefinition beans.

Generating Bytecode


I've been looking at the Javassist project. It's really nice. It's a higher level API for manipulating and generating bytecode. Below is an example generating a new class that implements and interface, and then implements the interface. By generating this, it avoids reflection and is significantly faster in tests I've run.



ClassPool pool = ClassPool.getDefault();
CtClass cc = pool.makeClass(classFunctionName);

cc.addInterface(pool.get("org.springmodules.validation.valang.functions.Function"));

StringBuilder generatedMethod = new StringBuilder();
generatedMethod.append("public Object getResult(Object target) {");
generatedMethod.append(" return ");
generatedMethod.append(" ((" + className + ")target).");
generatedMethod.append(property);
generatedMethod.append("();");

CtMethod m = CtNewMethod.make(generatedMethod.toString(), cc);
cc.addMethod(m);

result = (Function)cc.toClass().newInstance();

Sunday, February 1, 2009

Spring by Example Update

I've been working for a while on an update to Spring by Example. It just happens to be the 1.00 release so I wanted it to be a little bit bigger. It's over a years worth of work at this point and I think it shows how much work I've put into it. I'm happy that it's helping people and will continue to work on it as I have time. I wish I had more. Especially to keep doing more work with the Spring dm Server. Below is a list of the lastest examples posted and I'm working on other ones that I'll hopefully be able to finalize over the next month.