/** * <p>Construct a map of the specified type for use on a POJO field. You can override this with Guice or whatnot.</p> * * <p>The default is to call construct(Class), with one twist - if a Map or SortedMap List interface is presented, * Objectify will construct a HashMap or TreeMap (respectively). If you override this method with * dependency injection and you use uninitialized fields of these interface types in your entity pojos, you will * need to bind these interfaces to concrete types.</p> */ @SuppressWarnings("unchecked") public <T extends Map<?, ?>> T constructMap(final Class<T> type) { if ((Class<?>)type == Map.class) return (T)new HashMap<>(); else if ((Class<?>)type == SortedMap.class) return (T)new TreeMap<>(); else return construct(type); }
/** * <p>Construct a collection of the specified type and the specified size for use on a POJO field. You can override * this with Guice or whatnot.</p> * * <p>The default is to call construct(Class), with one twist - if a Set, SortedSet, or List interface is presented, * Objectify will construct a HashSet, TreeSet, or ArrayList (respectively). If you override this method with * dependency injection and you use uninitialized fields of these interface types in your entity pojos, you will * need to bind these interfaces to concrete types.</p> */ @SuppressWarnings("unchecked") public <T extends Collection<?>> T constructCollection(final Class<T> type, final int size) { if ((Class<?>)type == List.class || (Class<?>)type == Collection.class) return (T)new ArrayList<>(size); else if ((Class<?>)type == Set.class) return (T)new HashSet<>((int)(size * 1.5)); else if ((Class<?>)type == SortedSet.class) return (T)new TreeSet<>(); else return construct(type); }
/** */ public If<?, ?> createIf(Class<? extends If<?, ?>> ifClass, Field field) { If<?, ?> created = fact.construct(ifClass); if (created instanceof InitializeIf) ((InitializeIf)created).init(fact, field); return created; } }
@Override public void init(final ObjectifyFactory fact, final Field field) { final Object pojo = fact.construct(field.getDeclaringClass()); this.defaultValue = TypeUtils.field_get(field, pojo); }
@Override public Translator<Object, Object> create(TypeKey<Object> tk, CreateContext ctx, Path path) { final Translate translateAnno = tk.getAnnotation(Translate.class); if (translateAnno == null) return null; if (earlyOnly && !translateAnno.early()) return null; @SuppressWarnings("unchecked") TranslatorFactory<Object, Object> transFact = (TranslatorFactory<Object, Object>)ctx.getFactory().construct(translateAnno.value()); Translator<Object, Object> trans = transFact.create(tk, ctx, path); if (trans == null) { path.throwIllegalState("TranslatorFactory " + transFact + " was unable to produce a Translator for " + tk.getType()); return null; // never gets here } else { return trans; } } }
final Stringifier<Object> stringifier = (Stringifier<Object>)fact.construct(stringifierClass); if (stringifier instanceof InitializeStringifier) ((InitializeStringifier)stringifier).init(fact, keyType);
final Mapper<Object, Object> mapper = (Mapper<Object, Object>)fact.construct(mapify.value());