1   /***************************************************************************************
2    * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved.                 *
3    * http://aspectwerkz.codehaus.org                                                    *
4    * ---------------------------------------------------------------------------------- *
5    * The software in this package is published under the terms of the LGPL license      *
6    * a copy of which has been included with this distribution in the license.txt file.  *
7    **************************************************************************************/
8   package test.clapp;
9   
10  import org.codehaus.aspectwerkz.hook.ClassPreProcessor;
11  
12  import java.io.ByteArrayInputStream;
13  import java.util.Arrays;
14  import java.util.Hashtable;
15  
16  /***
17   * Add marker interface java.util.EventListener to processed class
18   * 
19   * @author <a href="mailto:alex@gnilux.com">Alexandre Vasseur </a>
20   */
21  public class AddInterfacePreProcessor implements ClassPreProcessor {
22      private void log(String s) {
23          System.out.println(Thread.currentThread().getName() + ": AddInterfacePreProcessor: " + s);
24      }
25  
26      public void initialize(Hashtable hashtable) {
27          ;
28      }
29  
30      public byte[] preProcess(String klass, byte[] abyte, ClassLoader caller) {
31          try {
32              log(klass);
33              
34  //            // build the ClassGen
35  //            ClassParser parser = new ClassParser(new ByteArrayInputStream(abyte), "<generated>"); //@todo
36  //                                                                                                  // is
37  //                                                                                                  // this
38  //            // needed
39  //            // _"+klass+">");
40  //            ClassGen cg = new ClassGen(parser.parse());
41  //
42  //            // instrument
43  //            if (!cg.isInterface()
44  //                && !Arrays.asList(cg.getInterfaceNames()).contains("java.util.EventListener")) {
45  //                cg.addInterface("java.util.EventListener");
46  //            }
47  //            try {
48  //                cg.getJavaClass().dump("_dump/" + klass.replace('.', '/') + ".class");
49  //            } catch (Exception e) {
50  //                System.err.println("failed to dump " + klass);
51  //                e.printStackTrace();
52  //            }
53  //            return cg.getJavaClass().getBytes();
54  return null;
55          } catch (Exception e) {
56              e.printStackTrace();
57              throw new RuntimeException(e.getMessage());
58          }
59      }
60  }