
br.com.objectos.flat.FieldMethod 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.flat;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import br.com.objectos.code.AnnotationInfo;
import br.com.objectos.code.EnumConstantInfo;
import br.com.objectos.code.SimpleTypeInfo;
import br.com.objectos.flat.pojo.FieldAnnotation;
import br.com.objectos.pojo.plugin.BuilderProperty;
import br.com.objectos.pojo.plugin.PojoInfo;
import br.com.objectos.pojo.plugin.PojoProperty;
import br.com.objectos.pojo.plugin.Property;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.CodeBlock.Builder;
import com.squareup.javapoet.FieldSpec;
/**
* @author [email protected] (Marcio Endo)
*/
abstract class FieldMethod {
final Property property;
final AnnotationInfo fieldAnnotation;
FieldMethod(Property property, AnnotationInfo fieldAnnotation) {
this.property = property;
this.fieldAnnotation = fieldAnnotation;
}
public static FieldMethod code(Property property) {
AnnotationInfo fieldAnnotation = property.annotationInfoAnnotatedWith(FieldAnnotation.class).get();
String fieldAnnotationName = fieldAnnotation.simpleName();
switch (fieldAnnotationName) {
case "CustomFormat":
return CustomFormatMethod.code(property, fieldAnnotation);
case "DecimalFormat":
return DecimalFormatMethod.code(property, fieldAnnotation);
case "Fill":
return FillMethod.code(property, fieldAnnotation);
case "Fixed":
return FixedMethod.code(property, fieldAnnotation);
case "FlatEnumFormat":
return FlatEnumFormatMethod.code(property, fieldAnnotation);
case "IntegerFormat":
return IntegerFormatMethod.code(property, fieldAnnotation);
case "LocalDateFormat":
return LocalDateFormatMethod.code(property, fieldAnnotation);
case "LocalDateTimeFormat":
return LocalDateTimeFormatMethod.code(property, fieldAnnotation);
case "Text":
return TextMethod.code(property, fieldAnnotation);
default:
throw new AssertionError();
}
}
public static List fieldMethodListOf(PojoInfo pojoInfo) {
return pojoInfo.propertyStream()
.map(FieldMethod::code)
.collect(Collectors.toList());
}
public void accept(FlatEntityConstructor constructor) {
if (fieldAnnotation.annotationInfo(FieldAnnotation.class)
.get()
.booleanValue("builder", false)) {
constructor.add(property);
}
}
public void accept(ReadMethodSpec code) {
Body body = new ReadBody(code);
recordReaderCode(body);
}
public void accept(WriteToMethodSpec code) {
Body body = new WriteToBody(code);
recordWriterCode(body);
}
public BuilderProperty builderProperty() {
return fieldAnnotation.annotationInfo(FieldAnnotation.class)
.get()
.booleanValue("builder", false)
? property.standardBuilderProperty()
: BuilderProperty.ignore();
}
public abstract int length();
public boolean prefixMethod() {
return false;
}
public Optional parserFieldSpec() {
return Optional.empty();
}
public final PojoProperty pojoProperty() {
return PojoProperty.of(
pojoFieldSpec(),
pojoConstructor(),
property.standardPojoMethod());
}
public PojoProperty pojoConstructor() {
return property.standardPojoConstructorStatement();
}
public PojoProperty pojoFieldSpec() {
return property.standardPojoField();
}
public final void recordBuilderCode(CodeBlock.Builder theBody) {
Body body = new CodeBody(theBody);
recordBuilderCode(body);
}
public final void recordReaderCode(CodeBlock.Builder theBody) {
Body body = new CodeBody(theBody);
recordReaderCode(body);
}
public final void recordWriterCode(CodeBlock.Builder theBody) {
Body body = new CodeBody(theBody);
recordWriterCode(body);
}
char fieldAnnotationCharValue(String name) {
return fieldAnnotation.annotationValueInfo(name)
.get()
.charValue();
}
List fieldAnnotationEnumArray(String name) {
return fieldAnnotation.enumConstantInfoArrayValue(name).get();
}
> E fieldAnnotationEnumValue(String name, Class enumType) {
return fieldAnnotation.annotationValueInfo(name)
.get()
.enumValue(enumType);
}
int fieldAnnotationIntValue(String name) {
return fieldAnnotation.annotationValueInfo(name)
.get()
.intValue();
}
SimpleTypeInfo fieldAnnotationSimpleTypeInfoValue(String name) {
return fieldAnnotation.annotationValueInfo(name)
.get()
.simpleTypeInfoValue();
}
String fieldAnnotationStringValue(String name) {
return fieldAnnotation.annotationValueInfo(name)
.get()
.stringValue();
}
void recordBuilderCode(Body body) {
SimpleTypeInfo returnTypeInfo = property.returnTypeInfo();
if (returnTypeInfo.isPrimitive()) {
body.add(".$L(record.$LValue())", property.name(), returnTypeInfo.simpleName());
} else {
body.add(".$L(record.<$T> get())", property.name(), returnTypeInfo.typeName());
}
}
abstract void recordReaderCode(Body body);
abstract void recordWriterCode(Body body);
static interface Body {
void add(String format, Object... args);
}
static class CodeBody implements Body {
private final CodeBlock.Builder body;
public CodeBody(Builder body) {
this.body = body;
}
@Override
public void add(String format, Object... args) {
body.add(" " + format + "\n", args);
}
}
static class ReadBody implements Body {
private final ReadMethodSpec method;
public ReadBody(ReadMethodSpec method) {
this.method = method;
}
@Override
public void add(String format, Object... args) {
method.add(format, args);
}
}
static class WriteToBody implements Body {
private final WriteToMethodSpec method;
public WriteToBody(WriteToMethodSpec method) {
this.method = method;
}
@Override
public void add(String format, Object... args) {
method.add(format, args);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy