Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.portable » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements. See the NOTICE file distributed with this
    4    * work for additional information regarding copyright ownership. The ASF
    5    * licenses this file to You under the Apache License, Version 2.0 (the
    6    * "License"); you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
    9    * or agreed to in writing, software distributed under the License is
   10    * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   11    * KIND, either express or implied. See the License for the specific language
   12    * governing permissions and limitations under the License.
   13    */
   14   package org.apache.webbeans.portable;
   15   
   16   import java.lang.annotation.Annotation;
   17   import java.lang.reflect.Type;
   18   import java.util.HashSet;
   19   import java.util.Set;
   20   
   21   import javax.enterprise.inject.spi.Annotated;
   22   
   23   import org.apache.webbeans.util.ClassUtil;
   24   
   25   /**
   26    * Abstract implementation of the {@link Annotated} contract.
   27    * 
   28    * @version $Rev: 788940 $ $Date: 2009-06-27 11:52:08 +0300 (Sat, 27 Jun 2009) $
   29    */
   30   abstract class AbstractAnnotated implements Annotated
   31   {
   32       /**Base type of an annotated element*/
   33       private Type baseType = null;
   34       
   35       /**Type closures*/
   36       private Set<Type> typeClosures = new HashSet<Type>();
   37       
   38       /**Set of annotations*/
   39       private Set<Annotation> annotations = new HashSet<Annotation>();
   40       
   41       /**
   42        * Createa a new annotated element.
   43        * 
   44        * @param baseType annotated element type
   45        */
   46       protected AbstractAnnotated(Type baseType)
   47       {
   48           this.baseType = baseType;
   49           ClassUtil.setTypeHierarchy(this.typeClosures, this.baseType);
   50       }
   51   
   52       /**
   53        * Adds new annotation to set.
   54        * 
   55        * @param annotation new annotation
   56        */
   57       protected void addAnnotation(Annotation annotation)
   58       {
   59           this.annotations.add(annotation);
   60       }
   61       
   62   
   63       /**
   64        * Adds new annotation to set.
   65        * 
   66        * @param annotation new annotation
   67        */
   68       protected void setAnnotations(Annotation[] annotations)
   69       {        
   70           this.annotations.clear();
   71           for(Annotation annotation : annotations)
   72           {
   73               this.annotations.add(annotation);
   74           }
   75       }
   76       
   77       /**
   78        * {@inheritDoc}
   79        */
   80       @Override
   81       @SuppressWarnings("unchecked")
   82       public <T extends Annotation> T getAnnotation(Class<T> annotationType)
   83       {
   84           for(Annotation ann : this.annotations)
   85           {
   86               if(ann.annotationType().equals(annotationType))
   87               {
   88                   return (T)ann;
   89               }
   90           }
   91           
   92           return null;
   93   
   94       }
   95   
   96       /**
   97        * {@inheritDoc}
   98        */
   99       @Override
  100       public Set<Annotation> getAnnotations()
  101       {
  102           return this.annotations;
  103       }
  104   
  105       /**
  106        * {@inheritDoc}
  107        */
  108       @Override
  109       public Type getBaseType()
  110       {
  111           return this.baseType;
  112       }
  113   
  114       /**
  115        * {@inheritDoc}
  116        */
  117       @Override
  118       public Set<Type> getTypeClosure()
  119       {
  120           return this.typeClosures;
  121       }
  122   
  123       /**
  124        * {@inheritDoc}
  125        */
  126       @Override
  127       public boolean isAnnotationPresent(Class<? extends Annotation> annotationType)
  128       {
  129           for(Annotation ann : this.annotations)
  130           {
  131               if(ann.annotationType().equals(annotationType))
  132               {
  133                   return true;
  134               }
  135           }        
  136           
  137           return false;
  138       }
  139       
  140       public String toString()
  141       {
  142           StringBuilder builder = new StringBuilder();
  143           
  144           builder.append("Base Type : " + baseType.toString() + ",");
  145           builder.append("Type Closures : " + typeClosures.toString() + ",");
  146           builder.append("Annotations : " + annotations.toString());
  147           
  148           return builder.toString();
  149       }
  150   
  151   }

Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.portable » [javadoc | source]