de.team33.patterns.reflect.pandora.Getter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of reflect-pandora Show documentation
Show all versions of reflect-pandora Show documentation
Provides tools and utilities for reflective processing of properties.
package de.team33.patterns.reflect.pandora;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.function.Function;
class Getter implements Function {
private static final String MESSAGE = "could not apply a subject to the associated method%n" +
" subject ...%n" +
" - type : %s%n" +
" - string representation : %s%n" +
" method ...%n" +
" - string representation : %s%n";
private final Method method;
private final String name;
Getter(final Method method) {
assert 0 == method.getParameterCount();
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
this.method = method;
this.name = Methods.normalName(method);
}
final String name() {
return name;
}
final Class> type() {
return method.getReturnType();
}
@Override
public final Object apply(final T subject) {
try {
return method.invoke(subject);
} catch (final IllegalAccessException | InvocationTargetException e) {
final Class> subjectClass = (null == subject) ? null : subject.getClass();
throw new IllegalStateException(String.format(MESSAGE, subjectClass, subject, method), e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy