com.squareup.javapoet.TypeVariableName Java Examples

The following examples show how to use com.squareup.javapoet.TypeVariableName. 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: MethodParamModelUtilsTest.java    From litho with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetTypeVariablesOnParameterizedTypeRecursive() {
  TypeVariableName type1 = TypeVariableName.get("R");
  TypeVariableName type2 = TypeVariableName.get("S");
  TypeVariableName type3 = TypeVariableName.get("T");

  TypeName type =
      ParameterizedTypeName.get(
          ClassName.bestGuess("java.lang.Object"),
          type1,
          type2,
          ParameterizedTypeName.get(ClassName.bestGuess("java.lang.Object"), type3));
  assertThat(MethodParamModelUtils.getTypeVariables(type)).hasSize(3);
  assertThat(MethodParamModelUtils.getTypeVariables(type)).contains(type1);
  assertThat(MethodParamModelUtils.getTypeVariables(type)).contains(type2);
  assertThat(MethodParamModelUtils.getTypeVariables(type)).contains(type3);
}
 
Example #2
Source File: SolidityFunctionWrapper.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
private static MethodSpec.Builder getDeployMethodSpec(
        String className, Class authType, String authName, boolean isPayable) {
    MethodSpec.Builder builder = MethodSpec.methodBuilder("deploy")
            .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
            .returns(
                    buildRemoteCall(TypeVariableName.get(className, Type.class)))
            .addParameter(Web3j.class, WEB3J)
            .addParameter(authType, authName)
            .addParameter(BigInteger.class, GAS_PRICE)
            .addParameter(BigInteger.class, GAS_LIMIT);
    if (isPayable) {
        return builder.addParameter(BigInteger.class, INITIAL_VALUE);
    } else {
        return builder;
    }
}
 
Example #3
Source File: SolidityFunctionWrapper.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
private static MethodSpec.Builder getDeployMethodSpec(
        String className, Class authType, String authName,
        boolean isPayable, boolean withGasProvider) {
    MethodSpec.Builder builder = MethodSpec.methodBuilder("deploy")
            .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
            .returns(
                    buildRemoteCall(TypeVariableName.get(className, Type.class)))
            .addParameter(Web3j.class, WEB3J)
            .addParameter(authType, authName);
    if (isPayable && !withGasProvider) {
        return builder.addParameter(BigInteger.class, GAS_PRICE)
                .addParameter(BigInteger.class, GAS_LIMIT)
                .addParameter(BigInteger.class, INITIAL_VALUE);
    } else if (isPayable && withGasProvider) {
        return builder.addParameter(GasProvider.class, CONTRACT_GAS_PROVIDER)
                .addParameter(BigInteger.class, INITIAL_VALUE);
    } else if (!isPayable && withGasProvider) {
        return builder.addParameter(GasProvider.class, CONTRACT_GAS_PROVIDER);
    } else {
        return builder.addParameter(BigInteger.class, GAS_PRICE)
                .addParameter(BigInteger.class, GAS_LIMIT);
    }
}
 
Example #4
Source File: BaseClientBuilderInterface.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Override
public TypeSpec poetSpec() {
    TypeSpec.Builder builder = PoetUtils.createInterfaceBuilder(builderInterfaceName)
                    .addTypeVariable(PoetUtils.createBoundedTypeVariableName("B", builderInterfaceName, "B", "C"))
                    .addTypeVariable(TypeVariableName.get("C"))
                    .addSuperinterface(PoetUtils.createParameterizedTypeName(AwsClientBuilder.class, "B", "C"))
                    .addJavadoc(getJavadoc());

    if (model.getEndpointOperation().isPresent()) {
        if (model.getCustomizationConfig().isEnableEndpointDiscoveryMethodRequired()) {
            builder.addMethod(enableEndpointDiscovery());
        }

        builder.addMethod(endpointDiscovery());
    }

    if (model.getCustomizationConfig().getServiceSpecificClientConfigClass() != null) {
        builder.addMethod(serviceConfigurationMethod());
        builder.addMethod(serviceConfigurationConsumerBuilderMethod());
    }

    return builder.build();
}
 
