/** * 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; }
/** * Returns this method as a BoundMethod for the given receiver. * * @param target an Object on which to get or set this field * @return a {@link BoundField} for this field and the target * @throws IllegalStateException if the method is static * @throws IllegalArgumentException if the receiver's class is incompatible */ public BoundField<T> bind(Object target) { Preconditions.checkState(!isStatic() || this == AlwaysNull.INSTANCE, "Cannot bind static field " + name); Preconditions.checkArgument( field.getDeclaringClass().isAssignableFrom(target.getClass()), "Cannot bind field " + name + " to instance of " + target.getClass()); return new BoundField<>(this, target); }
/** * 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; }
public T get() { return field.get(null); }
/** * Returns the first valid implementation as a BoundMethod or throws a * RuntimeException if there is none. * * @param target an Object on which to get and set the field * @param <T> Java class stored in the field * @return a {@link BoundField} with a valid implementation and target * @throws IllegalStateException if the method is static * @throws IllegalArgumentException if the receiver's class is incompatible * @throws RuntimeException if no implementation was found */ public <T> BoundField<T> build(Object target) { return this.<T>build().bind(target); }
/** * Returns the first valid implementation as a BoundMethod or throws a * NoSuchMethodException if there is none. * * @param target an Object on which to get and set the field * @param <T> Java class stored in the field * @return a {@link BoundField} with a valid implementation and target * @throws IllegalStateException if the method is static * @throws IllegalArgumentException if the receiver's class is incompatible * @throws NoSuchFieldException if no implementation was found */ public <T> BoundField<T> buildChecked(Object target) throws NoSuchFieldException { return this.<T>buildChecked().bind(target); }
public void set(T value) { field.set(target, value); } }
public void set(T value) { field.set(null, value); } }
public T get() { return field.get(target); }
/** * Returns the first valid implementation as a StaticField or throws a * NoSuchFieldException if there is none. * * @param <T> Java class stored in the field * @return a {@link StaticField} with a valid implementation * @throws IllegalStateException if the method is not static * @throws NoSuchFieldException if no implementation was found */ public <T> StaticField<T> buildStaticChecked() throws NoSuchFieldException { return this.<T>buildChecked().asStatic(); }
/** * Returns this field as a StaticField. * * @return a {@link StaticField} for this field * @throws IllegalStateException if the method is not static */ public StaticField<T> asStatic() { Preconditions.checkState(isStatic(), "Field " + name + " is not static"); return new StaticField<>(this); }
/** * Returns the first valid implementation as a StaticField or throws a * RuntimeException if there is none. * * @param <T> Java class stored in the field * @return a {@link StaticField} with a valid implementation * @throws IllegalStateException if the method is not static * @throws RuntimeException if no implementation was found */ public <T> StaticField<T> buildStatic() { return this.<T>build().asStatic(); }