/** * Checks for an implementation. * * @param targetClass a class instance * @param fieldName name of a field (different from constructor) * @return this Builder for method chaining * @see java.lang.Class#forName(String) * @see java.lang.Class#getField(String) */ public Builder impl(Class<?> targetClass, String fieldName) { // don't do any work if an implementation has been found if (field != null || targetClass == null) { return this; } try { this.field = new UnboundField<>( targetClass.getField(fieldName), fieldName); } catch (NoSuchFieldException e) { // not the right implementation candidates.add(targetClass.getName() + "." + fieldName); } return this; }
/** * Checks for a hidden implementation. * * @param targetClass a class instance * @param fieldName name of a field (different from constructor) * @return this Builder for method chaining * @see java.lang.Class#forName(String) * @see java.lang.Class#getField(String) */ public Builder hiddenImpl(Class<?> targetClass, String fieldName) { // don't do any work if an implementation has been found if (field != null || targetClass == null) { return this; } try { Field hidden = targetClass.getDeclaredField(fieldName); AccessController.doPrivileged(new MakeFieldAccessible(hidden)); this.field = new UnboundField(hidden, fieldName); } catch (SecurityException | NoSuchFieldException e) { // unusable candidates.add(targetClass.getName() + "." + fieldName); } return this; }