1 22 package org.jboss.aop.instrument; 23 24 import javassist.CtMember; 25 26 44 public class WrapperTransformer 45 { 46 47 private static final byte UNWRAPPED = (byte) 0; 48 49 private static final byte WRAPPED = (byte) 1; 50 51 private static final byte NOT_PREPARED = (byte) 2; 52 53 private static final String WRAPPER_STATUS_ATTRIBUTE = 54 "aop$wrapperStatus$aop"; 55 56 61 protected static final String [] SINGLE_TRANSFORMATION = new String [] {WRAPPER_STATUS_ATTRIBUTE}; 62 68 protected static final int SINGLE_TRANSFORMATION_INDEX = 0; 69 70 private String [] transformationsStatus; 71 72 79 public WrapperTransformer(String [] transformations) 80 { 81 if (transformations == SINGLE_TRANSFORMATION) 82 { 83 this.transformationsStatus = transformations; 84 } 85 else 86 { 87 this.transformationsStatus = new String [transformations.length]; 88 for (int i = 0; i < transformations.length; i++) 89 { 90 transformationsStatus[i] = WRAPPER_STATUS_ATTRIBUTE + "$" + transformations[i]; 91 } 92 } 93 } 94 95 101 public void prepareForWrapping(final CtMember member, final int transformationIndex) { 102 if (getWrapperStatusAttribute(member, transformationIndex) != NOT_PREPARED) 103 { 104 throw new RuntimeException ("Member already prepared:" + member); 105 } 106 this.setWrapperStatusAttribute(member, transformationIndex, UNWRAPPED); 107 } 108 109 115 public void wrap(CtMember member, int transformationIndex) { 116 byte wrapperStatus = getWrapperStatusAttribute(member, transformationIndex); 117 if (wrapperStatus != UNWRAPPED) 118 { 119 String errorMessage = "Member not unwrapped: " + member.getName() + "." + 120 member.getDeclaringClass().getName(); 121 errorMessage += "\nMember status: "; 122 errorMessage += (wrapperStatus == NOT_PREPARED)? "NOT_PREPARED" : "WRAPPED"; 123 124 throw new RuntimeException (errorMessage); 125 } 126 setWrapperStatusAttribute(member, transformationIndex, WRAPPED); 127 } 128 129 135 public void unwrap(CtMember member, int transformationIndex) { 136 byte wrapperStatus = getWrapperStatusAttribute(member, transformationIndex); 137 if (wrapperStatus != WRAPPED) 138 { 139 String errorMessage = "Member not wrapped: " + member.getName() + "." + 140 member.getDeclaringClass().getName(); 141 errorMessage += "\nMember status: "; 142 errorMessage += (wrapperStatus == NOT_PREPARED)? "NOT_PREPARED" : "UNWRAPPED"; 143 throw new RuntimeException (errorMessage); 144 } 145 setWrapperStatusAttribute(member, transformationIndex, UNWRAPPED); 146 } 147 148 154 public boolean isNotPrepared(CtMember member, int transformationIndex) 155 { 156 byte wrapperStatus = getWrapperStatusAttribute(member, transformationIndex); 157 return wrapperStatus == NOT_PREPARED; 158 } 159 160 166 public boolean isWrapped(CtMember member, int transformationIndex) 167 { 168 byte wrapperStatus = getWrapperStatusAttribute(member, transformationIndex); 169 return wrapperStatus == WRAPPED; 170 } 171 172 180 private byte getWrapperStatusAttribute(CtMember member, int transformationIndex) 181 { 182 byte[] attributeValue = member.getAttribute(transformationsStatus[transformationIndex]); 183 if (attributeValue == null) 184 { 185 return NOT_PREPARED; 186 } 187 return attributeValue[0]; 188 } 189 190 198 private void setWrapperStatusAttribute(CtMember member, int transformationIndex, byte status) 199 { 200 member.setAttribute(transformationsStatus[transformationIndex], new byte[] {status}); 201 } 202 } | Popular Tags |