下面列出了javax.lang.model.type.TypeVariable#getUpperBound ( ) 实例代码,或者点击链接到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();
}
private List<CName> getParameterTypes(ExecutableElement executableElement) {
List<? extends VariableElement> methodParameters = executableElement.getParameters();
List<CName> parameterTypes = new ArrayList<>();
for (VariableElement variableElement : methodParameters) {
TypeMirror methodParameterType = variableElement.asType();
if (methodParameterType instanceof TypeVariable) {
TypeVariable typeVariable = (TypeVariable) methodParameterType;
methodParameterType = typeVariable.getUpperBound();
}
parameterTypes.add(new CName(methodParameterType));
}
return parameterTypes;
}
@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;
}
private void parseBindExtra(Element element, Map<TypeElement, IntentBindingAdapterGenerator> targetClassMap, Set<String> erasedTargetNames)
throws InvalidTypeException {
TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
// Verify that the target has all the appropriate information for type
TypeMirror type = element.asType();
if (type instanceof TypeVariable) {
TypeVariable typeVariable = (TypeVariable) type;
type = typeVariable.getUpperBound();
}
TypeMirror intentSerializer = getAnnotationElementClass(element, IntentSerializer.class);
validateSerializer(element, IntentSerializer.class, intentSerializer, PocketKnifeIntentSerializer.class);
validateNotRequiredArguments(element);
validateBindingPackage(BindExtra.class, element);
validateForCodeGeneration(BindExtra.class, element);
Access access = getAccess(BindExtra.class, element, enclosingElement);
// Assemble information on the bind point
String name = element.getSimpleName().toString();
String intentType = null;
KeySpec key = getKey(element);
boolean required = element.getAnnotation(NotRequired.class) == null;
boolean hasDefault = false;
boolean needsToBeCast = false;
if (intentSerializer == null) {
intentType = typeUtil.getIntentType(type);
hasDefault = typeUtil.isPrimitive(type);
needsToBeCast = typeUtil.needToCastIntentType(type);
}
IntentBindingAdapterGenerator intentBindingAdapterGenerator = getOrCreateTargetClass(targetClassMap, enclosingElement);
IntentFieldBinding binding = new IntentFieldBinding(name, access, type, intentType, key, needsToBeCast, hasDefault, required, intentSerializer);
intentBindingAdapterGenerator.addField(binding);
// Add the type-erased version to the valid targets set.
erasedTargetNames.add(enclosingElement.toString());
}
@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);
}
private void parseBindTopping(Element element, Map<TypeElement, BindingClass> targetClassMap){
TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
// Start by verifying common generated code restrictions.
boolean hasError = isInaccessibleViaGeneratedCode(BindTopping.class, "fields", element)
|| isBindingInWrongPackage(BindTopping.class, element);
// Verify that the target type extends from View.
TypeMirror elementType = element.asType();
if (elementType.getKind() == TypeKind.TYPEVAR) {
TypeVariable typeVariable = (TypeVariable) elementType;
elementType = typeVariable.getUpperBound();
}
if (!isSubtypeOfType(elementType, VIEW_TYPE) && !isInterface(elementType)) {
error(element, "@%s fields must extend from View or be an interface. (%s.%s)",
BindTopping.class.getSimpleName(), enclosingElement.getQualifiedName(),
element.getSimpleName());
hasError = true;
}
if (hasError) {
return;
}
BindTopping annotation = element.getAnnotation(BindTopping.class);
// Start assembling binding information
BindingClass bindingClass = getOrCreateTargetClass(targetClassMap, enclosingElement);
String name = element.getSimpleName().toString();
TypeName type = TypeName.get(elementType);
ClassName adapter = className(asTypeElement(getAdapterTypeMirror(annotation)));
ClassName interpolator = className(asTypeElement(getInterpolatorTypeMirror(annotation)));
FieldViewBinding binding = new FieldViewBinding(annotation.value(), name, adapter, interpolator, annotation.duration());
bindingClass.addViewBinding(binding);
}
private void parseBindToppingStatus(Element element, Map<TypeElement, BindingClass> targetClassMap){
// Verify that element is of type Class
if(element.getKind() != ElementKind.CLASS){
error(element, "Only classes can be annotated with %s", BindToppingStatus.class.getSimpleName());
}else{
// Start by verifying common generated code restrictions.
boolean hasError = false;
// Verify that the target type extends from View.
TypeMirror elementType = element.asType();
if (elementType.getKind() == TypeKind.TYPEVAR) {
TypeVariable typeVariable = (TypeVariable) elementType;
elementType = typeVariable.getUpperBound();
}
if (!isSubtypeOfType(elementType, ACTIVITY_TYPE) && !isInterface(elementType)) {
error(element, "@%s classes must extend from Activity(%s.%s)",
BindToppingStatus.class.getSimpleName(), element.getSimpleName());
hasError = true;
}
if (hasError) {
return;
}
BindToppingStatus annotation = element.getAnnotation(BindToppingStatus.class);
// Start assembling binding information
BindingClass bindingClass = getOrCreateTargetClass(targetClassMap, (TypeElement) element);
ClassName interpolator = className(asTypeElement(getInterpolatorTypeMirror(annotation)));
ClassStatusBarBinding binding = new ClassStatusBarBinding(annotation.value(), interpolator, annotation.duration());
bindingClass.setStatusBarBinding(binding);
}
}
protected TypeMirror getTypeParameterUpperBound(String typeName, int typeParameterIndex) {
TypeParameterElement typeParameter =
elements.getTypeElement(typeName).getTypeParameters().get(typeParameterIndex);
TypeVariable typeVariable = (TypeVariable) typeParameter.asType();
return typeVariable.getUpperBound();
}
private void parseBindText(Element element, Map<TypeElement, BindingClass> targetClassMap,
Set<String> erasedTargetNames) {
TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
TypeMirror elementType = element.asType();
if (elementType.getKind() == TypeKind.TYPEVAR) {
TypeVariable typeVariable = (TypeVariable) elementType;
elementType = typeVariable.getUpperBound();
}
// Assemble information on the field.
int[] ids = element.getAnnotation(BindText.class).value();
BindingClass bindingClass = getOrCreateTargetClass(targetClassMap, enclosingElement, false, false);
for (int id : ids) {
if (bindingClass != null) {
KBindings bindings = bindingClass.getKBindings(String.valueOf(id));
if (bindings != null) {
Iterator<FieldViewBinding> iterator = bindings.getFieldBindings().iterator();
if (iterator.hasNext()) {
FieldViewBinding existingBinding = iterator.next();
error(element, "Attempt to use @%s for an already bound ID %s on '%s'. (%s.%s)",
BindText.class.getSimpleName(), id, existingBinding.getName(),
enclosingElement.getQualifiedName(), element.getSimpleName());
return;
}
}
} else {
bindingClass = getOrCreateTargetClass(targetClassMap, enclosingElement, false, false);
}
String name = element.getSimpleName().toString();
TypeName type = TypeName.get(elementType);
boolean required = isRequiredBinding(element);
FieldViewBinding binding = new FieldViewBinding(name, type, required);
bindingClass.addField(String.valueOf(id), binding);
}
// Add the type-erased version to the valid binding targets set.
erasedTargetNames.add(enclosingElement.toString());
}
private void parseElement(Element element, List<ParsedInfo> infos, Class<? extends Annotation> annotationClass) {
if (isBindingInWrongPackage(annotationClass, element)) {
return;
}
TypeElement enclosingElement = (TypeElement) element;
// Verify that the target type extends from ViewGroup.
TypeMirror elementType = element.asType();
if (elementType.getKind() == TypeKind.TYPEVAR) {
TypeVariable typeVariable = (TypeVariable) elementType;
elementType = typeVariable.getUpperBound();
}
if (!isSubtypeOfType(elementType, VIEW_GROUP)) {
error(element, "@%s fields must extend from ViewGroup. (%s)",
annotationClass.getSimpleName(), enclosingElement.getQualifiedName());
}
String name = element.getSimpleName().toString();
String canonicalName = enclosingElement.getQualifiedName().toString();
String superCanonicalName = ((TypeElement) element).getSuperclass().toString();
String superName = superCanonicalName.substring(superCanonicalName.lastIndexOf('.') + 1);
if (superCanonicalName.startsWith("android.widget.")) {
superCanonicalName = superCanonicalName.substring(superCanonicalName.lastIndexOf('.') + 1);
}
ParsedInfo info = new ParsedInfo();
info.name = name;
info.canonicalName = canonicalName;
info.superName = superName;
info.superCanonicalName = superCanonicalName;
infos.add(info);
}
private void parseBindView(Element element, Map<TypeElement, BindingSet.Builder> builderMap,
Set<TypeElement> erasedTargetNames) {
TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
// Start by verifying common generated code restrictions.
boolean hasError = isInaccessibleViaGeneratedCode(BindView.class, "fields", element)
|| isBindingInWrongPackage(BindView.class, element);
// Verify that the target type extends from View.
TypeMirror elementType = element.asType();
if (elementType.getKind() == TypeKind.TYPEVAR) {
TypeVariable typeVariable = (TypeVariable) elementType;
elementType = typeVariable.getUpperBound();
}
Name qualifiedName = enclosingElement.getQualifiedName();
Name simpleName = element.getSimpleName();
if (!isSubtypeOfType(elementType, VIEW_TYPE) && !isInterface(elementType)) {
if (elementType.getKind() == TypeKind.ERROR) {
note(element, "@%s field with unresolved type (%s) "
+ "must elsewhere be generated as a View or interface. (%s.%s)",
BindView.class.getSimpleName(), elementType, qualifiedName, simpleName);
} else {
error(element, "@%s fields must extend from View or be an interface. (%s.%s)",
BindView.class.getSimpleName(), qualifiedName, simpleName);
hasError = true;
}
}
if (hasError) {
return;
}
// Assemble information on the field.
int id = element.getAnnotation(BindView.class).value();
BindingSet.Builder builder = builderMap.get(enclosingElement);
Id resourceId = elementToId(element, BindView.class, id);
if (builder != null) {
String existingBindingName = builder.findExistingBindingName(resourceId);
if (existingBindingName != null) {
error(element, "Attempt to use @%s for an already bound ID %d on '%s'. (%s.%s)",
BindView.class.getSimpleName(), id, existingBindingName,
enclosingElement.getQualifiedName(), element.getSimpleName());
return;
}
} else {
builder = getOrCreateBindingBuilder(builderMap, enclosingElement);
}
String name = simpleName.toString();
TypeName type = TypeName.get(elementType);
boolean required = isFieldRequired(element);
builder.addField(resourceId, new FieldViewBinding(name, type, required));
// Add the type-erased version to the valid binding targets set.
erasedTargetNames.add(enclosingElement);
}
private void parseBindViews(Element element, Map<TypeElement, BindingSet.Builder> builderMap,
Set<TypeElement> erasedTargetNames) {
TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
// Start by verifying common generated code restrictions.
boolean hasError = isInaccessibleViaGeneratedCode(BindViews.class, "fields", element)
|| isBindingInWrongPackage(BindViews.class, element);
// Verify that the type is a List or an array.
TypeMirror elementType = element.asType();
String erasedType = doubleErasure(elementType);
TypeMirror viewType = null;
FieldCollectionViewBinding.Kind kind = null;
if (elementType.getKind() == TypeKind.ARRAY) {
ArrayType arrayType = (ArrayType) elementType;
viewType = arrayType.getComponentType();
kind = FieldCollectionViewBinding.Kind.ARRAY;
} else if (LIST_TYPE.equals(erasedType)) {
DeclaredType declaredType = (DeclaredType) elementType;
List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments();
if (typeArguments.size() != 1) {
error(element, "@%s List must have a generic component. (%s.%s)",
BindViews.class.getSimpleName(), enclosingElement.getQualifiedName(),
element.getSimpleName());
hasError = true;
} else {
viewType = typeArguments.get(0);
}
kind = FieldCollectionViewBinding.Kind.LIST;
} else {
error(element, "@%s must be a List or array. (%s.%s)", BindViews.class.getSimpleName(),
enclosingElement.getQualifiedName(), element.getSimpleName());
hasError = true;
}
if (viewType != null && viewType.getKind() == TypeKind.TYPEVAR) {
TypeVariable typeVariable = (TypeVariable) viewType;
viewType = typeVariable.getUpperBound();
}
// Verify that the target type extends from View.
if (viewType != null && !isSubtypeOfType(viewType, VIEW_TYPE) && !isInterface(viewType)) {
if (viewType.getKind() == TypeKind.ERROR) {
note(element, "@%s List or array with unresolved type (%s) "
+ "must elsewhere be generated as a View or interface. (%s.%s)",
BindViews.class.getSimpleName(), viewType, enclosingElement.getQualifiedName(),
element.getSimpleName());
} else {
error(element, "@%s List or array type must extend from View or be an interface. (%s.%s)",
BindViews.class.getSimpleName(), enclosingElement.getQualifiedName(),
element.getSimpleName());
hasError = true;
}
}
// Assemble information on the field.
String name = element.getSimpleName().toString();
int[] ids = element.getAnnotation(BindViews.class).value();
if (ids.length == 0) {
error(element, "@%s must specify at least one ID. (%s.%s)", BindViews.class.getSimpleName(),
enclosingElement.getQualifiedName(), element.getSimpleName());
hasError = true;
}
Integer duplicateId = findDuplicate(ids);
if (duplicateId != null) {
error(element, "@%s annotation contains duplicate ID %d. (%s.%s)",
BindViews.class.getSimpleName(), duplicateId, enclosingElement.getQualifiedName(),
element.getSimpleName());
hasError = true;
}
if (hasError) {
return;
}
TypeName type = TypeName.get(requireNonNull(viewType));
boolean required = isFieldRequired(element);
BindingSet.Builder builder = getOrCreateBindingBuilder(builderMap, enclosingElement);
builder.addFieldCollection(new FieldCollectionViewBinding(name, type, requireNonNull(kind),
new ArrayList<>(elementToIds(element, BindViews.class, ids).values()), required));
erasedTargetNames.add(enclosingElement);
}
@Override public Boolean visitTypeVariable(TypeVariable t, Void p) {
return t.getUpperBound() != null
&& t.getUpperBound().getKind().name().equals("INTERSECTION");
}
private void parseBindAnnotation(Element element, Map<TypeElement, BundleBindingAdapterGenerator> targetClassMap, Set<String> erasedTargetNames)
throws InvalidTypeException {
TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
// Verify that the target has all the appropriate information for type
TypeMirror type = element.asType();
if (element instanceof TypeVariable) {
TypeVariable typeVariable = (TypeVariable) type;
type = typeVariable.getUpperBound();
}
validateNotRequiredArguments(element);
validateBindingPackage(BindArgument.class, element);
validateForCodeGeneration(BindArgument.class, element);
Access access = getAccess(BindArgument.class, element, enclosingElement);
TypeMirror bundleSerializer = getAnnotationElementClass(element, BundleSerializer.class);
validateSerializer(element, BundleSerializer.class, bundleSerializer, PocketKnifeBundleSerializer.class);
// Assemble information on the bind point
String name = element.getSimpleName().toString();
String bundleType = null;
KeySpec key = getKey(element);
boolean canHaveDefault = false;
boolean needsToBeCast = false;
NotRequired notRequired = element.getAnnotation(NotRequired.class);
boolean required = notRequired == null;
int minSdk = Build.VERSION_CODES.FROYO;
if (!required) {
minSdk = notRequired.value();
}
if (bundleSerializer == null) {
bundleType = typeUtil.getBundleType(type);
canHaveDefault = !required && canHaveDefault(type, minSdk);
needsToBeCast = typeUtil.needToCastBundleType(type);
}
BundleBindingAdapterGenerator bundleBindingAdapterGenerator = getOrCreateTargetClass(targetClassMap, enclosingElement);
BundleFieldBinding binding = new BundleFieldBinding(BundleFieldBinding.AnnotationType.ARGUMENT, name, access, type, bundleType, key,
needsToBeCast, canHaveDefault, required, bundleSerializer);
bundleBindingAdapterGenerator.orRequired(required);
bundleBindingAdapterGenerator.addField(binding);
// Add the type-erased version to the valid targets set.
erasedTargetNames.add(enclosingElement.toString());
}