Discuss / Java / 交作业

交作业

Topic source

墨染尘霞

#1 Created at ... [Delete] [Delete and Lock User]
static void check(Person person) throws IllegalArgumentException, ReflectiveOperationException {
        for (Field field : person.getClass().getFields()) {
            Range range = field.getAnnotation(Range.class);
            if (range != null) {
                Object value = field.get(person);
                // TODO:
                if (value instanceof String s) {
                    if (s.length() < range.min() || s.length() > range.max()) {
                        throw new IllegalArgumentException("String");
                    }
                } else if (value instanceof Integer i) {
                    if (i < range.min() || i > range.max()) {
                        throw new IllegalArgumentException("Integer");
                    }
                }
            }
        }
    }

  • 1

Reply