Java Code Examples for com.squareup.javapoet.ClassName#OBJECT

The following examples show how to use com.squareup.javapoet.ClassName#OBJECT . These examples are extracted from open source projects. 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 Project: litho   File: EventGeneratorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {
  Elements elements = mCompilationRule.getElements();
  Types types = mCompilationRule.getTypes();
  TypeElement typeElement = elements.getTypeElement(TestSpec.class.getCanonicalName());
  mSpecModel =
      mLayoutSpecModelFactory.create(
          elements, types, typeElement, mock(Messager.class), RunMode.normal(), null, null);

  EventDeclarationModel eventDeclarationModel =
      new EventDeclarationModel(
          ClassName.OBJECT,
          ClassName.OBJECT,
          ImmutableList.of(
              new FieldModel(
                  FieldSpec.builder(TypeName.INT, "field1", Modifier.PUBLIC).build(),
                  new Object()),
              new FieldModel(
                  FieldSpec.builder(TypeName.INT, "field2", Modifier.PUBLIC).build(),
                  new Object())),
          new Object());
  when(mMockSpecModel.getEventDeclarations()).thenReturn(ImmutableList.of(eventDeclarationModel));
  when(mMockSpecModel.getContextClass()).thenReturn(ClassNames.COMPONENT_CONTEXT);
  when(mMockSpecModel.getComponentName()).thenReturn("Test");
  when(mMockSpecModel.getScopeMethodName()).thenReturn("getComponentScope");
}
 
Example 2
Source Project: litho   File: EventCaseGeneratorTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testBasicGeneratorCase() {
  final MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder("method");
  final EventDeclarationModel model =
      new EventDeclarationModel(ClassName.OBJECT, TypeName.VOID, ImmutableList.of(), null);

  EventCaseGenerator.builder()
      .contextClass(ClassNames.COMPONENT_CONTEXT)
      .eventMethodModels(
          ImmutableList.of(
              SpecMethodModel.<EventMethod, EventDeclarationModel>builder()
                  .name("event")
                  .returnTypeSpec(new TypeSpec(TypeName.VOID))
                  .typeModel(model)
                  .build()))
      .writeTo(methodBuilder);

  assertThat(methodBuilder.build().toString())
      .isEqualTo(
          "void method() {\n"
              + "  case 96891546: {\n"
              + "    java.lang.Object _event = (java.lang.Object) eventState;\n"
              + "    event(\n"
              + "          eventHandler.mHasEventDispatcher);\n"
              + "    return null;\n"
              + "  }\n"
              + "}\n");
}
 
Example 3
Source Project: litho   File: EventGeneratorTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testGenerateEventDispatchersIgnoresGenericTypesInFields() {
  FieldModel fieldWithTypeArguments =
      new FieldModel(
          FieldSpec.builder(
                  ParameterizedTypeName.get(List.class, String.class), "field1", Modifier.PUBLIC)
              .build(),
          new Object());

  EventDeclarationModel eventDeclarationModel =
      new EventDeclarationModel(
          ClassName.OBJECT,
          ClassName.OBJECT,
          ImmutableList.of(fieldWithTypeArguments),
          new Object());
  when(mMockSpecModel.getEventDeclarations()).thenReturn(ImmutableList.of(eventDeclarationModel));

  TypeSpecDataHolder dataHolder = EventGenerator.generateEventDispatchers(mMockSpecModel);
  assertThat(dataHolder.getMethodSpecs().get(0).toString())
      .isEqualTo(
          "static java.lang.Object dispatchObject(com.facebook.litho.EventHandler _eventHandler,\n"
              + "    java.util.List field1) {\n"
              + "  final java.lang.Object _eventState = new java.lang.Object();\n"
              + "  _eventState.field1 = field1;\n"
              + "  com.facebook.litho.EventDispatcher _lifecycle = _eventHandler.mHasEventDispatcher.getEventDispatcher();\n"
              + "  return (java.lang.Object) _lifecycle.dispatchOnEvent(_eventHandler, _eventState);\n"
              + "}\n");
}
 
Example 4
Source Project: aws-sdk-java-v2   File: AwsServiceModel.java    License: Apache License 2.0 5 votes vote down vote up
private TypeName modelSuperClass() {
    switch (shapeModel.getShapeType()) {
        case Request:
            return requestBaseClass();
        case Response:
            return responseBaseClass();
        case Exception:
            return exceptionBaseClass();
        default:
            return ClassName.OBJECT;
    }
}
 
Example 5
private TypeName builderImplSuperClass() {
    if (isRequest()) {
        return new AwsServiceBaseRequestSpec(intermediateModel).className().nestedClass("BuilderImpl");
    }

    if (isResponse()) {
        return new AwsServiceBaseResponseSpec(intermediateModel).className().nestedClass("BuilderImpl");
    }

    return ClassName.OBJECT;
}
 
Example 6
Source Project: apicurio-studio   File: OpenApi2JaxRs.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Generates a Jax-rs interface from the given codegen information.
 * @param _interface
 */
