spring.turbo.util.reflection.MethodPredicateFactories Maven / Gradle / Ivy
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* ____ _ _____ _
* / ___| _ __ _ __(_)_ __ __ |_ _| _ _ __| |__ ___
* \___ \| '_ \| '__| | '_ \ / _` || || | | | '__| '_ \ / _ \
* ___) | |_) | | | | | | | (_| || || |_| | | | |_) | (_) |
* |____/| .__/|_| |_|_| |_|\__, ||_| \__,_|_| |_.__/ \___/
* |_| |___/ https://github.com/yingzhuo/spring-turbo
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package spring.turbo.util.reflection;
import spring.turbo.util.Asserts;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.function.Predicate;
/**
* @author 应卓
* @see MethodUtils
* @since 1.2.1
*/
public final class MethodPredicateFactories {
/**
* 私有构造方法
*/
private MethodPredicateFactories() {
super();
}
// -----------------------------------------------------------------------------------------------------------------
public static Predicate alwaysTrue() {
return m -> true;
}
public static Predicate alwaysFalse() {
return m -> false;
}
// -----------------------------------------------------------------------------------------------------------------
public static Predicate not(Predicate predicate) {
Asserts.notNull(predicate);
return m -> !predicate.test(m);
}
@SafeVarargs
public static Predicate any(Predicate... predicates) {
Asserts.notNull(predicates);
Asserts.isTrue(predicates.length >= 1);
return m -> {
for (final Predicate predicate : predicates) {
if (predicate.test(m)) {
return true;
}
}
return false;
};
}
@SafeVarargs
public static Predicate all(Predicate... predicates) {
Asserts.notNull(predicates);
Asserts.isTrue(predicates.length >= 1);
return m -> {
for (final Predicate predicate : predicates) {
if (!predicate.test(m)) {
return false;
}
}
return true;
};
}
// -----------------------------------------------------------------------------------------------------------------
public static Predicate isUserDeclaredMethod() {
return m -> !m.isBridge() && !m.isSynthetic() && (m.getDeclaringClass() != Object.class);
}
public static Predicate isNotUserDeclaredMethod() {
return not(isUserDeclaredMethod());
}
public static Predicate withAnnotation(Class extends Annotation> annotationClass) {
return m -> m.getAnnotation(annotationClass) != null;
}
public static Predicate withoutAnnotation(Class extends Annotation> annotationClass) {
return not(withAnnotation(annotationClass));
}
public static Predicate isPublic() {
return m -> Modifier.isPublic(m.getModifiers());
}
public static Predicate isNotPublic() {
return not(isPublic());
}
public static Predicate isPrivate() {
return m -> Modifier.isPrivate(m.getModifiers());
}
public static Predicate isNotPrivate() {
return not(isPrivate());
}
public static Predicate isProtected() {
return m -> Modifier.isProtected(m.getModifiers());
}
public static Predicate isNotProtected() {
return not(isProtected());
}
public static Predicate isStatic() {
return m -> Modifier.isStatic(m.getModifiers());
}
public static Predicate isNotStatic() {
return not(isStatic());
}
public static Predicate isFinal() {
return m -> Modifier.isFinal(m.getModifiers());
}
public static Predicate isNotFinal() {
return not(isFinal());
}
public static Predicate isAbstract() {
return m -> Modifier.isAbstract(m.getModifiers());
}
public static Predicate isNotAbstract() {
return not(isAbstract());
}
public static Predicate isNative() {
return m -> Modifier.isNative(m.getModifiers());
}
public static Predicate isNotNative() {
return not(isNative());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy