为 Java 中的不同类型应用通用逻辑
Apply Generic logic for different types in Java
我在下面给出了 Java 中的代码快照(两种方法)。
请注意,这里的Field
是java.lang.Field
,Method
是java.lang.Method
我正在使用 jdk1.7
.
/**
* Apply predicate filter and add into list if it apply(return true)
*
* @param methods list of methods in which method will be added in case predicate returns the true.
* @param method method which is being to check against the predicate
* @param predicate the predicate
*/
private static void applyPredicateForMethods(List<Method> methods, Method method, Predicate<Method> predicate){
if (predicate == null || predicate.apply(method)) {
if (!method.isAccessible())
method.setAccessible(true);
methods.add(method);
}
}
/**
* Apply predicate filter and add into list if it apply(return true)
*
* @param fields the list of fields in which method will be added in case predicate returns the true.
* @param field the field which is being to check against the predicate
* @param predicate the predicate
*/
private static void applyPredicateForFields(List<Field> fields, Field field, Predicate<Field> predicate){
if (predicate == null || predicate.apply(field)) {
if (!field.isAccessible())
field.setAccessible(true);
fields.add(field);
}
}
这两种方法都在执行相同的任务(过滤),但针对不同的 Class/Type/Context
有没有什么办法让它通用化,只写一次逻辑?
两者都具有超级 class (java.lang.reflect.AccessibleObject
),并且您正在从那个 class 访问相同的方法。看看 class java.lang.reflect.AccessibleObject
。将其用作类型。
两者 java.lang.reflect.Field
and java.lang.reflect.Method
extend java.lang.reflect.AccessibleObject
,它提供了您正在使用的方法。只需编写您的代码以引用它:
private static void applyPredicate
(List<AccsibleObjects> objects,
AccessibleObject object,
Predicate<AccessibleObject> predicate) {
if (predicate == null || predicate.apply(object)) {
if (!object.isAccessible())
object.setAccessible(true);
objects.add(object);
}
}
这是通用解决方案:
private static <T extends AccessibleObject> void applyPredicateForFields(List<T> objects,
T object, Predicate<T> predicate){
if (predicate == null || predicate.apply(object)) {
if (!object.isAccessible())
object.setAccessible(true);
objects.add(object);
}
}
我在下面给出了 Java 中的代码快照(两种方法)。
请注意,这里的Field
是java.lang.Field
,Method
是java.lang.Method
我正在使用 jdk1.7
.
/**
* Apply predicate filter and add into list if it apply(return true)
*
* @param methods list of methods in which method will be added in case predicate returns the true.
* @param method method which is being to check against the predicate
* @param predicate the predicate
*/
private static void applyPredicateForMethods(List<Method> methods, Method method, Predicate<Method> predicate){
if (predicate == null || predicate.apply(method)) {
if (!method.isAccessible())
method.setAccessible(true);
methods.add(method);
}
}
/**
* Apply predicate filter and add into list if it apply(return true)
*
* @param fields the list of fields in which method will be added in case predicate returns the true.
* @param field the field which is being to check against the predicate
* @param predicate the predicate
*/
private static void applyPredicateForFields(List<Field> fields, Field field, Predicate<Field> predicate){
if (predicate == null || predicate.apply(field)) {
if (!field.isAccessible())
field.setAccessible(true);
fields.add(field);
}
}
这两种方法都在执行相同的任务(过滤),但针对不同的 Class/Type/Context
有没有什么办法让它通用化,只写一次逻辑?
两者都具有超级 class (java.lang.reflect.AccessibleObject
),并且您正在从那个 class 访问相同的方法。看看 class java.lang.reflect.AccessibleObject
。将其用作类型。
两者 java.lang.reflect.Field
and java.lang.reflect.Method
extend java.lang.reflect.AccessibleObject
,它提供了您正在使用的方法。只需编写您的代码以引用它:
private static void applyPredicate
(List<AccsibleObjects> objects,
AccessibleObject object,
Predicate<AccessibleObject> predicate) {
if (predicate == null || predicate.apply(object)) {
if (!object.isAccessible())
object.setAccessible(true);
objects.add(object);
}
}
这是通用解决方案:
private static <T extends AccessibleObject> void applyPredicateForFields(List<T> objects,
T object, Predicate<T> predicate){
if (predicate == null || predicate.apply(object)) {
if (!object.isAccessible())
object.setAccessible(true);
objects.add(object);
}
}