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.joinpoint.impl;
9   
10  import org.codehaus.aspectwerkz.joinpoint.FieldRtti;
11  import org.codehaus.aspectwerkz.joinpoint.Rtti;
12  
13  import java.lang.ref.WeakReference;
14  import java.lang.reflect.Field;
15  
16  /***
17   * Implementation for the field signature.
18   *
19   * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
20   */
21  public class FieldRttiImpl implements FieldRtti {
22      private final FieldSignatureImpl m_signature;
23  
24      private WeakReference m_thisRef;
25  
26      private WeakReference m_targetRef;
27  
28      private Object m_fieldValue;
29  
30      /***
31       * Creates a new field RTTI.
32       *
33       * @param signature
34       * @param thisInstance
35       * @param targetInstance
36       */
37      public FieldRttiImpl(final FieldSignatureImpl signature, final Object thisInstance, final Object targetInstance) {
38          m_signature = signature;
39          m_thisRef = new WeakReference(thisInstance);
40          m_targetRef = new WeakReference(targetInstance);
41      }
42  
43      /***
44       * Clones the RTTI instance.
45       *
46       * @param thisInstance
47       * @param targetInstance
48       * @return
49       */
50      public Rtti cloneFor(final Object thisInstance, final Object targetInstance) {
51          return new FieldRttiImpl(m_signature, thisInstance, targetInstance);
52      }
53  
54      /***
55       * Returns the target instance.
56       *
57       * @return the target instance
58       */
59      public Object getTarget() {
60          return m_targetRef.get();
61      }
62  
63      /***
64       * Returns the instance currently executing.
65       *
66       * @return the instance currently executing
67       */
68      public Object getThis() {
69          return m_thisRef.get();
70      }
71  
72      /***
73       * Returns the declaring class.
74       *
75       * @return the declaring class
76       */
77      public Class getDeclaringType() {
78          return m_signature.getDeclaringType();
79      }
80  
81      /***
82       * Returns the modifiers for the signature. <p/>Could be used like this:
83       * <p/>
84       * <pre>
85       * boolean isPublic = java.lang.reflect.Modifier.isPublic(signature.getModifiers());
86       * </pre>
87       *
88       * @return the mofifiers
89       */
90      public int getModifiers() {
91          return m_signature.getModifiers();
92      }
93  
94      /***
95       * Returns the name (f.e. name of method of field).
96       *
97       * @return the name
98       */
99      public String getName() {
100         return m_signature.getName();
101     }
102 
103     /***
104      * Returns the field.
105      *
106      * @return the field
107      */
108     public Field getField() {
109         return m_signature.getField();
110     }
111 
112     /***
113      * Returns the field type.
114      *
115      * @return the field type
116      */
117     public Class getFieldType() {
118         return m_signature.getFieldType();
119     }
120 
121     /***
122      * Returns the value of the field.
123      *
124      * @return the value of the field
125      */
126     public Object getFieldValue() {
127         return m_fieldValue;
128     }
129 
130     /***
131      * Sets the value of the field.
132      *
133      * @param fieldValue the value of the field
134      */
135     public void setFieldValue(final Object fieldValue) {
136         m_fieldValue = fieldValue;
137     }
138 
139     /***
140      * Returns a string representation of the signature.
141      *
142      * @return a string representation
143      * @TODO: implement toString to something meaningful
144      */
145     public String toString() {
146         return super.toString();
147     }
148 
149     /***
150      * TODO: Needed for stupid JIT compiler. Remove for 2.0.
151      *
152      * @return
153      */
154     public Object[] getParameterValues() {
155         return new Object[]{m_fieldValue};
156     }
157 }