Example #5
Source File: StateGenerator.java    From litho with Apache License 2.0 6 votes vote down vote up
private static String getStateContainerClassNameWithTypeVars(SpecModel specModel) {
  if (specModel.getStateValues().isEmpty()) {
    return specModel.getStateContainerClass().toString();
  }

  final StringBuilder stringBuilder = new StringBuilder();

  final ImmutableList<TypeVariableName> typeVariables = specModel.getTypeVariables();
  if (!typeVariables.isEmpty()) {
    stringBuilder.append("<");
    for (int i = 0, size = typeVariables.size(); i < size - 1; i++) {
      stringBuilder.append(typeVariables.get(i).name).append(", ");
    }

    stringBuilder.append(typeVariables.get(typeVariables.size() - 1)).append(">");
  }

  return getStateContainerClassName(specModel) + stringBuilder;
}
 
Example #6
Source File: PsiTypeVariablesExtractor.java    From litho with Apache License 2.0 6 votes vote down vote up
public static ImmutableList<TypeVariableName> getTypeVariables(PsiClass psiClass) {
  PsiTypeParameter[] psiTypeParameters = psiClass.getTypeParameters();

  final List<TypeVariableName> typeVariables = new ArrayList<>(psiTypeParameters.length);
  for (PsiTypeParameter psiTypeParameter : psiTypeParameters) {
    final PsiReferenceList extendsList = psiTypeParameter.getExtendsList();
    final PsiClassType[] psiClassTypes = extendsList.getReferencedTypes();

    final TypeName[] boundsTypeNames = new TypeName[psiClassTypes.length];
    for (int i = 0, size = psiClassTypes.length; i < size; i++) {
      boundsTypeNames[i] = PsiTypeUtils.getTypeName(psiClassTypes[i]);
    }

    final TypeVariableName typeVariable =
        TypeVariableName.get(psiTypeParameter.getName(), boundsTypeNames);
    typeVariables.add(typeVariable);
  }

  return ImmutableList.copyOf(typeVariables);
}
 
Example #7
Source File: CopyReplacer.java    From data-mediator with Apache License 2.0 6 votes vote down vote up
@Override
public TypeName replaceReturnType(String currentPkg, String directParentInterface, String curClassname,
                                  List<? extends TypeMirror> superInterfaces, TypeName superClass, ExecutableElement method) {
    final TypeMirror returnType = method.getReturnType();
    switch (method.getSimpleName().toString()){
        case TypeCopyableFiller.NAME_COPY: {
            switch (returnType.getKind()) {
                case TYPEVAR: //泛型
                    return  TypeVariableName.get(directParentInterface);

                default:
                    return TypeName.get(returnType);
            }
        }

        default:
            return TypeName.get(returnType);

    }

}
 
Example #8
Source File: SpecMethodModel.java    From litho with Apache License 2.0 6 votes vote down vote up
public SpecMethodModel(
    ImmutableList<Annotation> annotations,
    ImmutableList<Modifier> modifiers,
    CharSequence name,
    TypeSpec returnTypeSpec,
    ImmutableList<TypeVariableName> typeVariables,
    ImmutableList<MethodParamModel> methodParams,
    Object representedObject,
    @Nullable A typeModel) {
  this.annotations = annotations;
  this.modifiers = modifiers;
  this.name = name;
  this.returnTypeSpec = returnTypeSpec;
  this.returnType = returnTypeSpec != null ? returnTypeSpec.getTypeName() : null;
  this.typeVariables = typeVariables;
  this.methodParams = methodParams;
  this.representedObject = representedObject;
  this.typeModel = typeModel;
}
 
Example #9
Source File: SpecParser.java    From dataenum with Apache License 2.0 6 votes vote down vote up
public static Spec parse(Element element, ProcessingEnvironment processingEnv) {
  Messager messager = processingEnv.getMessager();

  if (element.getKind() != ElementKind.INTERFACE) {
    messager.printMessage(
        Diagnostic.Kind.ERROR, "@DataEnum can only be used on interfaces.", element);
    return null;
  }

  TypeElement dataEnum = (TypeElement) element;

  List<TypeVariableName> typeVariableNames = new ArrayList<>();
  for (TypeParameterElement typeParameterElement : dataEnum.getTypeParameters()) {
    typeVariableNames.add(TypeVariableName.get(typeParameterElement));
  }

  List<Value> values = ValuesParser.parse(dataEnum, processingEnv);
  if (values == null) {
    return null;
  }

  ClassName enumInterface = ClassName.get(dataEnum);
  return new Spec(enumInterface, typeVariableNames, values);
}
 
