下面列出了org.eclipse.jdt.core.dom.WildcardType#isUpperBound ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public boolean visit(WildcardType type) {
/*
* WildcardType: ? [ ( extends | super) Type ]
*/
activateDiffStyle(type);
appendQuestionMark();
if(type.getBound() != null) {
if (type.isUpperBound()) {
styledString.append("extends", determineDiffStyle(type, new StyledStringStyler(keywordStyle)));
} else {
styledString.append("super", determineDiffStyle(type, new StyledStringStyler(keywordStyle)));
}
handleType(type.getBound());
}
deactivateDiffStyle(type);
return false;
}
@Override
public boolean visit(final WildcardType node) {
this.appendToBuffer("?");
Type bound = node.getBound();
if ((bound != null)) {
boolean _isUpperBound = node.isUpperBound();
if (_isUpperBound) {
this.appendToBuffer(" extends ");
} else {
this.appendToBuffer(" super ");
}
bound.accept(this);
}
return false;
}
/**
* @param type
* @return
*/
protected String getNameOfType(final Type type) {
String nameOfType = "";
if (type != null) {
if (type.isPrimitiveType()) {
nameOfType = type.toString();
} else if (type.isParameterizedType()) {
nameOfType = getParametrizedType((ParameterizedType) type);
} else if (type.isArrayType()) {
final ArrayType array = (ArrayType) type;
nameOfType = getNameOfType(array.getElementType()) /*+ "[]"*/;
} else if (type.isUnionType()) {
// TODO: this is used for exceptions till now
// So we will just capture the first type that we encounter
final UnionType uType = (UnionType) type;
final StringBuilder sb = new StringBuilder();
for (final Object unionedType : uType.types()) {
sb.append(getNameOfType((Type) unionedType));
break;
}
nameOfType = sb.toString();
} else if (type.isWildcardType()) {
final WildcardType wType = (WildcardType) type;
nameOfType = (wType.isUpperBound() ? "? extends " : "? super ")
+ getNameOfType(wType.getBound());
} else {
nameOfType = getFullyQualifiedNameFor(type.toString());
}
}
return nameOfType;
}
/**
* @param type
* @return
*/
protected String getNameOfType(final Type type) {
final String nameOfType;
if (type.isPrimitiveType()) {
nameOfType = type.toString();
} else if (type.isParameterizedType()) {
nameOfType = getParametrizedType((ParameterizedType) type);
} else if (type.isArrayType()) {
final ArrayType array = (ArrayType) type;
nameOfType = getNameOfType(array.getElementType()) + "[]";
} else if (type.isUnionType()) {
final UnionType uType = (UnionType) type;
final StringBuffer sb = new StringBuffer();
for (final Object unionedType : uType.types()) {
sb.append(getNameOfType(((Type) unionedType)));
sb.append(" | ");
}
sb.delete(sb.length() - 3, sb.length());
nameOfType = sb.toString();
} else if (type.isWildcardType()) {
final WildcardType wType = (WildcardType) type;
if (wType.getBound() == null)
return "?";
nameOfType = (wType.isUpperBound() ? "? extends " : "? super ") + getNameOfType(wType.getBound());
} else {
nameOfType = getFullyQualifiedNameFor(type.toString());
}
return nameOfType;
}
public boolean visit(WildcardType type) {
if(type.getBound() != null) {
if (type.isUpperBound()) {
bindingKeys.add("extends");
} else {
bindingKeys.add("super");
}
handleType(type.getBound());
}
return false;
}
private static void getFullyQualifiedName(Type type, StringBuffer buffer) {
switch (type.getNodeType()) {
case ASTNode.ARRAY_TYPE:
ArrayType arrayType = (ArrayType) type;
getFullyQualifiedName(arrayType.getElementType(), buffer);
for (int i = 0, length = arrayType.getDimensions(); i < length; i++) {
buffer.append('[');
buffer.append(']');
}
break;
case ASTNode.PARAMETERIZED_TYPE:
ParameterizedType parameterizedType = (ParameterizedType) type;
getFullyQualifiedName(parameterizedType.getType(), buffer);
buffer.append('<');
Iterator iterator = parameterizedType.typeArguments().iterator();
boolean isFirst = true;
while (iterator.hasNext()) {
if (!isFirst)
buffer.append(',');
else
isFirst = false;
Type typeArgument = (Type) iterator.next();
getFullyQualifiedName(typeArgument, buffer);
}
buffer.append('>');
break;
case ASTNode.PRIMITIVE_TYPE:
buffer.append(((PrimitiveType) type).getPrimitiveTypeCode().toString());
break;
case ASTNode.QUALIFIED_TYPE:
buffer.append(((QualifiedType) type).getName().getFullyQualifiedName());
break;
case ASTNode.SIMPLE_TYPE:
buffer.append(((SimpleType) type).getName().getFullyQualifiedName());
break;
case ASTNode.WILDCARD_TYPE:
buffer.append('?');
WildcardType wildcardType = (WildcardType) type;
Type bound = wildcardType.getBound();
if (bound == null) return;
if (wildcardType.isUpperBound()) {
buffer.append(" extends "); //$NON-NLS-1$
} else {
buffer.append(" super "); //$NON-NLS-1$
}
getFullyQualifiedName(bound, buffer);
break;
}
}