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

br.com.objectos.way.pojo.plugin.InvalidateMethod Maven / Gradle / Ivy

There is a newer version: 0.6.0
Show newest version
/*
 * Copyright 2014-2016 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.way.pojo.plugin;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

import br.com.objectos.core.util.MoreCollectors;
import br.com.objectos.way.code.AccessInfo;
import br.com.objectos.way.code.AnnotationInfo;
import br.com.objectos.way.code.MethodInfo;
import br.com.objectos.way.code.MethodInfoOverrideWriter;
import br.com.objectos.way.code.ModifierInfo;
import br.com.objectos.way.code.ParameterInfo;
import br.com.objectos.way.code.SimpleTypeInfo;
import br.com.objectos.way.code.SimpleTypePrimitives;
import br.com.objectos.way.code.TypeInfo;
import br.com.objectos.way.pojo.InvalidateAnnotation;

import com.squareup.javapoet.TypeSpec;

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

  private final MethodInfo methodInfo;
  private final AnnotationInfo annotationInfo;
  private final boolean self;

  private final List contributionList = new ArrayList<>();

  private InvalidateMethod(MethodInfo methodInfo, AnnotationInfo annotationInfo, boolean self) {
    this.methodInfo = methodInfo;
    this.annotationInfo = annotationInfo;
    this.self = self;
  }

  static List of(TypeInfo typeInfo) {
    return typeInfo.methodInfoStream()
        .map(method -> of0(typeInfo, method))
        .filter(Optional::isPresent)
        .map(Optional::get)
        .collect(MoreCollectors.toImmutableList());
  }

  private static Optional of0(TypeInfo typeInfo, MethodInfo method) {
    return method
        .annotationInfoAnnotatedWith(InvalidateAnnotation.class)
        .flatMap(ann -> of1(typeInfo, method, ann));
  }

  private static Optional of1(TypeInfo typeInfo, MethodInfo method, AnnotationInfo annotation) {
    String annotationName = annotation.simpleName();

    if (method.hasModifierInfo(ModifierInfo.FINAL)) {
      method.compilationError("@%s method cannot be final", annotationName);
      return Optional.empty();
    }

    if (method.hasAccessInfo(AccessInfo.PRIVATE)) {
      method.compilationError("@%s method cannot be private", annotationName);
      return Optional.empty();
    }

    SimpleTypeInfo returnTypeInfo = method.returnTypeInfo();
    boolean self = returnTypeInfo.equals(typeInfo.toSimpleTypeInfo());
    if (!returnTypeInfo.equals(SimpleTypePrimitives.VOID) && !self) {
      method.compilationError("@%s method must return void or self", annotationName);
      return Optional.empty();
    }

    InvalidateMethod invalidateMethod = new InvalidateMethod(method, annotation, self);
    return Optional.of(invalidateMethod);
  }

  public boolean annotationInstanceOf(Class annotationType) {
    return annotationInfo.simpleTypeInfo().isInfoOf(annotationType);
  }

  public void compilationError(String template, Object... arguments) {
    methodInfo.compilationError(template, arguments);
  }

  public boolean hasParameterInfoTypes(Class... types) {
    return methodInfo.hasParameterInfoTypes(types);
  }

  public Stream parameterInfoStream() {
    return methodInfo.parameterInfoStream();
  }

  void accept(InvalidateMethodAction action) {
    InvalidateMethodContribution contribution = action.execute(this);
    addContribution(contribution);
  }

  void accept(List pluginList) {
    pluginList.stream()
        .map(plugin -> plugin.execute(this))
        .forEach(this::addContribution);
  }

  void accept(TypeSpec.Builder type) {
    MethodInfoOverrideWriter writer = methodInfo.overrideWriter();

    if (!methodInfo.hasModifierInfo(ModifierInfo.ABSTRACT)) {
      writer.addStatement("super.$L()", methodInfo.name());
    }

    contributionList.forEach(contribution -> contribution.accept(writer));

    if (self) {
      writer.addStatement("return this");
    }

    type.addMethod(writer.write());
  }

  private void addContribution(InvalidateMethodContribution contribution) {
    if (contribution != null) {
      contributionList.add(contribution);
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy