001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.aspectwerkz.proxy;
005:
006: import java.lang.reflect.Field;
007: import java.util.HashMap;
008: import java.util.HashSet;
009: import java.util.Iterator;
010: import java.util.Set;
011:
012: import com.tc.backport175.bytecode.AnnotationReader;
013: import com.tc.backport175.bytecode.spi.BytecodeProvider;
014:
015: import com.tc.aspectwerkz.definition.SystemDefinition;
016: import com.tc.aspectwerkz.definition.SystemDefinitionContainer;
017: import com.tc.aspectwerkz.exception.WrappedRuntimeException;
018: import com.tc.aspectwerkz.joinpoint.management.JoinPointManager;
019: import com.tc.aspectwerkz.transform.InstrumentationContext;
020: import com.tc.aspectwerkz.transform.TransformationConstants;
021: import com.tc.aspectwerkz.transform.WeavingStrategy;
022: import com.tc.aspectwerkz.transform.inlining.AsmHelper;
023: import com.tc.aspectwerkz.transform.inlining.EmittedJoinPoint;
024: import com.tc.aspectwerkz.transform.inlining.ProxyWeavingStrategy;
025:
026: /**
027: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr</a>
028: */
029: public class ProxyCompilerHelper {
030:
031: /**
032: * The proxy weaving strategy.
033: */
034: private final static WeavingStrategy s_weavingStrategy = new ProxyWeavingStrategy();
035:
036: /**
037: * Weaves and defines the newly generated proxy class.
038: *
039: * @param bytes
040: * @param loader
041: * @param proxyClassName
042: * @param definition
043: * @return
044: */
045: public static Class weaveAndDefineProxyClass(final byte[] bytes,
046: final ClassLoader loader, final String proxyClassName,
047: final SystemDefinition definition) {
048:
049: // register the bytecode provider
050: // TODO AV - could be optimized ?(f.e. recompile everytime instead of creating many provider)
051: AnnotationReader.setBytecodeProviderFor(proxyClassName, loader,
052: new BytecodeProvider() {
053: public byte[] getBytecode(String className,
054: ClassLoader l) throws Exception {
055: return bytes;
056: }
057: });
058:
059: // final Set definitions = new HashSet();
060: // definitions.add(definition);
061:
062: final Set definitions;
063: if (definition == null) {
064: definitions = SystemDefinitionContainer
065: .getDefinitionsFor(loader);
066: } else {
067: definitions = new HashSet();
068: definitions.add(definition);
069:
070: for (Iterator it = SystemDefinitionContainer
071: .getDefinitionsFor(loader).iterator(); it.hasNext();) {
072: definitions.add(it.next());
073: }
074: }
075:
076: final InstrumentationContext ctx = new InstrumentationContext(
077: proxyClassName, bytes, loader, definitions);
078: ctx.markAsProxy();
079:
080: s_weavingStrategy.transform(proxyClassName, ctx);
081:
082: byte[] transformedBytes = ctx.getCurrentBytecode();
083:
084: // load and define the class
085: return AsmHelper.defineClass(loader, transformedBytes,
086: proxyClassName);
087: }
088:
089: /**
090: * Eagerly compiles and loads the JIT join point.
091: *
092: * @param proxyClass
093: */
094: public static void compileJoinPoint(final Class proxyClass,
095: SystemDefinition definition) {
096: try {
097: Field field = proxyClass
098: .getDeclaredField(TransformationConstants.EMITTED_JOINPOINTS_FIELD_NAME);
099: field.setAccessible(true);
100:
101: HashMap emittedJoinPoints = (HashMap) field.get(null);
102:
103: // Object[] arr = emittedJoinPoints.getValues();
104: // for (int i = 0; i < arr.length; i++) {
105: // EmittedJoinPoint emittedJoinPoint = (EmittedJoinPoint) arr[i];
106: for (Iterator it = emittedJoinPoints.values().iterator(); it
107: .hasNext();) {
108: EmittedJoinPoint emittedJoinPoint = (EmittedJoinPoint) it
109: .next();
110: JoinPointManager.loadJoinPoint(emittedJoinPoint
111: .getJoinPointType(), proxyClass,
112: emittedJoinPoint.getCallerMethodName(),
113: emittedJoinPoint.getCallerMethodDesc(),
114: emittedJoinPoint.getCallerMethodModifiers(),
115: emittedJoinPoint.getCalleeClassName(),
116: emittedJoinPoint.getCalleeMemberName(),
117: emittedJoinPoint.getCalleeMemberDesc(),
118: emittedJoinPoint.getCalleeMemberModifiers(),
119: emittedJoinPoint.getJoinPointHash(),
120: emittedJoinPoint.getJoinPointClassName(),
121: definition);
122: }
123: } catch (NoSuchFieldException e) {
124: // ignore
125: } catch (Exception e) {
126: throw new WrappedRuntimeException(e);
127: }
128: }
129: }
|