001/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*-
002 *
003 * Copyright © 2024–2025 microBean™.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
006 * the License. You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
011 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
012 * specific language governing permissions and limitations under the License.
013 */
014package org.microbean.bean;
015
016import java.lang.constant.ClassDesc;
017import java.lang.constant.Constable;
018import java.lang.constant.ConstantDesc;
019import java.lang.constant.DynamicConstantDesc;
020import java.lang.constant.MethodHandleDesc;
021
022import java.util.List;
023import java.util.Optional;
024
025import javax.lang.model.type.TypeMirror;
026
027import org.microbean.constant.Constables;
028
029import org.microbean.qualifier.NamedAttributeMap;
030
031import static java.lang.constant.ConstantDescs.BSM_INVOKE;
032import static java.lang.constant.ConstantDescs.CD_List;
033
034/**
035 * A pairing of a {@link TypeMirror} with a {@link List} of {@link NamedAttributeMap}s.
036 *
037 * @param type a {@link TypeMirror}
038 *
039 * @param attributes a {@link List} of {@link NamedAttributeMap}s
040 *
041 * @author <a href="https://about.me/lairdnelson" target="_top">Laird Nelson</a>
042 */
043public final record AttributedType(TypeMirror type, List<NamedAttributeMap<?>> attributes) implements Constable {
044
045  private static final ClassDesc CD_TypeMirror = ClassDesc.of("javax.lang.model.type.TypeMirror");
046
047  /**
048   * Creates a new {@link AttributedType}.
049   *
050   * @param type a {@link TypeMirror}; must not be {@code null}; must be a {@linkplain
051   * javax.lang.model.type.TypeKind#isPrimitive() primitive}, {@linkplain javax.lang.model.type.TypeKind#ARRAY array} or
052   * {@linkplain javax.lang.model.type.TypeKind#DECLARED declared} type
053   *
054   * @param attributes a {@link List} of {@link NamedAttributeMap}s; must not be {@code null}
055   *
056   * @exception NullPointerException if either argument is {@code null}
057   *
058   * @exception IllegalArgumentException if {@code type} is the wrong kind of type
059   */
060  public AttributedType {
061    type = switch (type.getKind()) {
062    case ARRAY, BOOLEAN, BYTE, CHAR, DECLARED, DOUBLE, FLOAT, INT, LONG, SHORT -> type;
063    case ERROR, EXECUTABLE, INTERSECTION, MODULE, NONE, NULL, OTHER, PACKAGE, TYPEVAR, UNION, VOID, WILDCARD ->
064      throw new IllegalArgumentException("type: " + type);
065    };
066    attributes = List.copyOf(attributes);
067  }
068
069  /**
070   * Creates a new {@link AttributedType}.
071   *
072   * @param type a {@link TypeMirror}; must not be {@code null}; must be a {@linkplain
073   * javax.lang.model.type.TypeKind#isPrimitive() primitive}, {@linkplain javax.lang.model.type.TypeKind#ARRAY array} or
074   * {@linkplain javax.lang.model.type.TypeKind#DECLARED declared} type
075   *
076   * @exception NullPointerException if {@code type} is {@code null}
077   *
078   * @exception IllegalArgumentException if {@code type} is the wrong kind of type
079   */
080  public AttributedType(final TypeMirror type) {
081    this(type, List.of());
082  }
083
084  /**
085   * Returns an {@link Optional} containing a {@link ConstantDesc} describing this {@link AttributedType}, or an
086   * {@linkplain Optional#isEmpty() empty <code>Optional</code>} if it could not be described.
087   *
088   * @return an {@link Optional} containing a {@link ConstantDesc} describing this {@link AttributedType}, or an
089   * {@linkplain Optional#isEmpty() empty <code>Optional</code>} if it could not be describe; never {@code null}
090   */
091  @Override // Constable
092  public Optional<? extends ConstantDesc> describeConstable() {
093    return this.type() instanceof Constable t ? t.describeConstable() : Optional.<ConstantDesc>empty()
094      .flatMap(typeDesc -> Constables.describeConstable(this.attributes())
095               .map(attributesDesc -> DynamicConstantDesc.of(BSM_INVOKE,
096                                                             MethodHandleDesc.ofConstructor(ClassDesc.of(this.getClass().getName()),
097                                                                                            CD_TypeMirror,
098                                                                                            CD_List),
099                                                             typeDesc,
100                                                             attributesDesc)));
101  }
102
103}