public static Class createProxy(final Class<?> classToProxy, final ClassLoader cl, final Class... interfaces) { return createProxy(classToProxy, cl, classToProxy.getName() + "$$LocalBeanProxy", interfaces); }
public static InvocationHandler getInvocationHandler(final Object proxy) { if (LocalBeanProxyFactory.isProxy(proxy.getClass())) { return LocalBeanProxyFactory.getInvocationHandler(proxy); } checkDefaultFactory(); return defaultFactory.getInvocationHandler(proxy); }
public static Object newProxyInstance(final ClassLoader classLoader, final InvocationHandler handler, final Class classToSubclass, final Class... interfaces) throws IllegalArgumentException { try { final Class proxyClass = createProxy(classToSubclass, classLoader, interfaces); return constructProxy(proxyClass, handler); } catch (final Throwable e) { throw new InternalError("LocalBeanProxyFactory.newProxyInstance: " + Debug.printStackTrace(e)); } }
/** * Gets the string to use for CHECKCAST instruction, returning the correct value for any type, including primitives and arrays * * @param returnType The type to cast to with CHECKCAST * @return CHECKCAST parameter */ static String getCastType(final Class<?> returnType) { if (returnType.isPrimitive()) { return getWrapperType(returnType); } else { return getAsmTypeAsString(returnType, false); } }
final MethodVisitor mv = cw.visitMethod(modifier, method.getName(), getMethodSignatureAsString(returnType, parameterTypes), null, null); mv.visitCode(); createArrayDefinition(mv, parameterTypes.length, Class.class); pushIntOntoStack(mv, i); final String wrapperType = getWrapperType(parameterType); mv.visitFieldInsn(GETSTATIC, wrapperType, "TYPE", "Ljava/lang/Class;"); } else { mv.visitLdcInsn(Type.getType(getAsmTypeAsString(parameterType, true))); createArrayDefinition(mv, parameterTypes.length, Object.class); pushIntOntoStack(mv, i); final String wrapperType = getWrapperType(parameterType); mv.visitVarInsn(getVarInsn(parameterType), index); mv.visitMethodInsn(INVOKESTATIC, wrapperType, "valueOf", "(" + getPrimitiveLetter(parameterType) + ")L" + wrapperType + ";", false); mv.visitInsn(AASTORE); mv.visitTypeInsn(CHECKCAST, getCastType(returnType)); mv.visitMethodInsn(INVOKEVIRTUAL, getWrapperType(returnType), getPrimitiveMethod(returnType), "()" + getPrimitiveLetter(returnType), false); mv.visitInsn(getReturnInsn(returnType)); } else { mv.visitInsn(POP);
public static Object proxy(final BeanContext beanContext, final Class<?>[] itfs) { if (beanContext.isLocalbean()) { return LocalBeanProxyFactory.newProxyInstance(itfs[0].getClassLoader(), new Handler(beanContext), itfs[0], IntraVmProxy.class, Serializable.class); } return Proxy.newProxyInstance(itfs[0].getClassLoader(), itfs, new Handler(beanContext)); }
@Override public MessageEndpoint createEndpoint(XAResource xaResource) throws UnavailableException { if (xaResource != null && xaResourceWrapper != null) { xaResource = xaResourceWrapper.wrap(xaResource, container.getContainerID().toString()); } final EndpointHandler endpointHandler = new EndpointHandler(container, beanContext, instanceFactory, xaResource); try { return (MessageEndpoint) LocalBeanProxyFactory.constructProxy(proxy, endpointHandler); } catch (final InternalError e) { // should be useless //try to create the proxy with tccl once again. try { return MessageEndpoint.class.cast(LocalBeanProxyFactory.newProxyInstance(Thread.currentThread().getContextClassLoader(), endpointHandler, beanContext.getBeanClass(), interfaces)); } catch (final InternalError ie) { try { return MessageEndpoint.class.cast(LocalBeanProxyFactory.newProxyInstance(classLoader, endpointHandler, beanContext.getBeanClass(), interfaces)); } catch (final InternalError ie2) { // no-op } } throw e; } }
if (parameterType.getComponentType().isPrimitive()) { final Class<?> componentType = parameterType.getComponentType(); return "[" + getPrimitiveLetter(componentType); } else { return "[" + getAsmTypeAsString(parameterType.getComponentType(), true); return getPrimitiveLetter(parameterType); } else { String className = parameterType.getCanonicalName();
private Collection<Class<?>> getContextTypes(final Object resourceObject) { if (!ProxyManager.isProxyClass(resourceObject.getClass()) && !LocalBeanProxyFactory.isProxy(resourceObject.getClass())) { return Collections.emptySet(); } final InvocationHandler handler = ProxyManager.getInvocationHandler(resourceObject); if (!(handler instanceof BeanContextInvocationHandler)) { return Collections.emptySet(); } final BeanContext beanContext = ((BeanContextInvocationHandler) handler).getBeanContext(); if (beanContext == null) { return Collections.emptySet(); } return contextTypes.get(beanContext.getBeanClass()); }
return LocalBeanProxyFactory.constructProxy(di.get(BeanContext.ProxyClass.class).getProxy(), handler); } else { final List<Class> interfaces = new ArrayList<Class>();
static String getMethodSignatureAsString(final Class<?> returnType, final Class<?>[] parameterTypes) { final StringBuilder builder = new StringBuilder(); builder.append("("); for (final Class<?> parameterType : parameterTypes) { builder.append(getAsmTypeAsString(parameterType, true)); } builder.append(")"); builder.append(getAsmTypeAsString(returnType, true)); return builder.toString(); }
public static Object constructProxy(final Class clazz, final InvocationHandler handler) throws IllegalStateException { final Object instance = Unsafe.allocateInstance(clazz); Unsafe.setValue(getDeclaredField(clazz, BUSSINESS_HANDLER_NAME), instance, handler); Unsafe.setValue(getDeclaredField(clazz, NON_BUSINESS_HANDLER_NAME), instance, NON_BUSINESS_HANDLER); return instance; }
public static Class createProxy(final Class<?> classToProxy, final ClassLoader cl, final String proxyName, final Class... interfaces) { final String classFileName = proxyName.replace('.', '/'); try { return cl.loadClass(proxyName); } catch (final Exception e) { // no-op } final ReentrantLock lock = LocalBeanProxyFactory.LOCK; lock.lock(); try { try { // Try it again, another thread may have beaten this one... return cl.loadClass(proxyName); } catch (final Exception e) { // no-op } final byte[] proxyBytes = generateProxy(classToProxy, classFileName, interfaces); return Unsafe.defineClass(classToProxy, proxyName, proxyBytes); } catch (final Exception e) { throw new InternalError("LocalBeanProxyFactory.createProxy: " + Debug.printStackTrace(e)); } finally { lock.unlock(); } }
final MethodVisitor mv = cw.visitMethod(modifier, method.getName(), getMethodSignatureAsString(returnType, parameterTypes), null, null); mv.visitCode(); createArrayDefinition(mv, parameterTypes.length, Class.class); pushIntOntoStack(mv, i); final String wrapperType = getWrapperType(parameterType); mv.visitFieldInsn(GETSTATIC, wrapperType, "TYPE", "Ljava/lang/Class;"); } else { mv.visitLdcInsn(Type.getType(getAsmTypeAsString(parameterType, true))); createArrayDefinition(mv, parameterTypes.length, Object.class); pushIntOntoStack(mv, i); final String wrapperType = getWrapperType(parameterType); mv.visitVarInsn(getVarInsn(parameterType), index); mv.visitMethodInsn(INVOKESTATIC, wrapperType, "valueOf", "(" + getPrimitiveLetter(parameterType) + ")L" + wrapperType + ";", false); mv.visitInsn(AASTORE); mv.visitTypeInsn(CHECKCAST, getCastType(returnType)); mv.visitMethodInsn(INVOKEVIRTUAL, getWrapperType(returnType), getPrimitiveMethod(returnType), "()" + getPrimitiveLetter(returnType), false); mv.visitInsn(getReturnInsn(returnType)); } else { mv.visitInsn(POP);
public static Object simpleProxy(final BeanContext beanContext, final Class<?>[] itfs) { if (beanContext.isLocalbean()) { return LocalBeanProxyFactory.newProxyInstance(itfs[0].getClassLoader(), new Handler(beanContext), itfs[0]); } return Proxy.newProxyInstance(itfs[0].getClassLoader(), itfs, new Handler(beanContext)); }
@Override public MessageEndpoint createEndpoint(XAResource xaResource) throws UnavailableException { if (xaResource != null && xaResourceWrapper != null) { xaResource = xaResourceWrapper.wrap(xaResource, container.getContainerID().toString()); } InvocationHandler endpointHandler = null; if (usePool) { endpointHandler = new PoolEndpointHandler(container, beanContext, instanceManager, xaResource); } else { endpointHandler = new EndpointHandler(container, beanContext, instanceFactory, xaResource); } try { return (MessageEndpoint) LocalBeanProxyFactory.constructProxy(proxy, endpointHandler); } catch (final InternalError e) { // should be useless //try to create the proxy with tccl once again. try { return MessageEndpoint.class.cast(LocalBeanProxyFactory.newProxyInstance(Thread.currentThread().getContextClassLoader(), endpointHandler, beanContext.getBeanClass(), interfaces)); } catch (final InternalError ie) { try { return MessageEndpoint.class.cast(LocalBeanProxyFactory.newProxyInstance(classLoader, endpointHandler, beanContext.getBeanClass(), interfaces)); } catch (final InternalError ie2) { // no-op } } throw e; } }
/** * Gets the string to use for CHECKCAST instruction, returning the correct value for any type, including primitives and arrays * * @param returnType The type to cast to with CHECKCAST * @return CHECKCAST parameter */ static String getCastType(final Class<?> returnType) { if (returnType.isPrimitive()) { return getWrapperType(returnType); } else { return getAsmTypeAsString(returnType, false); } }
if (parameterType.getComponentType().isPrimitive()) { final Class<?> componentType = parameterType.getComponentType(); return "[" + getPrimitiveLetter(componentType); } else { return "[" + getAsmTypeAsString(parameterType.getComponentType(), true); return getPrimitiveLetter(parameterType); } else { String className = parameterType.getCanonicalName();
return LocalBeanProxyFactory.constructProxy(di.get(BeanContext.ProxyClass.class).getProxy(), handler); } else { final List<Class> interfaces = new ArrayList<Class>();
static String getMethodSignatureAsString(final Class<?> returnType, final Class<?>[] parameterTypes) { final StringBuilder builder = new StringBuilder(); builder.append("("); for (final Class<?> parameterType : parameterTypes) { builder.append(getAsmTypeAsString(parameterType, true)); } builder.append(")"); builder.append(getAsmTypeAsString(returnType, true)); return builder.toString(); }