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

org.jboss.weld.metadata.cache.AnnotationModel Maven / Gradle / Ivy

There is a newer version: 3.0.0.Alpha1
Show newest version
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2008, Red Hat, Inc., and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jboss.weld.metadata.cache;

import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Set;

import javax.enterprise.inject.spi.AnnotatedType;

import org.jboss.weld.annotated.enhanced.EnhancedAnnotation;
import org.jboss.weld.logging.MetadataLogger;
import org.jboss.weld.logging.ReflectionLogger;

/**
 * Abstract representation of an annotation model
 *
 * @author Pete Muir
 */
public abstract class AnnotationModel {

    // The underlying annotation
    private final AnnotatedType annotatedAnnotation;
    // Is the data valid?
    protected boolean valid;

    /**
     * Constructor
     *
     * @param type The annotation type
     */
    public AnnotationModel(EnhancedAnnotation enhancedAnnotatedAnnotation) {
        this.annotatedAnnotation = enhancedAnnotatedAnnotation.slim();
        init(enhancedAnnotatedAnnotation);
    }

    /**
     * Initializes the type and validates it
     */
    protected void init(EnhancedAnnotation annotatedAnnotation) {
        initType(annotatedAnnotation);
        initValid(annotatedAnnotation);
        check(annotatedAnnotation);
    }

    /**
     * Initializes the type
     */
    protected void initType(EnhancedAnnotation annotatedAnnotation) {
        if (!Annotation.class.isAssignableFrom(getRawType())) {
            throw MetadataLogger.LOG.metaAnnotationOnWrongType(getMetaAnnotationTypes(), getRawType());
        }
    }

    /**
     * Validates the data for correct annotation
     */
    protected void initValid(EnhancedAnnotation annotatedAnnotation) {
        this.valid = false;
        for (Class annotationType : getMetaAnnotationTypes()) {
            if (annotatedAnnotation.isAnnotationPresent(annotationType)) {
                this.valid = true;
            }
        }
    }

    protected void check(EnhancedAnnotation annotatedAnnotation) {
        if (valid && (!annotatedAnnotation.isAnnotationPresent(Retention.class) || annotatedAnnotation.isAnnotationPresent(Retention.class) && !annotatedAnnotation.getAnnotation(Retention.class).value().equals(RetentionPolicy.RUNTIME))) {
            this.valid = false;
            ReflectionLogger.LOG.missingRetention(annotatedAnnotation);
        }
    }

    /**
     * Gets the type of the annotation
     *
     * @return The type
     */
    public Class getRawType() {
        return annotatedAnnotation.getJavaClass();
    }

    /**
     * Gets the meta-annotation that should be present
     *
     * @return
     */
    protected abstract Set> getMetaAnnotationTypes();

    /**
     * Indicates if the annotation is valid
     *
     * @return True if valid, false otherwise
     */
    public boolean isValid() {
        return valid;
    }

    /**
     * Gets the annotated annotation
     *
     * @return The annotation
     */
    public AnnotatedType getAnnotatedAnnotation() {
        return annotatedAnnotation;
    }

    /**
     * Gets a string representation of the annotation model
     *
     * @return The string representation
     */
    @Override
    public String toString() {
        return (isValid() ? "Valid" : "Invalid") + " annotation model for " + getRawType();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy