1 8 package org.codehaus.aspectwerkz.definition; 9 10 import org.codehaus.aspectwerkz.expression.ExpressionInfo; 11 import org.codehaus.aspectwerkz.reflect.ClassInfo; 12 import org.codehaus.aspectwerkz.reflect.ClassInfoHelper; 13 import org.codehaus.aspectwerkz.reflect.MethodInfo; 14 import org.codehaus.aspectwerkz.reflect.impl.asm.AsmClassInfo; 15 import org.codehaus.aspectwerkz.DeploymentModel; 16 import org.codehaus.aspectwerkz.aspect.DefaultMixinFactory; 17 import org.codehaus.aspectwerkz.intercept.AdvisableImpl; 18 import org.codehaus.aspectwerkz.intercept.Advisable; 19 import org.codehaus.aspectwerkz.transform.TransformationConstants; 20 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 25 import java.util.HashMap ; 26 import java.lang.ref.WeakReference ; 27 28 33 public class MixinDefinition { 34 35 private final static String DEFAULT_MIXIN_FACTORY = DefaultMixinFactory.class.getName().replace('/', '.'); 36 39 private DeploymentModel m_deploymentModel; 40 41 44 private boolean m_isTransient; 45 46 49 private final List m_methodsToIntroduce = new ArrayList (); 50 51 54 private final List m_interfaceClassNames = new ArrayList (); 55 56 59 private final String m_mixinImplClassName; 60 61 64 private final WeakReference m_loaderRef; 65 66 69 private ExpressionInfo[] m_expressionInfos = new ExpressionInfo[]{}; 70 71 74 private String m_attribute = ""; 75 76 79 private String m_factoryClassName; 80 81 84 private SystemDefinition m_systemDefinition; 85 86 89 private Map m_parameters = new HashMap (); 90 91 99 public MixinDefinition(ClassInfo mixinClass, 100 final DeploymentModel deploymentModel, 101 final boolean isTransient, 102 final SystemDefinition systemDef) { 103 if (isSystemMixin(mixinClass)) { 104 mixinClass = defineSystemMixin(mixinClass.getClassLoader()); 105 } else { 106 List allInterfaces = ClassInfoHelper.collectInterfaces(mixinClass); 107 for (Iterator iterator = allInterfaces.iterator(); iterator.hasNext();) { 108 ClassInfo interfaceInfo = (ClassInfo) iterator.next(); 109 m_interfaceClassNames.add(interfaceInfo.getName()); 110 } 111 112 List interfaceDeclaredMethods = ClassInfoHelper.collectMethodsFromInterfacesImplementedBy(mixinClass); 113 List sortedMethodList = ClassInfoHelper.createInterfaceDefinedMethodList( 114 mixinClass, interfaceDeclaredMethods 115 ); 116 for (Iterator iterator = sortedMethodList.iterator(); iterator.hasNext();) { 117 MethodInfo methodInfo = (MethodInfo) iterator.next(); 118 m_methodsToIntroduce.add(methodInfo); 119 } 120 } 121 122 m_mixinImplClassName = mixinClass.getName(); 123 m_loaderRef = new WeakReference (mixinClass.getClassLoader()); 124 m_systemDefinition = systemDef; 125 m_expressionInfos = new ExpressionInfo[]{}; 126 127 m_deploymentModel = deploymentModel; 128 m_isTransient = isTransient; 129 130 setFactoryClassName(DEFAULT_MIXIN_FACTORY); 132 } 133 134 139 public void setFactoryClassName(final String factoryClassName) { 140 m_factoryClassName = factoryClassName; 141 } 142 143 148 public String getFactoryClassName() { 149 return m_factoryClassName; 150 } 151 152 157 public List getMethodsToIntroduce() { 158 return m_methodsToIntroduce; 159 } 160 161 166 public DeploymentModel getDeploymentModel() { 167 return m_deploymentModel; 168 } 169 170 175 public void setDeploymentModel(final DeploymentModel deploymentModel) { 176 m_deploymentModel = deploymentModel; 177 } 178 179 184 public boolean isTransient() { 185 return m_isTransient; 186 } 187 188 193 public void setTransient(boolean isTransient) { 194 m_isTransient = isTransient; 195 } 196 197 202 public ClassInfo getMixinImpl() { 203 return AsmClassInfo.getClassInfo(m_mixinImplClassName, (ClassLoader ) m_loaderRef.get()); 204 } 205 206 211 public ExpressionInfo[] getExpressionInfos() { 212 return m_expressionInfos; 213 } 214 215 220 public List getInterfaceClassNames() { 221 return m_interfaceClassNames; 222 } 223 224 229 public String getAttribute() { 230 return m_attribute; 231 } 232 233 238 public void setAttribute(final String attribute) { 239 m_attribute = attribute; 240 } 241 242 247 public SystemDefinition getSystemDefinition() { 248 return m_systemDefinition; 249 } 250 251 256 public void addExpressionInfo(final ExpressionInfo expression) { 257 final ExpressionInfo[] tmpExpressions = new ExpressionInfo[m_expressionInfos.length + 1]; 258 java.lang.System.arraycopy(m_expressionInfos, 0, tmpExpressions, 0, m_expressionInfos.length); 259 tmpExpressions[m_expressionInfos.length] = expression; 260 m_expressionInfos = new ExpressionInfo[m_expressionInfos.length + 1]; 261 java.lang.System.arraycopy(tmpExpressions, 0, m_expressionInfos, 0, tmpExpressions.length); 262 } 263 264 269 public void addExpressionInfos(final ExpressionInfo[] expressions) { 270 final ExpressionInfo[] tmpExpressions = new ExpressionInfo[m_expressionInfos.length + expressions.length]; 271 java.lang.System.arraycopy(m_expressionInfos, 0, tmpExpressions, 0, m_expressionInfos.length); 272 java.lang.System.arraycopy(expressions, 0, tmpExpressions, m_expressionInfos.length, expressions.length); 273 m_expressionInfos = new ExpressionInfo[m_expressionInfos.length + expressions.length]; 274 java.lang.System.arraycopy(tmpExpressions, 0, m_expressionInfos, 0, tmpExpressions.length); 275 } 276 277 283 private ClassInfo defineSystemMixin(final ClassLoader loader) { 284 ClassInfo mixinClass = AsmClassInfo.getClassInfo(AdvisableImpl.class.getName(), loader); 286 MethodInfo[] methods = mixinClass.getMethods(); 287 for (int i = 0; i < methods.length; i++) { 288 MethodInfo method = methods[i]; 289 if (method.getName().startsWith(TransformationConstants.SYNTHETIC_MEMBER_PREFIX) 290 || method.getName().startsWith("aw_")) { m_methodsToIntroduce.add(method); 292 } 293 } 294 m_interfaceClassNames.add(Advisable.class.getName()); 295 return mixinClass; 296 } 297 298 304 private boolean isSystemMixin(final ClassInfo mixinClass) { 305 return mixinClass.getName().equals(AdvisableImpl.class.getName()); 306 } 307 308 314 public void addParameter(final String name, final String value) { 315 m_parameters.put(name, value); 316 } 317 318 323 public Map getParameters() { 324 return m_parameters; 325 } 326 327 } | Popular Tags |