Top Description Inners Constructors Methods
com.google.auto.value.processor

pack-priv final Class TypeVariables

extends Object
Class Inheritance
Static Imports
com.google.auto.common.MoreStreams.toImmutableMap

Methods for handling type variables.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
TypeVariables.SubstitutionVisitor

Rewrites types such that references to type variables in the given map are replaced by the values of those variables.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
pack-priv static boolean
canAssignStaticMethodResult(javax.lang.model.element.ExecutableElement method, javax.lang.model.type.TypeMirror actualParameterType, javax.lang.model.type.TypeMirror targetType, javax.lang.model.util.Types typeUtils)

Tests whether a given parameter can be given to a static method like ImmutableMap.copyOf to produce a value that can be assigned to the given target type.

pack-priv static ImmutableMap<javax.lang.model.element.ExecutableElement, javax.lang.model.type.TypeMirror>
rewriteReturnTypes(javax.lang.model.util.Elements elementUtils, javax.lang.model.util.Types typeUtils, Collection<javax.lang.model.element.ExecutableElement>
the methods whose return types are to be rewritten.
methods
,
javax.lang.model.element.TypeElement
the class containing those methods (Foo in the example).
sourceType
,
javax.lang.model.element.TypeElement
the class to translate the methods into (Foo.Builder<T>) in the example.
targetType
)

Returns a map from methods to return types, where the return types are not necessarily the original return types of the methods.

pack-priv static javax.lang.model.type.TypeMirror
substituteTypeVariables(javax.lang.model.type.TypeMirror input, Function<javax.lang.model.type.TypeVariable, javax.lang.model.type.TypeMirror> substitute, javax.lang.model.util.Types typeUtils)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

TypeVariablesback to summary
private TypeVariables()

Method Detail

canAssignStaticMethodResultback to summary
pack-priv static boolean canAssignStaticMethodResult(javax.lang.model.element.ExecutableElement method, javax.lang.model.type.TypeMirror actualParameterType, javax.lang.model.type.TypeMirror targetType, javax.lang.model.util.Types typeUtils)

Tests whether a given parameter can be given to a static method like ImmutableMap.copyOf to produce a value that can be assigned to the given target type.

For example, suppose we have this method in ImmutableMap:
static <K, V> ImmutableMap<K, V> copyOf(Map<? extends K, ? extends V>)
and we want to know if we can do this:

ImmutableMap<String, Integer> actualParameter = ...;
ImmutableMap<String, Number> target = ImmutableMap.copyOf(actualParameter);
We will infer K=String, V=Number based on the target type, and then rewrite the formal parameter type from
Map<? extends K, ? extends V> to
Map<? extends String, ? extends Number>. Then we can check whether actualParameter is assignable to that.

The logic makes some simplifying assumptions, which are met for the copyOf and 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 ImmutableMap.copyOf example above.

rewriteReturnTypesback to summary
pack-priv static ImmutableMap<javax.lang.model.element.ExecutableElement, javax.lang.model.type.TypeMirror> rewriteReturnTypes(javax.lang.model.util.Elements elementUtils, javax.lang.model.util.Types typeUtils, Collection<javax.lang.model.element.ExecutableElement> methods, javax.lang.model.element.TypeElement sourceType, javax.lang.model.element.TypeElement targetType)

Returns a map from methods to return types, where the return types are not necessarily the original return types of the methods. Consider this example:

@AutoValue class Foo<T> {
  abstract T getFoo();

  @AutoValue.Builder
  abstract class Builder<T> {
    abstract Builder setFoo(T t);
    abstract Foo<T> build();
  }
}
We want to be able to check that the parameter type of setFoo is the same as the return type of getFoo. But in fact it isn't, because the T of Foo<T> is not the same as the T of Foo.Builder<T>. So we create a parallel Foo<T> where the T is the one from Foo.Builder<T>. That way the types do correspond. This method then returns the return types of the given methods as they appear in that parallel class, meaning the type given for getFoo() is the T of Foo.Builder<T>.

We do the rewrite this way around (applying the type parameter from Foo.Builder to Foo) because if we hit one of the historical Eclipse bugs with Types#asMemberOf then EclipseHack#methodReturnType can use fallback logic, which only works for methods with no arguments.

Parameters
methods:Collection<javax.lang.model.element.ExecutableElement>

the methods whose return types are to be rewritten.

sourceType:javax.lang.model.element.TypeElement

the class containing those methods (Foo in the example).

targetType:javax.lang.model.element.TypeElement

the class to translate the methods into (Foo.Builder<T>) in the example.

substituteTypeVariablesback to summary
pack-priv static javax.lang.model.type.TypeMirror substituteTypeVariables(javax.lang.model.type.TypeMirror input, Function<javax.lang.model.type.TypeVariable, javax.lang.model.type.TypeMirror> substitute, javax.lang.model.util.Types typeUtils)
com.google.auto.value.processor back to summary

private Class TypeVariables.SubstitutionVisitor

extends javax.lang.model.util.SimpleTypeVisitor8
Class Inheritance
  • java.lang.Object
  • ⋱
  • javax.lang.model.util.SimpleTypeVisitor8
  • com.google.auto.value.processor.TypeVariables.SubstitutionVisitor

Rewrites types such that references to type variables in the given map are replaced by the values of those variables.

Field Summary

Modifier and TypeField and Description
private final Function<javax.lang.model.type.TypeVariable, javax.lang.model.type.TypeMirror>
private final javax.lang.model.util.Types

Constructor Summary

AccessConstructor and Description
pack-priv
SubstitutionVisitor(Function<javax.lang.model.type.TypeVariable, javax.lang.model.type.TypeMirror> substitute, javax.lang.model.util.Types typeUtils)

Method Summary

Modifier and TypeMethod and Description
protected javax.lang.model.type.TypeMirror
defaultAction(javax.lang.model.type.TypeMirror t, Void p)

public javax.lang.model.type.TypeMirror
visitArray(javax.lang.model.type.ArrayType t, Void p)

public javax.lang.model.type.TypeMirror
visitDeclared(javax.lang.model.type.DeclaredType t, Void p)

private javax.lang.model.type.TypeMirror
visitOrNull(javax.lang.model.type.TypeMirror t)

public javax.lang.model.type.TypeMirror
visitTypeVariable(javax.lang.model.type.TypeVariable t, Void p)

public javax.lang.model.type.TypeMirror
visitWildcard(javax.lang.model.type.WildcardType t, Void p)

Field Detail

substituteback to summary
private final Function<javax.lang.model.type.TypeVariable, javax.lang.model.type.TypeMirror> substitute
typeUtilsback to summary
private final javax.lang.model.util.Types typeUtils

Constructor Detail

SubstitutionVisitorback to summary
pack-priv SubstitutionVisitor(Function<javax.lang.model.type.TypeVariable, javax.lang.model.type.TypeMirror> substitute, javax.lang.model.util.Types typeUtils)

Method Detail

defaultActionback to summary
protected javax.lang.model.type.TypeMirror defaultAction(javax.lang.model.type.TypeMirror t, Void p)
Annotations
@Override
visitArrayback to summary
public javax.lang.model.type.TypeMirror visitArray(javax.lang.model.type.ArrayType t, Void p)
Annotations
@Override
visitDeclaredback to summary
public javax.lang.model.type.TypeMirror visitDeclared(javax.lang.model.type.DeclaredType t, Void p)
Annotations
@Override
visitOrNullback to summary
private javax.lang.model.type.TypeMirror visitOrNull(javax.lang.model.type.TypeMirror t)
visitTypeVariableback to summary
public javax.lang.model.type.TypeMirror visitTypeVariable(javax.lang.model.type.TypeVariable t, Void p)
Annotations
@Override
visitWildcardback to summary
public javax.lang.model.type.TypeMirror visitWildcard(javax.lang.model.type.WildcardType t, Void p)
Annotations
@Override