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.attributes.Attributed; 028import org.microbean.attributes.Attributes; 029 030import org.microbean.constant.Constables; 031 032import static java.lang.constant.ConstantDescs.BSM_INVOKE; 033import static java.lang.constant.ConstantDescs.CD_List; 034import static java.util.Arrays.asList; 035 036/** 037 * A pairing of a {@link TypeMirror} with a {@link List} of {@link Attributes}s. 038 * 039 * @param type a {@link TypeMirror} 040 * 041 * @param attributes a {@link List} of {@link Attributes}s 042 * 043 * @author <a href="https://about.me/lairdnelson" target="_top">Laird Nelson</a> 044 */ 045public final record AttributedType(TypeMirror type, List<Attributes> attributes) implements Attributed, Constable { 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 an array of {@link Attributes}; may be {@code null} 055 * 056 * @exception NullPointerException if {@code type} is {@code null} 057 * 058 * @exception IllegalArgumentException if {@code type} is the wrong kind of type 059 */ 060 public AttributedType(final TypeMirror type, final Attributes... attributes) { 061 this(type, attributes == null || attributes.length <= 0 ? List.of() : asList(attributes)); 062 } 063 064 /** 065 * Creates a new {@link AttributedType}. 066 * 067 * @param type a {@link TypeMirror}; must not be {@code null}; must be a {@linkplain 068 * javax.lang.model.type.TypeKind#isPrimitive() primitive}, {@linkplain javax.lang.model.type.TypeKind#ARRAY array} or 069 * {@linkplain javax.lang.model.type.TypeKind#DECLARED declared} type 070 * 071 * @param attributes a {@link List} of {@link Attributes}; must not be {@code null} 072 * 073 * @exception NullPointerException if either argument is {@code null} 074 * 075 * @exception IllegalArgumentException if {@code type} is the wrong kind of type 076 */ 077 public AttributedType { 078 switch (type.getKind()) { 079 case ARRAY, BOOLEAN, BYTE, CHAR, DECLARED, DOUBLE, FLOAT, INT, LONG, SHORT: 080 break; 081 case ERROR, EXECUTABLE, INTERSECTION, MODULE, NONE, NULL, OTHER, PACKAGE, TYPEVAR, UNION, VOID, WILDCARD: 082 default: 083 throw new IllegalArgumentException("type: " + type); 084 } 085 attributes = List.copyOf(attributes); 086 } 087 088 /** 089 * Creates a new {@link AttributedType}. 090 * 091 * @param type a {@link TypeMirror}; must not be {@code null}; must be a {@linkplain 092 * javax.lang.model.type.TypeKind#isPrimitive() primitive}, {@linkplain javax.lang.model.type.TypeKind#ARRAY array} or 093 * {@linkplain javax.lang.model.type.TypeKind#DECLARED declared} type 094 * 095 * @exception NullPointerException if {@code type} is {@code null} 096 * 097 * @exception IllegalArgumentException if {@code type} is the wrong kind of type 098 */ 099 public AttributedType(final TypeMirror type) { 100 this(type, List.of()); 101 } 102 103 /** 104 * Returns an {@link Optional} containing a {@link ConstantDesc} describing this {@link AttributedType}, or an 105 * {@linkplain Optional#isEmpty() empty <code>Optional</code>} if it could not be described. 106 * 107 * @return an {@link Optional} containing a {@link ConstantDesc} describing this {@link AttributedType}, or an 108 * {@linkplain Optional#isEmpty() empty <code>Optional</code>} if it could not be describe; never {@code null} 109 */ 110 @Override // Constable 111 public Optional<? extends ConstantDesc> describeConstable() { 112 return this.type() instanceof Constable t ? t.describeConstable() : Optional.<ConstantDesc>empty() 113 .flatMap(typeDesc -> Constables.describeConstable(this.attributes()) 114 .map(attributesDesc -> DynamicConstantDesc.of(BSM_INVOKE, 115 MethodHandleDesc.ofConstructor(ClassDesc.of(this.getClass().getName()), 116 ClassDesc.of(TypeMirror.class.getName()), 117 CD_List), 118 typeDesc, 119 attributesDesc))); 120 } 121 122 /** 123 * Returns an {@link AttributedType} comprising the supplied arguments. 124 * 125 * @param type a {@link TypeMirror}; must not be {@code null}; must be a {@linkplain 126 * javax.lang.model.type.TypeKind#isPrimitive() primitive}, {@linkplain javax.lang.model.type.TypeKind#ARRAY array} or 127 * {@linkplain javax.lang.model.type.TypeKind#DECLARED declared} type 128 * 129 * @param attributes an array of {@link Attributes}; may be {@code null} 130 * 131 * @return a non-{@code null} {@link AttributedType} 132 * 133 * @exception NullPointerException if {@code type} is {@code null} 134 * 135 * @exception IllegalArgumentException if {@code type} is the wrong kind of type 136 */ 137 public static final AttributedType of(final TypeMirror type, Attributes... attributes) { 138 return new AttributedType(type, attributes == null || attributes.length <= 0 ? List.of() : asList(attributes)); 139 } 140 141}