protected String generateJavaInterface(CodegenJavaInterface _interface) {
    // Create the JAX-RS interface spec itself.
    Builder interfaceBuilder = TypeSpec
            .interfaceBuilder(ClassName.get(_interface.getPackage(), _interface.getName()));
    interfaceBuilder.addModifiers(Modifier.PUBLIC)
            .addAnnotation(AnnotationSpec.builder(ClassName.get("javax.ws.rs", "Path"))
                    .addMember("value", "$S", _interface.getPath()).build())
            .addJavadoc("A JAX-RS interface.  An implementation of this interface must be provided.\n");

    // Add specs for all the methods.
    for (CodegenJavaMethod cgMethod : _interface.getMethods()) {
        com.squareup.javapoet.MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(cgMethod.getName());
        methodBuilder.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT);
        // The @Path annotation.
        if (cgMethod.getPath() != null) {
            methodBuilder.addAnnotation(AnnotationSpec.builder(ClassName.get("javax.ws.rs", "Path"))
                    .addMember("value", "$S", cgMethod.getPath()).build());
        }
        // The @GET, @PUT, @POST, etc annotation
        methodBuilder.addAnnotation(AnnotationSpec.builder(ClassName.get("javax.ws.rs", cgMethod.getMethod().toUpperCase())).build());
        // The @Produces annotation
        if (cgMethod.getProduces() != null && !cgMethod.getProduces().isEmpty()) {
            methodBuilder.addAnnotation(AnnotationSpec.builder(ClassName.get("javax.ws.rs", "Produces"))
                    .addMember("value", "$L", toStringArrayLiteral(cgMethod.getProduces())).build());
        }
        // The @Consumes annotation
        if (cgMethod.getConsumes() != null && !cgMethod.getConsumes().isEmpty()) {
            methodBuilder.addAnnotation(AnnotationSpec.builder(ClassName.get("javax.ws.rs", "Consumes"))
                    .addMember("value", "$L", toStringArrayLiteral(cgMethod.getConsumes())).build());
        }
        // The method return type.
        if (cgMethod.getReturn() != null) {
            TypeName returnType = generateTypeName(cgMethod.getReturn().getCollection(),
                    cgMethod.getReturn().getType(), cgMethod.getReturn().getFormat(), true,
                    ClassName.get("javax.ws.rs.core", "Response"));
            if (getSettings().reactive || cgMethod.isAsync()) {
                returnType = generateReactiveTypeName(returnType);
            }
            methodBuilder.returns(returnType);
        }
        
        // The method arguments.
        if (cgMethod.getArguments() != null && !cgMethod.getArguments().isEmpty()) {
            for (CodegenJavaArgument cgArgument : cgMethod.getArguments()) {
                TypeName defaultParamType = ClassName.OBJECT;
                if (cgArgument.getIn().equals("body")) {
                    defaultParamType = ClassName.get("java.io", "InputStream");
                }
                TypeName paramType = generateTypeName(cgArgument.getCollection(), cgArgument.getType(),
                        cgArgument.getFormat(), cgArgument.getRequired(), defaultParamType);
                if (cgArgument.getTypeSignature() != null) {
                    // TODO try to find a re-usable data type that matches the type signature
                }
                com.squareup.javapoet.ParameterSpec.Builder paramBuilder = ParameterSpec.builder(paramType,
                        paramNameToJavaArgName(cgArgument.getName()));
                if (cgArgument.getIn().equals("path")) {
                    paramBuilder.addAnnotation(AnnotationSpec.builder(ClassName.get("javax.ws.rs", "PathParam"))
                            .addMember("value", "$S", cgArgument.getName()).build());
                }
                if (cgArgument.getIn().equals("query")) {
                    paramBuilder.addAnnotation(AnnotationSpec.builder(ClassName.get("javax.ws.rs", "QueryParam"))
                            .addMember("value", "$S", cgArgument.getName()).build());
                }
                if (cgArgument.getIn().equals("header")) {
                    paramBuilder.addAnnotation(AnnotationSpec.builder(ClassName.get("javax.ws.rs", "HeaderParam"))
                            .addMember("value", "$S", cgArgument.getName()).build());
                }
                methodBuilder.addParameter(paramBuilder.build());
            }
        }
        
        // TODO:: error responses (4xx and 5xx)
        // Should errors be modeled in some way?  JAX-RS has a few ways to handle them.  I'm inclined to 
        // not generate anything in the interface for error responses.
        
        // Javadoc
        if (cgMethod.getDescription() != null) {
            methodBuilder.addJavadoc(cgMethod.getDescription());
            methodBuilder.addJavadoc("\n");
        }
        
        interfaceBuilder.addMethod(methodBuilder.build());
    }
    
    TypeSpec jaxRsInterface = interfaceBuilder.build();
    
    JavaFile javaFile = JavaFile.builder(_interface.getPackage(), jaxRsInterface).build();
    return javaFile.toString();
}