list.add(new ReflectiveServiceLayer());
@Override public Object getProperty(Object domainObject, String property) { try { Method getter = getTop().getGetter(domainObject.getClass(), property); if (getter == null) { die(null, "Could not determine getter for property %s on type %s", property, domainObject .getClass().getCanonicalName()); } Object value = getter.invoke(domainObject); return value; } catch (IllegalAccessException e) { return die(e, "Could not retrieve property %s", property); } catch (InvocationTargetException e) { return report(e); } }
@Override public Method getSetter(Class<?> domainType, String property) { Method setter = getBeanMethod(BeanMethod.SET, domainType, property); if (setter == null) { setter = getBeanMethod(BeanMethod.SET_BUILDER, domainType, property); } return setter; }
private Method getFind(Class<?> clazz) { if (clazz == null) { return die(null, "Could not find static method with a single" + " parameter of a key type"); } String searchFor = "find" + clazz.getSimpleName(); for (Method method : clazz.getMethods()) { if (!Modifier.isStatic(method.getModifiers())) { continue; } if (!searchFor.equals(method.getName())) { continue; } if (method.getParameterTypes().length != 1) { continue; } if (!isKeyType(method.getParameterTypes()[0])) { continue; } return method; } return getFind(clazz.getSuperclass()); }
@Override public <T> T createDomainObject(Class<T> clazz) { Throwable ex; try { Constructor<T> c = clazz.getConstructor(); c.setAccessible(true); return c.newInstance(); } catch (InstantiationException e) { e.printStackTrace(); return this.<T> report("Could not create a new instance of the requested type"); } catch (NoSuchMethodException e) { return this.<T> report("The requested type is not default-instantiable"); } catch (InvocationTargetException e) { return this.<T> report(e); } catch (IllegalAccessException e) { ex = e; } catch (SecurityException e) { ex = e; } catch (IllegalArgumentException e) { ex = e; } return this.<T> die(ex, "Could not create a new instance of domain type %s", clazz .getCanonicalName()); }
@Override public List<Object> loadDomainObjects(List<Class<?>> classes, List<Object> domainIds) { if (classes.size() != domainIds.size()) { die(null, "Size mismatch in paramaters. classes.size() = %d domainIds.size=%d", classes .size(), domainIds.size()); } List<Object> toReturn = new ArrayList<Object>(classes.size()); Iterator<Class<?>> classIt = classes.iterator(); Iterator<Object> idIt = domainIds.iterator(); while (classIt.hasNext()) { toReturn.add(getTop().loadDomainObject(classIt.next(), idIt.next())); } return toReturn; }
@Override public Object getVersion(Object domainObject) { return getTop().getProperty(domainObject, "version"); }
@Override public Class<?> getIdType(Class<?> domainType) { return getFind(domainType).getParameterTypes()[0]; }
@Override public Type getRequestReturnType(Method contextMethod) { Class<?> returnClass = contextMethod.getReturnType(); if (InstanceRequest.class.isAssignableFrom(returnClass)) { Type[] params = TypeUtils .getParameterization(InstanceRequest.class, contextMethod.getGenericReturnType()); assert params.length == 2; return params[1]; } else if (Request.class.isAssignableFrom(returnClass)) { Type param = TypeUtils.getSingleParameterization(Request.class, contextMethod.getGenericReturnType()); return param; } else { return die(null, "Unknown RequestContext return type %s", returnClass.getCanonicalName()); } }
private Method getFind(Class<?> clazz) { if (clazz == null) { return die(null, "Could not find static method with a single" + " parameter of a key type"); } String searchFor = "find" + clazz.getSimpleName(); for (Method method : clazz.getMethods()) { if (!Modifier.isStatic(method.getModifiers())) { continue; } if (!searchFor.equals(method.getName())) { continue; } if (method.getParameterTypes().length != 1) { continue; } if (!isKeyType(method.getParameterTypes()[0])) { continue; } return method; } return getFind(clazz.getSuperclass()); }
@Override public Object invoke(Method domainMethod, Object... args) { Throwable ex; try { domainMethod.setAccessible(true); if (Modifier.isStatic(domainMethod.getModifiers())) { return domainMethod.invoke(null, args); } else { Object[] realArgs = new Object[args.length - 1]; System.arraycopy(args, 1, realArgs, 0, realArgs.length); return domainMethod.invoke(args[0], realArgs); } } catch (IllegalArgumentException e) { ex = e; } catch (IllegalAccessException e) { ex = e; } catch (InvocationTargetException e) { return report(e); } return die(ex, "Could not invoke method %s", domainMethod.getName()); }
@Override public List<Object> loadDomainObjects(List<Class<?>> classes, List<Object> domainIds) { if (classes.size() != domainIds.size()) { die(null, "Size mismatch in paramaters. classes.size() = %d domainIds.size=%d", classes .size(), domainIds.size()); } List<Object> toReturn = new ArrayList<Object>(classes.size()); Iterator<Class<?>> classIt = classes.iterator(); Iterator<Object> idIt = domainIds.iterator(); while (classIt.hasNext()) { toReturn.add(getTop().loadDomainObject(classIt.next(), idIt.next())); } return toReturn; }
@Override public Object getId(Object domainObject) { return getTop().getProperty(domainObject, "id"); }
@Override public Class<?> getIdType(Class<?> domainType) { return getFind(domainType).getParameterTypes()[0]; }
@Override public Type getRequestReturnType(Method contextMethod) { Class<?> returnClass = contextMethod.getReturnType(); if (InstanceRequest.class.isAssignableFrom(returnClass)) { Type[] params = TypeUtils .getParameterization(InstanceRequest.class, contextMethod.getGenericReturnType()); assert params.length == 2; return params[1]; } else if (Request.class.isAssignableFrom(returnClass)) { Type param = TypeUtils.getSingleParameterization(Request.class, contextMethod.getGenericReturnType()); return param; } else { return die(null, "Unknown RequestContext return type %s", returnClass.getCanonicalName()); } }
@Override public void setProperty(Object domainObject, String property, Class<?> expectedType, Object value) { try { Method setter = getTop().getSetter(domainObject.getClass(), property); if (setter == null) { die(null, "Could not locate setter for property %s in type %s", property, domainObject .getClass().getCanonicalName()); } setter.invoke(domainObject, value); return; } catch (IllegalAccessException e) { die(e, "Could not set property %s", property); } catch (InvocationTargetException e) { report(e); } }