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

br.com.objectos.code.pojo.PojoClass 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.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.lang.model.element.Modifier;

import br.com.objectos.code.AccessInfo;
import br.com.objectos.code.ConstructorInfo;
import br.com.objectos.core.util.MoreCollectors;

import com.google.common.collect.ImmutableList;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterSpec;
import com.squareup.javapoet.TypeSpec;

/**
 * @author [email protected] (Marcio Endo)
 */
class PojoClass extends AbstractHasPojoInfo {

  public PojoClass(PojoInfo info) {
    super(info);
  }

  public JavaFile generate(Configuration configuration) {
    TypeSpec type = type(configuration);
    return generate(type);
  }

  List constructor(Configuration configuration) {
    List customParamList = configuration.parameterList();
    List body = constructorBody(configuration);
    return constructorInfoStream()
        .map(constructor -> constructor0(customParamList, body, constructor))
        .collect(Collectors.toList());
  }

  List customField(Configuration configuration) {
    Stream pojoStream = configuration.pojoClassFieldStream();
    Stream thisStream = attributeMethodStream().map(AttributeMethod::pojoClassField);
    return Stream.concat(pojoStream, thisStream).collect(Collectors.toList());
  }

  List field(Configuration configuration) {
    return ImmutableList. builder()
        .addAll(configuration.pojoClassFieldList())
        .addAll(attributeMethodStream()
            .map(AttributeMethod::pojoClassField)
            .collect(MoreCollectors.toImmutableList()))
        .build();
  }

  List lazyInnerClass() {
    return lazyMethodStream().map(LazyMethod::pojoClassInnerClass)
        .collect(MoreCollectors.toImmutableList());
  }

  List lazyField() {
    return lazyMethodStream().map(LazyMethod::pojoClassField)
        .collect(MoreCollectors.toImmutableList());
  }

  List lazyMethod() {
    return lazyMethodStream().map(LazyMethod::pojoClassMethod)
        .collect(MoreCollectors.toImmutableList());
  }

  List method() {
    return ImmutableList. builder()
        .addAll(attributeMethodStream()
            .map(AttributeMethod::pojoClassMethod)
            .collect(Collectors.toList()))
        .build();
  }

  TypeSpec type(Configuration configuration) {
    TypeSpec.Builder type = TypeSpec
        .classBuilder(naming().pojoSimpleName())
        .addModifiers(Modifier.FINAL)
        .addAnnotation(configuration.annotationSpec())
        .superclass(naming().superClassTypeName());

    naming().typeVariableNameUnboundedListTo(type);

    configuration.onType(type);
    configuration.onField(type, field(configuration));
    configuration.onLazyField(type, lazyField());
    configuration.onConstructor(type, constructor(configuration));
    configuration.onTestable(type, testable());
    configuration.onMethod(type, method());
    configuration.onLazyMethod(type, lazyMethod());
    configuration.onInvalidate(type, invalidate());
    configuration.onLazyInnerClass(type, lazyInnerClass());

    return type.build();
  }

  private MethodSpec constructor0(List customParamList, List body, ConstructorInfo input) {
    CodeBlock statement = input.statementWriter()
        .add("super(")
        .add("$N").forEachParameter(", ")
        .add(")")
        .setParameterName()
        .write();

    return input.constructorWriter()
        .accessInfo(AccessInfo.PUBLIC)
        .addParameterList()
        .addParameterList(customParamList)
        .addParameter(naming().builderClassTypeName(), "builder")
        .addCode(statement)
        .addCode(body)
        .write();
  }

  private List constructorBody(Configuration configuration) {
    Stream pojoStream = configuration.pojoClassConstructorBodyStream();
    Stream thisStream = builderMethodStream().map(AttributeMethod::pojoClassConstructorBody);
    return Stream.concat(pojoStream, thisStream).collect(Collectors.toList());
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy