1 4 package com.tc.aspectwerkz.proxy; 5 6 7 import com.tc.aspectwerkz.definition.SystemDefinition; 8 import com.tc.aspectwerkz.exception.WrappedRuntimeException; 9 10 import java.util.Map ; 11 import java.util.WeakHashMap ; 12 13 19 public class ProxySubclassingStrategy { 20 21 24 public static final String PROXY_SUFFIX = "$$ProxiedByAWSubclassing$$"; 25 26 29 private static final Map PROXY_CLASS_CACHE = new WeakHashMap (); 30 31 38 static Object newInstance( 39 final Class clazz, final SystemDefinition definition) { 40 try { 41 Class proxyClass = getProxyClassFor(clazz, true, false, definition); 42 return proxyClass.newInstance(); 43 } catch (Throwable t) { 44 throw new WrappedRuntimeException(t); 45 } 46 } 47 48 59 static Object newInstance( 60 final Class clazz, final Class [] argumentTypes, final Object [] argumentValues, 61 final SystemDefinition definition) { 62 try { 63 Class proxyClass = getProxyClassFor(clazz, true, false, definition); 64 return proxyClass.getDeclaredConstructor(argumentTypes).newInstance(argumentValues); 65 } catch (Throwable t) { 66 throw new WrappedRuntimeException(t); 67 } 68 } 69 70 80 static Object newInstance( 81 final Class clazz, final boolean useCache, final boolean makeAdvisable, 82 final SystemDefinition definition) { 83 try { 84 Class proxyClass = getProxyClassFor(clazz, useCache, makeAdvisable, definition); 85 return proxyClass.newInstance(); 86 } catch (Throwable t) { 87 throw new WrappedRuntimeException(t); 88 } 89 } 90 91 103 static Object newInstance( 104 final Class clazz, final Class [] argumentTypes, final Object [] argumentValues, 105 final boolean useCache, final boolean makeAdvisable, 106 final SystemDefinition definition) { 107 try { 108 Class proxyClass = getProxyClassFor(clazz, useCache, makeAdvisable, definition); 109 return proxyClass.getDeclaredConstructor(argumentTypes).newInstance(argumentValues); 110 } catch (Throwable t) { 111 throw new WrappedRuntimeException(t); 112 } 113 } 114 115 124 static Class getProxyClassFor( 125 final Class clazz, final boolean useCache, final boolean makeAdvisable, 126 final SystemDefinition definition) { 127 128 if (clazz.getName().startsWith("java.")) { 130 throw new RuntimeException ( 131 "can not create proxies from system classes (java.*)"); 132 } 133 final Class proxyClass; 134 if (!useCache) { 135 proxyClass = getNewProxyClassFor(clazz, makeAdvisable, definition); 136 } else { 137 synchronized (PROXY_CLASS_CACHE) { 138 Object cachedProxyClass = PROXY_CLASS_CACHE.get(clazz); 139 if (cachedProxyClass != null) { 140 return (Class ) cachedProxyClass; 141 } 142 proxyClass = getNewProxyClassFor(clazz, makeAdvisable, definition); 143 PROXY_CLASS_CACHE.put(clazz, proxyClass); 144 } 145 } 146 ProxyCompilerHelper.compileJoinPoint(proxyClass, definition); 147 return proxyClass; 148 } 149 150 159 private static Class getNewProxyClassFor( 160 final Class clazz, final boolean makeAdvisable, 161 final SystemDefinition definition) { 162 ClassLoader loader = clazz.getClassLoader(); 163 String proxyClassName = definition.getUuid(); 164 if (makeAdvisable) { 165 Proxy.makeProxyAdvisable(proxyClassName, loader, definition); 166 } 167 final byte[] bytes = ProxySubclassingCompiler.compileProxyFor(clazz, proxyClassName); 168 return ProxyCompilerHelper.weaveAndDefineProxyClass( 169 bytes, loader, proxyClassName.replace('/', '.'), definition); 170 } 171 172 178 static String getUniqueClassNameFromProxy( 179 final String proxyClassName) { 180 int index = proxyClassName.lastIndexOf(PROXY_SUFFIX); 181 if (index > 0) { 182 return proxyClassName.substring(0, index); 183 } else { 184 return null; 185 } 186 } 187 } 188 | Popular Tags |