All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.objectos.io.compiler.FieldMethod Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2015 Objectos, Fábrica de Software LTDA.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package br.com.objectos.io.compiler;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import javax.lang.model.element.Modifier;

import br.com.objectos.code.AnnotationInfo;
import br.com.objectos.code.EnumConstantInfo;
import br.com.objectos.code.MethodInfo;
import br.com.objectos.code.ModifierInfo;
import br.com.objectos.code.SimpleTypeInfo;
import br.com.objectos.code.SimpleTypePrimitives;
import br.com.objectos.code.TypeInfo;
import br.com.objectos.io.flat.annotation.FieldAnnotation;

import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.CodeBlock.Builder;
import com.squareup.javapoet.FieldSpec;

/**
 * @author [email protected] (Marcio Endo)
 */
abstract class FieldMethod {

  final MethodInfo methodInfo;
  final AnnotationInfo fieldAnnotation;

  FieldMethod(MethodInfo methodInfo, AnnotationInfo fieldAnnotation) {
    this.methodInfo = methodInfo;
    this.fieldAnnotation = fieldAnnotation;
  }

  public static List fieldMethodListOf(TypeInfo typeInfo) {
    return typeInfo.methodInfoStream()
        .filter(m -> m.hasModifierInfo(ModifierInfo.ABSTRACT))
        .filter(m -> !m.hasReturnTypeInfo(SimpleTypePrimitives.VOID))
        .filter(m -> m.hasParameterInfoListSize(0))
        .map(m -> code(m))
        .collect(Collectors.toList());
  }

  public static FieldMethod code(MethodInfo methodInfo) {
    AnnotationInfo fieldAnnotation = methodInfo.annotationInfoAnnotatedWith(FieldAnnotation.class).get();
    String fieldAnnotationName = fieldAnnotation.simpleName();
    switch (fieldAnnotationName) {
    case "CustomFormat":
      return CustomFormatMethod.code(methodInfo, fieldAnnotation);

    case "DecimalFormat":
      return DecimalFormatMethod.code(methodInfo, fieldAnnotation);

    case "Fill":
      return FillMethod.code(methodInfo, fieldAnnotation);

    case "Fixed":
      return FixedMethod.code(methodInfo, fieldAnnotation);

    case "FlatEnumFormat":
      return FlatEnumFormatMethod.code(methodInfo, fieldAnnotation);

    case "IntegerFormat":
      return IntegerFormatMethod.code(methodInfo, fieldAnnotation);

    case "LocalDateFormat":
      return LocalDateFormatMethod.code(methodInfo, fieldAnnotation);

    case "Text":
      return TextMethod.code(methodInfo, fieldAnnotation);

    default:
      throw new AssertionError();
    }
  }

  public boolean prefixMethod() {
    return false;
  }

  public Optional parserFieldSpec() {
    return Optional.empty();
  }

  public FieldSpec pojoFieldSpec() {
    return methodInfo.fieldWriter()
        .modifiers(Modifier.PRIVATE, Modifier.FINAL)
        .write();
  }

  public final void recordBuilderCode(CodeBlock.Builder theBody) {
    Body body = new Body(theBody);
    recordBuilderCode(body);
  }

  public final void recordReaderCode(CodeBlock.Builder theBody) {
    Body body = new Body(theBody);
    recordReaderCode(body);
  }

  public final void recordWriterCode(CodeBlock.Builder theBody) {
    Body body = new Body(theBody);
    recordWriterCode(body);
  }

  char fieldAnnotationCharValue(String name) {
    return fieldAnnotation.annotationValueInfo(name)
        .get()
        .charValue();
  }

  List fieldAnnotationEnumArray(String name) {
    return fieldAnnotation.enumConstantInfoArrayValue(name).get();
  }

  > E fieldAnnotationEnumValue(String name, Class enumType) {
    return fieldAnnotation.annotationValueInfo(name)
        .get()
        .enumValue(enumType);
  }

  int fieldAnnotationIntValue(String name) {
    return fieldAnnotation.annotationValueInfo(name)
        .get()
        .intValue();
  }

  SimpleTypeInfo fieldAnnotationSimpleTypeInfoValue(String name) {
    return fieldAnnotation.annotationValueInfo(name)
        .get()
        .simpleTypeInfoValue();
  }

  String fieldAnnotationStringValue(String name) {
    return fieldAnnotation.annotationValueInfo(name)
        .get()
        .stringValue();
  }

  void recordBuilderCode(Body body) {
    SimpleTypeInfo returnTypeInfo = methodInfo.returnTypeInfo();
    if (returnTypeInfo.isPrimitive()) {
      body.add(".$L(record.$LValue())", methodInfo.fieldName(), returnTypeInfo.simpleName());
    } else {
      body.add(".$L(record.<$T> get())", methodInfo.fieldName(), returnTypeInfo.typeName());
    }
  }

  abstract void recordReaderCode(Body body);

  abstract void recordWriterCode(Body body);

  static class Body {

    private final CodeBlock.Builder body;

    public Body(Builder body) {
      this.body = body;
    }

    public void add(String format, Object... args) {
      body.add("    " + format + "\n", args);
    }

  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy