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; }
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 <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 String superField(String required) { return capitalize(required.substring(0, required.indexOf("."))); }
/** * 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; } }
public Tag addLinks(Tag tag) { ReflectionHelper.assignChildModel(tag, parentType).setId(parentId); return LinkHelper.addLinks(getUriInfo(), tag); }
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 boolean different(Object lhs, Object rhs, String name) { Method lhsm = getMethod(lhs, GET_ROOT + name); Method rhsm = getMethod(rhs, GET_ROOT + name); Object lhsr = lhsm != null ? invoke(lhs, lhsm) : null; Object rhsr = rhsm != null ? invoke(rhs, rhsm) : null; return !(lhsr == null || lhsr.equals(rhsr)); }
/** * Evaluate whether a resource satisfies a given constraint. * * @param resource the resource under test * @param constraint the constraint being impose * @return true iff the constraint is satisfied */ @Override public boolean satisfies(R resource, String constraint) { boolean satisfied = false; try { constraint = constraint.substring(constraint.indexOf(":") + 1).trim(); String[] tokens = constraint.split("="); Object value = ReflectionHelper.get(resource, tokens[0]); satisfied = value instanceof String ? ((String)value).matches(tokens[1].replaceAll("\\*", ".*")) : value instanceof Boolean ? ((Boolean)value).equals(Boolean.parseBoolean(constraint)) : value instanceof Integer ? ((Integer)value).equals(Integer.parseInt(constraint)) : false; } catch (Exception e) { // any parse exception implies a mismatch e.printStackTrace(); } return satisfied; }
Object superType = get(model, superField(r)); if(superType != null) missing.addAll(joinSuperType(doAssertRequired(reason, detail, superType ,frameOffset, subField(r)), superType)); boolean found = false; for (String superField : superField(r).split(ALTERNATIVE)) { found = found || (isSet(model, capitalize(superField)) && assertFields(model, superField, subField(r)));
DiskType type = DiskType.fromValue(disk.getType()); if (type != null) { buf.append(" -disktype " + ReflectionHelper.capitalize(type.name().toLowerCase()));
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()) + ";");