本文整理汇总了Java中javax.lang.model.type.TypeVariable类的典型用法代码示例。如果您正苦于以下问题:Java TypeVariable类的具体用法?Java TypeVariable怎么用?Java TypeVariable使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TypeVariable类属于javax.lang.model.type包,在下文中一共展示了TypeVariable类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: visitTypeVariable
import javax.lang.model.type.TypeVariable; //导入依赖的package包/类
@Override
public StringBuilder visitTypeVariable(TypeVariable t, Boolean p) {
Element e = t.asElement();
if (e != null) {
String name = e.getSimpleName().toString();
if (!CAPTURED_WILDCARD.equals(name))
return DEFAULT_VALUE.append(name);
}
DEFAULT_VALUE.append("?"); //NOI18N
TypeMirror bound = t.getLowerBound();
if (bound != null && bound.getKind() != TypeKind.NULL) {
DEFAULT_VALUE.append(" super "); //NOI18N
visit(bound, p);
} else {
bound = t.getUpperBound();
if (bound != null && bound.getKind() != TypeKind.NULL) {
DEFAULT_VALUE.append(" extends "); //NOI18N
if (bound.getKind() == TypeKind.TYPEVAR)
bound = ((TypeVariable)bound).getLowerBound();
visit(bound, p);
}
}
return DEFAULT_VALUE;
}
示例2: verifyTypeVarAccessible
import javax.lang.model.type.TypeVariable; //导入依赖的package包/类
private static boolean verifyTypeVarAccessible(ExecutableElement method, TypeMirror forType, List<Element> usedLocalTypeVariables, Element target) {
Collection<TypeVariable> typeVars = Utilities.containedTypevarsRecursively(forType);
if (method != null) {
for (Iterator<TypeVariable> it = typeVars.iterator(); it.hasNext(); ) {
TypeVariable tvar = it.next();
Element tvarEl = tvar.asElement();
if (method.getTypeParameters().contains(tvarEl)) {
usedLocalTypeVariables.add(tvarEl);
it.remove();
}
}
}
return allTypeVarsAccessible(typeVars, target);
}
示例3: containedTypevarsRecursively
import javax.lang.model.type.TypeVariable; //导入依赖的package包/类
private static void containedTypevarsRecursively(@NonNull TypeMirror tm, @NonNull Collection<TypeVariable> typeVars) {
switch (tm.getKind()) {
case TYPEVAR:
typeVars.add((TypeVariable) tm);
break;
case DECLARED:
DeclaredType type = (DeclaredType) tm;
for (TypeMirror t : type.getTypeArguments()) {
containedTypevarsRecursively(t, typeVars);
}
break;
case ARRAY:
containedTypevarsRecursively(((ArrayType) tm).getComponentType(), typeVars);
break;
case WILDCARD:
if (((WildcardType) tm).getExtendsBound() != null) {
containedTypevarsRecursively(((WildcardType) tm).getExtendsBound(), typeVars);
}
if (((WildcardType) tm).getSuperBound() != null) {
containedTypevarsRecursively(((WildcardType) tm).getSuperBound(), typeVars);
}
break;
}
}
示例4: resolveTypeName
import javax.lang.model.type.TypeVariable; //导入依赖的package包/类
/**
* uses FQNs where possible since javadoc does not match imports for
* parameter types
*/
private CharSequence resolveTypeName(TypeMirror asType, boolean isVarArgs) {
CharSequence ptype;
if (asType.getKind() == TypeKind.DECLARED) {
// snip generics
Element e = ((DeclaredType) asType).asElement();
ptype = e.getKind().isClass() || e.getKind().isInterface()
? ((TypeElement) e).getQualifiedName()
: e.getSimpleName();
} else if (asType.getKind() == TypeKind.TYPEVAR) {
do {
// Type Erasure JLS 4.6
asType = ((TypeVariable) asType).getUpperBound();
} while (asType.getKind() == TypeKind.TYPEVAR);
ptype = resolveTypeName(asType, isVarArgs);
} else if (isVarArgs && asType.getKind() == TypeKind.ARRAY) {
ptype = resolveTypeName(((ArrayType)asType).getComponentType(), false) + "..."; //NOI18N
} else {
ptype = asType.toString();
}
return ptype;
}
示例5: visitTypeVariable
import javax.lang.model.type.TypeVariable; //导入依赖的package包/类
@Override
public Void visitTypeVariable(TypeVariable type, Void p) {
Element e = type.asElement();
if (e != null) {
CharSequence name = e.getSimpleName();
if (!CAPTURED_WILDCARD.contentEquals(name)) {
builder.append(name);
return null;
}
}
builder.append("?"); //NOI18N
TypeMirror bound = type.getLowerBound();
if (bound != null && bound.getKind() != TypeKind.NULL) {
builder.append(" super "); //NOI18N
visit(bound);
} else {
bound = type.getUpperBound();
if (bound != null && bound.getKind() != TypeKind.NULL) {
builder.append(" extends "); //NOI18N
if (bound.getKind() == TypeKind.TYPEVAR)
bound = ((TypeVariable)bound).getLowerBound();
visit(bound);
}
}
return null;
}
示例6: getMethodParameterTypes
import javax.lang.model.type.TypeVariable; //导入依赖的package包/类
/**
* 取得方法参数类型列表
*/
private List<String> getMethodParameterTypes(ExecutableElement executableElement) {
List<? extends VariableElement> methodParameters = executableElement.getParameters();
if (methodParameters.size() == 0) {
return null;
}
List<String> types = new ArrayList<>();
for (VariableElement variableElement : methodParameters) {
TypeMirror methodParameterType = variableElement.asType();
if (methodParameterType instanceof TypeVariable) {
TypeVariable typeVariable = (TypeVariable) methodParameterType;
methodParameterType = typeVariable.getUpperBound();
}
types.add(methodParameterType.toString());
}
return types;
}
示例7: getErasedType
import javax.lang.model.type.TypeVariable; //导入依赖的package包/类
static String getErasedType(TypeMirror type) {
switch (type.getKind()) {
case DECLARED:
DeclaredType declared = (DeclaredType) type;
TypeElement element = (TypeElement) declared.asElement();
return element.getQualifiedName().toString();
case TYPEVAR:
return getErasedType(((TypeVariable) type).getUpperBound());
case WILDCARD:
return getErasedType(((WildcardType) type).getExtendsBound());
case ARRAY:
return getErasedType(((ArrayType) type).getComponentType()) + "[]";
default:
return type.toString();
}
}
示例8: appendSimpleTypeName
import javax.lang.model.type.TypeVariable; //导入依赖的package包/类
private static void appendSimpleTypeName(StringBuilder ret, TypeMirror type) {
switch (type.getKind()) {
case DECLARED:
DeclaredType declared = (DeclaredType) type;
TypeElement element = (TypeElement) declared.asElement();
ret.append(element.getSimpleName());
break;
case TYPEVAR:
appendSimpleTypeName(ret, ((TypeVariable) type).getUpperBound());
break;
case WILDCARD:
appendSimpleTypeName(ret, ((WildcardType) type).getExtendsBound());
break;
case ARRAY:
appendSimpleTypeName(ret, ((ArrayType) type).getComponentType());
ret.append("Array");
break;
default:
ret.append(type);
}
}
示例9: visitTypeVariable
import javax.lang.model.type.TypeVariable; //导入依赖的package包/类
@Override
public Boolean visitTypeVariable(TypeVariable a, EqualVisitorParam p) {
if (p.type.getKind().equals(TYPEVAR)) {
TypeVariable b = (TypeVariable) p.type;
TypeParameterElement aElement = (TypeParameterElement) a.asElement();
TypeParameterElement bElement = (TypeParameterElement) b.asElement();
Set<ComparedElements> newVisiting = visitingSetPlus(p.visiting, aElement, bElement);
if (newVisiting.equals(p.visiting)) {
// We're already visiting this pair of elements.
// This can happen with our friend Eclipse when looking at <T extends Comparable<T>>.
// It incorrectly reports the upper bound of T as T itself.
return true;
}
// We use aElement.getBounds() instead of a.getUpperBound() to avoid having to deal with
// the different way intersection types (like <T extends Number & Comparable<T>>) are
// represented before and after Java 8. We do have an issue that this code may consider
// that <T extends Foo & Bar> is different from <T extends Bar & Foo>, but it's very
// hard to avoid that, and not likely to be much of a problem in practice.
return equalLists(aElement.getBounds(), bElement.getBounds(), newVisiting)
&& equal(a.getLowerBound(), b.getLowerBound(), newVisiting)
&& a.asElement().getSimpleName().equals(b.asElement().getSimpleName());
}
return false;
}
示例10: get
import javax.lang.model.type.TypeVariable; //导入依赖的package包/类
/**
* Make a TypeVariableName for the given TypeMirror. This form is used internally to avoid
* infinite recursion in cases like {@code Enum<E extends Enum<E>>}. When we encounter such a
* thing, we will make a TypeVariableName without bounds and add that to the {@code typeVariables}
* map before looking up the bounds. Then if we encounter this TypeVariable again while
* constructing the bounds, we can just return it from the map. And, the code that put the entry
* in {@code variables} will make sure that the bounds are filled in before returning.
*/
static com.wrmsr.wava.java.poet.TypeVariableName get(
TypeVariable mirror, Map<TypeParameterElement, com.wrmsr.wava.java.poet.TypeVariableName> typeVariables)
{
TypeParameterElement element = (TypeParameterElement) mirror.asElement();
com.wrmsr.wava.java.poet.TypeVariableName typeVariableName = typeVariables.get(element);
if (typeVariableName == null) {
// Since the bounds field is public, we need to make it an unmodifiableList. But we control
// the List that that wraps, which means we can change it before returning.
List<TypeName> bounds = new ArrayList<>();
List<TypeName> visibleBounds = Collections.unmodifiableList(bounds);
typeVariableName = new com.wrmsr.wava.java.poet.TypeVariableName(element.getSimpleName().toString(), visibleBounds);
typeVariables.put(element, typeVariableName);
for (TypeMirror typeMirror : element.getBounds()) {
bounds.add(TypeName.get(typeMirror, typeVariables));
}
bounds.remove(OBJECT);
}
return typeVariableName;
}
示例11: get
import javax.lang.model.type.TypeVariable; //导入依赖的package包/类
/**
* Make a TypeVariableName for the given TypeMirror. This form is used internally to avoid
* infinite recursion in cases like {@code Enum<E extends Enum<E>>}. When we encounter such a
* thing, we will make a TypeVariableName without bounds and add that to the {@code typeVariables}
* map before looking up the bounds. Then if we encounter this TypeVariable again while
* constructing the bounds, we can just return it from the map. And, the code that put the entry
* in {@code variables} will make sure that the bounds are filled in before returning.
*/
static TypeVariableName get(
TypeVariable mirror, Map<TypeParameterElement, TypeVariableName> typeVariables) {
TypeParameterElement element = (TypeParameterElement) mirror.asElement();
TypeVariableName typeVariableName = typeVariables.get(element);
if (typeVariableName == null) {
// Since the bounds field is public, we need to make it an unmodifiableList. But we control
// the List that that wraps, which means we can change it before returning.
List<TypeName> bounds = new ArrayList<>();
List<TypeName> visibleBounds = Collections.unmodifiableList(bounds);
typeVariableName = new TypeVariableName(element.getSimpleName().toString(), visibleBounds);
typeVariables.put(element, typeVariableName);
for (TypeMirror typeMirror : element.getBounds()) {
bounds.add(TypeName.get(typeMirror, typeVariables));
}
bounds.remove(OBJECT);
}
return typeVariableName;
}
示例12: getFeatureParameterTypeVariableName
import javax.lang.model.type.TypeVariable; //导入依赖的package包/类
private TypeName getFeatureParameterTypeVariableName(DeclaredType featureType,
int featureParameterIndex) {
Element paramElem = getFeatureParameterElement(featureType, featureParameterIndex);
if (paramElem == null) {
return null;
}
if (paramElem.getKind() == ElementKind.TYPE_PARAMETER) {
return TypeVariableName.get((TypeVariable) paramElem.asType());
} else if (paramElem.getKind() == ElementKind.CLASS) {
return TypeName.get(paramElem.asType());
}
return null;
}
示例13: get
import javax.lang.model.type.TypeVariable; //导入依赖的package包/类
/**
* Make a TypeVariableName for the given TypeMirror. This form is used internally to avoid
* infinite recursion in cases like {@code Enum<E extends Enum<E>>}. When we encounter such a
* thing, we will make a TypeVariableName without bounds and add that to the {@code typeVariables}
* map before looking up the bounds. Then if we encounter this TypeVariable again while
* constructing the bounds, we can just return it from the map. And, the code that put the entry
* in {@code variables} will make sure that the bounds are filled in before returning.
*/
static TypeVariableName get(
TypeVariable mirror, Map<TypeParameterElement, TypeVariableName> typeVariables) {
TypeParameterElement element = (TypeParameterElement) mirror.asElement();
TypeVariableName typeVariableName = typeVariables.get(element);
if (typeVariableName == null) {
// Since the bounds field is public, we need to make it an unmodifiableList. But we control
// the List that that wraps, which means we can change it before returning.
List<TypeName> bounds = new ArrayList<>();
List<TypeName> visibleBounds = Collections.unmodifiableList(bounds);
typeVariableName = new TypeVariableName(element.getSimpleName().toString(), visibleBounds);
typeVariables.put(element, typeVariableName);
for (TypeMirror typeMirror : element.getBounds()) {
bounds.add(TypeName.get(typeMirror, typeVariables));
}
bounds.remove(OBJECT);
}
return typeVariableName;
}
示例14: visitTypeVariable
import javax.lang.model.type.TypeVariable; //导入依赖的package包/类
@Override public TypeKey visitTypeVariable(TypeVariable t, Set<TypeParameterElement> visited) {
TypeParameterElement element = (TypeParameterElement) t.asElement();
if (visited.contains(element)) {
// This avoids infinite recursion with adapted types like <T extends Comparable<T>>.
// It should probably check that T is bound correctly, but this is unlikely to be an issue
// in the wild.
return AnyKey.get(t.toString());
}
visited.add(element);
ImmutableList.Builder<TypeKey> builder = ImmutableList.builder();
for (TypeMirror bound : element.getBounds()) {
TypeKey boundKey = bound.accept(this, visited);
if (!boundKey.equals(OBJECT)) {
builder.add(boundKey);
}
}
ImmutableList<TypeKey> bounds = builder.build();
if (bounds.size() == 0) {
return AnyKey.get(t.toString());
} else {
return BoundedKey.get(t.toString(), bounds);
}
}
示例15: AnnotationInfo
import javax.lang.model.type.TypeVariable; //导入依赖的package包/类
public AnnotationInfo(TypeMirror tm, String idProperty, boolean keepNonIdProperty,
TypeMirror idGeneratorType, boolean customGenerator) {
this.tm = tm;
if (tm.getKind() != TypeKind.DECLARED) {
throw new RuntimeException(tm + " should be declared");
}
DeclaredType dt = (DeclaredType) tm;
List<? extends TypeMirror> typeArguments = dt.getTypeArguments();
if (typeArguments != null) {
for (TypeMirror tms : typeArguments) {
if (tms instanceof TypeVariable) {
typeVariables.add((TypeVariable) tms);
} else if (tms instanceof WildcardType) {
typeWildcards.add((WildcardType) tms);
}
}
}
this.idGeneratorType = idGeneratorType;
this.customGenerator = customGenerator;
this.idProperty = Strings.nullToEmpty(idProperty).trim();
this.keepNonIdProperty = keepNonIdProperty;
}