KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > FieldInfo


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.aop;
23
24 import java.lang.reflect.AccessibleObject JavaDoc;
25 import java.lang.reflect.Field JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import java.security.AccessController JavaDoc;
28 import java.security.PrivilegedActionException JavaDoc;
29 import java.security.PrivilegedExceptionAction JavaDoc;
30
31 import org.jboss.aop.SecurityActions.SetAccessibleAction;
32 import org.jboss.aop.joinpoint.FieldJoinpoint;
33 import org.jboss.aop.joinpoint.Joinpoint;
34 import org.jboss.aop.util.MethodHashing;
35
36 /**
37  * Comment
38  *
39  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
40  * @version $Revision$
41  */

42 public class FieldInfo extends JoinPointInfo
43 {
44    private int index;
45    private Field JavaDoc advisedField;
46    private Method JavaDoc wrapper;
47    private boolean read;
48    
49    public FieldInfo()
50    {
51       
52    }
53    
54    public FieldInfo(Class JavaDoc clazz, int index, String JavaDoc fieldName, long wrapperHash, Advisor advisor, boolean read)
55    {
56       super(advisor, clazz);
57
58       try
59       {
60          this.index = index;
61          this.advisedField = (System.getSecurityManager() == null) ?
62                GetDeclaredFieldAction.NON_PRIVILEGED.get(this, clazz, fieldName) :
63                   GetDeclaredFieldAction.PRIVILEGED.get(this, clazz, fieldName);
64          this.wrapper = MethodHashing.findMethodByHash(clazz, wrapperHash);
65          this.setAdvisor(advisor);
66          this.read = read;
67       }
68       catch (Exception JavaDoc e)
69       {
70          throw new RuntimeException JavaDoc(e);
71       }
72    }
73    
74    /*
75     * For copying
76     */

77    private FieldInfo(FieldInfo other)
78    {
79       super(other);
80       this.index = other.index;
81       this.advisedField = other.advisedField;
82       this.wrapper = other.wrapper;
83       this.read = other.read;
84    }
85    
86    protected Joinpoint internalGetJoinpoint()
87    {
88       return new FieldJoinpoint(advisedField);
89    }
90    
91    public JoinPointInfo copy()
92    {
93       return new FieldInfo(this);
94    }
95    
96    public String JavaDoc toString()
97    {
98       StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Field ");
99       sb.append(read ? " Read" : "Write");
100       sb.append("[");
101       sb.append("field=" + advisedField);
102       sb.append("]");
103       return sb.toString();
104    }
105
106    public void setIndex(int index)
107    {
108       this.index = index;
109    }
110
111    public int getIndex()
112    {
113       return index;
114    }
115
116    public void setAdvisedField(Field JavaDoc advisedField)
117    {
118       this.advisedField = advisedField;
119    }
120
121    public Field JavaDoc getAdvisedField()
122    {
123       return advisedField;
124    }
125
126    public void setWrapper(Method JavaDoc wrapper)
127    {
128       this.wrapper = wrapper;
129    }
130
131    public Method JavaDoc getWrapper()
132    {
133       return wrapper;
134    }
135
136    public void setRead(boolean read)
137    {
138       this.read = read;
139    }
140
141    public boolean isRead()
142    {
143       return read;
144    }
145
146    private Field JavaDoc doGet(Class JavaDoc clazz, String JavaDoc name)throws NoSuchFieldException JavaDoc
147    {
148       Field JavaDoc field = null;
149       Class JavaDoc superClass = clazz;
150       while (superClass != null)
151       {
152          try
153          {
154             field = superClass.getDeclaredField(name);
155             break;
156          }
157          catch (NoSuchFieldException JavaDoc e)
158          {
159          }
160          //Check super class
161
superClass = superClass.getSuperclass();
162       }
163       
164       if (field == null)
165       {
166          throw new NoSuchFieldException JavaDoc("Cannot find field in " + clazz.getName() + " or any of its superclasses");
167       }
168       return field;
169    }
170    
171    interface GetDeclaredFieldAction
172    {
173       Field JavaDoc get(FieldInfo target, Class JavaDoc clazz, String JavaDoc name) throws NoSuchFieldException JavaDoc;
174       
175       GetDeclaredFieldAction PRIVILEGED = new GetDeclaredFieldAction()
176       {
177          public Field JavaDoc get(final FieldInfo target, final Class JavaDoc clazz, final String JavaDoc name) throws NoSuchFieldException JavaDoc
178          {
179             try
180             {
181                return (Field JavaDoc)AccessController.doPrivileged(new PrivilegedExceptionAction JavaDoc()
182                {
183                   public Object JavaDoc run() throws Exception JavaDoc
184                   {
185                      return target.doGet(clazz, name);//clazz.getDeclaredField(name);
186
}
187                });
188             }
189             catch (PrivilegedActionException JavaDoc e)
190             {
191                Exception JavaDoc ex = e.getException();
192                if (ex instanceof NoSuchFieldException JavaDoc)
193                {
194                   throw (NoSuchFieldException JavaDoc)ex;
195                }
196                throw new RuntimeException JavaDoc(ex);
197             }
198          }
199       };
200
201       GetDeclaredFieldAction NON_PRIVILEGED = new GetDeclaredFieldAction()
202       {
203          public Field JavaDoc get(FieldInfo target, Class JavaDoc clazz, String JavaDoc name) throws NoSuchFieldException JavaDoc
204          {
205             return target.doGet(clazz, name);//clazz.getDeclaredField(name);
206
}
207       };
208    }
209
210 }
211
Popular Tags