mirror of
https://github.com/google/guice.git
synced 2026-10-19 16:16:21 +00:00
Rewrite DeclaredMembers to use stream ordering, so evaluation of subsequent comparators is lazy.
PiperOrigin-RevId: 347713768
This commit is contained in:
@@ -15,82 +15,45 @@
|
||||
*/
|
||||
package com.google.inject.internal;
|
||||
|
||||
import com.google.common.collect.ComparisonChain;
|
||||
import com.google.common.collect.Ordering;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
|
||||
/**
|
||||
* Utility class for retrieving declared fields or methods in a particular order, because the JVM
|
||||
* doesn't guarantee ordering for listing declared methods. We don't externally guarantee an
|
||||
* ordering, but having a consistent ordering allows deterministic behavior and simpler tests.
|
||||
*
|
||||
* <p>For class ordering, within a single classloader there can only be one class with a given name,
|
||||
* so we just compare the names.
|
||||
*
|
||||
* <p>For method ordering, within a single class it is sufficient to compare the non-generic method
|
||||
* signature which consists of the name, return type and parameter types.
|
||||
*/
|
||||
public final class DeclaredMembers {
|
||||
|
||||
private DeclaredMembers() {}
|
||||
|
||||
public static Field[] getDeclaredFields(Class<?> type) {
|
||||
Field[] fields = type.getDeclaredFields();
|
||||
Arrays.sort(fields, FIELD_ORDERING);
|
||||
return fields;
|
||||
return Arrays.stream(type.getDeclaredFields())
|
||||
.sorted(
|
||||
Comparator.comparing(Field::getName)
|
||||
.thenComparing(Field::getType, Comparator.comparing(Class::getName)))
|
||||
.toArray(Field[]::new);
|
||||
}
|
||||
|
||||
public static Method[] getDeclaredMethods(Class<?> type) {
|
||||
Method[] methods = type.getDeclaredMethods();
|
||||
Arrays.sort(methods, METHOD_ORDERING);
|
||||
return methods;
|
||||
return Arrays.stream(type.getDeclaredMethods())
|
||||
.sorted(
|
||||
Comparator.comparing(Method::getName)
|
||||
.thenComparing(Method::getReturnType, Comparator.comparing(Class::getName))
|
||||
.thenComparing(
|
||||
method -> Arrays.asList(method.getParameterTypes()),
|
||||
// TODO: use Comparators.lexicographical when it's not @Beta.
|
||||
Ordering.<Class<?>>from(Comparator.comparing(Class::getName))
|
||||
.lexicographical()))
|
||||
.toArray(Method[]::new);
|
||||
}
|
||||
|
||||
/**
|
||||
* An ordering suitable for comparing two classes if they are loaded by the same classloader
|
||||
*
|
||||
* <p>Within a single classloader there can only be one class with a given name, so we just
|
||||
* compare the names.
|
||||
*/
|
||||
private static final Ordering<Class<?>> CLASS_ORDERING =
|
||||
new Ordering<Class<?>>() {
|
||||
@Override
|
||||
public int compare(Class<?> o1, Class<?> o2) {
|
||||
return o1.getName().compareTo(o2.getName());
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* An ordering suitable for comparing two fields if they are owned by the same class.
|
||||
*
|
||||
* <p>Within a single class it is sufficent to compare the non-generic field signature which
|
||||
* consists of the field name and type.
|
||||
*/
|
||||
private static final Ordering<Field> FIELD_ORDERING =
|
||||
new Ordering<Field>() {
|
||||
@Override
|
||||
public int compare(Field left, Field right) {
|
||||
return ComparisonChain.start()
|
||||
.compare(left.getName(), right.getName())
|
||||
.compare(left.getType(), right.getType(), CLASS_ORDERING)
|
||||
.result();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* An ordering suitable for comparing two methods if they are owned by the same class.
|
||||
*
|
||||
* <p>Within a single class it is sufficient to compare the non-generic method signature which
|
||||
* consists of the name, return type and parameter types.
|
||||
*/
|
||||
private static final Ordering<Method> METHOD_ORDERING =
|
||||
new Ordering<Method>() {
|
||||
@Override
|
||||
public int compare(Method left, Method right) {
|
||||
return ComparisonChain.start()
|
||||
.compare(left.getName(), right.getName())
|
||||
.compare(left.getReturnType(), right.getReturnType(), CLASS_ORDERING)
|
||||
.compare(
|
||||
Arrays.asList(left.getParameterTypes()),
|
||||
Arrays.asList(right.getParameterTypes()),
|
||||
CLASS_ORDERING.lexicographical())
|
||||
.result();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user