KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > proxy > ProxyCompilerHelper


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.aspectwerkz.proxy;
5
6 import java.lang.reflect.Field JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.HashSet JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.Set JavaDoc;
11
12 import com.tc.backport175.bytecode.AnnotationReader;
13 import com.tc.backport175.bytecode.spi.BytecodeProvider;
14
15 import com.tc.aspectwerkz.definition.SystemDefinition;
16 import com.tc.aspectwerkz.definition.SystemDefinitionContainer;
17 import com.tc.aspectwerkz.exception.WrappedRuntimeException;
18 import com.tc.aspectwerkz.joinpoint.management.JoinPointManager;
19 import com.tc.aspectwerkz.transform.InstrumentationContext;
20 import com.tc.aspectwerkz.transform.TransformationConstants;
21 import com.tc.aspectwerkz.transform.WeavingStrategy;
22 import com.tc.aspectwerkz.transform.inlining.AsmHelper;
23 import com.tc.aspectwerkz.transform.inlining.EmittedJoinPoint;
24 import com.tc.aspectwerkz.transform.inlining.ProxyWeavingStrategy;
25
26 /**
27  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr</a>
28  */

29 public class ProxyCompilerHelper {
30
31   /**
32    * The proxy weaving strategy.
33    */

34   private final static WeavingStrategy s_weavingStrategy = new ProxyWeavingStrategy();
35
36   /**
37    * Weaves and defines the newly generated proxy class.
38    *
39    * @param bytes
40    * @param loader
41    * @param proxyClassName
42    * @param definition
43    * @return
44    */

45   public static Class JavaDoc weaveAndDefineProxyClass(final byte[] bytes,
46                                                final ClassLoader JavaDoc loader,
47                                                final String JavaDoc proxyClassName,
48                                                final SystemDefinition definition) {
49
50     // register the bytecode provider
51
// TODO AV - could be optimized ?(f.e. recompile everytime instead of creating many provider)
52
AnnotationReader.setBytecodeProviderFor(proxyClassName, loader, new BytecodeProvider() {
53       public byte[] getBytecode(String JavaDoc className, ClassLoader JavaDoc l) throws Exception JavaDoc {
54         return bytes;
55       }
56     });
57
58 // final Set definitions = new HashSet();
59
// definitions.add(definition);
60

61     final Set JavaDoc definitions;
62     if (definition == null) {
63       definitions = SystemDefinitionContainer.getDefinitionsFor(loader);
64     } else {
65       definitions = new HashSet JavaDoc();
66       definitions.add(definition);
67
68       for (Iterator JavaDoc it = SystemDefinitionContainer.getDefinitionsFor(loader).iterator(); it.hasNext();) {
69         definitions.add(it.next());
70       }
71     }
72
73     final InstrumentationContext ctx = new InstrumentationContext(
74             proxyClassName,
75             bytes,
76             loader,
77             definitions);
78     ctx.markAsProxy();
79
80     s_weavingStrategy.transform(proxyClassName, ctx);
81
82     byte[] transformedBytes = ctx.getCurrentBytecode();
83
84     // load and define the class
85
return AsmHelper.defineClass(loader, transformedBytes, proxyClassName);
86   }
87
88   /**
89    * Eagerly compiles and loads the JIT join point.
90    *
91    * @param proxyClass
92    */

93   public static void compileJoinPoint(final Class JavaDoc proxyClass, SystemDefinition definition) {
94     try {
95       Field JavaDoc field = proxyClass.getDeclaredField(TransformationConstants.EMITTED_JOINPOINTS_FIELD_NAME);
96       field.setAccessible(true);
97
98       HashMap JavaDoc emittedJoinPoints = (HashMap JavaDoc) field.get(null);
99
100 // Object[] arr = emittedJoinPoints.getValues();
101
// for (int i = 0; i < arr.length; i++) {
102
// EmittedJoinPoint emittedJoinPoint = (EmittedJoinPoint) arr[i];
103
for (Iterator JavaDoc it = emittedJoinPoints.values().iterator(); it.hasNext();) {
104         EmittedJoinPoint emittedJoinPoint = (EmittedJoinPoint) it.next();
105         JoinPointManager.loadJoinPoint(
106                 emittedJoinPoint.getJoinPointType(),
107                 proxyClass,
108                 emittedJoinPoint.getCallerMethodName(),
109                 emittedJoinPoint.getCallerMethodDesc(),
110                 emittedJoinPoint.getCallerMethodModifiers(),
111                 emittedJoinPoint.getCalleeClassName(),
112                 emittedJoinPoint.getCalleeMemberName(),
113                 emittedJoinPoint.getCalleeMemberDesc(),
114                 emittedJoinPoint.getCalleeMemberModifiers(),
115                 emittedJoinPoint.getJoinPointHash(),
116                 emittedJoinPoint.getJoinPointClassName(),
117                 definition);
118       }
119     } catch (NoSuchFieldException JavaDoc e) {
120       // ignore
121
} catch (Exception JavaDoc e) {
122       throw new WrappedRuntimeException(e);
123     }
124   }
125 }
126
Popular Tags