Example #10
Source File: TypeVariablesExtractor.java    From litho with Apache License 2.0 6 votes vote down vote up
/** Get the type variables from the given {@link TypeElement}. */
public static List<TypeVariableName> getTypeVariables(TypeElement typeElement) {
  final List<? extends TypeParameterElement> typeParameters = typeElement.getTypeParameters();
  final int typeParameterCount = typeParameters.size();

  final List<TypeVariableName> typeVariables = new ArrayList<>(typeParameterCount);
  for (TypeParameterElement typeParameterElement : typeParameters) {
    final int boundTypesCount = typeParameterElement.getBounds().size();

    final TypeName[] boundsTypeNames = new TypeName[boundTypesCount];
    for (int i = 0; i < boundTypesCount; i++) {
      boundsTypeNames[i] = TypeName.get(typeParameterElement.getBounds().get(i));
    }

    final TypeVariableName typeVariable =
        TypeVariableName.get(typeParameterElement.getSimpleName().toString(), boundsTypeNames);
    typeVariables.add(typeVariable);
  }

  return typeVariables;
}
 
Example #11
Source File: ValueTypeFactory.java    From dataenum with Apache License 2.0 6 votes vote down vote up
private static MethodSpec createAsSpecMethod(OutputValue value, OutputSpec spec) {
  List<TypeVariableName> missingTypeVariables = extractMissingTypeVariablesForValue(value, spec);

  Builder builder =
      MethodSpec.methodBuilder("as" + spec.outputClass().simpleName())
          .addModifiers(Modifier.PUBLIC, Modifier.FINAL)
          .returns(spec.parameterizedOutputClass())
          .addTypeVariables(missingTypeVariables)
          .addStatement("return ($T) this", spec.parameterizedOutputClass());

  // if there are type variables that this sub-type doesn't use, they will lead to 'unchecked
  // cast'
  // warnings when compiling the generated code. These warnings are safe to suppress, since this
  // sub type will never use those type variables.
  if (!missingTypeVariables.isEmpty()) {
    builder.addAnnotation(SUPPRESS_UNCHECKED_WARNINGS);
  }

  return builder.build();
}
 
Example #12
Source File: SolidityFunctionWrapper.java    From web3sdk with Apache License 2.0 6 votes vote down vote up
private static MethodSpec.Builder getDeployMethodSpec(
        String className,
        Class authType,
        String authName,
        boolean isPayable,
        boolean withGasProvider) {
    MethodSpec.Builder builder =
            MethodSpec.methodBuilder("deploy")
                    .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
                    .returns(buildRemoteCall(TypeVariableName.get(className, Type.class)))
                    .addParameter(Web3j.class, WEB3J)
                    .addParameter(authType, authName);
    if (isPayable && !withGasProvider) {
        return builder.addParameter(BigInteger.class, GAS_PRICE)
                .addParameter(BigInteger.class, GAS_LIMIT)
                .addParameter(BigInteger.class, INITIAL_VALUE);
    } else if (isPayable && withGasProvider) {
        return builder.addParameter(ContractGasProvider.class, CONTRACT_GAS_PROVIDER)
                .addParameter(BigInteger.class, INITIAL_VALUE);
    } else if (!isPayable && withGasProvider) {
        return builder.addParameter(ContractGasProvider.class, CONTRACT_GAS_PROVIDER);
    } else {
        return builder.addParameter(BigInteger.class, GAS_PRICE)
                .addParameter(BigInteger.class, GAS_LIMIT);
    }
}
 
Example #13
Source File: ValueTypeFactory.java    From dataenum with Apache License 2.0 6 votes vote down vote up
private static TypeName getSuperclassForValue(OutputValue value, OutputSpec spec)
    throws ParserException {
  if (!spec.hasTypeVariables()) {
    return spec.outputClass();
  }

  List<TypeName> superParameters = new ArrayList<>();
  for (TypeVariableName typeVariable : spec.typeVariables()) {
    if (Iterables.contains(value.typeVariables(), typeVariable)) {
      superParameters.add(typeVariable);
    } else {
      if (typeVariable.bounds.size() == 0) {
        superParameters.add(TypeName.OBJECT);
      } else if (typeVariable.bounds.size() == 1) {
        superParameters.add(typeVariable.bounds.get(0));
      } else {
        throw new ParserException("More than one generic type bound is not supported ");
      }
    }
  }

  return ParameterizedTypeName.get(
      spec.outputClass(), superParameters.toArray(new TypeName[] {}));
}
 
