public Object createInstance(Mapper mapr, MappedField mf, DBObject dbObj) { Class c = getClass(dbObj); if (c == null) c = mf.isSingleValue ? mf.getConcreteType() : mf.getSubClass(); try { return createInstance(c, dbObj); } catch (RuntimeException e) { ConstructorArgs argAnn = mf.getAnnotation(ConstructorArgs.class); if (argAnn == null) throw e; //TODO: now that we have a mapr, get the arg types that way by getting the fields by name. + Validate names Object[] args = new Object[argAnn.value().length]; Class[] argTypes = new Class[argAnn.value().length]; for(int i = 0; i < argAnn.value().length; i++) { //TODO: run converters and stuff against these. Kinda like the List of List stuff, using a fake MappedField to hold the value Object val = dbObj.get(argAnn.value()[i]); args[i] = val; argTypes[i] = val.getClass(); } try { Constructor ctor = c.getDeclaredConstructor(argTypes); ctor.setAccessible(true); return ctor.newInstance(args); } catch (Exception ex) { throw new RuntimeException(ex); } } }
public Object createInstance(final Mapper mapper, final MappedField mf, final DBObject dbObj) { Class c = getClass(dbObj); if (c == null) { c = mf.isSingleValue ? mf.getConcreteType() : mf.getSubClass(); } try { return createInstance(c, dbObj); } catch (RuntimeException e) { final ConstructorArgs argAnn = mf.getAnnotation(ConstructorArgs.class); if (argAnn == null) { throw e; } //TODO: now that we have a mapper, get the arg types that way by getting the fields by name. + Validate names final Object[] args = new Object[argAnn.value().length]; final Class[] argTypes = new Class[argAnn.value().length]; for (int i = 0; i < argAnn.value().length; i++) { //TODO: run converters and stuff against these. Kinda like the List of List stuff, using a fake MappedField to hold the value final Object val = dbObj.get(argAnn.value()[i]); args[i] = val; argTypes[i] = val.getClass(); } try { final Constructor constructor = c.getDeclaredConstructor(argTypes); constructor.setAccessible(true); return constructor.newInstance(args); } catch (Exception ex) { throw new RuntimeException(ex); } } }