Java Code Examples for com.squareup.javapoet.WildcardTypeName#subtypeOf()

The following examples show how to use com.squareup.javapoet.WildcardTypeName#subtypeOf() . 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: PsiTypeUtils.java    From litho with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
static TypeName getWildcardTypeName(String wildcardTypeName) {
  if (wildcardTypeName.equals("?")) {
    return WildcardTypeName.subtypeOf(Object.class);
  }
  Matcher matcher = SUPER_PATTERN.matcher(wildcardTypeName);
  if (matcher.find()) {
    return WildcardTypeName.supertypeOf(guessClassName(matcher.group(1)));
  }
  matcher = EXTENDS_PATTERN.matcher(wildcardTypeName);
  if (matcher.find()) {
    return WildcardTypeName.subtypeOf(guessClassName(matcher.group(1)));
  }
  return guessClassName(wildcardTypeName);
}
 
Example 2
Source File: BuilderGenerator.java    From litho with Apache License 2.0 5 votes vote down vote up
private static TypeName[] getBuilderGenericTypes(TypeName type, ClassName builderClass) {
  if (builderClass.equals(ClassNames.COMPONENT_BUILDER)
      || builderClass.equals(ClassNames.SECTION_BUILDER)) {
    return new TypeName[] {WildcardTypeName.subtypeOf(TypeName.OBJECT)};
  } else {
    final TypeName typeParameter =
        type instanceof ParameterizedTypeName
                && !((ParameterizedTypeName) type).typeArguments.isEmpty()
            ? ((ParameterizedTypeName) type).typeArguments.get(0)
            : WildcardTypeName.subtypeOf(ClassNames.COMPONENT);

    return new TypeName[] {typeParameter};
  }
}
 
Example 3
Source File: MatcherGenerator.java    From litho with Apache License 2.0 5 votes vote down vote up
private static TypeName[] getBuilderGenericTypes(
    final TypeName type, final ClassName builderClass) {
  final TypeName typeParameter =
      type instanceof ParameterizedTypeName
              && !((ParameterizedTypeName) type).typeArguments.isEmpty()
          ? ((ParameterizedTypeName) type).typeArguments.get(0)
          : WildcardTypeName.subtypeOf(ClassNames.COMPONENT_LIFECYCLE);

  if (builderClass.equals(ClassNames.COMPONENT_BUILDER)) {
    return new TypeName[] {WildcardTypeName.subtypeOf(TypeName.OBJECT)};
  } else {
    return new TypeName[] {typeParameter};
  }
}
 
Example 4
Source File: TypeProvider.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private TypeName listParameterType(ListModel listModel, boolean preserveEnum) {
    MemberModel elementModel = listModel.getListMemberModel();
    TypeName listElementType = parameterType(elementModel, preserveEnum);
    if (isContainerType(elementModel)) {
        listElementType = WildcardTypeName.subtypeOf(listElementType);
    }
    return ParameterizedTypeName.get(ClassName.get(Collection.class), listElementType);
}
 
Example 5
Source File: TypeProvider.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private TypeName mapValueParameterType(MapModel mapModel, boolean preserveEnum) {
    TypeName valueType = parameterType(mapModel.getValueModel(), preserveEnum);
    if (mapModel.getValueModel().isList()) {
        valueType = WildcardTypeName.subtypeOf(valueType);
    }

    return valueType;
}
 
Example 6
Source File: JTypeName.java    From gwt-jackson with Apache License 2.0 5 votes vote down vote up
private WildcardTypeName wildcardName( JWildcardType type ) {
    switch ( type.getBoundType() ) {
        case SUPER:
            return WildcardTypeName.supertypeOf( typeName( type.getFirstBound() ) );
        default:
            return WildcardTypeName.subtypeOf( typeName( type.getFirstBound() ) );
    }
}
 
Example 7
Source File: TargetViewBinder.java    From Witch-Android with Apache License 2.0 4 votes vote down vote up
private TypeSpec createBinderFactory() throws WitchException {
    TypeSpec.Builder targetViewBinderFactory =
            TypeSpec.classBuilder(targetViewBinderClassName)
                    .addModifiers(Modifier.PUBLIC, Modifier.FINAL)
                    .addSuperinterface(TARGET_VIEW_BINDER_FACTORY);

    TypeName targetViewBinderType = ParameterizedTypeName.get(TARGET_VIEW_BINDER, targetTypeName
            , viewHolderClassName);
    MethodSpec.Builder createBinder = MethodSpec.methodBuilder("createBinder")
            .addModifiers(Modifier.PUBLIC)
            .returns(targetViewBinderType);

    TypeName dataTypeName = WildcardTypeName.subtypeOf(Object.class);
    TypeName viewTypeName = WildcardTypeName.subtypeOf(Object.class);
    TypeName viewBinderType = ParameterizedTypeName.get(VIEW_BINDER, targetTypeName, viewTypeName,
            dataTypeName, viewHolderClassName);
    createBinder.addStatement("$T viewBinders = new $T<>()",
            ParameterizedTypeName.get(LIST, viewBinderType), ARRAY_LIST);
    createBinder.addCode("\n");

    TypeSpec.Builder targetViewBinder =
            TypeSpec.anonymousClassBuilder("viewBinders")
                    .addSuperinterface(targetViewBinderType);

    // Description
    MethodSpec.Builder describeTarget = MethodSpec.methodBuilder("describeTarget")
            .addParameter(targetTypeName, "target")
            .addModifiers(Modifier.PUBLIC)
            .addStatement("$T description = \"\"", STRING)
            .returns(STRING);

    // All view binders
    for (ViewBinder.Builder builder: viewBinders) {
        ViewBinder binder = builder.build();
        String data = builder.getPropertyName();
        createBinder.addComment("===========================================");
        createBinder.addComment("Binder - " + data + " BEGIN");
        createBinder.addStatement("viewBinders.add($L)", binder.newInstance());

        String dataName = binder.getDataName();
        String accessData = binder.getAccessData();
        if (dataName != null && accessData != null) {
            describeTarget.addStatement("description += \"$N : \" + \"\" + $N", dataName, accessData);
            describeTarget.addStatement("description += \"\\n\"");
        }

        createBinder.addComment("Binder - " + data + " END");
        createBinder.addComment("===========================================");
        createBinder.addCode("\n");
    }

    describeTarget.addStatement("return description");
    targetViewBinder.addMethod(describeTarget.build());

    // Return
    createBinder.addStatement("return $L", targetViewBinder.build());
    targetViewBinderFactory.addMethod(createBinder.build());

    return targetViewBinderFactory.build();
}
 
Example 8
Source File: WriterUtil.java    From sqlitemagic with Apache License 2.0 4 votes vote down vote up
public static WildcardTypeName anyWildcardTypeName() {
  return WildcardTypeName.subtypeOf(Object.class);
}