Example #14
Source File: MapMethods.java    From dataenum with Apache License 2.0 6 votes vote down vote up
private MethodSpec.Builder createFoldSignature(Iterable<TypeVariableName> availableTypeVariables)
    throws ParserException {
  MethodSpec.Builder builder =
      MethodSpec.methodBuilder("map")
          .addTypeVariable(FOLD_RETURN_TYPE)
          .addModifiers(Modifier.PUBLIC)
          .returns(FOLD_RETURN_TYPE);

  for (OutputValue arg : values) {
    TypeName visitor =
        ParameterizedTypeName.get(
            ClassName.get(Function.class),
            TypeVariableUtils.withoutMissingTypeVariables(
                arg.parameterizedOutputClass(), availableTypeVariables),
            FOLD_RETURN_TYPE);

    builder.addParameter(
        ParameterSpec.builder(visitor, asCamelCase(arg.name()))
            .addAnnotation(Nonnull.class)
            .build());
  }

  return builder;
}
 
Example #15
Source File: MatchMethods.java    From dataenum with Apache License 2.0 6 votes vote down vote up
private MethodSpec.Builder createFoldVoidSignature(
    Iterable<TypeVariableName> availableTypeVariables) throws ParserException {
  MethodSpec.Builder builder =
      MethodSpec.methodBuilder("match").addModifiers(Modifier.PUBLIC).returns(void.class);

  for (OutputValue arg : values) {
    TypeName visitor =
        ParameterizedTypeName.get(
            ClassName.get(Consumer.class),
            withoutMissingTypeVariables(arg.parameterizedOutputClass(), availableTypeVariables));

    builder.addParameter(
        ParameterSpec.builder(visitor, asCamelCase(arg.name()))
            .addAnnotation(Nonnull.class)
            .build());
  }

  return builder;
}
 
Example #16
Source File: OutputValueFactory.java    From dataenum with Apache License 2.0 6 votes vote down vote up
private static boolean typeNeedsTypeVariable(TypeName type, TypeVariableName typeVariable) {
  if (typeVariable.equals(type)) {
    return true;
  }

  if (type instanceof ParameterizedTypeName) {
    ParameterizedTypeName parameterized = ((ParameterizedTypeName) type);
    for (TypeName typeArgument : parameterized.typeArguments) {
      if (typeVariable.equals(typeArgument)) {
        return true;
      }
      if (typeNeedsTypeVariable(typeArgument, typeVariable)) {
        return true;
      }
    }
  }

  if (type instanceof ArrayTypeName) {
    ArrayTypeName arrayType = (ArrayTypeName) type;
    if (typeVariable.equals(arrayType.componentType)) {
      return true;
    }
  }
  return false;
}
 
Example #17
Source File: BaseClientBuilderInterface.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private MethodSpec serviceConfigurationConsumerBuilderMethod() {
    ClassName serviceConfiguration = ClassName.get(basePackage,
                                                    model.getCustomizationConfig().getServiceSpecificClientConfigClass());
    TypeName consumerBuilder = ParameterizedTypeName.get(ClassName.get(Consumer.class),
                                                         serviceConfiguration.nestedClass("Builder"));
    return MethodSpec.methodBuilder("serviceConfiguration")
                     .addModifiers(Modifier.DEFAULT, Modifier.PUBLIC)
                     .returns(TypeVariableName.get("B"))
                     .addParameter(consumerBuilder, "serviceConfiguration")
                     .addStatement("return serviceConfiguration($T.builder().applyMutation(serviceConfiguration).build())",
                                   serviceConfiguration)
                     .build();
}
 
Example #18
Source File: InvokingMessageProcessor.java    From SmartEventBus with Apache License 2.0 5 votes vote down vote up
private String generateEventInterfaceClass(List<EventInfo> events, String originalPackageName, String originalClassName) {
    String interfaceName = generateClassName(originalClassName);
    TypeSpec.Builder builder = TypeSpec.interfaceBuilder(interfaceName)
            .addModifiers(Modifier.PUBLIC)
            .addSuperinterface(IEventsDefine.class)
            .addJavadoc("Auto generate code, do not modify!!!");
    for (EventInfo event : events) {
        ClassName lebClassName = ClassName.get(LEB_PACKAGE_NAME, LEB_CLASS_NAME);
        ClassName obsClassName = lebClassName.nestedClass(LEB_OBSERVER_CLASS_NAME);
        TypeName returnType;
        String eventTypeStr = event.getType();
        if (eventTypeStr == null || eventTypeStr.length() == 0) {
            returnType = ParameterizedTypeName.get(obsClassName, ClassName.get(Object.class));
        } else {
            Type eventType = getType(eventTypeStr);
            if (eventType != null) {
                returnType = ParameterizedTypeName.get(obsClassName, ClassName.get(eventType));
            } else {
                returnType = ParameterizedTypeName.get(obsClassName, TypeVariableName.get(eventTypeStr));
            }
        }
        MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(event.getName())
                .addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
                .returns(returnType);
        builder.addMethod(methodBuilder.build());
    }
    TypeSpec typeSpec = builder.build();
    String packageName = originalPackageName + GEN_PKG;
    try {
        JavaFile.builder(packageName, typeSpec)
                .build()
                .writeTo(filer);
    } catch (IOException e) {
        e.printStackTrace();
    }
    String className = packageName + "." + interfaceName;
    System.out.println(TAG + "generateEventInterfaceClass: " + className);
    return className;
}
 
