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

com.fasterxml.classmate.Annotations Maven / Gradle / Ivy

Go to download

Library for introspecting types with full generic information including resolving of field and method types.

There is a newer version: 1.7.0
Show newest version
package com.fasterxml.classmate;

import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.util.*;

/**
 * Container class used for storing set of annotations resolved for types (classes)
 * as members (methods, fields, constructors).
 * 
 * @author tatu
 */
@SuppressWarnings("serial")
public class Annotations implements Serializable, Iterable
{
    private final Annotation[] NO_ANNOTATIONS = new Annotation[0];
     
    protected LinkedHashMap,Annotation> _annotations;

    /*
    /**********************************************************************
    /* Life-cycle
    /**********************************************************************
     */
    
    public Annotations() { }

    /**
     * Method for adding specified annotation, overriding existing value
     * for the annotation type.
     */
    public void add(Annotation override)
    {
        if (_annotations == null) {
            _annotations = new LinkedHashMap,Annotation>();
        }
        _annotations.put(override.annotationType(), override);
    }

    /**
     * Method for adding all annotations from specified set, as overrides
     * to annotations this set has
     */
    public void addAll(Annotations overrides)
    {
        if (_annotations == null) {
            _annotations = new LinkedHashMap,Annotation>();
        }
        for (Annotation override : overrides._annotations.values()) {
            _annotations.put(override.annotationType(), override);
        }
    }
    
    /**
     * Method for adding specified annotation if and only if no value
     * exists for the annotation type.
     */
    public void addAsDefault(Annotation defValue)
    {
        Class type = defValue.annotationType();
        if (_annotations == null) {
            _annotations = new LinkedHashMap,Annotation>();
            _annotations.put(type, defValue);
        } else if (!_annotations.containsKey(type)) {
            _annotations.put(type, defValue);
        }
    }

    /*
    /**********************************************************************
    /* Accessors
    /**********************************************************************
     */

    @Override
    public Iterator iterator()
    {
        if (_annotations == null) {
            _annotations = new LinkedHashMap,Annotation>();
        }
        return _annotations.values().iterator();
    }
    
    public int size() {
        return (_annotations == null) ? 0 : _annotations.size();
    }

    @SuppressWarnings("unchecked")
    public  A get(Class cls)
    {
        if (_annotations == null) {
            return null;
        }
        return (A) _annotations.get(cls);
    }

    /**
     * @since 1.1.1
     */
    public Annotation[] asArray() {
         if (_annotations == null || _annotations.isEmpty()) {
              return NO_ANNOTATIONS;
         }
         return _annotations.values().toArray(new Annotation[_annotations.size()]);
    }

    /**
     * @since 1.1.1
     */
    public List asList() {
         if (_annotations == null || _annotations.isEmpty()) {
              return Collections.emptyList();
         }
         List l = new ArrayList(_annotations.size());
         l.addAll(_annotations.values());
         return l;
    }

    /*
    /**********************************************************************
    /* Standard method overrides
    /**********************************************************************
     */
    
    @Override public String toString()
    {
        if (_annotations == null) {
            return "[null]";
        }
        return _annotations.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy