Java Code Examples for com.squareup.javapoet.ClassName#nestedClass()

The following examples show how to use com.squareup.javapoet.ClassName#nestedClass() . 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: MemberCopierSpec.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
private MethodSpec builderCopyMethodForMap() {
    TypeName keyType = typeProvider.getTypeNameForSimpleType(memberModel.getMapModel().getKeyModel()
                                                                        .getVariable().getVariableType());
    ClassName valueParameter = poetExtensions.getModelClass(memberModel.getMapModel().getValueModel().getC2jShape());
    ClassName builderForParameter = valueParameter.nestedClass("Builder");
    TypeName parameterType =
        ParameterizedTypeName.get(ClassName.get(Map.class), keyType, WildcardTypeName.subtypeOf(builderForParameter));

    CodeBlock code =
        CodeBlock.builder()
                 .beginControlFlow("if ($N == null)", memberParamName())
                 .addStatement("return null")
                 .endControlFlow()
                 .addStatement("return $N($N.entrySet().stream().collect(toMap($T::getKey, e -> e.getValue().build())))",
                               serviceModelCopiers.copyMethodName(),
                               memberParamName(),
                               Map.Entry.class)
                 .build();

    return MethodSpec.methodBuilder(serviceModelCopiers.builderCopyMethodName())
                     .addModifiers(Modifier.STATIC)
                     .addParameter(parameterType, memberParamName())
                     .returns(typeProvider.fieldType(memberModel))
                     .addCode(code)
                     .build();
}
 
Example 2
Source File: MemberCopierSpec.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
private MethodSpec builderCopyMethodForList() {
    ClassName listParameter = poetExtensions.getModelClass(memberModel.getListModel().getListMemberModel().getC2jShape());
    ClassName builderForParameter = listParameter.nestedClass("Builder");

    TypeName parameterType =
        ParameterizedTypeName.get(ClassName.get(Collection.class), WildcardTypeName.subtypeOf(builderForParameter));

    CodeBlock code = CodeBlock.builder()
                              .beginControlFlow("if ($N == null)", memberParamName())
                              .addStatement("return null")
                              .endControlFlow()
                              .addStatement("return $N($N.stream().map($T::$N).collect(toList()))",
                                            serviceModelCopiers.copyMethodName(),
                                            memberParamName(),
                                            builderForParameter,
                                            "build")
                              .build();

    return MethodSpec.methodBuilder(serviceModelCopiers.builderCopyMethodName())
                     .addModifiers(Modifier.STATIC)
                     .addParameter(parameterType, memberParamName())
                     .returns(typeProvider.fieldType(memberModel))
                     .addCode(code)
                     .build();
}
 
Example 3
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 4
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 5
Source File: NonCollectionSetters.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private MethodSpec fluentConsumerFluentSetter(TypeName returnType) {
    ClassName memberClass = poetExtensions.getModelClass(memberModel().getShape().getC2jName());
    ClassName builderClass = memberClass.nestedClass("Builder");
    return fluentDefaultSetterDeclaration(builderConsumerParam(builderClass), returnType)
        .addModifiers(Modifier.DEFAULT)
        .addStatement("return $N($T.builder().applyMutation($N).build())",
                      memberModel().getFluentSetterMethodName(),
                      memberClass,
                      fieldName())
        .addJavadoc("$L", memberModel().getDefaultConsumerFluentSetterDocumentation())
        .build();
}
 
Example 6
Source File: ListSetters.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
private TypeName listElementConsumerBuilderType() {
    TypeName listElementType = listElementType();
    ClassName classType = Validate.isInstanceOf(ClassName.class, listElementType,
                                                "List element type must be of type class, but was %s", listElementType);
    return classType.nestedClass("Builder");
}
 
Example 7
Source File: EntityGraph.java    From spring-data-jpa-entity-graph with MIT License 4 votes vote down vote up
public EntityGraph(
    ClassName entityGraphClassName, RootComposer rootComposer, NodeComposer nodeComposer) {
  this.className = entityGraphClassName;

  FieldSpec typeField =
      FieldSpec.builder(Constants.ENTITY_GRAPH_TYPE_CLASS_NAME, "type")
          .addModifiers(Modifier.PRIVATE, Modifier.FINAL)
          .build();

  FieldSpec attributePathsField =
      FieldSpec.builder(ParameterizedTypeName.get(List.class, String.class), "attributePaths")
          .addModifiers(Modifier.PRIVATE, Modifier.FINAL)
          .build();

  ClassName rootComposerClassName = entityGraphClassName.nestedClass(rootComposer.simpleName());

  MethodSpec constructor =
      MethodSpec.constructorBuilder()
          .addModifiers(Modifier.PRIVATE)
          .addParameter(rootComposerClassName, "rootComposer")
          .addStatement("this.type = rootComposer.entityGraphType")
          .addStatement(
              "this.attributePaths = rootComposer.entityGraphAttributePaths.stream()"
                  + ".map(pathParts -> String.join(\".\", pathParts)).collect($T.toList())",
              Collectors.class)
          .build();

  MethodSpec rootStaticMethod =
      MethodSpec.methodBuilder(Constants.PATH_SEPARATOR)
          .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
          .returns(rootComposerClassName)
          .addStatement("return new $N()", rootComposer.simpleName())
          .build();

  MethodSpec rootStaticMethodWithEntityGraphType =
      MethodSpec.methodBuilder(Constants.PATH_SEPARATOR)
          .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
          .addParameter(Constants.ENTITY_GRAPH_TYPE_CLASS_NAME, "entityGraphType")
          .returns(rootComposerClassName)
          .addStatement("return new $N(entityGraphType)", rootComposer.simpleName())
          .build();

  MethodSpec getEntityGraphTypeMethod =
      MethodSpec.methodBuilder("getEntityGraphType")
          .addModifiers(Modifier.PUBLIC)
          .addAnnotation(Override.class)
          .returns(Constants.ENTITY_GRAPH_TYPE_CLASS_NAME)
          .addStatement("return type")
          .build();

  MethodSpec getEntityGraphAttributePathsMethod =
      MethodSpec.methodBuilder("getEntityGraphAttributePaths")
          .addModifiers(Modifier.PUBLIC)
          .addAnnotation(Override.class)
          .returns(ParameterizedTypeName.get(List.class, String.class))
          .addStatement("return attributePaths")
          .build();

  MethodSpec getEntityGraphNameMethod =
      MethodSpec.methodBuilder("getEntityGraphName")
          .addModifiers(Modifier.PUBLIC)
          .addAnnotation(Override.class)
          .returns(String.class)
          .addStatement("return null")
          .build();

  MethodSpec isOptionalMethod =
      MethodSpec.methodBuilder("isOptional")
          .addModifiers(Modifier.PUBLIC)
          .addAnnotation(Override.class)
          .returns(boolean.class)
          .addStatement("return false")
          .build();

  typeSpec =
      TypeSpec.classBuilder(entityGraphClassName)
          .addModifiers(Modifier.PUBLIC)
          .addSuperinterface(Constants.ENTITY_GRAPH_CLASS_NAME)
          .addField(typeField)
          .addField(attributePathsField)
          .addMethod(constructor)
          .addMethod(rootStaticMethod)
          .addMethod(rootStaticMethodWithEntityGraphType)
          .addMethod(getEntityGraphTypeMethod)
          .addMethod(getEntityGraphAttributePathsMethod)
          .addMethod(getEntityGraphNameMethod)
          .addMethod(isOptionalMethod)
          .addType(rootComposer.toTypeSpec())
          .addType(nodeComposer.toTypeSpec())
          .build();
}