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