Home » apache-openwebbeans-1.0.0-incubating-M3-sources » org.apache.webbeans.annotation » [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.annotation;
   15   
   16   import java.lang.annotation.Annotation;
   17   import java.lang.reflect.Method;
   18   import java.util.Collections;
   19   import java.util.HashMap;
   20   import java.util.Map;
   21   
   22   import javassist.util.proxy.MethodHandler;
   23   
   24   
   25   import org.apache.webbeans.exception.inject.ExecutionException;
   26   import org.apache.webbeans.util.Asserts;
   27   
   28   /**
   29    * Defines the {@link Annotation} implementation. It is used for creating
   30    * annotations from the XML configuration files.
   31    * 
   32    * @author <a href="mailto:gurkanerdogdu@yahoo.com">Gurkan Erdogdu</a>
   33    * @since 1.0
   34    */
   35   public class WebBeansAnnotation implements Annotation, MethodHandler
   36   {
   37       /** Annotation class type for this annotation */
   38       private Class<? extends Annotation> annotationType = null;
   39   
   40       /** Annotation members map values */
   41       private Map<String, Object> annotationMembersValueMap = new HashMap<String, Object>();
   42   
   43       /** Annotation type class methods members */
   44       private Method[] members = null;
   45   
   46       /**
   47        * Creates new annotation type object from given annotation type
   48        * 
   49        * @param annotationType annotation class type
   50        */
   51       public WebBeansAnnotation(Class<? extends Annotation> annotationType)
   52       {
   53           this.annotationType = annotationType;
   54           this.members = annotationType.getDeclaredMethods();
   55   
   56           /* Set default method values */
   57           setDefaultValues();
   58       }
   59   
   60       /**
   61        * Javassist proxy object method handler. It is used in equality comparison
   62        * with {@link Annotation} types.
   63        * <p>
   64        * If method is not implemented by this proxy, then proceed method is null.
   65        * So, if the method is in the implemented annotation member method, then it
   66        * is taken from the map values of this annotation member values.
   67        * </p>
   68        * 
   69        * @param self proxy object
   70        * @param m method invoked method
   71        * @param proceed actual method on this proxy object self
   72        * @param args method arguments
   73        * @throws Exception if any exception occurs
   74        */
   75       public Object invoke(Object self, Method m, Method proceed, Object[] args) throws Exception
   76       {
   77           WebBeansAnnotation anno = (WebBeansAnnotation) self;
   78   
   79           if (isMethodExist(m) && proceed == null)
   80           {
   81               return anno.getAnnotationMembersValueMap().get(m.getName());
   82           }
   83   
   84           return proceed.invoke(self, args);
   85       }
   86   
   87       /**
   88        * Gets
   89        * 
   90        * @return the unmodifiable map
   91        */
   92       public Map<String, Object> getAnnotationMembersValueMap()
   93       {
   94           return Collections.unmodifiableMap(this.annotationMembersValueMap);
   95       }
   96   
   97       public Class<? extends Annotation> annotationType()
   98       {
   99           return annotationType;
  100       }
  101   
  102       public void setMemberValue(String memberName, Object memberValue)
  103       {
  104           Asserts.assertNotNull(memberValue, "memberName parameter can not be null");
  105           Asserts.assertNotNull(memberValue, "memberValue parameter can not be null");
  106   
  107           this.annotationMembersValueMap.put(memberName, memberValue);
  108       }
  109   
  110       @Override
  111       public String toString()
  112       {
  113   
  114           StringBuilder sb = new StringBuilder("@" + annotationType().getName() + "(");
  115           for (int i = 0; i < members.length; i++)
  116           {
  117               sb.append(members[i].getName()).append("=");
  118               sb.append(this.invoke(members[i]));
  119               if (i < members.length - 1)
  120               {
  121                   sb.append(",");
  122               }
  123           }
  124           
  125           sb.append(")");
  126           
  127           return sb.toString();
  128       }
  129   
  130       @Override
  131       public boolean equals(Object other)
  132       {
  133           if (other instanceof Annotation)
  134           {
  135               Annotation that = (Annotation) other;
  136               if (this.annotationType().equals(that.annotationType()))
  137               {
  138                   for (Method member : members)
  139                   {
  140                       Object thisValue = this.invoke(member);
  141                       Object thatValue = callMethod(member, that);
  142                       
  143                       if (!thisValue.equals(thatValue))
  144                       {
  145                           return false;
  146                       }
  147                   }
  148                   return true;
  149               }
  150           }
  151           else if (other instanceof WebBeansAnnotation)
  152           {
  153               WebBeansAnnotation that = (WebBeansAnnotation) other;
  154               if (this.annotationType().equals(that.annotationType()))
  155               {
  156                   for (Method member : members)
  157                   {
  158                       Object thisValue = this.invoke(member);
  159                       Object thatValue = that.invoke(member);
  160                       if (!thisValue.equals(thatValue))
  161                       {
  162                           return false;
  163                       }
  164                   }
  165                   return true;
  166               }
  167   
  168           }
  169           return false;
  170       }
  171   
  172       @Override
  173       public int hashCode()
  174       {
  175           int hashCode = 0;
  176           for (Method member : members)
  177           {
  178               int hashCodeName = 127 * member.getName().hashCode();
  179               int hashCodeValue = invoke(member).hashCode();
  180               hashCode += hashCodeName ^ hashCodeValue;
  181           }
  182           return hashCode;
  183       }
  184   
  185       private Object invoke(Method method)
  186       {
  187           String memberName = method.getName();
  188   
  189           return this.annotationMembersValueMap.get(memberName);
  190       }
  191   
  192       private static Object callMethod(Method method, Object instance)
  193       {
  194           try
  195           {
  196               return method.invoke(instance);
  197   
  198           }
  199           catch (Exception e)
  200           {
  201               throw new ExecutionException(e);
  202           }
  203       }
  204   
  205       private boolean isMethodExist(Method method)
  206       {
  207           for (Method m : members)
  208           {
  209               if (m.equals(method))
  210               {
  211                   return true;
  212               }
  213           }
  214   
  215           return false;
  216       }
  217   
  218       private void setDefaultValues()
  219       {
  220           for (Method m : members)
  221           {
  222               Object defValue = m.getDefaultValue();
  223               if (defValue != null)
  224               {
  225                   this.annotationMembersValueMap.put(m.getName(), defValue);
  226               }
  227           }
  228       }
  229   
  230   }

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