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

br.com.objectos.code.pojo.AttributeMethod 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.code.pojo;

import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

import javax.lang.model.element.Modifier;

import br.com.objectos.code.AccessInfo;
import br.com.objectos.code.Code;
import br.com.objectos.code.MethodInfo;
import br.com.objectos.code.SimpleTypeInfo;
import br.com.objectos.core.testing.Testable;

import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterSpec;
import com.squareup.javapoet.TypeName;

/**
 * @author [email protected] (Marcio Endo)
 */
abstract class AttributeMethod implements Testable {

  abstract Naming naming();
  abstract AccessInfo accessInfo();
  abstract String name();
  abstract String fieldName();
  abstract TypeName returnTypeName();

  AttributeMethod() {
  }

  public static AttributeMethod of(Naming naming, MethodInfo methodInfo) {
    SimpleTypeInfo returnTypeInfo = methodInfo.returnTypeInfo();
    return returnTypeInfo.isSubType(Collection.class)
        ? CollectionAttributeMethod.of(naming, methodInfo, returnTypeInfo)
        : returnTypeInfo.isInfoOf(Optional.class)
            ? OptionalAttributeMethod.of(naming, methodInfo, returnTypeInfo)
            : SimpleAttributeMethod.of(naming, methodInfo, returnTypeInfo);
  }

  FieldSpec builderClassFieldSpec() {
    return FieldSpec.builder(returnTypeName(), fieldName())
        .addModifiers(Modifier.PRIVATE)
        .build();
  }

  MethodSpec builderClassGetter() {
    return MethodSpec.methodBuilder(getterMethodName())
        .returns(returnTypeName())
        .addStatement("return $L", fieldName())
        .build();
  }

  Stream builderClassSetter() {
    MethodSpec methodSpec = MethodSpec.methodBuilder(fieldName())
        .addAnnotation(Override.class)
        .addModifiers(Modifier.PUBLIC)
        .returns(naming().builderInnerTypeName(this))
        .addParameter(ParameterSpec.builder(returnTypeName(), fieldName()).build())
        .addCode(builderClassSetterBody())
        .build();
    return Stream.of(methodSpec);
  }

  CodeBlock builderClassSetterBody() {
    CodeBlock.Builder code = CodeBlock.builder();

    Optional maybeNullCheck = Code.nullCheck(returnTypeName(), fieldName());
    if (maybeNullCheck.isPresent()) {
      code.add(maybeNullCheck.get());
    }

    return code
        .addStatement("this.$L = $L", fieldName(), fieldName())
        .addStatement("return this")
        .build();
  }

  abstract List builderInterfaceMiddle();

  String getterMethodName() {
    return "___get___" + fieldName();
  }

  CodeBlock pojoClassConstructorBody() {
    return CodeBlock.builder()
        .addStatement("$L = builder.___get___$L()", fieldName(), fieldName())
        .build();
  }

  FieldSpec pojoClassField() {
    return FieldSpec.builder(returnTypeName(), fieldName())
        .addModifiers(Modifier.PRIVATE, Modifier.FINAL)
        .build();
  }

  MethodSpec pojoClassMethod() {
    return MethodSpec.methodBuilder(name())
        .addAnnotation(Override.class)
        .addModifiers(accessInfo().modifiers())
        .returns(returnTypeName())
        .addStatement("return $L", fieldName())
        .build();
  }

  CodeBlock testableBody() {
    return CodeBlock.builder()
        .add("    .equal($L, that.$L())\n", fieldName(), name())
        .build();
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy