/** * Create a Bean populating projection for the given type and expressions * * @param <T> * @param type type of the projection * @param exprs arguments for the projection * @return */ public static <T> QBean<T> bean(Path<T> type, Expression<?>... exprs) { return new QBean<T>(type, exprs); }
@Override public Beans newInstance(Object... args) { int offset = 0; Map<RelationalPath<?>, Object> beans = new HashMap<RelationalPath<?>, Object>(); for (Map.Entry<RelationalPath<?>, QBean<?>> entry : qBeans.entrySet()) { RelationalPath<?> path = entry.getKey(); QBean<?> qBean = entry.getValue(); int argsSize = qBean.getArgs().size(); Object[] subArgs = ArrayUtils.subarray(args, offset, offset + argsSize); beans.put(path, qBean.newInstance(subArgs)); offset += argsSize; } return new Beans(beans); }
private List<Field> initFields(Map<String, ? extends Expression<?>> args) { List<Field> fields = new ArrayList<Field>(args.size()); for (Map.Entry<String,? extends Expression<?>> entry : args.entrySet()) { String property = entry.getKey(); Expression<?> expr = entry.getValue(); Class<?> beanType = getType(); Field field = null; while (!beanType.equals(Object.class)) { try { field = beanType.getDeclaredField(property); field.setAccessible(true); if (!normalize(field.getType()).isAssignableFrom(expr.getType())) { typeMismatch(field.getType(), expr); } beanType = Object.class; } catch (SecurityException e) { // do nothing } catch (NoSuchFieldException e) { beanType = beanType.getSuperclass(); } } if (field == null) { propertyNotFound(expr, property); } fields.add(field); } return fields; }
/** * Create a new QBean instance * * @param type * @param fieldAccess * @param bindings */ public QBean(Class<T> type, boolean fieldAccess, Map<String, ? extends Expression<?>> bindings) { super(type); this.bindings = ImmutableMap.copyOf(bindings); this.fieldAccess = fieldAccess; if (fieldAccess) { this.fields = initFields(bindings); this.setters = ImmutableList.of(); } else { this.fields = ImmutableList.of(); this.setters = initMethods(bindings); } }
@Override public T newInstance(Object... a) { try { T rv = create(getType()); if (fieldAccess) { for (int i = 0; i < a.length; i++) { Object value = a[i]; if (value != null) { Field field = fields.get(i); if (field != null) field.set(rv, value); } } } else { for (int i = 0; i < a.length; i++) { Object value = a[i]; if (value != null) { Method setter = setters.get(i); if (setter != null) setter.invoke(rv, value); } } } return rv; } catch (InstantiationException e) { throw new ExpressionException(e.getMessage(), e); } catch (IllegalAccessException e) { throw new ExpressionException(e.getMessage(), e); } catch (InvocationTargetException e) { throw new ExpressionException(e.getMessage(), e); } }
/** * Create a new QBean instance * * @param type * @param fieldAccess * @param args */ public QBean(Class<T> type, boolean fieldAccess, Expression<?>... args) { this(type, fieldAccess, createBindings(args)); }
/** * Create an alias for the expression * * @return */ @SuppressWarnings("unchecked") public Expression<T> as(Path<T> alias) { return OperationImpl.create(getType(),Ops.ALIAS, this, alias); }
public Iterable<T> iterable( Iterable<Map<String,Object>> result ) { List<T> entities = new ArrayList<T>( ); for( Map<String, Object> stringObjectMap : result ) { Object[] args = new Object[stringObjectMap.size()]; int idx = 0; for (Expression<?> expression : bean.getArgs()) { args[idx++] = stringObjectMap.get(((Path)expression).getMetadata().getExpression().toString()); } entities.add( bean.newInstance( args ) ); } return entities; } }
private List<Method> initMethods(Map<String, ? extends Expression<?>> args) { try { List<Method> methods = new ArrayList<Method>(args.size()); BeanInfo beanInfo = Introspector.getBeanInfo(getType()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (Map.Entry<String, ? extends Expression<?>> entry : args.entrySet()) { String property = entry.getKey(); Expression<?> expr = entry.getValue(); Method setter = null; for (PropertyDescriptor prop : propertyDescriptors) { if (prop.getName().equals(property)) { setter = prop.getWriteMethod(); if (!normalize(prop.getPropertyType()).isAssignableFrom(expr.getType())) { typeMismatch(prop.getPropertyType(), expr); } break; } } if (setter == null) { propertyNotFound(expr, property); } methods.add(setter); } return methods; } catch (IntrospectionException e) { throw new RuntimeException(e.getMessage(), e); } }
/** * Create a field access based Bean populating projection for the given type and expressions * * @param <T> * @param type type of the projection * @param exprs arguments for the projection * @return */ public static <T> QBean<T> fields(Path<T> type, Expression<?>... exprs) { return new QBean<T>(type, true, exprs); }
/** * Create a Bean populating projection for the given type and expressions * * <p>Example</p> * <pre> * UserDTO dto = query.singleResult( * Projections.bean(UserDTO.class, user.firstName, user.lastName)); * </pre> * * @param <T> * @param type type of the projection * @param exprs arguments for the projection * @return */ public static <T> QBean<T> bean(Class<T> type, Expression<?>... exprs) { return new QBean<T>(type, exprs); }
/** * Create a field access based Bean populating projection for the given type and expressions * * <p>Example</p> * <pre> * UserDTO dto = query.singleResult( * Projections.fields(UserDTO.class, user.firstName, user.lastName)); * </pre> * * @param <T> * @param type type of the projection * @param exprs arguments for the projection * @return */ public static <T> QBean<T> fields(Class<T> type, Expression<?>... exprs) { return new QBean<T>(type, true, exprs); }
@SuppressWarnings("unchecked") public QBeans(RelationalPath<?>... beanPaths) { super(Beans.class); try { final ImmutableList.Builder<Expression<?>> listBuilder = ImmutableList.builder(); final ImmutableMap.Builder<RelationalPath<?>, QBean<?>> mapBuilder = ImmutableMap.builder(); for (RelationalPath<?> path : beanPaths) { Map<String, Expression<?>> bindings = new LinkedHashMap<String, Expression<?>>(); for (Path<?> column : path.getColumns()) { bindings.put(column.getMetadata().getName(), column); listBuilder.add(column); } mapBuilder.put(path, new QBean<Object>((Class)path.getType(), bindings)); } expressions = listBuilder.build(); qBeans = mapBuilder.build(); } catch (Exception e) { throw new IllegalStateException(e); } }
private static <T> FactoryExpression<T> createBeanProjection(RelationalPath<T> path) { Map<String,Expression<?>> bindings = new LinkedHashMap<String,Expression<?>>(); for (Path<?> column : path.getColumns()) { bindings.put(column.getMetadata().getName(), column); } if (bindings.isEmpty()) { throw new IllegalArgumentException("No bindings could be derived from " + path); } return new QBean<T>((Class)path.getType(), true, bindings); }