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

br.com.objectos.lazy.LazyMethod 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.lazy;

import javax.lang.model.element.Modifier;

import br.com.objectos.code.AccessInfo;
import br.com.objectos.code.MethodInfo;
import br.com.objectos.code.SimpleTypeInfo;
import br.com.objectos.pojo.plugin.Contribution;
import br.com.objectos.pojo.plugin.Naming;
import br.com.objectos.pojo.plugin.PojoInfo;

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeSpec;

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

  private final Naming naming;

  private final AccessInfo accessInfo;
  private final String name;
  private final String fieldName;
  private final SimpleTypeInfo returnTypeInfo;

  private LazyMethod(Naming naming,
                     AccessInfo accessInfo,
                     String name,
                     String fieldName,
                     SimpleTypeInfo returnTypeInfo) {
    this.naming = naming;
    this.accessInfo = accessInfo;
    this.name = name;
    this.fieldName = fieldName;
    this.returnTypeInfo = returnTypeInfo;
  }

  public static LazyMethod of(PojoInfo pojoInfo, MethodInfo methodInfo) {
    return new LazyMethod(
        pojoInfo.naming(),
        methodInfo.accessInfo(),
        methodInfo.name(),
        methodInfo.fieldName(),
        methodInfo.returnTypeInfo());
  }

  public Contribution execute() {
    return Contribution.builder()
        .addField(lazyField())
        .addMethod(lazyMethod())
        .addType(lazyType())
        .build();
  }

  private FieldSpec lazyField() {
    return FieldSpec.builder(lazyTypeName(), fieldName)
        .addModifiers(Modifier.PRIVATE, Modifier.FINAL)
        .initializer("new $T()", lazyClassName())
        .build();
  }

  private MethodSpec lazyMethod() {
    return MethodSpec.methodBuilder(name)
        .addAnnotation(Override.class)
        .addModifiers(accessInfo.modifiers())
        .returns(returnTypeInfo.typeName())
        .addStatement("return $L.get()", fieldName)
        .build();
  }

  private TypeSpec lazyType() {
    return TypeSpec.classBuilder(lazyClassName().simpleName())
        .addModifiers(Modifier.PRIVATE)
        .superclass(lazyTypeName())
        .addMethod(MethodSpec.methodBuilder("compute")
            .addAnnotation(Override.class)
            .addModifiers(Modifier.PROTECTED)
            .returns(returnTypeInfo.typeName())
            .addStatement("return $T.super.$L()", naming.pojo(), name)
            .build())
        .build();
  }

  private ClassName lazyClassName() {
    return naming.lazyClassName(fieldName);
  }

  private ParameterizedTypeName lazyTypeName() {
    ClassName lazy = ClassName.get(br.com.objectos.lazy.Lazy.class);
    return ParameterizedTypeName.get(lazy, returnTypeInfo.typeName());
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy