下面列出了javax.lang.model.type.TypeVariable#getLowerBound ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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;
}
@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;
}
private static String describeGenericBound(TypeMirror type) {
if (type instanceof TypeVariable) {
StringBuilder description = new StringBuilder("<");
TypeVariable typeVar = (TypeVariable)type;
description.append(typeVar.toString());
TypeMirror lowerBound = typeVar.getLowerBound();
if (lowerBound.getKind() != TypeKind.NULL) {
description.append(" super ").append(lowerBound);
}
TypeMirror upperBound = typeVar.getUpperBound();
if (upperBound.getKind() != TypeKind.NULL) {
description.append(" extends ").append(upperBound);
}
return description.append(">").toString();
}
return type.toString();
}
@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
if (!insideCapturedWildcard) {
insideCapturedWildcard = true;
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);
}
}
insideCapturedWildcard = false;
}
return DEFAULT_VALUE;
}
@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
if (!insideCapturedWildcard) {
insideCapturedWildcard = true;
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);
}
}
insideCapturedWildcard = false;
}
return DEFAULT_VALUE;
}
@Override
public Tree visitMethodInvocation(MethodInvocationTree node, Element p) {
List<? extends ExpressionTree> arguments = node.getArguments();
for (int i = 0; i < arguments.size(); i++) {
ExpressionTree argument = arguments.get(i);
Element argElement = asElement(argument); // TODO: Slow and misses ternary expressions
if(p.equals(argElement)) {
Element element = asElement(node);
if (element.getKind() == ElementKind.METHOD) {
ExecutableElement method = (ExecutableElement) element;
VariableElement parameter = method.getParameters().get(i);
Types types = workingCopy.getTypes();
TypeMirror parameterType = parameter.asType();
if(parameterType.getKind().equals(TypeKind.TYPEVAR)) {
TypeVariable typeVariable = (TypeVariable) parameterType;
TypeMirror upperBound = typeVariable.getUpperBound();
TypeMirror lowerBound = typeVariable.getLowerBound();
if(upperBound != null && !types.isSubtype(superTypeElement.asType(), upperBound)) {
isReplCandidate = false;
}
if(lowerBound != null && !types.isSubtype(lowerBound, superTypeElement.asType())) {
isReplCandidate = false;
}
} else if(!types.isAssignable(superTypeElement.asType(), parameterType)) {
isReplCandidate = false;
}
}
}
}
return super.visitMethodInvocation(node, p);
}
@Override
public Void visitTypeVariable(TypeVariable t, Collection<DeclaredType> p) {
if (t.getLowerBound() != null) {
visit(t.getLowerBound(), p);
}
if (t.getUpperBound() != null) {
visit(t.getUpperBound(), p);
}
return DEFAULT_VALUE;
}
@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
if (!insideCapturedWildcard) {
insideCapturedWildcard = true;
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);
}
}
insideCapturedWildcard = false;
}
return DEFAULT_VALUE;
}
@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
if (!insideCapturedWildcard) {
insideCapturedWildcard = true;
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);
}
}
insideCapturedWildcard = false;
}
return DEFAULT_VALUE;
}
@Override
public TypeMirror visitTypeVariable(TypeVariable t, CompilationInfo p) {
TypeMirror lb = t.getLowerBound() == null ? null : visit(t.getLowerBound(), p);
TypeMirror ub = t.getUpperBound() == null ? null : visit(t.getUpperBound(), p);
if (ub.getKind() == TypeKind.DECLARED) {
DeclaredType dt = (DeclaredType)ub;
TypeElement tel = (TypeElement)dt.asElement();
if (tel.getQualifiedName().contentEquals("java.lang.Object")) { // NOI18N
ub = null;
} else if (tel.getSimpleName().length() == 0) {
ub = null;
}
}
return p.getTypes().getWildcardType(ub, lb);
}