Example #19
Source File: OutputValueFactory.java    From dataenum with Apache License 2.0 5 votes vote down vote up
static OutputValue create(Value value, ClassName specOutputClass, Spec spec)
    throws ParserException {
  ClassName outputClass = specOutputClass.nestedClass(value.name());
  Iterable<TypeVariableName> typeVariables = getTypeVariables(value, spec.typeVariables());

  List<Parameter> parameters = new ArrayList<>();
  for (Parameter parameter : value.parameters()) {
    parameters.add(parameterWithoutDataEnumSuffix(parameter));
  }

  return new OutputValue(
      outputClass, value.name(), value.javadoc(), parameters, typeVariables, value.annotations());
}
 
Example #20
Source File: BaseClientBuilderInterface.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private MethodSpec serviceConfigurationMethod() {
    ClassName serviceConfiguration = ClassName.get(basePackage,
                                                    model.getCustomizationConfig().getServiceSpecificClientConfigClass());
    return MethodSpec.methodBuilder("serviceConfiguration")
                     .addModifiers(Modifier.ABSTRACT, Modifier.PUBLIC)
                     .returns(TypeVariableName.get("B"))
                     .addParameter(serviceConfiguration, "serviceConfiguration")
                     .build();
}
 
Example #21
Source File: OutputValueFactory.java    From dataenum with Apache License 2.0 5 votes vote down vote up
private static Iterable<TypeVariableName> getTypeVariables(
    Value value, Iterable<TypeVariableName> typeVariables) {
  Set<TypeVariableName> output = new LinkedHashSet<>();
  for (TypeVariableName typeVariable : typeVariables) {
    for (Parameter parameter : value.parameters()) {
      if (typeNeedsTypeVariable(parameter.type(), typeVariable)) {
        output.add(typeVariable);
      }
    }
  }
  return output;
}
 
Example #22
Source File: MethodExtractorUtils.java    From litho with Apache License 2.0 5 votes vote down vote up
static List<TypeVariableName> getTypeVariables(ExecutableElement method) {
  final List<TypeVariableName> typeVariables = new ArrayList<>();
  for (TypeParameterElement typeParameterElement : method.getTypeParameters()) {
    typeVariables.add(
        TypeVariableName.get(
            typeParameterElement.getSimpleName().toString(), getBounds(typeParameterElement)));
  }

  return typeVariables;
}
 
Example #23
Source File: ValueTypeFactory.java    From dataenum with Apache License 2.0 5 votes vote down vote up
static List<TypeVariableName> extractMissingTypeVariablesForValue(
    OutputValue value, OutputSpec spec) {
  List<TypeVariableName> missingTypeVariables = new ArrayList<>();
  for (TypeVariableName typeVariableName : spec.typeVariables()) {
    if (!Iterables.contains(value.typeVariables(), typeVariableName)) {
      missingTypeVariables.add(typeVariableName);
    }
  }
  return missingTypeVariables;
}
 
Example #24
Source File: TestSpecModel.java    From litho with Apache License 2.0 5 votes vote down vote up
public TestSpecModel(
    String qualifiedSpecClassName,
    String componentClassName,
    ImmutableList<PropModel> props,
    ImmutableList<InjectPropModel> injectProps,
    ImmutableList<BuilderMethodModel> builderMethodModels,
    ImmutableList<PropJavadocModel> propJavadocs,
    ImmutableList<TypeVariableName> typeVariables,
    SpecModel enclosedSpecModel,
    TestSpecGenerator testSpecGenerator,
    String classJavadoc,
    @Nullable DependencyInjectionHelper dependencyInjectionHelper) {
  mSpecModel =
      SpecModelImpl.newBuilder()
          .qualifiedSpecClassName(qualifiedSpecClassName)
          .componentClassName(componentClassName)
          .componentClass(ClassNames.COMPONENT)
          .props(props)
          .injectProps(injectProps)
          .extraBuilderMethods(builderMethodModels)
          .classJavadoc(classJavadoc)
          .propJavadocs(propJavadocs)
          .typeVariables(typeVariables)
          .representedObject(enclosedSpecModel)
          .dependencyInjectionHelper(dependencyInjectionHelper)
          .build();
  mEnclosedSpecModel = enclosedSpecModel;
  mTestSpecGenerator = testSpecGenerator;
}
 
