001/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*- 002 * 003 * Copyright © 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.assign; 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.element.Element; 026 027import javax.lang.model.type.TypeMirror; 028 029import org.microbean.attributes.Attributed; 030import org.microbean.attributes.Attributes; 031 032import org.microbean.constant.Constables; 033 034import static java.lang.constant.ConstantDescs.BSM_INVOKE; 035import static java.lang.constant.ConstantDescs.CD_List; 036 037import static java.util.Objects.requireNonNull; 038 039/** 040 * A pairing of an {@link Element} with a {@link List} of {@link Attributes}s. 041 * 042 * @param element an {@link Element} 043 * 044 * @param attributes a {@link List} of {@link Attributes}s 045 * 046 * @author <a href="https://about.me/lairdnelson/" target="_top">Laird Nelson</a> 047 */ 048public final record AttributedElement(Element element, List<Attributes> attributes) implements Attributed, AttributedTyped, Constable { 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 Attributes}; must not be {@code null} 056 * 057 * @exception NullPointerException if either argument is {@code null} 058 */ 059 public AttributedElement { 060 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 @Override // AttributedTyped 081 public final AttributedType attributedType() { 082 return new AttributedType(this.type(), this.attributes()); 083 } 084 085 /** 086 * Returns an {@link Optional} containing a {@link ConstantDesc} describing this {@link AttributedType}, or an 087 * {@linkplain Optional#isEmpty() empty <code>Optional</code>} if it could not be described. 088 * 089 * @return an {@link Optional} containing a {@link ConstantDesc} describing this {@link AttributedType}, or an 090 * {@linkplain Optional#isEmpty() empty <code>Optional</code>} if it could not be describe; never {@code null} 091 */ 092 @Override // Constable 093 public Optional<? extends ConstantDesc> describeConstable() { 094 return this.element() instanceof Constable e ? e.describeConstable() : Optional.<ConstantDesc>empty() 095 .flatMap(elementDesc -> Constables.describeConstable(this.attributes()) 096 .map(attributesDesc -> DynamicConstantDesc.of(BSM_INVOKE, 097 MethodHandleDesc.ofConstructor(ClassDesc.of(this.getClass().getName()), 098 ClassDesc.of("javax.lang.model.element.Element"), 099 CD_List), 100 elementDesc, 101 attributesDesc))); 102 } 103 104}