com.bendb.thrifty.schema.ServiceMethod Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of thrifty-schema Show documentation
Show all versions of thrifty-schema Show documentation
A simple Thrift IDL parser and validator
/*
* Copyright (C) 2015-2016 Benjamin Bader
*
* 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 com.bendb.thrifty.schema;
import com.bendb.thrifty.schema.parser.AnnotationElement;
import com.bendb.thrifty.schema.parser.FieldElement;
import com.bendb.thrifty.schema.parser.FunctionElement;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.util.List;
public final class ServiceMethod {
private final FunctionElement element;
private final ImmutableList paramTypes;
private final ImmutableList exceptionTypes;
private final ImmutableMap annotations;
private ThriftType returnType;
public ServiceMethod(FunctionElement element) {
this.element = element;
ImmutableList.Builder params = ImmutableList.builder();
for (FieldElement field : element.params()) {
params.add(new Field(field, FieldNamingPolicy.DEFAULT));
}
this.paramTypes = params.build();
ImmutableList.Builder exceptions = ImmutableList.builder();
for (FieldElement field : element.exceptions()) {
exceptions.add(new Field(field, FieldNamingPolicy.DEFAULT));
}
this.exceptionTypes = exceptions.build();
ImmutableMap.Builder annotationBuilder = ImmutableMap.builder();
AnnotationElement anno = element.annotations();
if (anno != null) {
annotationBuilder.putAll(anno.values());
}
this.annotations = annotationBuilder.build();
}
public String documentation() {
return element.documentation();
}
public boolean hasJavadoc() {
return JavadocUtil.hasJavadoc(this);
}
public String name() {
return element.name();
}
public boolean oneWay() {
return element.oneWay();
}
public Optional returnType() {
return Optional.fromNullable(returnType);
}
public List paramTypes() {
return paramTypes;
}
public List exceptionTypes() {
return exceptionTypes;
}
public ImmutableMap annotations() {
return annotations;
}
void link(Linker linker) {
if (element.returnType().name().equals("void")) {
returnType = ThriftType.VOID;
} else {
returnType = linker.resolveType(element.returnType());
}
for (Field field : paramTypes) {
field.link(linker);
}
for (Field field : exceptionTypes) {
field.link(linker);
}
}
}