View Javadoc

1   /***************************************************************************************
2    * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved.                 *
3    * http://aspectwerkz.codehaus.org                                                    *
4    * ---------------------------------------------------------------------------------- *
5    * The software in this package is published under the terms of the LGPL license      *
6    * a copy of which has been included with this distribution in the license.txt file.  *
7    **************************************************************************************/
8   package org.codehaus.aspectwerkz.reflect.impl.asm;
9   
10  import org.codehaus.aspectwerkz.annotation.instrumentation.asm.CustomAttribute;
11  import org.codehaus.aspectwerkz.annotation.instrumentation.asm.CustomAttributeHelper;
12  import org.codehaus.aspectwerkz.annotation.AnnotationInfo;
13  import org.codehaus.aspectwerkz.reflect.ClassInfo;
14  import org.codehaus.aspectwerkz.reflect.MemberInfo;
15  import org.codehaus.aspectwerkz.UnbrokenObjectInputStream;
16  import org.objectweb.asm.Attribute;
17  import org.objectweb.asm.attrs.RuntimeInvisibleAnnotations;
18  import org.objectweb.asm.attrs.Annotation;
19  import org.objectweb.asm.attrs.RuntimeVisibleAnnotations;
20  
21  import java.io.ByteArrayInputStream;
22  import java.lang.ref.WeakReference;
23  import java.util.*;
24  
25  /***
26   * ASM implementation of the MemberInfo interface.
27   *
28   * @author <a href="mailto:jboner@codehaus.org">Jonas Bonér </a>
29   */
30  public abstract class AsmMemberInfo implements MemberInfo {
31  
32      /***
33       * The member.
34       */
35      protected final MemberStruct m_member;
36  
37      /***
38       * The class loader wrapped in a weak ref.
39       */
40      protected final WeakReference m_loaderRef;
41  
42      /***
43       * The declaring type name.
44       */
45      protected final String m_declaringTypeName;
46  
47      /***
48       * The declaring type.
49       */
50      protected ClassInfo m_declaringType;
51  
52      /***
53       * The annotations.
54       */
55      protected List m_annotations = null;
56  
57      /***
58       * The class info repository.
59       */
60      protected final AsmClassInfoRepository m_classInfoRepository;
61  
62      /***
63       * Creates a new member meta data instance.
64       *
65       * @param member
66       * @param declaringType
67       * @param loader
68       */
69      AsmMemberInfo(final MemberStruct member, final String declaringType, final ClassLoader loader) {
70          if (member == null) {
71              throw new IllegalArgumentException("member can not be null");
72          }
73          if (declaringType == null) {
74              throw new IllegalArgumentException("declaring type can not be null");
75          }
76          m_member = member;
77          m_loaderRef = new WeakReference(loader);
78          m_declaringTypeName = declaringType.replace('/', '.');
79          m_classInfoRepository = AsmClassInfoRepository.getRepository(loader);
80      }
81  
82      /***
83       * Returns the name.
84       *
85       * @return the name
86       */
87      public String getName() {
88          return m_member.name;
89      }
90  
91      /***
92       * Returns the modifiers.
93       *
94       * @return the modifiers
95       */
96      public int getModifiers() {
97          return m_member.modifiers;
98      }
99  
100     /***
101      * Returns the declaring type.
102      *
103      * @return the declaring type
104      */
105     public ClassInfo getDeclaringType() {
106         if (m_declaringType == null) {
107             m_declaringType = m_classInfoRepository.getClassInfo(m_declaringTypeName);
108         }
109         return m_declaringType;
110     }
111 
112     /***
113      * Retrieves and adds the annotations.
114      *
115      * @param attrs
116      */
117     private void addAnnotations(final Attribute attrs) {
118         Attribute attributes = attrs;
119         while (attributes != null) {
120             if (attributes instanceof RuntimeInvisibleAnnotations) {
121                 for (Iterator it = ((RuntimeInvisibleAnnotations)attributes).annotations.iterator(); it.hasNext();) {
122                     Annotation annotation = (Annotation)it.next();
123                     if (CustomAttribute.TYPE.equals(annotation.type)) {
124                         m_annotations.add(CustomAttributeHelper.extractCustomAnnotation(annotation));
125                     } else {
126                         AnnotationInfo annotationInfo = AsmClassInfo.getAnnotationInfo(
127                                 annotation,
128                                 (ClassLoader)m_loaderRef.get()
129                         );
130                         m_annotations.add(annotationInfo);
131                     }
132                 }
133             }
134             if (attributes instanceof RuntimeVisibleAnnotations) {
135                 for (Iterator it = ((RuntimeVisibleAnnotations)attributes).annotations.iterator(); it.hasNext();) {
136                     Annotation annotation = (Annotation)it.next();
137                     AnnotationInfo annotationInfo = AsmClassInfo.getAnnotationInfo(
138                             annotation,
139                             (ClassLoader)m_loaderRef.get()
140                     );
141                     m_annotations.add(annotationInfo);
142                 }
143             }
144             attributes = attributes.next;
145         }
146     }
147 }