Java Code Examples for javax.lang.model.type.TypeVariable#getUpperBound()

The following examples show how to use javax.lang.model.type.TypeVariable#getUpperBound() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TypeUtils.java    From Mixin with MIT License 6 votes vote down vote up
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();
}
 
Example 2
Source File: SpringXMLConfigCompletionItem.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 3
Source File: AutoImport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 4
Source File: IntimateProcesser.java    From Intimate with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 5
Source File: AutoLayoutProcessor.java    From AutoLayout-Android with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 6
Source File: KratosProcessor.java    From Kratos with GNU Lesser General Public License v3.0 5 votes vote down vote up
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());
}
 
Example 7
Source File: CompilerTreeApiTest.java    From buck with Apache License 2.0 5 votes vote down vote up
protected TypeMirror getTypeParameterUpperBound(String typeName, int typeParameterIndex) {
  TypeParameterElement typeParameter =
      elements.getTypeElement(typeName).getTypeParameters().get(typeParameterIndex);
  TypeVariable typeVariable = (TypeVariable) typeParameter.asType();

  return typeVariable.getUpperBound();
}
 
Example 8
Source File: ScoopsProcesssor.java    From Scoops with Apache License 2.0 5 votes vote down vote up
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);
        }

    }
 
Example 9
Source File: ScoopsProcesssor.java    From Scoops with Apache License 2.0 5 votes vote down vote up
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);

}
 
Example 10
Source File: ControllerGenerator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 11
Source File: Utilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 12
Source File: MethodModelSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 13
Source File: DeclaredTypeCollector.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 14
Source File: IntentBindingProcessor.java    From pocketknife with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example 15
Source File: TypeUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 16
Source File: JavaSymbolProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 17
Source File: ButterKnifeProcessor.java    From butterknife with Apache License 2.0 4 votes vote down vote up
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);
}
 
Example 18
Source File: Utils.java    From paperparcel with Apache License 2.0 4 votes vote down vote up
@Override public Boolean visitTypeVariable(TypeVariable t, Void p) {
  return t.getUpperBound() != null
      && t.getUpperBound().getKind().name().equals("INTERSECTION");
}
 
Example 19
Source File: ButterKnifeProcessor.java    From butterknife with Apache License 2.0 4 votes vote down vote up
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);
}
 
Example 20
Source File: BundleBindingProcessor.java    From pocketknife with Apache License 2.0 4 votes vote down vote up
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());
}