All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.jstach.rainbowgum.apt.prism.PassThroughParameterPrism Maven / Gradle / Ivy

package io.jstach.rainbowgum.apt.prism;

import java.util.Map;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.ElementFilter;

import org.eclipse.jdt.annotation.Nullable;

/**
 * A Prism representing an {@code @io.jstach.rainbowgum.annotation.LogConfigurable.PassThroughParameter} annotation. 
 */ 
public class PassThroughParameterPrism {
    /**
     * Qualified class name of annotation.
     */
    public static final String PRISM_ANNOTATION_TYPE = "io.jstach.rainbowgum.annotation.LogConfigurable.PassThroughParameter";

    /**
     * Return a prism representing the {@code @io.jstach.rainbowgum.annotation.LogConfigurable.PassThroughParameter} annotation on 'e'. 
     * similar to {@code e.getAnnotation(io.jstach.rainbowgum.annotation.LogConfigurable.PassThroughParameter.class)} except that 
     * an instance of this class rather than an instance of {@code io.jstach.rainbowgum.annotation.LogConfigurable.PassThroughParameter}
     * is returned.
     * @param e element. 
     * @return prism for element. 
     */
    public static @Nullable PassThroughParameterPrism getInstanceOn(Element e) {
        AnnotationMirror m = getMirror(PRISM_ANNOTATION_TYPE, e);
        if(m == null) return null;
        return getInstance(m);
   }

    /**
     * Return a prism of the {@code @io.jstach.rainbowgum.annotation.LogConfigurable.PassThroughParameter} annotation whose mirror is mirror. 
     * @param mirror mirror. 
     * @return prism for mirror 
     */

    public static PassThroughParameterPrism getInstance(AnnotationMirror mirror) {
        String mirrorType = mirror.getAnnotationType().toString();
        if(!PRISM_ANNOTATION_TYPE.equals(mirrorType)) {
             throw new java.lang.IllegalArgumentException("expected: " + PRISM_ANNOTATION_TYPE + " got: " + mirrorType);
        }
        return new PassThroughParameterPrism(mirror);
    }

    private PassThroughParameterPrism(AnnotationMirror mirror) {
        for(var e : mirror.getElementValues().entrySet()) {
            memberValues.put(e.getKey().getSimpleName().toString(), e.getValue());
        }
        for(ExecutableElement member : ElementFilter.methodsIn(mirror.getAnnotationType().asElement().getEnclosedElements())) {
            var defaultValue = member.getDefaultValue();
            if (defaultValue == null) continue;
            defaults.put(member.getSimpleName().toString(), defaultValue);
        }
        this.mirror = mirror;
        this.isValid = valid;
    }

    /**
     * Determine whether the underlying AnnotationMirror has no errors.
     * True if the underlying AnnotationMirror has no errors.
     * When true is returned, none of the methods will return null.
     * When false is returned, a least one member will either return null, or another
     * prism that is not valid.
     */
    public final boolean isValid;
    
    /**
     * The underlying AnnotationMirror of the annotation
     * represented by this Prism. 
     * Primarily intended to support using Messager.
     */
    public final AnnotationMirror mirror;
    private Map defaults = new java.util.HashMap<>();
    private Map memberValues = new java.util.HashMap<>();
    private boolean valid = true;

    private static @Nullable AnnotationMirror getMirror(String fqn, Element target) {
        for (AnnotationMirror m :target.getAnnotationMirrors()) {
            CharSequence mfqn = ((TypeElement)m.getAnnotationType().asElement()).getQualifiedName();
            if(fqn.contentEquals(mfqn)) return m;
        }
        return null;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy