/** * Compiles an accessor that can be used for fast access for the nested * property of the objects of a given class. * * @since 4.0 */ public static Accessor accessor(String nestedPropertyName) { if (nestedPropertyName == null) { throw new IllegalArgumentException("Null property name."); } if (nestedPropertyName.length() == 0) { throw new IllegalArgumentException("Empty property name."); } // PathAccessor is simply a chain of path segment wrappers. The actual // accessor is resolved (with caching) during evaluation. Otherwise we // won't be able to handle subclasses of declared property types... // TODO: perhaps Java 7 MethodHandles are the answer to truly "compiled" // path accessor? return compilePathAccessor(nestedPropertyName); }
static Accessor compilePathAccessor(String path) { Accessor accessor = PATH_ACCESSORS.get(path); if (accessor == null) { int dot = path.indexOf(Entity.PATH_SEPARATOR); if (dot == 0 || dot == path.length() - 1) { throw new IllegalArgumentException("Invalid path: " + path); } String segment = dot < 0 ? path : path.substring(0, dot); Accessor remainingAccessor = dot < 0 ? null : compilePathAccessor(path.substring(dot + 1)); Accessor newAccessor = new PathAccessor(segment, remainingAccessor); Accessor existingAccessor = PATH_ACCESSORS.putIfAbsent(path, newAccessor); accessor = existingAccessor != null ? existingAccessor : newAccessor; } return accessor; }