Example #25
Source File: DataLoaderProcessor.java    From DataLoader with Apache License 2.0 5 votes vote down vote up
private TypeName getTypeName(String name) {
    if (name == null || name.length() == 0) {
        return null;
    }
    java.lang.reflect.Type type = getType(name);
    if (type != null) {
        return ClassName.get(type);
    } else {
        return TypeVariableName.get(name);
    }
}
 
Example #26
Source File: OutputValue.java    From dataenum with Apache License 2.0 5 votes vote down vote up
public OutputValue(
    ClassName outputClass,
    String name,
    String javadoc,
    Iterable<Parameter> parameters,
    Iterable<TypeVariableName> typeVariables,
    Iterable<AnnotationSpec> annotations) {
  this.outputClass = outputClass;
  this.name = name;
  this.javadoc = javadoc;
  this.parameters = parameters;
  this.typeVariables = typeVariables;
  this.annotations = annotations;
}
 
Example #27
Source File: OutputValue.java    From dataenum with Apache License 2.0 5 votes vote down vote up
public TypeName parameterizedOutputClass() {
  if (!hasTypeVariables()) {
    return outputClass();
  }

  TypeName[] typeNames = Iterables.toArray(typeVariables(), TypeVariableName.class);
  return ParameterizedTypeName.get(outputClass(), typeNames);
}
 
Example #28
Source File: AbiTypesGenerator.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
private <T extends Type> void generateStaticArrayTypes(
        Class<T> superclass, String path) throws IOException {
    String packageName = createPackageName(superclass);
    ClassName className;

    for (int length = 1; length <= StaticArray.MAX_SIZE_OF_STATIC_ARRAY; length++) {

        TypeVariableName typeVariableName = TypeVariableName.get("T").withBounds(Type.class);

        MethodSpec constructorSpec = MethodSpec.constructorBuilder()
                .addModifiers(Modifier.PUBLIC)
                .addParameter(ParameterizedTypeName.get(ClassName.get(List.class),
                        typeVariableName), "values")
                .addStatement("super($L, $N)", length, "values")
                .build();

        MethodSpec arrayOverloadConstructorSpec = MethodSpec.constructorBuilder()
                .addAnnotation(SafeVarargs.class)
                .addModifiers(Modifier.PUBLIC)
                .addParameter(ArrayTypeName.of(typeVariableName), "values")
                .varargs()
                .addStatement("super($L, $N)", length, "values")
                .build();

        className = ClassName.get(packageName, superclass.getSimpleName() + length);

        TypeSpec bytesType = TypeSpec
                .classBuilder(className.simpleName())
                .addTypeVariable(typeVariableName)
                .addJavadoc(CODEGEN_WARNING)
                .superclass(ParameterizedTypeName.get(ClassName.get(superclass),
                        typeVariableName))
                .addModifiers(Modifier.PUBLIC)
                .addMethods(Arrays.asList(constructorSpec, arrayOverloadConstructorSpec))
                .build();

        write(packageName, bytesType, path);
    }
}
 
Example #29
Source File: MethodParamModelUtils.java    From litho with Apache License 2.0 5 votes vote down vote up
public static List<TypeVariableName> getTypeVariables(TypeName typeName) {
  final List<TypeVariableName> typeVariables = new ArrayList<>();

  if (typeName instanceof TypeVariableName) {
    typeVariables.add((TypeVariableName) typeName);
  } else if (typeName instanceof ParameterizedTypeName) {
    for (TypeName typeArgument : ((ParameterizedTypeName) typeName).typeArguments) {
      typeVariables.addAll(getTypeVariables(typeArgument));
    }
  }

  return typeVariables;
}
 
Example #30
Source File: BaseClientBuilderInterface.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private MethodSpec endpointDiscovery() {
    return MethodSpec.methodBuilder("endpointDiscoveryEnabled")
                     .addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
                     .returns(TypeVariableName.get("B"))
                     .addParameter(boolean.class, "endpointDiscovery")
                     .build();
}