
br.com.objectos.way.lazy.LazyMethod Maven / Gradle / Ivy
/*
* 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.way.lazy;
import javax.lang.model.element.Modifier;
import br.com.objectos.way.code.AccessInfo;
import br.com.objectos.way.code.MethodInfo;
import br.com.objectos.way.code.SimpleTypeInfo;
import br.com.objectos.way.pojo.Invalidate;
import br.com.objectos.way.pojo.plugin.Contribution;
import br.com.objectos.way.pojo.plugin.InvalidateMethod;
import br.com.objectos.way.pojo.plugin.InvalidateMethodAction;
import br.com.objectos.way.pojo.plugin.InvalidateMethodContribution;
import br.com.objectos.way.pojo.plugin.Naming;
import br.com.objectos.way.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 implements InvalidateMethodAction {
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())
.addInvalidateMethodAction(this)
.build();
}
@Override
public InvalidateMethodContribution execute(InvalidateMethod method) {
return InvalidateMethodContribution.builder()
.when(method.annotationInstanceOf(Invalidate.class))
.addStatement("$L.unset()", fieldName)
.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.way.lazy.Lazy.class);
return ParameterizedTypeName.get(lazy, returnTypeInfo.typeName());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy