/*
 * Copyright 2019 Immutables Authors and Contributors
 *
 * 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 org.immutables.value.processor.meta;

import javax.lang.model.type.ArrayType;
import javax.lang.model.type.ExecutableType;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.SimpleTypeVisitor6;

/**
 * Static util methods related to {@link javax.lang.model.type.TypeMirror}
 */
public class MoreTypes {

  /**
   * Returns a {@link ExecutableType} if the {@link TypeMirror} represents an executable type such
   * as a method, constructor, or initializer or throws an {@link IllegalArgumentException}.
   */
  public static ExecutableType asExecutable(TypeMirror maybeExecutableType) {
    return maybeExecutableType.accept(ExecutableTypeVisitor.INSTANCE, null);
  }

  private static final class ExecutableTypeVisitor extends AbstractVisitor<ExecutableType> {
    private static final ExecutableTypeVisitor INSTANCE = new ExecutableTypeVisitor();

    ExecutableTypeVisitor() {
      super("executable type");
    }

    @Override
    public ExecutableType visitExecutable(ExecutableType type, Void ignore) {
      return type;
    }
  }

  /**
   * Returns a {@link ArrayType} if the {@link TypeMirror} represents a primitive array or throws an
   * {@link IllegalArgumentException}.
   */
  public static ArrayType asArray(TypeMirror maybeArrayType) {
    return maybeArrayType.accept(ArrayTypeVisitor.INSTANCE, null);
  }

  private static final class ArrayTypeVisitor extends AbstractVisitor<ArrayType> {
    private static final ArrayTypeVisitor INSTANCE = new ArrayTypeVisitor();

    ArrayTypeVisitor() {
      super("primitive array");
    }

    @Override
    public ArrayType visitArray(ArrayType type, Void ignore) {
      return type;
    }
  }


  private abstract static class AbstractVisitor<T> extends SimpleTypeVisitor6<T, Void> {
    private final String label;

    AbstractVisitor(String label) {
      this.label = label;
    }

    @Override
    protected T defaultAction(TypeMirror e, Void v) {
      throw new IllegalArgumentException(e + " does not represent a " + label);
    }
  }

}

@@ -692,24 +692,67 @@
 .ArrayType;%0A
+import javax.lang.model.type.DeclaredType;%0A
 import javax
@@ -2690,10 +2690,720 @@
  %7D%0A  %7D%0A%0A
+  /**%0A   * Returns a %7B@link DeclaredType%7D if the %7B@link TypeMirror%7D represents a declared type such as a%0A   * class, interface, union/compound, or enum or throws an %7B@link IllegalArgumentException%7D.%0A   */%0A  public static DeclaredType asDeclared(TypeMirror maybeDeclaredType) %7B%0A    return maybeDeclaredType.accept(DeclaredTypeVisitor.INSTANCE, null);%0A  %7D%0A%0A  private static final class DeclaredTypeVisitor extends AbstractVisitor%3CDeclaredType%3E %7B%0A    private static final DeclaredTypeVisitor INSTANCE = new DeclaredTypeVisitor();%0A%0A    DeclaredTypeVisitor() %7B%0A      super(%22declared type%22);%0A    %7D%0A%0A    @Override%0A    public DeclaredType visitDeclared(DeclaredType type, Void ignore) %7B%0A      return type;%0A    %7D%0A  %7D%0A%0A%0A
 %7D%0A

Add asDeclared():DeclaredType to MoreTypes util class