1 8 package org.codehaus.aspectwerkz.hook.impl; 9 10 import org.codehaus.aspectwerkz.hook.ClassPreProcessor; 11 12 import java.security.ProtectionDomain ; 13 import java.lang.reflect.Method ; 14 15 23 public class ClassPreProcessorHelper { 24 27 private static ClassPreProcessor preProcessor; 28 29 32 private static boolean preProcessorInitialized; 33 34 37 private static String PRE_PROCESSOR_CLASSNAME_PROPERTY = "aspectwerkz.classloader.preprocessor"; 38 39 42 private static String PRE_PROCESSOR_CLASSNAME_DEFAULT = "org.codehaus.aspectwerkz.transform.AspectWerkzPreProcessor"; 43 44 static { 45 initializePreProcessor(); 46 } 47 48 53 public static ClassPreProcessor getClassPreProcessor() { 54 return preProcessor; 55 } 56 57 61 public static synchronized void initializePreProcessor() { 62 if (preProcessorInitialized) { 63 return; 64 } 65 preProcessorInitialized = true; 66 Class klass = null; 67 String s = System.getProperty(PRE_PROCESSOR_CLASSNAME_PROPERTY, PRE_PROCESSOR_CLASSNAME_DEFAULT); 68 try { 69 klass = Class.forName(s, true, ClassLoader.getSystemClassLoader()); 72 } catch (ClassNotFoundException _ex) { 73 System.err.println("AspectWerkz - WARN - Pre-processor class '" + s + "' not found"); 74 } 75 if (klass != null) { 76 try { 77 preProcessor = (ClassPreProcessor) klass.newInstance(); 78 preProcessor.initialize(); 79 System.out.println("AspectWerkz - INFO - Pre-processor " + s + " loaded and initialized"); 80 } catch (Throwable throwable) { 81 System.err.println("AspectWerkz - WARN - Error initializing pre-processor class " + s + ':'); 82 throwable.printStackTrace(); 83 } 84 } 85 } 86 87 90 public static byte[] defineClass0Pre(ClassLoader caller, 91 String name, 92 byte[] b, 93 int off, 94 int len, 95 ProtectionDomain pd) { 96 if (preProcessor == null) { 97 byte[] obyte = new byte[len]; 101 System.arraycopy(b, off, obyte, 0, len); 102 return obyte; 103 } else { 104 try { 105 byte[] ibyte = new byte[len]; 106 System.arraycopy(b, off, ibyte, 0, len); 107 return preProcessor.preProcess(name, ibyte, caller); 108 } catch (Throwable throwable) { 109 System.err.println( 110 "AspectWerkz - WARN - Error pre-processing class " 111 + name 112 + " in " 113 + Thread.currentThread() 114 ); 115 throwable.printStackTrace(); 116 byte[] obyte = new byte[len]; 118 System.arraycopy(b, off, obyte, 0, len); 119 return obyte; 120 } 121 } 122 } 123 124 138 public static Object defineClass0Pre(ClassLoader caller, 139 String name, 140 Object byteBuffer, 141 int off, 142 int len, 143 ProtectionDomain pd) { 144 byte[] bytes = new byte[len]; 145 byteBufferGet(byteBuffer, bytes, off, len); 147 byte[] newbytes = defineClass0Pre(caller, name, bytes, 0, bytes.length, pd); 148 Object newBuffer = byteBufferWrap(newbytes); 150 return newBuffer; 151 } 152 153 162 private static void byteBufferGet(Object byteBuffer, byte[] dest, int offset, int length) { 163 try { 164 Class cByteBuffer = Class.forName("java.nio.ByteBuffer"); 165 Method mGet = cByteBuffer.getDeclaredMethod("get", new Class []{BYTE_ARRAY_CLASS, int.class, int.class}); 166 mGet.invoke(byteBuffer, new Object []{dest, new Integer (offset), new Integer (length)}); 167 } catch (Throwable t) { 168 System.err.println("AW : java.nio not supported"); 169 throw new RuntimeException (t.toString()); 170 } 171 } 172 173 180 private static Object byteBufferWrap(byte[] bytes) { 181 try { 182 Class cByteBuffer = Class.forName("java.nio.ByteBuffer"); 183 Method mGet = cByteBuffer.getDeclaredMethod("wrap", new Class []{BYTE_ARRAY_CLASS}); 184 Object byteBuffer = mGet.invoke(null, new Object []{bytes}); 185 return byteBuffer; 186 } catch (Throwable t) { 187 System.err.println("AW : java.nio not supported"); 188 throw new RuntimeException (t.toString()); 189 } 190 } 191 192 private final static byte[] EMPTY_BYTEARRAY = new byte[0]; 193 private final static Class BYTE_ARRAY_CLASS = EMPTY_BYTEARRAY.getClass(); 194 195 } | Popular Tags |