下面列出了com.google.common.base.Equivalence#Wrapper ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
static MethodSignature create(
CompilationUnitTree compilationUnitTree, MethodTree tree, Trees trees) {
ImmutableList.Builder<Equivalence.Wrapper<TypeMirror>> parameterTypes =
ImmutableList.builder();
for (VariableTree parameter : tree.getParameters()) {
parameterTypes.add(
MoreTypes.equivalence()
.wrap(trees.getTypeMirror(trees.getPath(compilationUnitTree, parameter))));
}
return new AutoValue_TreeDiffer_MethodSignature(
tree.getName().toString(), parameterTypes.build());
}
private Optional<RequestResolver> getOwningResolver(ProvisionBinding provisionBinding) {
Optional<Equivalence.Wrapper<AnnotationMirror>> bindingScope =
provisionBinding.wrappedScope();
for (RequestResolver requestResolver : getResolverLineage()) {
if (bindingScope.equals(requestResolver.targetScope)
|| requestResolver.explicitProvisionBindings.containsValue(provisionBinding)) {
return Optional.of(requestResolver);
}
}
return Optional.absent();
}
ProxyGenerator(
TypeName outerClassTypeName,
ImmutableList<TypeVariableName> typeVariableNames,
ImmutableList<PropertyMirror> propertyMirrors,
ImmutableMap<Equivalence.Wrapper<TypeMirror>, Serializer> serializersMap) {
this.outerClassTypeName = outerClassTypeName;
this.typeVariableNames = typeVariableNames;
this.propertyMirrors = propertyMirrors;
this.serializersMap = serializersMap;
}
/**
* Wraps an {@link Optional} of a type in an {@code Optional} of a {@link Wrapper} for that type.
*/
static <T> Optional<Equivalence.Wrapper<T>> wrapOptionalInEquivalence(
Equivalence<T> equivalence, Optional<T> optional) {
return optional.isPresent()
? Optional.of(equivalence.wrap(optional.get()))
: Optional.<Equivalence.Wrapper<T>>absent();
}
/**
* Tests whether a given parameter can be given to a static method like
* {@code ImmutableMap.copyOf} to produce a value that can be assigned to the given target type.
*
* <p>For example, suppose we have this method in {@code ImmutableMap}:<br>
* {@code static <K, V> ImmutableMap<K, V> copyOf(Map<? extends K, ? extends V>)}<br>
* and we want to know if we can do this:
*
* <pre>
* {@code ImmutableMap<String, Integer> actualParameter = ...;}
* {@code ImmutableMap<String, Number> target = ImmutableMap.copyOf(actualParameter);}
* </pre>
*
* We will infer {@code K=String}, {@code V=Number} based on the target type, and then rewrite the
* formal parameter type from<br>
* {@code Map<? extends K, ? extends V>} to<br>
* {@code Map<? extends String, ? extends Number>}. Then we can check whether
* {@code actualParameter} is assignable to that.
*
* <p>The logic makes some simplifying assumptions, which are met for the {@code copyOf} and
* {@code of} methods that we use this for. The method must be static, it must have exactly one
* parameter, and it must have type parameters without bounds that are the same as the type
* parameters of its return type. We can see that these assumptions are met for the
* {@code ImmutableMap.copyOf} example above.
*/
static boolean canAssignStaticMethodResult(
ExecutableElement method,
TypeMirror actualParameterType,
TypeMirror targetType,
Types typeUtils) {
if (!targetType.getKind().equals(TypeKind.DECLARED)
|| !method.getModifiers().contains(Modifier.STATIC)
|| method.getParameters().size() != 1) {
return false;
}
List<? extends TypeParameterElement> typeParameters = method.getTypeParameters();
List<? extends TypeMirror> targetTypeArguments =
MoreTypes.asDeclared(targetType).getTypeArguments();
if (typeParameters.size() != targetTypeArguments.size()) {
return false;
}
Map<Equivalence.Wrapper<TypeVariable>, TypeMirror> typeVariables = new LinkedHashMap<>();
for (int i = 0; i < typeParameters.size(); i++) {
TypeVariable v = MoreTypes.asTypeVariable(typeParameters.get(i).asType());
typeVariables.put(MoreTypes.equivalence().wrap(v), targetTypeArguments.get(i));
}
TypeMirror formalParameterType = method.getParameters().get(0).asType();
SubstitutionVisitor substitutionVisitor = new SubstitutionVisitor(typeVariables, typeUtils);
TypeMirror substitutedParameterType = substitutionVisitor.visit(formalParameterType, null);
if (substitutedParameterType.getKind().equals(TypeKind.WILDCARD)) {
// If the target type is Optional<? extends Foo> then <T> T Optional.of(T) will give us
// ? extends Foo here, and typeUtils.isAssignable will return false. But we can in fact
// give a Foo as an argument, so we just replace ? extends Foo with Foo.
WildcardType wildcard = MoreTypes.asWildcard(substitutedParameterType);
if (wildcard.getExtendsBound() != null) {
substitutedParameterType = wildcard.getExtendsBound();
}
}
return typeUtils.isAssignable(actualParameterType, substitutedParameterType);
}
/**
* Unwraps an {@link Optional} of a {@link Equivalence.Wrapper} into an {@code Optional} of the
* underlying type.
*/
static <T> Optional<T> unwrapOptionalEquivalence(
Optional<Equivalence.Wrapper<T>> wrappedOptional) {
return wrappedOptional.isPresent()
? Optional.of(wrappedOptional.get().get())
: Optional.<T>absent();
}
static MethodSignature fromExecutableType(String methodName, ExecutableType methodType) {
checkNotNull(methodType);
ImmutableList.Builder<Equivalence.Wrapper<TypeMirror>> parameters = ImmutableList.builder();
ImmutableList.Builder<Equivalence.Wrapper<TypeMirror>> thrownTypes = ImmutableList.builder();
for (TypeMirror parameter : methodType.getParameterTypes()) {
parameters.add(MoreTypes.equivalence().wrap(parameter));
}
for (TypeMirror thrownType : methodType.getThrownTypes()) {
thrownTypes.add(MoreTypes.equivalence().wrap(thrownType));
}
return new AutoValue_MethodSignature(
methodName,
parameters.build(),
thrownTypes.build());
}
private static <B, S extends B> Map<Equivalence.Wrapper<B>, Integer> getCardinalityMap(Collection<S> coll, Equivalence<B> equiv) {
Map<Equivalence.Wrapper<B>, Integer> count = new HashMap<>();
for (S obj : coll) count.merge(equiv.wrap(obj), 1, Integer::sum);
return count;
}
@Override
Equivalence.Wrapper<ReasonerAtomicQuery> queryToKey(ReasonerAtomicQuery query) {
return unifierType().equivalence().wrap(query);
}
public boolean contains(Equivalence.Wrapper<ReasonerQueryImpl> q){
return wrappedCollection.contains(q);
}
public static QuerySet create(Collection<Equivalence.Wrapper<ReasonerQueryImpl>> queries){
return new QuerySet(queries.stream().map(Equivalence.Wrapper::get).collect(Collectors.toSet()));
}
private Equivalence.Wrapper<TypeMirror> wrap(TypeMirror typeMirror) {
return MoreTypes.equivalence().wrap(typeMirror);
}
/**
* The type represented by this key.
*
* As documented in {@link TypeMirror}, equals and hashCode aren't implemented to represent
* logical equality, so {@link MoreTypes#equivalence()} wraps this type.
*/
abstract Equivalence.Wrapper<TypeMirror> wrappedType();
/**
* An optional annotation constraining the scope of this component wrapped in an
* {@link com.google.common.base.Equivalence.Wrapper} to preserve comparison semantics of
* {@link AnnotationMirror}.
*/
abstract Optional<Equivalence.Wrapper<AnnotationMirror>> wrappedScope();
/**
* An optional annotation constraining the scope of this component wrapped in an
* {@link com.google.common.base.Equivalence.Wrapper} to preserve comparison semantics of
* {@link AnnotationMirror}.
*/
abstract Optional<Equivalence.Wrapper<AnnotationMirror>> wrappedScope();
abstract ImmutableList<Equivalence.Wrapper<TypeMirror>> parameterTypes();
abstract ImmutableList<Equivalence.Wrapper<TypeMirror>> thrownTypes();
abstract ImmutableList<Equivalence.Wrapper<TypeMirror>> parameterTypes();