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.Objects; 024import java.util.Optional; 025 026import javax.lang.model.element.Element; 027 028import javax.lang.model.type.TypeMirror; 029 030import org.microbean.constant.Constables; 031 032import org.microbean.qualifier.NamedAttributeMap; 033 034import static java.lang.constant.ConstantDescs.BSM_INVOKE; 035import static java.lang.constant.ConstantDescs.CD_List; 036 037/** 038 * A pairing of an {@link Element} with a {@link List} of {@link NamedAttributeMap}s. 039 * 040 * @param element an {@link Element} 041 * 042 * @param attributes a {@link List} of {@link NamedAttributeMap}s 043 * 044 * @author <a href="https://about.me/lairdnelson/" target="_top">Laird Nelson</a> 045 */ 046public final record AttributedElement(Element element, List<NamedAttributeMap<?>> attributes) implements Constable { 047 048 private static final ClassDesc CD_Element = ClassDesc.of("javax.lang.model.element.Element"); 049 050 /** 051 * Creates a new {@link AttributedElement}. 052 * 053 * @param element a {@link Element}; must not be {@code null} 054 * 055 * @param attributes a {@link List} of {@link NamedAttributeMap}s; must not be {@code null} 056 * 057 * @exception NullPointerException if either argument is {@code null} 058 */ 059 public AttributedElement { 060 Objects.requireNonNull(element, "element"); 061 attributes = List.copyOf(attributes); 062 } 063 064 /** 065 * Returns this {@link AttributedElement}'s {@linkplain Element#asType() type}. 066 * 067 * @return this {@link AttributedElement}'s {@linkplain Element#asType() type}; never {@code null} 068 */ 069 public final TypeMirror type() { 070 return this.element().asType(); 071 } 072 073 /** 074 * Returns this {@link AttributedElement}'s {@link AttributedType}. 075 * 076 * @return this {@link AttributedElement}'s {@link AttributedType}; never {@code null} 077 * 078 * @see AttributedType 079 */ 080 public final AttributedType attributedType() { 081 return new AttributedType(this.type(), this.attributes()); 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.element() instanceof Constable e ? e.describeConstable() : Optional.<ConstantDesc>empty() 094 .flatMap(elementDesc -> Constables.describeConstable(this.attributes()) 095 .map(attributesDesc -> DynamicConstantDesc.of(BSM_INVOKE, 096 MethodHandleDesc.ofConstructor(ClassDesc.of(this.getClass().getName()), 097 CD_Element, 098 CD_List), 099 elementDesc, 100 attributesDesc))); 101 } 102 103}