Search in sources :

Example 1 with AnnotationSpec

use of com.squareup.javapoet.AnnotationSpec in project tiger by google.

the class Utils method getElementKeyForBuiltinBinding.

/**
   * Return {@link NewBindingKey} for element of the give {@link NewBindingKey} that
   * has built-in binding, null if not built-in building.
   */
@Nullable
public static NewBindingKey getElementKeyForBuiltinBinding(NewBindingKey key) {
    if (!hasBuiltinBinding(key)) {
        return null;
    }
    ParameterizedTypeName parameterizedTypeName = (ParameterizedTypeName) key.getTypeName();
    TypeName typeName = Iterables.getOnlyElement(parameterizedTypeName.typeArguments);
    AnnotationSpec qualifier = key.getQualifier();
    return NewBindingKey.get(typeName, qualifier);
}
Also used : TypeName(com.squareup.javapoet.TypeName) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) ArrayTypeName(com.squareup.javapoet.ArrayTypeName) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) Nullable(javax.annotation.Nullable)

Example 2 with AnnotationSpec

use of com.squareup.javapoet.AnnotationSpec in project tiger by google.

the class NewInjectorGenerator method getSourceCodeName.

/**
   * Returns {@link #getSourceCodeName}(qualifier) + "__" + {@link #getSourceCodeName} (type), or
   * {@link #getSourceCodeName}(type) if no qualifier.
   */
private String getSourceCodeName(NewBindingKey key) {
    // System.out.println("getMethodName for key: " + key);
    StringBuilder builder = new StringBuilder();
    AnnotationSpec qualifier = key.getQualifier();
    if (qualifier != null) {
        ClassName qualifierType = (ClassName) qualifier.type;
        builder.append(Utils.getSourceCodeName(qualifierType));
        /**
       * TODO(freeman): handle all illegal chars.
       */
        if (Utils.getCanonicalName(qualifierType).equals(Named.class.getCanonicalName())) {
            builder.append("_").append(qualifier.members.get("value").toString().replace("\"", "_").replace("[", "_").replace("]", "_"));
        }
        builder.append("__");
    }
    builder.append(Utils.getSourceCodeName(key.getTypeName()));
    return builder.toString();
}
Also used : Named(javax.inject.Named) ClassName(com.squareup.javapoet.ClassName) AnnotationSpec(com.squareup.javapoet.AnnotationSpec)

Example 3 with AnnotationSpec

use of com.squareup.javapoet.AnnotationSpec in project tiger by google.

the class Utils method specializeIfNeeded.

/**
   * Returns a {@link TypeName} with TypeVariable replaced by specialType if
   * applicable.
   */
public static NewBindingKey specializeIfNeeded(NewBindingKey dependencyKey, Map<TypeVariableName, TypeName> map) {
    AnnotationSpec qualifier = dependencyKey.getQualifier();
    TypeName typeName = specializeIfNeeded(dependencyKey.getTypeName(), map);
    return NewBindingKey.get(typeName, qualifier);
}
Also used : TypeName(com.squareup.javapoet.TypeName) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) ArrayTypeName(com.squareup.javapoet.ArrayTypeName) AnnotationSpec(com.squareup.javapoet.AnnotationSpec)

Example 4 with AnnotationSpec

use of com.squareup.javapoet.AnnotationSpec in project epoxy by airbnb.

the class AttributeInfo method buildAnnotationLists.

/**
   * Keeps track of annotations on the attribute so that they can be used in the generated setter
   * and getter method. Setter and getter annotations are stored separately since the annotation may
   * not target both method and parameter types.
   */
private void buildAnnotationLists(List<? extends AnnotationMirror> annotationMirrors) {
    for (AnnotationMirror annotationMirror : annotationMirrors) {
        if (!annotationMirror.getElementValues().isEmpty()) {
            // Not supporting annotations with values for now
            continue;
        }
        ClassName annotationClass = ClassName.bestGuess(annotationMirror.getAnnotationType().toString());
        if (annotationClass.equals(ClassName.get(EpoxyAttribute.class))) {
            // Don't include our own annotation
            continue;
        }
        DeclaredType annotationType = annotationMirror.getAnnotationType();
        // A target may exist on an annotation type to specify where the annotation can
        // be used, for example fields, methods, or parameters.
        Target targetAnnotation = annotationType.asElement().getAnnotation(Target.class);
        // Allow all target types if no target was specified on the annotation
        List<ElementType> elementTypes = Arrays.asList(targetAnnotation == null ? ElementType.values() : targetAnnotation.value());
        AnnotationSpec annotationSpec = AnnotationSpec.builder(annotationClass).build();
        if (elementTypes.contains(ElementType.PARAMETER)) {
            setterAnnotations.add(annotationSpec);
        }
        if (elementTypes.contains(ElementType.METHOD)) {
            getterAnnotations.add(annotationSpec);
        }
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) Target(java.lang.annotation.Target) ElementType(java.lang.annotation.ElementType) ClassName(com.squareup.javapoet.ClassName) AnnotationSpec(com.squareup.javapoet.AnnotationSpec) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

AnnotationSpec (com.squareup.javapoet.AnnotationSpec)4 ArrayTypeName (com.squareup.javapoet.ArrayTypeName)2 ClassName (com.squareup.javapoet.ClassName)2 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)2 TypeName (com.squareup.javapoet.TypeName)2 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)2 ElementType (java.lang.annotation.ElementType)1 Target (java.lang.annotation.Target)1 Nullable (javax.annotation.Nullable)1 Named (javax.inject.Named)1 AnnotationMirror (javax.lang.model.element.AnnotationMirror)1 DeclaredType (javax.lang.model.type.DeclaredType)1