001/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*- 002 * 003 * Copyright © 2024 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 037import static org.microbean.lang.ConstantDescs.CD_Element; 038 039/** 040 * An {@link Element} that has been decorated with {@linkplain NamedAttributeMap attributes}. 041 * 042 * <p>Dependency injection frameworks often refer to this sort of thing as an <dfn>injection point</dfn>.</p> 043 * 044 * @param element a non-{@code null} {@link Element} 045 * 046 * @param attributes a non-{@code null} {@link List} of {@link NamedAttributeMap} instances representing attributes 047 * 048 * @author <a href="https://about.me/lairdnelson/" target="_top">Laird Nelson</a> 049 */ 050public final record AttributedElement(Element element, List<NamedAttributeMap<?>> attributes) implements Constable { 051 052 public AttributedElement { 053 Objects.requireNonNull(element, "element"); 054 attributes = List.copyOf(attributes); 055 } 056 057 public final TypeMirror type() { 058 return this.element().asType(); 059 } 060 061 public final AttributedType attributedType() { 062 return new AttributedType(this.type(), this.attributes()); 063 } 064 065 /** 066 * Returns an {@link Optional} containing a {@link ConstantDesc} describing this {@link AttributedType}, or an 067 * {@linkplain Optional#isEmpty() empty <code>Optional</code>} if it could not be described. 068 * 069 * @return an {@link Optional} containing a {@link ConstantDesc} describing this {@link AttributedType}, or an 070 * {@linkplain Optional#isEmpty() empty <code>Optional</code>} if it could not be describe; never {@code null} 071 */ 072 @Override // Constable 073 public Optional<? extends ConstantDesc> describeConstable() { 074 return this.element() instanceof Constable e ? e.describeConstable() : Optional.<ConstantDesc>empty() 075 .flatMap(elementDesc -> Constables.describeConstable(this.attributes()) 076 .map(attributesDesc -> DynamicConstantDesc.of(BSM_INVOKE, 077 MethodHandleDesc.ofConstructor(ClassDesc.of(this.getClass().getName()), 078 CD_Element, 079 CD_List), 080 elementDesc, 081 attributesDesc))); 082 } 083 084}