MethodDeclaration.java

  1. package net.morimekta.providence.reflect.model;

  2. import net.morimekta.providence.reflect.parser.ThriftToken;
  3. import net.morimekta.util.collect.UnmodifiableList;

  4. import javax.annotation.Nonnull;
  5. import javax.annotation.Nullable;
  6. import java.util.List;

  7. /**
  8.  * <pre>{@code
  9.  * function ::= 'oneway'? {type} {name} '(' {field}* ')' ('throws' '(' {field}* ')')? {annotations}?
  10.  * }</pre>
  11.  */
  12. public class MethodDeclaration extends Declaration {
  13.     private final ThriftToken            oneway;
  14.     private final List<ThriftToken>      returnTypeTokens;
  15.     private final List<FieldDeclaration> params;
  16.     private final ThriftToken            requestTypeToken;
  17.     private final List<FieldDeclaration> throwing;

  18.     public MethodDeclaration(@Nullable String documentation,
  19.                              @Nullable ThriftToken oneway,
  20.                              @Nonnull List<ThriftToken> returnTypeTokens,
  21.                              @Nonnull ThriftToken name,
  22.                              @Nullable List<FieldDeclaration> params,
  23.                              @Nullable ThriftToken requestTypeToken,
  24.                              @Nullable List<FieldDeclaration> throwing,
  25.                              @Nullable List<AnnotationDeclaration> annotations) {
  26.         super(documentation, name, annotations);
  27.         if (requestTypeToken == null && params == null) {
  28.             throw new IllegalArgumentException("Either request type or params must be non-null");
  29.         } else if (requestTypeToken != null && params != null) {
  30.             throw new IllegalArgumentException("Only one of request type and params may be non-null");
  31.         }

  32.         this.oneway = oneway;
  33.         this.returnTypeTokens = UnmodifiableList.copyOf(returnTypeTokens);
  34.         this.requestTypeToken = requestTypeToken;
  35.         this.params = requestTypeToken == null ? UnmodifiableList.copyOf(params) : null;
  36.         this.throwing = throwing == null || throwing.isEmpty() ? null : UnmodifiableList.copyOf(throwing);
  37.     }

  38.     public boolean isOneWay() {
  39.         return oneway != null;
  40.     }

  41.     @Nullable
  42.     public ThriftToken getRequestTypeToken() {
  43.         return requestTypeToken;
  44.     }

  45.     @Nullable
  46.     public ThriftToken getOneWayToken() {
  47.         return oneway;
  48.     }

  49.     @Nonnull
  50.     public String getReturnType() {
  51.         return DeclarationUtil.toTypeString(returnTypeTokens);
  52.     }

  53.     @Nonnull
  54.     public List<ThriftToken> getReturnTypeTokens() {
  55.         return returnTypeTokens;
  56.     }

  57.     @Nonnull
  58.     public List<FieldDeclaration> getParams() {
  59.         return params;
  60.     }

  61.     @Nullable
  62.     public List<FieldDeclaration> getThrowing() {
  63.         return throwing;
  64.     }
  65. }