/** * Select a field from given class's declared field as a test target. * * @throws RejectedChoice If class has no fields to choose from. */ protected Field targetFieldFrom(Class<?> clazz, FieldGetMethod howToGet) throws RejectedChoice { Field[] fields = null; switch (howToGet) { case GET_DECLARED_FIELD: case GET_DECLARED_FIELDS: fields = ReflectiveInterceptor.jlClassGetDeclaredFields(clazz); break; case GET_FIELD: case GET_FIELDS: fields = ReflectiveInterceptor.jlClassGetFields(clazz); break; } // To be deterministic we must sort these in a predictable fashion! Arrays.sort(fields, new ToStringComparator()); Field f = choice(fields); toStringValue.append(f.getName()); try { switch (howToGet) { case GET_DECLARED_FIELDS: case GET_FIELDS: return f; case GET_DECLARED_FIELD: return ReflectiveInterceptor.jlClassGetDeclaredField(clazz, f.getName()); case GET_FIELD: return ReflectiveInterceptor.jlClassGetField(clazz, f.getName()); } } catch (Exception e) { throw new Error(e); } return f; }
/** * Select a Constructor from given class's declared constructors as a test target. * * @throws RejectedChoice If class has no Constructors to choose from. */ protected Constructor<?> targetConstructorFrom(Class<?> clazz) throws RejectedChoice { Constructor<?>[] constructors = ReflectiveInterceptor.jlClassGetDeclaredConstructors(clazz); // To be deterministic we must sort these methods in a predictable fashion! Arrays.sort(constructors, new ToStringComparator()); Constructor<?> c = choice(constructors); toStringValue.append(c); return c; }
/** * Select a method from given class's declared methods as a test target. * * @throws RejectedChoice If class has no methods to choose from. */ protected Method targetMethodFrom(Class<?> targetClass) throws RejectedChoice { Method[] methods = ReflectiveInterceptor.jlClassGetDeclaredMethods(targetClass); // To be deterministic we must sort these methods in a predictable fashion! Otherwise the test // may compare results from one method in the first run with those of another method in the // second // run and fail. Arrays.sort(methods, new ToStringComparator()); Method method = choice(methods); toStringValue.append(method); return method; }