1 22 package org.jboss.aop; 23 24 import java.lang.reflect.AccessibleObject ; 25 import java.lang.reflect.Field ; 26 import java.lang.reflect.Method ; 27 import java.security.AccessController ; 28 import java.security.PrivilegedActionException ; 29 import java.security.PrivilegedExceptionAction ; 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 42 public class FieldInfo extends JoinPointInfo 43 { 44 private int index; 45 private Field advisedField; 46 private Method wrapper; 47 private boolean read; 48 49 public FieldInfo() 50 { 51 52 } 53 54 public FieldInfo(Class clazz, int index, String 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 e) 69 { 70 throw new RuntimeException (e); 71 } 72 } 73 74 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 toString() 97 { 98 StringBuffer sb = new StringBuffer ("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 advisedField) 117 { 118 this.advisedField = advisedField; 119 } 120 121 public Field getAdvisedField() 122 { 123 return advisedField; 124 } 125 126 public void setWrapper(Method wrapper) 127 { 128 this.wrapper = wrapper; 129 } 130 131 public Method 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 doGet(Class clazz, String name)throws NoSuchFieldException 147 { 148 Field field = null; 149 Class superClass = clazz; 150 while (superClass != null) 151 { 152 try 153 { 154 field = superClass.getDeclaredField(name); 155 break; 156 } 157 catch (NoSuchFieldException e) 158 { 159 } 160 superClass = superClass.getSuperclass(); 162 } 163 164 if (field == null) 165 { 166 throw new NoSuchFieldException ("Cannot find field in " + clazz.getName() + " or any of its superclasses"); 167 } 168 return field; 169 } 170 171 interface GetDeclaredFieldAction 172 { 173 Field get(FieldInfo target, Class clazz, String name) throws NoSuchFieldException ; 174 175 GetDeclaredFieldAction PRIVILEGED = new GetDeclaredFieldAction() 176 { 177 public Field get(final FieldInfo target, final Class clazz, final String name) throws NoSuchFieldException 178 { 179 try 180 { 181 return (Field )AccessController.doPrivileged(new PrivilegedExceptionAction () 182 { 183 public Object run() throws Exception 184 { 185 return target.doGet(clazz, name); } 187 }); 188 } 189 catch (PrivilegedActionException e) 190 { 191 Exception ex = e.getException(); 192 if (ex instanceof NoSuchFieldException ) 193 { 194 throw (NoSuchFieldException )ex; 195 } 196 throw new RuntimeException (ex); 197 } 198 } 199 }; 200 201 GetDeclaredFieldAction NON_PRIVILEGED = new GetDeclaredFieldAction() 202 { 203 public Field get(FieldInfo target, Class clazz, String name) throws NoSuchFieldException 204 { 205 return target.doGet(clazz, name); } 207 }; 208 } 209 210 } 211 | Popular Tags |