br.com.objectos.html.AttributeAnnotation 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.html;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.lang.model.element.Modifier;
import br.com.objectos.code.AnnotationInfo;
import br.com.objectos.core.lang.Strings;
import br.com.objectos.pojo.Pojo;
import br.com.objectos.testable.Testable;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeVariableName;
/**
* @author [email protected] (Marcio Endo)
*/
@Pojo
abstract class AttributeAnnotation implements Testable {
abstract String name();
abstract String identifier();
abstract TypeName typeName();
AttributeAnnotation() {
}
public static AttributeAnnotationBuilder builder() {
return new AttributeAnnotationBuilderPojo();
}
public static List list(Optional maybeAttributes) {
return maybeAttributes
.flatMap(info -> info.annotationInfoArrayValue("value"))
.map(list -> list.stream())
.orElse(Stream.of())
.map(AttributeAnnotation::of)
.collect(Collectors.toList());
}
public static AttributeAnnotation of(AnnotationInfo annotationInfo) {
String name = annotationInfo.stringValue("name").get();
String maybeIdentifier = annotationInfo.stringValue("identifier").get();
String identifier = Strings.isNullOrEmpty(maybeIdentifier) ? name : maybeIdentifier;
return AttributeAnnotation.builder()
.name(name)
.identifier(identifier)
.typeName(annotationInfo.simpleTypeInfoValue("type").get().typeName())
.build();
}
public MethodSpec setter(ClassName returns) {
return MethodSpec.methodBuilder(identifier())
.addModifiers(Modifier.PUBLIC)
.addParameter(typeName(), identifier())
.returns(returns)
.addStatement("attr($S, $L)", name(), identifier())
.addStatement("return this")
.build();
}
public MethodSpec setter(TypeVariableName returns) {
return MethodSpec.methodBuilder(identifier())
.addModifiers(Modifier.PUBLIC)
.addParameter(typeName(), identifier())
.returns(returns)
.addStatement("attr($S, $L)", name(), identifier())
.addStatement("return self()")
.build();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy