KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > ClassicWeavingStrategy


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.aop;
23
24 import org.jboss.aop.classpool.AOPClassPool;
25 import org.jboss.aop.instrument.Instrumentor;
26 import org.jboss.aop.instrument.InstrumentorFactory;
27
28 import javassist.ByteArrayClassPath;
29 import javassist.CtClass;
30 import javassist.NotFoundException;
31
32 /**
33  * Classic weaving.
34  *
35  * @author <a HREF="stalep@conduct.no">Stale W. Pedersen</a>
36  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
37  * @version $Revision:
38  */

39 public class ClassicWeavingStrategy extends WeavingStrategySupport
40 {
41     private boolean verbose = AspectManager.verbose;
42     /**
43      * This is the translate version that was always there
44      */

45     public byte[] translate(AspectManager manager, String JavaDoc className, ClassLoader JavaDoc loader, byte[] classfileBuffer) throws Exception JavaDoc
46     {
47        if (isReEntry())
48        {
49           return null;
50        }
51        setReEntry();
52        manager.transformationStarted = true;
53        try
54        {
55           if (manager.isNonAdvisableClassName(className))
56           {
57              return null;
58           }
59           AOPClassPool pool = (AOPClassPool) manager.registerClassLoader(loader);
60           CtClass clazz = null;
61           try
62           {
63              clazz = pool.getLocally(className);
64           }
65           catch (NotFoundException e)
66           {
67              // todo Bill Burke: this scares the shit out of me, but it must be done
68
// I think it will screw up hotdeployment at some time. Then again, maybe not ;)
69
ByteArrayClassPath cp = new ByteArrayClassPath(className, classfileBuffer);
70              pool.insertClassPath(cp);
71              clazz = pool.getLocally(className);
72           }
73           if (clazz.isArray())
74           {
75              if (verbose) System.out.println("[cannot compile] isArray: " + className);
76              pool.flushClass(className);
77              return null;
78           }
79           if (clazz.isInterface())
80           {
81              if (verbose) System.out.println("[cannot compile] isInterface: " + className);
82              pool.flushClass(className);
83              return null;
84           }
85           if (clazz.isFrozen())
86           {
87              if (verbose) System.out.println("[warning] isFrozen: " + className);
88              clazz.defrost();
89           }
90
91           ClassAdvisor advisor = new ClassAdvisor(className, manager);
92           Instrumentor instrumentor = InstrumentorFactory.getInstrumentor(
93                 pool,
94                 manager,
95                 manager.dynamicStrategy.getJoinpointClassifier(),
96                 manager.dynamicStrategy.getDynamicTransformationObserver(clazz));
97
98           if (!Instrumentor.isTransformable(clazz))
99           {
100              if (verbose) System.out.println("[cannot compile] implements Untransformable: " + className);
101              pool.flushClass(className);
102              return null;
103           }
104
105           manager.attachMetaData(advisor, clazz, true);
106           manager.applyInterfaceIntroductions(advisor, clazz);
107           boolean transformed = instrumentor.transform(clazz, advisor);
108           if (transformed)
109           {
110              pool.lockInCache(clazz);
111              if (AspectManager.debugClasses)
112              {
113                 clazz.debugWriteFile();
114              }
115
116              byte[] rtn = clazz.toBytecode();
117              if (AspectManager.getPrune()) clazz.prune();
118              return rtn;
119           }
120           else
121           {
122              pool.soften(clazz);
123           }
124           return null;
125        }
126        catch (Exception JavaDoc ex)
127        {
128           if (!(ex instanceof NotFoundException))
129           {
130              if (verbose)
131                 ex.printStackTrace();
132              else
133                 System.err.println("[error] " + ex.getMessage() + ".. Do verbose mode if you want full stack trace.");
134
135           }
136           throw ex;
137        }
138        finally
139        {
140            clearReEntry();
141        }
142     }
143  }
Popular Tags