Java Code Examples for com.squareup.javapoet.TypeSpec.interfaceBuilder()

The following are Jave code examples for showing how to use interfaceBuilder() of the com.squareup.javapoet.TypeSpec class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: react4j   File: Generator.java   View Source Code Vote up 4 votes
@Nonnull
private static TypeSpec buildNativeLifecycleInterface( @Nonnull final ComponentDescriptor descriptor )
{
  final TypeSpec.Builder builder = TypeSpec.interfaceBuilder( "Lifecycle" );

  builder.addAnnotation( AnnotationSpec.builder( JS_TYPE_CLASSNAME ).
    addMember( "isNative", "true" ).
    build() );

  builder.addModifiers( Modifier.STATIC );

  // Lifecycle methods
  {

    for ( final MethodDescriptor lifecycleMethod : descriptor.getLifecycleMethods() )
    {
      final String methodName = lifecycleMethod.getMethod().getSimpleName().toString();
      final MethodSpec.Builder method =
        MethodSpec.methodBuilder( methodName ).
          addModifiers( Modifier.ABSTRACT, Modifier.PUBLIC ).
          returns( ClassName.get( lifecycleMethod.getMethodType().getReturnType() ) );

      ProcessorUtil.copyTypeParameters( lifecycleMethod.getMethodType(), method );

      final List<? extends VariableElement> sourceParameters = lifecycleMethod.getMethod().getParameters();
      final List<? extends TypeMirror> sourceParameterTypes = lifecycleMethod.getMethodType().getParameterTypes();
      final int parameterCount = sourceParameters.size();
      for ( int i = 0; i < parameterCount; i++ )
      {
        final VariableElement parameter = sourceParameters.get( i );
        final TypeMirror parameterType = sourceParameterTypes.get( i );
        final String parameterName = parameter.getSimpleName().toString();
        final ParameterSpec.Builder parameterSpec =
          ParameterSpec.builder( TypeName.get( parameterType ), parameterName ).addAnnotation( NONNULL_CLASSNAME );
        method.addParameter( parameterSpec.build() );
      }

      builder.addMethod( method.build() );
    }
  }

  return builder.build();
}