private static String superField(String required) { return capitalize(required.substring(0, required.indexOf("."))); }
public static Object get(Object o, String field) { Object ret = null; String name = GET_ROOT + capitalize(field); for (Method m : o.getClass().getMethods()) { if (m.getName().equals(name)) { try { ret = m.invoke(o); } catch (Exception e) { // InvocationTargetException etc. should never occur // as this is a simple getter } break; } } return ret; }
public static <R extends BaseResource> R assignChildModel(BaseResource parent, Class<R> childType) { Method setter = getMethod(parent, SET_ROOT + capitalize(childType.getSimpleName().toLowerCase())); R child = newModel(childType); try { setter.invoke(parent, child); } catch (Exception e) { // InvocationTargetException etc. should never occur // as this is a simple setter } return child; }
private static boolean assertFields(Object model, String superField, String subFields) { String[] splitFields = subFields.split(ALTERNATIVE); boolean found = false; for (String subField : splitFields) { found = found || isSet(superField != null ? get(model, superField) : model, capitalize(subField)); } return found; }
public static boolean set(Object o, String field, String value) { boolean success = false; String name = SET_ROOT + capitalize(field); for (Method m : o.getClass().getMethods()) { if (m.getName().equals(name) && isPrimitive(m)) { Object arg = isString(m) ? value : isBoolean(m) ? Boolean.parseBoolean(value) : isInteger(m) ? Integer.parseInt(value) : null; if (arg != null) { try { m.invoke(o, arg); success = true; } catch (Exception e) { // InvocationTargetException etc. should never occur // as this is a simple getter } } break; } } return success; }
/** * Impose immutability constraints. * * @param <T> representation type * @param strict array of strictly immutable field names * @param incoming incoming representation * @param existing existing representation * @param reason the fault reason * @param detail the fault detail * @return error Response if appropriate */ public static <T extends BaseResource> Response imposeConstraints(String[] strict, T incoming, T existing, String reason, String detail) { for (String s: strict) { String field = capitalize(s); if (isSet(incoming, field) && different(incoming, existing, field)) { Fault fault = new Fault(); fault.setReason(reason); fault.setDetail(MessageFormat.format(detail, s)); return Response.status(BROKEN_CONSTRAINT_STATUS) .entity(fault) .build(); } } return null; } }
DiskType type = DiskType.fromValue(disk.getType()); if (type != null) { buf.append(" -disktype " + ReflectionHelper.capitalize(type.name().toLowerCase()));
boolean found = false; for (String superField : superField(r).split(ALTERNATIVE)) { found = found || (isSet(model, capitalize(superField)) && assertFields(model, superField, subField(r)));
VmType vmType = VmType.fromValue(template.getType()); if (vmType != null) { buf.append(" -vmtype " + ReflectionHelper.capitalize(vmType.value()));
VmType vmType = VmType.fromValue(vm.getType()); if (vmType != null) { buf.append(" -vmtype " + ReflectionHelper.capitalize(vmType.value()));
VmType vmType = VmType.fromValue(template.getType()); if (vmType != null) { buf.append("$t.vmtype = " + ReflectionHelper.capitalize(vmType.value()) + ";");
VmType vmType = VmType.fromValue(vm.getType()); if (vmType != null) { buf.append("$v.vmtype = " + ReflectionHelper.capitalize(vmType.value()) + ";");