001/*
002 * ============================================================================
003 * Copyright © 2015 Square, Inc.
004 * Copyright for the modifications © 2018-2024 by Thomas Thrien.
005 * ============================================================================
006 *
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020package org.tquadrat.foundation.javacomposer.internal;
021
022import static org.apiguardian.api.API.Status.INTERNAL;
023
024import javax.lang.model.element.AnnotationMirror;
025import javax.lang.model.element.AnnotationValue;
026import javax.lang.model.element.VariableElement;
027import javax.lang.model.type.TypeMirror;
028import javax.lang.model.util.SimpleAnnotationValueVisitor14;
029import java.util.List;
030
031import org.apiguardian.api.API;
032import org.tquadrat.foundation.annotation.ClassVersion;
033import org.tquadrat.foundation.javacomposer.internal.AnnotationSpecImpl.BuilderImpl;
034
035/**
036 *  Annotation value visitor adding members to the given builder instance.
037 *
038 *  @author Square, Inc.
039 *  @version $Id: AnnotationValueVisitor.java 1085 2024-01-05 16:23:28Z tquadrat $
040 *  @modified Thomas Thrien - thomas.thrien@tquadrat.org
041 *  @UMLGraph.link
042 *  @since 0.0.5
043 */
044@ClassVersion( sourceVersion = "$Id: AnnotationValueVisitor.java 1085 2024-01-05 16:23:28Z tquadrat $" )
045@API( status = INTERNAL, since = "0.0.5" )
046public class AnnotationValueVisitor extends SimpleAnnotationValueVisitor14<BuilderImpl, String>
047{
048        /*------------*\
049    ====** Attributes **=======================================================
050        \*------------*/
051    /**
052     * The builder.
053     */
054    @SuppressWarnings( "UseOfConcreteClass" )
055    private final BuilderImpl m_Builder;
056
057        /*--------------*\
058    ====** Constructors **=====================================================
059        \*--------------*/
060    /**
061     * Creates a new {@code Visitor} instance.
062     *
063     * @param builder The builder that takes the new members.
064     */
065    public AnnotationValueVisitor( @SuppressWarnings( "UseOfConcreteClass" ) final BuilderImpl builder )
066    {
067        super( builder );
068        m_Builder = builder;
069    }   //  Visitor()
070
071        /*---------*\
072    ====** Methods **==========================================================
073        \*---------*/
074    /**
075     * {@inheritDoc}
076     */
077    @Override
078    protected final BuilderImpl defaultAction( final Object o, final String name )
079    {
080        m_Builder.addMemberForValue( name, o );
081
082        //---* Done *----------------------------------------------------------
083        return m_Builder;
084    }   //  defaultAction()
085
086    /**
087     * {@inheritDoc}
088     */
089    @Override
090    public final BuilderImpl visitAnnotation( final AnnotationMirror a, final String name )
091    {
092        m_Builder.addMember( name, "$L", m_Builder.getFactory().createAnnotation( a ) );
093
094        //---* Done *----------------------------------------------------------
095        return m_Builder;
096    }   //  visitAnnotation()
097
098    /**
099     * {@inheritDoc}
100     */
101    @Override
102    public final BuilderImpl visitEnumConstant( final VariableElement c, final String name )
103    {
104        m_Builder.addMember( name, "$T.$L", c.asType(), c.getSimpleName() );
105
106        //---* Done *----------------------------------------------------------
107        return m_Builder;
108    }   //  visitEnumConstant()
109
110    /**
111     * {@inheritDoc}
112     */
113    @Override
114    public final BuilderImpl visitType( final TypeMirror t, final String name )
115    {
116        m_Builder.addMember( name, "$T.class", t );
117
118        //---* Done *----------------------------------------------------------
119        return m_Builder;
120    }   //  visitType()
121
122    /**
123     * {@inheritDoc}
124     */
125    @Override
126    public final BuilderImpl visitArray( final List<? extends AnnotationValue> values, final String name )
127    {
128        for( final var value : values )
129        {
130            value.accept( this, name );
131        }
132
133        //---* Done *----------------------------------------------------------
134        return m_Builder;
135    }   //  visitArray()
136}
137//  class AnnotationValueVisitor
138
139/*
140 *  End of File
141 */