You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by db...@apache.org on 2012/09/13 02:34:33 UTC
svn commit: r1384168 - in /openwebbeans/trunk:
webbeans-ejb/src/main/java/org/apache/webbeans/ejb/common/interceptor/
webbeans-impl/src/main/java/org/apache/webbeans/component/
webbeans-impl/src/main/java/org/apache/webbeans/decorator/
webbeans-impl/sr...
Author: dblevins
Date: Thu Sep 13 00:34:32 2012
New Revision: 1384168
URL: http://svn.apache.org/viewvc?rev=1384168&view=rev
Log:
OWB-701 - Remove Javassist
Working towards making Javassist *optional* (rather making asm an option)
Moving Javassist-specific code behind the ProxyFactory interface
Added:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/AsmProxyFactory.java
- copied, changed from r1380956, openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/LocalBeanProxyFactory.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/ProxyGenerationException.java
- copied, changed from r1380956, openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/ProxyGenerationException.java
Modified:
openwebbeans/trunk/webbeans-ejb/src/main/java/org/apache/webbeans/ejb/common/interceptor/OpenWebBeansEjbInterceptor.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/BuildInOwbBean.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/ManagedBean.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/ResourceBean.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/decorator/AbstractDecoratorMethodHandler.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorHandler.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/JavassistProxyFactory.java
Modified: openwebbeans/trunk/webbeans-ejb/src/main/java/org/apache/webbeans/ejb/common/interceptor/OpenWebBeansEjbInterceptor.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-ejb/src/main/java/org/apache/webbeans/ejb/common/interceptor/OpenWebBeansEjbInterceptor.java?rev=1384168&r1=1384167&r2=1384168&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-ejb/src/main/java/org/apache/webbeans/ejb/common/interceptor/OpenWebBeansEjbInterceptor.java (original)
+++ openwebbeans/trunk/webbeans-ejb/src/main/java/org/apache/webbeans/ejb/common/interceptor/OpenWebBeansEjbInterceptor.java Thu Sep 13 00:34:32 2012
@@ -33,9 +33,6 @@ import java.util.WeakHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
-import javassist.util.proxy.ProxyFactory;
-import javassist.util.proxy.ProxyObject;
-
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.PostActivate;
@@ -64,7 +61,6 @@ import org.apache.webbeans.intercept.Int
import javax.enterprise.inject.spi.InterceptionType;
import org.apache.webbeans.intercept.InvocationContextImpl;
import org.apache.webbeans.logger.WebBeansLoggerFacade;
-import org.apache.webbeans.proxy.JavassistProxyFactory;
import org.apache.webbeans.spi.ContextsService;
import org.apache.webbeans.util.WebBeansUtil;
@@ -489,18 +485,11 @@ public class OpenWebBeansEjbInterceptor
{
logger.fine("Obtaining a delegate");
}
- Class<?> proxyClass = webBeansContext.getJavassistProxyFactory().getInterceptorProxyClasses().get(injectionTarget);
- if (proxyClass == null)
- {
- JavassistProxyFactory proxyFactory = webBeansContext.getJavassistProxyFactory();
- ProxyFactory delegateFactory = proxyFactory.createProxyFactory(injectionTarget);
- proxyClass = proxyFactory.getProxyClass(delegateFactory);
- proxyFactory.getInterceptorProxyClasses().put(injectionTarget, proxyClass);
- }
- Object delegate = proxyClass.newInstance();
+
delegateHandler = new DelegateHandler(this.contextual, ejbContext);
- ((ProxyObject)delegate).setHandler(delegateHandler);
-
+
+ final Object delegate =
+ webBeansContext.getJavassistProxyFactory().createDecoratorDelegate(injectionTarget, delegateHandler);
// Gets component decorator stack
decorators = WebBeansDecoratorConfig.getDecoratorStack(injectionTarget, instance, delegate,
(CreationalContextImpl<?>)this.cc);
Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/BuildInOwbBean.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/BuildInOwbBean.java?rev=1384168&r1=1384167&r2=1384168&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/BuildInOwbBean.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/BuildInOwbBean.java Thu Sep 13 00:34:32 2012
@@ -31,7 +31,6 @@ import java.util.logging.Level;
import javax.enterprise.context.spi.CreationalContext;
import javassist.util.proxy.MethodHandler;
-import javassist.util.proxy.ProxyObject;
import org.apache.webbeans.config.WebBeansContext;
@@ -208,14 +207,16 @@ public abstract class BuildInOwbBean<T>
T proxy = (T) webBeansContext.getJavassistProxyFactory().createBuildInBeanProxy(this);
if (handlerClassName.equals(PROXY_HANDLER_VALUE_DEFAULT))
{
- ((ProxyObject)proxy).setHandler(new BuildInBeanMethodHandler(this, actualInstance));
+ final MethodHandler handler = new BuildInBeanMethodHandler(this, actualInstance);
+ webBeansContext.getJavassistProxyFactory().setHandler(proxy, handler);
return proxy;
}
else if (handlerContructor != null)
{
try
{
- ((ProxyObject)proxy).setHandler( (MethodHandler)(handlerContructor.newInstance(this, actualInstance)));
+ webBeansContext.getJavassistProxyFactory().setHandler(proxy,
+ (MethodHandler) (handlerContructor.newInstance(this, actualInstance)));
return proxy;
}
catch (Exception e)
@@ -226,7 +227,7 @@ public abstract class BuildInOwbBean<T>
}
return null;
}
-
+
protected abstract T createActualInstance(CreationalContext<T> creationalContext);
Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/ManagedBean.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/ManagedBean.java?rev=1384168&r1=1384167&r2=1384168&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/ManagedBean.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/ManagedBean.java Thu Sep 13 00:34:32 2012
@@ -22,8 +22,6 @@ import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.annotation.Annotation;
-import javassist.util.proxy.ProxyObject;
-
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.Any;
import javax.enterprise.inject.Default;
@@ -98,7 +96,7 @@ public class ManagedBean<T> extends Abst
//If this is an abstract Decorator, we need to set the handler on the Proxy instance
if(isAbstractDecorator)
{
- ((ProxyObject)instance).setHandler(new AbstractDecoratorMethodHandler());
+ webBeansContext.getJavassistProxyFactory().setHandler(instance, new AbstractDecoratorMethodHandler());
}
return instance;
Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/ResourceBean.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/ResourceBean.java?rev=1384168&r1=1384167&r2=1384168&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/ResourceBean.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/component/ResourceBean.java Thu Sep 13 00:34:32 2012
@@ -22,7 +22,6 @@ import java.lang.annotation.Annotation;
import java.lang.reflect.Modifier;
import javax.enterprise.context.spi.CreationalContext;
-import javassist.util.proxy.ProxyObject;
import org.apache.webbeans.exception.WebBeansException;
import org.apache.webbeans.proxy.ResourceProxyHandler;
import org.apache.webbeans.spi.ResourceInjectionService;
@@ -56,7 +55,7 @@ public class ResourceBean<X, T extends A
}
X proxyInstance = (X) getWebBeansContext().getJavassistProxyFactory().getResourceBeanProxyClass(this).newInstance();
- ((ProxyObject) proxyInstance).setHandler(new ResourceProxyHandler(this,instance));
+ webBeansContext.getJavassistProxyFactory().setHandler(proxyInstance, new ResourceProxyHandler(this,instance));
return proxyInstance;
}
catch (Exception e)
Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/decorator/AbstractDecoratorMethodHandler.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/decorator/AbstractDecoratorMethodHandler.java?rev=1384168&r1=1384167&r2=1384168&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/decorator/AbstractDecoratorMethodHandler.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/decorator/AbstractDecoratorMethodHandler.java Thu Sep 13 00:34:32 2012
@@ -30,7 +30,12 @@ public class AbstractDecoratorMethodHand
{
private static final long serialVersionUID = 1L;
-
+
+ public AbstractDecoratorMethodHandler()
+ {
+ new Exception().fillInStackTrace().printStackTrace();
+ }
+
public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable
{
//Don't attempt to call it if the method doesn't exist
Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorHandler.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorHandler.java?rev=1384168&r1=1384167&r2=1384168&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorHandler.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/intercept/InterceptorHandler.java Thu Sep 13 00:34:32 2012
@@ -22,6 +22,7 @@ import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
+import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
@@ -33,8 +34,6 @@ import java.util.logging.Logger;
import javax.interceptor.InvocationContext;
import javassist.util.proxy.MethodHandler;
-import javassist.util.proxy.ProxyFactory;
-import javassist.util.proxy.ProxyObject;
import org.apache.webbeans.component.InjectionTargetBean;
import org.apache.webbeans.component.OwbBean;
@@ -46,6 +45,7 @@ import org.apache.webbeans.decorator.Del
import org.apache.webbeans.decorator.WebBeansDecoratorConfig;
import org.apache.webbeans.decorator.WebBeansDecoratorInterceptor;
import org.apache.webbeans.logger.WebBeansLoggerFacade;
+import org.apache.webbeans.proxy.JavassistProxyFactory;
import org.apache.webbeans.util.ClassUtil;
/**
@@ -140,7 +140,7 @@ import org.apache.webbeans.util.ClassUti
* @see org.apache.webbeans.decorator.WebBeansDecorator
* @see org.apache.webbeans.intercept.ejb.EJBInterceptorConfig
*/
-public abstract class InterceptorHandler implements MethodHandler, Serializable
+public abstract class InterceptorHandler implements InvocationHandler, MethodHandler, Serializable
{
/**Default serial id*/
private static final long serialVersionUID = 1L;
@@ -165,6 +165,7 @@ public abstract class InterceptorHandler
{
this.bean = bean;
webBeansContext = bean.getWebBeansContext();
+// new Exception().fillInStackTrace().printStackTrace();
}
/**
@@ -344,16 +345,10 @@ public abstract class InterceptorHandler
{
if (decoratorDelegateHandler == null)
{
- Class<?> proxyClass = webBeansContext.getJavassistProxyFactory().getInterceptorProxyClasses().get(bean);
- if (proxyClass == null)
- {
- ProxyFactory delegateFactory = webBeansContext.getJavassistProxyFactory().createProxyFactory(bean);
- proxyClass = webBeansContext.getJavassistProxyFactory().getProxyClass(delegateFactory);
- webBeansContext.getJavassistProxyFactory().getInterceptorProxyClasses().put(bean, proxyClass);
- }
- Object delegate = proxyClass.newInstance();
- DelegateHandler newDelegateHandler = new DelegateHandler(bean);
- ((ProxyObject)delegate).setHandler(newDelegateHandler);
+ final DelegateHandler newDelegateHandler = new DelegateHandler(bean);
+ final JavassistProxyFactory javassistProxyFactory = webBeansContext.getJavassistProxyFactory();
+
+ final Object delegate = javassistProxyFactory.createDecoratorDelegate(bean, newDelegateHandler);
// Gets component decorator stack
List<Object> decorators = WebBeansDecoratorConfig.getDecoratorStack(injectionTarget, instance, delegate, ownerCreationalContext);
Copied: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/AsmProxyFactory.java (from r1380956, openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/LocalBeanProxyFactory.java)
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/AsmProxyFactory.java?p2=openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/AsmProxyFactory.java&p1=openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/LocalBeanProxyFactory.java&r1=1380956&r2=1384168&rev=1384168&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/LocalBeanProxyFactory.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/AsmProxyFactory.java Thu Sep 13 00:34:32 2012
@@ -1,29 +1,25 @@
/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
*/
-package org.apache.openejb.util.proxy;
+package org.apache.webbeans.proxy;
-import org.apache.openejb.util.Debug;
-import org.apache.xbean.asm.ClassWriter;
-import org.apache.xbean.asm.Label;
-import org.apache.xbean.asm.MethodVisitor;
-import org.apache.xbean.asm.Opcodes;
-import org.apache.xbean.asm.Type;
-
-import javax.ejb.EJBException;
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
import java.io.Serializable;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
@@ -43,44 +39,78 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
-public class LocalBeanProxyFactory implements Opcodes {
+import org.objectweb.asm.ClassWriter;
+import org.objectweb.asm.Label;
+import org.objectweb.asm.MethodVisitor;
+import org.objectweb.asm.Opcodes;
+import org.objectweb.asm.Type;
+
+public class AsmProxyFactory
+ implements Opcodes
+{
- public static final java.lang.reflect.InvocationHandler NON_BUSINESS_HANDLER = new NonBusinessHandler();
+ public static final InvocationHandler NON_BUSINESS_HANDLER = new NonBusinessHandler();
private static final String BUSSINESS_HANDLER_NAME = "businessHandler";
+
private static final String NON_BUSINESS_HANDLER_NAME = "nonBusinessHandler";
+
// This whole class could be turned static
- private static final LocalBeanProxyFactory GENERATOR = new LocalBeanProxyFactory();
+ private static final AsmProxyFactory GENERATOR = new AsmProxyFactory();
- public static Object newProxyInstance(ClassLoader classLoader, InvocationHandler handler, Class classToSubclass, final Class... interfaces) throws IllegalArgumentException {
- try {
+ public static Object newProxyInstance(ClassLoader classLoader, InvocationHandler handler, Class classToSubclass,
+ final Class... interfaces)
+ throws IllegalArgumentException
+ {
+ try
+ {
final Class proxyClass = GENERATOR.createProxy(classToSubclass, classLoader, interfaces);
final Object object = GENERATOR.constructProxy(proxyClass, handler);
return object;
- } catch (Throwable e) {
- throw new InternalError(Debug.printStackTrace(e));
+ }
+ catch (Throwable e)
+ {
+ throw new InternalError(printStackTrace(e));
}
}
- public static java.lang.reflect.InvocationHandler getInvocationHandler(Object proxy) {
- try {
+ public static String printStackTrace(Throwable t)
+ {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ t.printStackTrace(new PrintStream(baos));
+ return new String(baos.toByteArray());
+ }
+
+ public static InvocationHandler getInvocationHandler(Object proxy)
+ {
+ try
+ {
final Field field = proxy.getClass().getDeclaredField(BUSSINESS_HANDLER_NAME);
field.setAccessible(true);
- try {
- return (java.lang.reflect.InvocationHandler) field.get(proxy);
- } finally {
+ try
+ {
+ return (InvocationHandler) field.get(proxy);
+ }
+ finally
+ {
field.setAccessible(false);
}
- } catch (NoSuchFieldException e) {
+ }
+ catch (NoSuchFieldException e)
+ {
throw new IllegalArgumentException(e);
- } catch (IllegalAccessException e) {
+ }
+ catch (IllegalAccessException e)
+ {
throw new IllegalArgumentException(e);
}
}
- public Object constructProxy(final Class clazz, final java.lang.reflect.InvocationHandler handler) throws IllegalStateException {
+ public Object constructProxy(final Class clazz, final InvocationHandler handler)
+ throws IllegalStateException
+ {
final Object instance = Unsafe.allocateInstance(clazz);
@@ -90,43 +120,64 @@ public class LocalBeanProxyFactory imple
return instance;
}
- private static Field getDeclaredField(final Class clazz, final String fieldName) {
- try {
+ private static Field getDeclaredField(final Class clazz, final String fieldName)
+ {
+ try
+ {
return clazz.getDeclaredField(fieldName);
- } catch (NoSuchFieldException e) {
- final String message = String.format("Proxy class does not contain expected field \"%s\": %s", fieldName, clazz.getName());
+ }
+ catch (NoSuchFieldException e)
+ {
+ final String message =
+ String.format("Proxy class does not contain expected field \"%s\": %s", fieldName, clazz.getName());
throw new IllegalStateException(message, e);
}
}
- public static boolean isProxy(final Class<?> clazz) {
+ public static boolean isProxy(final Class<?> clazz)
+ {
return clazz.isAnnotationPresent(Proxy.class);
}
- public Class createProxy(final Class<?> classToProxy, final ClassLoader cl, final Class... interfaces) {
+ public Class createProxy(final Class<?> classToProxy, final ClassLoader cl, final Class... interfaces)
+ {
final String proxyName = classToProxy.getName() + "$LocalBeanProxy";
final String classFileName = proxyName.replace('.', '/');
- try {
+ try
+ {
return cl.loadClass(proxyName);
- } catch (Exception e) {
+ }
+ catch (Exception e)
+ {
}
- synchronized (LocalBeanProxyFactory.class) { // it can be done by concurrent threads
- try { // try it again
+ synchronized (AsmProxyFactory.class)
+ { // it can be done by concurrent threads
+ try
+ { // try it again
return cl.loadClass(proxyName);
- } catch (Exception e) {
}
- try {
+ catch (Exception e)
+ {
+ }
+ try
+ {
final byte[] proxyBytes = generateProxy(classToProxy, classFileName, interfaces);
return Unsafe.defineClass(classToProxy, proxyName, proxyBytes);
- } catch (Exception e) {
- throw new InternalError(e.toString());
+ }
+ catch (Exception e)
+ {
+ final InternalError internalError = new InternalError();
+ internalError.initCause(e);
+ throw internalError;
}
}
}
- private byte[] generateProxy(final Class<?> classToProxy, final String proxyName, final Class<?>... interfaces) throws ProxyGenerationException {
+ private byte[] generateProxy(final Class<?> classToProxy, final String proxyName, final Class<?>... interfaces)
+ throws ProxyGenerationException
+ {
final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
final String proxyClassFileName = proxyName.replace('.', '/');
@@ -134,9 +185,10 @@ public class LocalBeanProxyFactory imple
// push class signature
final String[] interfaceNames = new String[interfaces.length];
- for (int i = 0; i < interfaces.length; i++) {
+ for (int i = 0; i < interfaces.length; i++)
+ {
final Class<?> anInterface = interfaces[i];
- interfaceNames[i] = anInterface.getName().replace('.','/');
+ interfaceNames[i] = anInterface.getName().replace('.', '/');
}
cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, proxyClassFileName, null, classFileName, interfaceNames);
@@ -145,30 +197,37 @@ public class LocalBeanProxyFactory imple
cw.visitAnnotation("L" + Proxy.class.getName().replace('.', '/') + ";", true).visitEnd();
// push InvocationHandler fields
- cw.visitField(ACC_FINAL + ACC_PRIVATE, BUSSINESS_HANDLER_NAME, "Ljava/lang/reflect/InvocationHandler;", null, null).visitEnd();
- cw.visitField(ACC_FINAL + ACC_PRIVATE, NON_BUSINESS_HANDLER_NAME, "Ljava/lang/reflect/InvocationHandler;", null, null).visitEnd();
+ cw.visitField(ACC_FINAL + ACC_PRIVATE, BUSSINESS_HANDLER_NAME, "Ljava/lang/reflect/InvocationHandler;", null,
+ null).visitEnd();
+ cw.visitField(ACC_FINAL + ACC_PRIVATE, NON_BUSINESS_HANDLER_NAME, "Ljava/lang/reflect/InvocationHandler;", null,
+ null).visitEnd();
final Map<String, List<Method>> methodMap = new HashMap<String, List<Method>>();
getNonPrivateMethods(classToProxy, methodMap);
- for (Class<?> anInterface : interfaces) {
+ for (Class<?> anInterface : interfaces)
+ {
getNonPrivateMethods(anInterface, methodMap);
}
// Iterate over the public methods
- for (final Map.Entry<String, List<Method>> entry : methodMap.entrySet()) {
+ for (final Map.Entry<String, List<Method>> entry : methodMap.entrySet())
+ {
- for (final Method method : entry.getValue()) {
+ for (final Method method : entry.getValue())
+ {
final String name = method.getName();
- if (Modifier.isPublic(method.getModifiers())
- || (method.getParameterTypes().length == 0 && ("finalize".equals(name)
- || "clone".equals(name)))) {
- // forward invocations of any public methods or
- // finalize/clone methods to businessHandler
+ if (Modifier.isPublic(method.getModifiers()) ||
+ (method.getParameterTypes().length == 0 && ("finalize".equals(name) || "clone".equals(name))))
+ {
+ // forward invocations of any public methods or
+ // finalize/clone methods to businessHandler
processMethod(cw, method, proxyClassFileName, BUSSINESS_HANDLER_NAME);
- } else {
+ }
+ else
+ {
// forward invocations of any other methods to nonBusinessHandler
processMethod(cw, method, proxyClassFileName, NON_BUSINESS_HANDLER_NAME);
}
@@ -178,26 +237,34 @@ public class LocalBeanProxyFactory imple
return cw.toByteArray();
}
- private void getNonPrivateMethods(Class<?> clazz, Map<String, List<Method>> methodMap) {
- while (clazz != null) {
- for (Method method : clazz.getDeclaredMethods()) {
+ private void getNonPrivateMethods(Class<?> clazz, Map<String, List<Method>> methodMap)
+ {
+ while (clazz != null)
+ {
+ for (Method method : clazz.getDeclaredMethods())
+ {
final int modifiers = method.getModifiers();
- if (Modifier.isFinal(modifiers)
- || Modifier.isPrivate(modifiers)
- || Modifier.isStatic(modifiers)) {
+ if (Modifier.isFinal(modifiers) || Modifier.isPrivate(modifiers) || Modifier.isStatic(modifiers))
+ {
continue;
}
List<Method> methods = methodMap.get(method.getName());
- if (methods == null) {
+ if (methods == null)
+ {
methods = new ArrayList<Method>();
methods.add(method);
methodMap.put(method.getName(), methods);
- } else {
- if (isOverridden(methods, method)) {
+ }
+ else
+ {
+ if (isOverridden(methods, method))
+ {
// method is overridden in superclass, so do nothing
- } else {
+ }
+ else
+ {
// method is not overridden, so add it
methods.add(method);
}
@@ -208,17 +275,25 @@ public class LocalBeanProxyFactory imple
}
}
- private boolean isOverridden(final List<Method> methods, final Method method) {
- for (final Method m : methods) {
- if (Arrays.equals(m.getParameterTypes(), method.getParameterTypes())) {
+ private boolean isOverridden(final List<Method> methods, final Method method)
+ {
+ for (final Method m : methods)
+ {
+ if (Arrays.equals(m.getParameterTypes(), method.getParameterTypes()))
+ {
return true;
}
}
return false;
}
- private void processMethod(final ClassWriter cw, final Method method, final String proxyName, final String handlerName) throws ProxyGenerationException {
- if ("<init>".equals(method.getName())) {
+
+ private void processMethod(final ClassWriter cw, final Method method, final String proxyName,
+ final String handlerName)
+ throws ProxyGenerationException
+ {
+ if ("<init>".equals(method.getName()))
+ {
return;
}
@@ -229,13 +304,18 @@ public class LocalBeanProxyFactory imple
// push the method definition
int modifier = 0;
- if (Modifier.isPublic(modifiers)) {
+ if (Modifier.isPublic(modifiers))
+ {
modifier = ACC_PUBLIC;
- } else if (Modifier.isProtected(modifiers)) {
+ }
+ else if (Modifier.isProtected(modifiers))
+ {
modifier = ACC_PROTECTED;
}
- final MethodVisitor mv = cw.visitMethod(modifier, method.getName(), getMethodSignatureAsString(returnType, parameterTypes), null, null);
+ final MethodVisitor mv =
+ cw.visitMethod(modifier, method.getName(), getMethodSignatureAsString(returnType, parameterTypes), null,
+ null);
mv.visitCode();
// push try/catch block, to catch declared exceptions, and to catch java.lang.Throwable
@@ -243,7 +323,8 @@ public class LocalBeanProxyFactory imple
final Label l1 = new Label();
final Label l2 = new Label();
- if (exceptionTypes.length > 0) {
+ if (exceptionTypes.length > 0)
+ {
mv.visitTryCatchBlock(l0, l1, l2, "java/lang/reflect/InvocationTargetException");
}
@@ -264,7 +345,8 @@ public class LocalBeanProxyFactory imple
int length = 1;
// push parameters into array
- for (int i = 0; i < parameterTypes.length; i++) {
+ for (int i = 0; i < parameterTypes.length; i++)
+ {
// keep copy of array on stack
mv.visitInsn(DUP);
@@ -273,24 +355,31 @@ public class LocalBeanProxyFactory imple
// push number onto stack
pushIntOntoStack(mv, i);
- if (parameterType.isPrimitive()) {
+ if (parameterType.isPrimitive())
+ {
String wrapperType = getWrapperType(parameterType);
mv.visitFieldInsn(GETSTATIC, wrapperType, "TYPE", "Ljava/lang/Class;");
- } else {
+ }
+ else
+ {
mv.visitLdcInsn(Type.getType(getAsmTypeAsString(parameterType, true)));
}
mv.visitInsn(AASTORE);
- if (Long.TYPE.equals(parameterType) || Double.TYPE.equals(parameterType)) {
+ if (Long.TYPE.equals(parameterType) || Double.TYPE.equals(parameterType))
+ {
length += 2;
- } else {
+ }
+ else
+ {
length++;
}
}
// invoke getMethod() with the method name and the array of types
- mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getDeclaredMethod", "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;");
+ mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", "getDeclaredMethod",
+ "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;");
// store the returned method for later
mv.visitVarInsn(ASTORE, length);
@@ -318,7 +407,8 @@ public class LocalBeanProxyFactory imple
int index = 1;
// push parameters into array
- for (int i = 0; i < parameterTypes.length; i++) {
+ for (int i = 0; i < parameterTypes.length; i++)
+ {
// keep copy of array on stack
mv.visitInsn(DUP);
@@ -327,19 +417,26 @@ public class LocalBeanProxyFactory imple
// push number onto stack
pushIntOntoStack(mv, i);
- if (parameterType.isPrimitive()) {
+ if (parameterType.isPrimitive())
+ {
String wrapperType = getWrapperType(parameterType);
mv.visitVarInsn(getVarInsn(parameterType), index);
- mv.visitMethodInsn(INVOKESTATIC, wrapperType, "valueOf", "(" + getPrimitiveLetter(parameterType) + ")L" + wrapperType + ";");
+ mv.visitMethodInsn(INVOKESTATIC, wrapperType, "valueOf",
+ "(" + getPrimitiveLetter(parameterType) + ")L" + wrapperType + ";");
mv.visitInsn(AASTORE);
- if (Long.TYPE.equals(parameterType) || Double.TYPE.equals(parameterType)) {
+ if (Long.TYPE.equals(parameterType) || Double.TYPE.equals(parameterType))
+ {
index += 2;
- } else {
+ }
+ else
+ {
index++;
}
- } else {
+ }
+ else
+ {
mv.visitVarInsn(ALOAD, index);
mv.visitInsn(AASTORE);
index++;
@@ -347,39 +444,48 @@ public class LocalBeanProxyFactory imple
}
// invoke the invocationHandler
- mv.visitMethodInsn(INVOKEINTERFACE, "java/lang/reflect/InvocationHandler", "invoke", "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;");
+ mv.visitMethodInsn(INVOKEINTERFACE, "java/lang/reflect/InvocationHandler", "invoke",
+ "(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;");
// cast the result
mv.visitTypeInsn(CHECKCAST, getCastType(returnType));
- if (returnType.isPrimitive() && (!Void.TYPE.equals(returnType))) {
+ if (returnType.isPrimitive() && (!Void.TYPE.equals(returnType)))
+ {
// get the primitive value
- mv.visitMethodInsn(INVOKEVIRTUAL, getWrapperType(returnType), getPrimitiveMethod(returnType), "()" + getPrimitiveLetter(returnType));
+ mv.visitMethodInsn(INVOKEVIRTUAL, getWrapperType(returnType), getPrimitiveMethod(returnType),
+ "()" + getPrimitiveLetter(returnType));
}
// push return
mv.visitLabel(l1);
- if (!Void.TYPE.equals(returnType)) {
+ if (!Void.TYPE.equals(returnType))
+ {
mv.visitInsn(getReturnInsn(returnType));
- } else {
+ }
+ else
+ {
mv.visitInsn(POP);
mv.visitInsn(RETURN);
}
// catch InvocationTargetException
- if (exceptionTypes.length > 0) {
+ if (exceptionTypes.length > 0)
+ {
mv.visitLabel(l2);
mv.visitVarInsn(ASTORE, length);
final Label l5 = new Label();
mv.visitLabel(l5);
- for (int i = 0; i < exceptionTypes.length; i++) {
+ for (int i = 0; i < exceptionTypes.length; i++)
+ {
final Class<?> exceptionType = exceptionTypes[i];
mv.visitLdcInsn(Type.getType("L" + exceptionType.getCanonicalName().replace('.', '/') + ";"));
mv.visitVarInsn(ALOAD, length);
- mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/InvocationTargetException", "getCause", "()Ljava/lang/Throwable;");
+ mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/InvocationTargetException", "getCause",
+ "()Ljava/lang/Throwable;");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "equals", "(Ljava/lang/Object;)Z");
@@ -390,16 +496,19 @@ public class LocalBeanProxyFactory imple
mv.visitLabel(l7);
mv.visitVarInsn(ALOAD, length);
- mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/InvocationTargetException", "getCause", "()Ljava/lang/Throwable;");
+ mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/InvocationTargetException", "getCause",
+ "()Ljava/lang/Throwable;");
mv.visitTypeInsn(CHECKCAST, exceptionType.getCanonicalName().replace('.', '/'));
mv.visitInsn(ATHROW);
mv.visitLabel(l6);
- if (i == (exceptionTypes.length - 1)) {
+ if (i == (exceptionTypes.length - 1))
+ {
mv.visitTypeInsn(NEW, "java/lang/reflect/UndeclaredThrowableException");
mv.visitInsn(DUP);
mv.visitVarInsn(ALOAD, length);
- mv.visitMethodInsn(INVOKESPECIAL, "java/lang/reflect/UndeclaredThrowableException", "<init>", "(Ljava/lang/Throwable;)V");
+ mv.visitMethodInsn(INVOKESPECIAL, "java/lang/reflect/UndeclaredThrowableException", "<init>",
+ "(Ljava/lang/Throwable;)V");
mv.visitInsn(ATHROW);
}
}
@@ -412,26 +521,44 @@ public class LocalBeanProxyFactory imple
/**
* Gets the appropriate bytecode instruction for RETURN, according to what type we need to return
+ *
* @param type Type the needs to be returned
* @return The matching bytecode instruction
*/
- private int getReturnInsn(final Class<?> type) {
- if (type.isPrimitive()) {
- if (Integer.TYPE.equals(type)) {
+ private int getReturnInsn(final Class<?> type)
+ {
+ if (type.isPrimitive())
+ {
+ if (Integer.TYPE.equals(type))
+ {
return IRETURN;
- } else if (Boolean.TYPE.equals(type)) {
+ }
+ else if (Boolean.TYPE.equals(type))
+ {
return IRETURN;
- } else if (Character.TYPE.equals(type)) {
+ }
+ else if (Character.TYPE.equals(type))
+ {
return IRETURN;
- } else if (Byte.TYPE.equals(type)) {
+ }
+ else if (Byte.TYPE.equals(type))
+ {
return IRETURN;
- } else if (Short.TYPE.equals(type)) {
+ }
+ else if (Short.TYPE.equals(type))
+ {
return IRETURN;
- } else if (Float.TYPE.equals(type)) {
+ }
+ else if (Float.TYPE.equals(type))
+ {
return FRETURN;
- } else if (Long.TYPE.equals(type)) {
+ }
+ else if (Long.TYPE.equals(type))
+ {
return LRETURN;
- } else if (Double.TYPE.equals(type)) {
+ }
+ else if (Double.TYPE.equals(type))
+ {
return DRETURN;
}
}
@@ -442,26 +569,44 @@ public class LocalBeanProxyFactory imple
/**
* Returns the appropriate bytecode instruction to load a value from a variable to the stack
+ *
* @param type Type to load
* @return Bytecode instruction to use
*/
- private int getVarInsn(final Class<?> type) {
- if (type.isPrimitive()) {
- if (Integer.TYPE.equals(type)) {
+ private int getVarInsn(final Class<?> type)
+ {
+ if (type.isPrimitive())
+ {
+ if (Integer.TYPE.equals(type))
+ {
return ILOAD;
- } else if (Boolean.TYPE.equals(type)) {
+ }
+ else if (Boolean.TYPE.equals(type))
+ {
return ILOAD;
- } else if (Character.TYPE.equals(type)) {
+ }
+ else if (Character.TYPE.equals(type))
+ {
return ILOAD;
- } else if (Byte.TYPE.equals(type)) {
+ }
+ else if (Byte.TYPE.equals(type))
+ {
return ILOAD;
- } else if (Short.TYPE.equals(type)) {
+ }
+ else if (Short.TYPE.equals(type))
+ {
return ILOAD;
- } else if (Float.TYPE.equals(type)) {
+ }
+ else if (Float.TYPE.equals(type))
+ {
return FLOAD;
- } else if (Long.TYPE.equals(type)) {
+ }
+ else if (Long.TYPE.equals(type))
+ {
return LLOAD;
- } else if (Double.TYPE.equals(type)) {
+ }
+ else if (Double.TYPE.equals(type))
+ {
return DLOAD;
}
}
@@ -471,25 +616,42 @@ public class LocalBeanProxyFactory imple
/**
* Returns the name of the Java method to call to get the primitive value from an Object - e.g. intValue for java.lang.Integer
+ *
* @param type Type whose primitive method we want to lookup
* @return The name of the method to use
*/
- private String getPrimitiveMethod(final Class<?> type) {
- if (Integer.TYPE.equals(type)) {
+ private String getPrimitiveMethod(final Class<?> type)
+ {
+ if (Integer.TYPE.equals(type))
+ {
return "intValue";
- } else if (Boolean.TYPE.equals(type)) {
+ }
+ else if (Boolean.TYPE.equals(type))
+ {
return "booleanValue";
- } else if (Character.TYPE.equals(type)) {
+ }
+ else if (Character.TYPE.equals(type))
+ {
return "charValue";
- } else if (Byte.TYPE.equals(type)) {
+ }
+ else if (Byte.TYPE.equals(type))
+ {
return "byteValue";
- } else if (Short.TYPE.equals(type)) {
+ }
+ else if (Short.TYPE.equals(type))
+ {
return "shortValue";
- } else if (Float.TYPE.equals(type)) {
+ }
+ else if (Float.TYPE.equals(type))
+ {
return "floatValue";
- } else if (Long.TYPE.equals(type)) {
+ }
+ else if (Long.TYPE.equals(type))
+ {
return "longValue";
- } else if (Double.TYPE.equals(type)) {
+ }
+ else if (Double.TYPE.equals(type))
+ {
return "doubleValue";
}
@@ -498,40 +660,64 @@ public class LocalBeanProxyFactory imple
/**
* 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
*/
- String getCastType(final Class<?> returnType) {
- if (returnType.isPrimitive()) {
+ String getCastType(final Class<?> returnType)
+ {
+ if (returnType.isPrimitive())
+ {
return getWrapperType(returnType);
- } else {
+ }
+ else
+ {
return getAsmTypeAsString(returnType, false);
}
}
/**
* Returns the wrapper type for a primitive, e.g. java.lang.Integer for int
+ *
* @param type
* @return
*/
- private String getWrapperType(final Class<?> type) {
- if (Integer.TYPE.equals(type)) {
+ private String getWrapperType(final Class<?> type)
+ {
+ if (Integer.TYPE.equals(type))
+ {
return Integer.class.getCanonicalName().replace('.', '/');
- } else if (Boolean.TYPE.equals(type)) {
+ }
+ else if (Boolean.TYPE.equals(type))
+ {
return Boolean.class.getCanonicalName().replace('.', '/');
- } else if (Character.TYPE.equals(type)) {
+ }
+ else if (Character.TYPE.equals(type))
+ {
return Character.class.getCanonicalName().replace('.', '/');
- } else if (Byte.TYPE.equals(type)) {
+ }
+ else if (Byte.TYPE.equals(type))
+ {
return Byte.class.getCanonicalName().replace('.', '/');
- } else if (Short.TYPE.equals(type)) {
+ }
+ else if (Short.TYPE.equals(type))
+ {
return Short.class.getCanonicalName().replace('.', '/');
- } else if (Float.TYPE.equals(type)) {
+ }
+ else if (Float.TYPE.equals(type))
+ {
return Float.class.getCanonicalName().replace('.', '/');
- } else if (Long.TYPE.equals(type)) {
+ }
+ else if (Long.TYPE.equals(type))
+ {
return Long.class.getCanonicalName().replace('.', '/');
- } else if (Double.TYPE.equals(type)) {
+ }
+ else if (Double.TYPE.equals(type))
+ {
return Double.class.getCanonicalName().replace('.', '/');
- } else if (Void.TYPE.equals(type)) {
+ }
+ else if (Void.TYPE.equals(type))
+ {
return Void.class.getCanonicalName().replace('.', '/');
}
@@ -540,25 +726,42 @@ public class LocalBeanProxyFactory imple
/**
* Invokes the most appropriate bytecode instruction to put a number on the stack
+ *
* @param mv
* @param i
*/
- private void pushIntOntoStack(final MethodVisitor mv, final int i) {
- if (i == 0) {
+ private void pushIntOntoStack(final MethodVisitor mv, final int i)
+ {
+ if (i == 0)
+ {
mv.visitInsn(ICONST_0);
- } else if (i == 1) {
+ }
+ else if (i == 1)
+ {
mv.visitInsn(ICONST_1);
- } else if (i == 2) {
+ }
+ else if (i == 2)
+ {
mv.visitInsn(ICONST_2);
- } else if (i == 3) {
+ }
+ else if (i == 3)
+ {
mv.visitInsn(ICONST_3);
- } else if (i == 4) {
+ }
+ else if (i == 4)
+ {
mv.visitInsn(ICONST_4);
- } else if (i == 5) {
+ }
+ else if (i == 5)
+ {
mv.visitInsn(ICONST_5);
- } else if (i > 5 && i <= 255) {
+ }
+ else if (i > 5 && i <= 255)
+ {
mv.visitIntInsn(BIPUSH, i);
- } else {
+ }
+ else
+ {
mv.visitIntInsn(SIPUSH, i);
}
}
@@ -567,15 +770,18 @@ public class LocalBeanProxyFactory imple
* pushes an array of the specified size to the method visitor. The generated bytecode will leave
* the new array at the top of the stack.
*
- * @param mv MethodVisitor to use
+ * @param mv MethodVisitor to use
* @param size Size of the array to create
* @param type Type of array to create
* @throws ProxyGenerationException
*/
- private void createArrayDefinition(final MethodVisitor mv, final int size, final Class<?> type) throws ProxyGenerationException {
+ private void createArrayDefinition(final MethodVisitor mv, final int size, final Class<?> type)
+ throws ProxyGenerationException
+ {
// create a new array of java.lang.class (2)
- if (size < 0) {
+ if (size < 0)
+ {
throw new ProxyGenerationException("Array size cannot be less than zero");
}
@@ -585,10 +791,12 @@ public class LocalBeanProxyFactory imple
}
- String getMethodSignatureAsString(final Class<?> returnType, final Class<?>[] parameterTypes) {
+ String getMethodSignatureAsString(final Class<?> returnType, final Class<?>[] parameterTypes)
+ {
final StringBuilder builder = new StringBuilder();
builder.append("(");
- for (Class<?> parameterType : parameterTypes) {
+ for (Class<?> parameterType : parameterTypes)
+ {
builder.append(getAsmTypeAsString(parameterType, true));
}
@@ -600,27 +808,46 @@ public class LocalBeanProxyFactory imple
/**
* Returns the single letter that matches the given primitive in bytecode instructions
+ *
* @param type
* @return
*/
- private String getPrimitiveLetter(final Class<?> type) {
- if (Integer.TYPE.equals(type)) {
+ private String getPrimitiveLetter(final Class<?> type)
+ {
+ if (Integer.TYPE.equals(type))
+ {
return "I";
- } else if (Void.TYPE.equals(type)) {
+ }
+ else if (Void.TYPE.equals(type))
+ {
return "V";
- } else if (Boolean.TYPE.equals(type)) {
+ }
+ else if (Boolean.TYPE.equals(type))
+ {
return "Z";
- } else if (Character.TYPE.equals(type)) {
+ }
+ else if (Character.TYPE.equals(type))
+ {
return "C";
- } else if (Byte.TYPE.equals(type)) {
+ }
+ else if (Byte.TYPE.equals(type))
+ {
return "B";
- } else if (Short.TYPE.equals(type)) {
+ }
+ else if (Short.TYPE.equals(type))
+ {
return "S";
- } else if (Float.TYPE.equals(type)) {
+ }
+ else if (Float.TYPE.equals(type))
+ {
return "F";
- } else if (Long.TYPE.equals(type)) {
+ }
+ else if (Long.TYPE.equals(type))
+ {
return "J";
- } else if (Double.TYPE.equals(type)) {
+ }
+ else if (Double.TYPE.equals(type))
+ {
return "D";
}
@@ -629,42 +856,62 @@ public class LocalBeanProxyFactory imple
/**
* Converts a class to a String suitable for ASM.
+ *
* @param parameterType Class to convert
- * @param wrap True if a non-array object should be wrapped with L and ; - e.g. Ljava/lang/Integer;
+ * @param wrap True if a non-array object should be wrapped with L and ; - e.g. Ljava/lang/Integer;
* @return String to use for ASM
*/
- public String getAsmTypeAsString(final Class<?> parameterType, final boolean wrap) {
- if (parameterType.isArray()) {
- if (parameterType.getComponentType().isPrimitive()) {
+ public String getAsmTypeAsString(final Class<?> parameterType, final boolean wrap)
+ {
+ if (parameterType.isArray())
+ {
+ if (parameterType.getComponentType().isPrimitive())
+ {
final Class<?> componentType = parameterType.getComponentType();
return "[" + getPrimitiveLetter(componentType);
- } else {
+ }
+ else
+ {
return "[" + getAsmTypeAsString(parameterType.getComponentType(), true);
}
- } else {
- if (parameterType.isPrimitive()) {
+ }
+ else
+ {
+ if (parameterType.isPrimitive())
+ {
return getPrimitiveLetter(parameterType);
- } else {
+ }
+ else
+ {
String className = parameterType.getCanonicalName();
- if (parameterType.isMemberClass()) {
+ if (parameterType.isMemberClass())
+ {
int lastDot = className.lastIndexOf(".");
className = className.substring(0, lastDot) + "$" + className.substring(lastDot + 1);
}
- if (wrap) {
+ if (wrap)
+ {
return "L" + className.replace('.', '/') + ";";
- } else {
+ }
+ else
+ {
return className.replace('.', '/');
}
}
}
}
- static class NonBusinessHandler implements java.lang.reflect.InvocationHandler, Serializable {
-
- public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
- throw new EJBException("Calling non-public methods of a local bean without any interfaces is not allowed");
+ static class NonBusinessHandler
+ implements InvocationHandler, Serializable
+ {
+
+ public Object invoke(final Object proxy, final Method method, final Object[] args)
+ throws Throwable
+ {
+ throw new RuntimeException(
+ "Calling non-public methods of a local bean without any interfaces is not allowed");
}
}
@@ -672,125 +919,190 @@ public class LocalBeanProxyFactory imple
/**
* The methods of this class model sun.misc.Unsafe which is used reflectively
*/
- private static class Unsafe {
+ private static class Unsafe
+ {
// sun.misc.Unsafe
- private static final Object unsafe;
- private static final Method defineClass;
- private static final Method allocateInstance;
- private static final Method putObject;
- private static final Method objectFieldOffset;
+ private static final Object UNSAFE;
+
+ private static final Method DEFINE_CLASS;
+
+ private static final Method ALLOCATE_INSTANCE;
+
+ private static final Method PUT_OBJECT;
+
+ private static final Method OBJECT_FIELD_OFFSET;
- static {
+ static
+ {
final Class<?> unsafeClass;
- try {
- unsafeClass = AccessController.doPrivileged(new PrivilegedAction<Class<?>>() {
- public Class<?> run() {
- try {
+ try
+ {
+ unsafeClass = AccessController.doPrivileged(new PrivilegedAction<Class<?>>()
+ {
+ public Class<?> run()
+ {
+ try
+ {
return Thread.currentThread().getContextClassLoader().loadClass("sun.misc.Unsafe");
- } catch (Exception e) {
- try {
+ }
+ catch (Exception e)
+ {
+ try
+ {
return ClassLoader.getSystemClassLoader().loadClass("sun.misc.Unsafe");
- } catch (ClassNotFoundException e1) {
+ }
+ catch (ClassNotFoundException e1)
+ {
throw new IllegalStateException("Cannot get sun.misc.Unsafe", e);
}
}
}
});
- } catch (Exception e) {
+ }
+ catch (Exception e)
+ {
throw new IllegalStateException("Cannot get sun.misc.Unsafe class", e);
}
- unsafe = AccessController.doPrivileged(new PrivilegedAction<Object>() {
- public Object run() {
- try {
+ UNSAFE = AccessController.doPrivileged(new PrivilegedAction<Object>()
+ {
+ public Object run()
+ {
+ try
+ {
Field field = unsafeClass.getDeclaredField("theUnsafe");
field.setAccessible(true);
return field.get(null);
- } catch (Exception e) {
+ }
+ catch (Exception e)
+ {
throw new IllegalStateException("Cannot get sun.misc.Unsafe", e);
}
}
});
- allocateInstance = AccessController.doPrivileged(new PrivilegedAction<Method>() {
- public Method run() {
- try {
+ ALLOCATE_INSTANCE = AccessController.doPrivileged(new PrivilegedAction<Method>()
+ {
+ public Method run()
+ {
+ try
+ {
Method mtd = unsafeClass.getDeclaredMethod("allocateInstance", Class.class);
mtd.setAccessible(true);
return mtd;
- } catch (Exception e) {
+ }
+ catch (Exception e)
+ {
throw new IllegalStateException("Cannot get sun.misc.Unsafe.allocateInstance", e);
}
}
});
- objectFieldOffset = AccessController.doPrivileged(new PrivilegedAction<Method>() {
- public Method run() {
- try {
+ OBJECT_FIELD_OFFSET = AccessController.doPrivileged(new PrivilegedAction<Method>()
+ {
+ public Method run()
+ {
+ try
+ {
Method mtd = unsafeClass.getDeclaredMethod("objectFieldOffset", Field.class);
mtd.setAccessible(true);
return mtd;
- } catch (Exception e) {
+ }
+ catch (Exception e)
+ {
throw new IllegalStateException("Cannot get sun.misc.Unsafe.objectFieldOffset", e);
}
}
});
- putObject = AccessController.doPrivileged(new PrivilegedAction<Method>() {
- public Method run() {
- try {
+ PUT_OBJECT = AccessController.doPrivileged(new PrivilegedAction<Method>()
+ {
+ public Method run()
+ {
+ try
+ {
Method mtd = unsafeClass.getDeclaredMethod("putObject", Object.class, long.class, Object.class);
mtd.setAccessible(true);
return mtd;
- } catch (Exception e) {
+ }
+ catch (Exception e)
+ {
throw new IllegalStateException("Cannot get sun.misc.Unsafe.putObject", e);
}
}
});
- defineClass = AccessController.doPrivileged(new PrivilegedAction<Method>() {
- public Method run() {
- try {
- Method mtd = unsafeClass.getDeclaredMethod("defineClass", String.class, byte[].class, int.class, int.class, ClassLoader.class, ProtectionDomain.class);
+ DEFINE_CLASS = AccessController.doPrivileged(new PrivilegedAction<Method>()
+ {
+ public Method run()
+ {
+ try
+ {
+ Method mtd = unsafeClass.getDeclaredMethod("defineClass", String.class, byte[].class, int.class,
+ int.class, ClassLoader.class,
+ ProtectionDomain.class);
mtd.setAccessible(true);
return mtd;
- } catch (Exception e) {
+ }
+ catch (Exception e)
+ {
throw new IllegalStateException("Cannot get sun.misc.Unsafe.defineClass", e);
}
}
});
}
- private static Object allocateInstance(final Class clazz) {
- try {
- return allocateInstance.invoke(unsafe, clazz);
- } catch (IllegalAccessException e) {
+ private static Object allocateInstance(final Class clazz)
+ {
+ try
+ {
+ return ALLOCATE_INSTANCE.invoke(UNSAFE, clazz);
+ }
+ catch (IllegalAccessException e)
+ {
throw new IllegalStateException("Failed to allocateInstance of Proxy class " + clazz.getName(), e);
- } catch (InvocationTargetException e) {
+ }
+ catch (InvocationTargetException e)
+ {
Throwable throwable = e.getTargetException() != null ? e.getTargetException() : e;
- throw new IllegalStateException("Failed to allocateInstance of Proxy class " + clazz.getName(), throwable);
+ throw new IllegalStateException("Failed to allocateInstance of Proxy class " + clazz.getName(),
+ throwable);
}
}
- private static void setValue(final Field field, final Object object, final Object value) {
+ private static void setValue(final Field field, final Object object, final Object value)
+ {
final long offset;
- try {
- offset = (Long) objectFieldOffset.invoke(unsafe, field);
- } catch (Exception e) {
- throw new IllegalStateException("Failed getting offset for: field=" + field.getName() + " class=" + field.getDeclaringClass().getName(), e);
+ try
+ {
+ offset = (Long) OBJECT_FIELD_OFFSET.invoke(UNSAFE, field);
+ }
+ catch (Exception e)
+ {
+ throw new IllegalStateException("Failed getting offset for: field=" + field.getName() + " class=" +
+ field.getDeclaringClass().getName(), e);
}
- try {
- putObject.invoke(unsafe, object, offset, value);
- } catch (Exception e) {
- throw new IllegalStateException("Failed putting field=" + field.getName() + " class=" + field.getDeclaringClass().getName(), e);
+ try
+ {
+ PUT_OBJECT.invoke(UNSAFE, object, offset, value);
+ }
+ catch (Exception e)
+ {
+ throw new IllegalStateException(
+ "Failed putting field=" + field.getName() + " class=" + field.getDeclaringClass().getName(), e);
}
}
- private static Class defineClass(Class<?> clsToProxy, String proxyName, byte[] proxyBytes) throws IllegalAccessException, InvocationTargetException {
- return (Class<?>) defineClass.invoke(unsafe, proxyName, proxyBytes, 0, proxyBytes.length, clsToProxy.getClassLoader(), clsToProxy.getProtectionDomain());
+ private static Class defineClass(Class<?> clsToProxy, String proxyName, byte[] proxyBytes)
+ throws IllegalAccessException, InvocationTargetException
+ {
+ final ProtectionDomain protectionDomain = clsToProxy.getProtectionDomain();
+ return (Class<?>) DEFINE_CLASS.invoke(UNSAFE, proxyName, proxyBytes, 0, proxyBytes.length,
+ clsToProxy.getClassLoader(), protectionDomain);
}
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
- private static @interface Proxy {
+ private static @interface Proxy
+ {
}
}
Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/JavassistProxyFactory.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/JavassistProxyFactory.java?rev=1384168&r1=1384167&r2=1384168&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/JavassistProxyFactory.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/JavassistProxyFactory.java Thu Sep 13 00:34:32 2012
@@ -22,6 +22,7 @@ import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import java.util.HashSet;
import java.util.Iterator;
@@ -36,6 +37,7 @@ import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.Decorator;
import javassist.util.proxy.MethodFilter;
+import javassist.util.proxy.MethodHandler;
import javassist.util.proxy.ProxyFactory;
import javassist.util.proxy.ProxyFactory.ClassLoaderProvider;
import javassist.util.proxy.ProxyObject;
@@ -44,6 +46,7 @@ import org.apache.webbeans.component.Owb
import org.apache.webbeans.component.ResourceBean;
import org.apache.webbeans.config.OpenWebBeansConfiguration;
import org.apache.webbeans.context.creational.CreationalContextImpl;
+import org.apache.webbeans.decorator.DelegateHandler;
import org.apache.webbeans.decorator.WebBeansDecorator;
import org.apache.webbeans.exception.WebBeansConfigurationException;
import org.apache.webbeans.intercept.DependentScopedBeanInterceptorHandler;
@@ -78,7 +81,12 @@ public final class JavassistProxyFactory
private Map<String, Class<? extends InterceptorHandler>> interceptorHandlerClasses =
new ConcurrentHashMap<String, Class<? extends InterceptorHandler>>();
-
+ public void setHandler(Object proxy, MethodHandler handler)
+ {
+ ((ProxyObject)proxy).setHandler(handler);
+ }
+
+
public Map<OwbBean<?>, Class<?>> getInterceptorProxyClasses()
{
return interceptorProxyClasses;
@@ -109,6 +117,23 @@ public final class JavassistProxyFactory
return proxyClass;
}
+ public Object createDecoratorDelegate(OwbBean<?> bean, DelegateHandler newDelegateHandler)
+ throws Exception
+ {
+
+ Class<?> proxyClass = this.getInterceptorProxyClasses().get(bean);
+ if (proxyClass == null)
+ {
+ ProxyFactory delegateFactory = this.createProxyFactory(bean);
+ proxyClass = this.getProxyClass(delegateFactory);
+ this.getInterceptorProxyClasses().put(bean, proxyClass);
+ }
+
+ final Object delegate = proxyClass.newInstance();
+ setHandler(delegate, newDelegateHandler);
+ return delegate;
+ }
+
public Class<?> getResourceBeanProxyClass(ResourceBean<?, ?> resourceBean)
{
Class<?> proxyClass = null;
@@ -209,7 +234,52 @@ public final class JavassistProxyFactory
if (!(bean instanceof WebBeansDecorator<?>) && !(bean instanceof WebBeansInterceptor<?>))
{
- setInterceptorMethodHandler((ProxyObject) result, bean, creationalContext);
+ InterceptorHandler interceptorHandler = createInterceptorHandler(bean, creationalContext);
+
+ if (!true)
+ {
+ final Set<Type> types = bean.getTypes();
+ final Set<Class<?>> interfaceList = new HashSet<Class<?>>();
+ Class<?> superClass = null;
+ for (Type generic : types)
+ {
+ Class<?> type = ClassUtil.getClazz(generic);
+
+ if (type.isInterface())
+ {
+ interfaceList.add(type);
+ }
+
+ else if ((superClass == null) || (superClass.isAssignableFrom(type) && type != Object.class))
+ {
+ superClass = type;
+ }
+
+ }
+ if (!interfaceList.contains(Serializable.class))
+ {
+ interfaceList.add(Serializable.class);
+ }
+
+ Class<?>[] interfaceArray = new Class<?>[interfaceList.size()];
+ interfaceArray = interfaceList.toArray(interfaceArray);
+
+ if (superClass == null || superClass.equals(Object.class))
+ {
+ return Proxy.newProxyInstance(WebBeansUtil.getCurrentClassLoader(), interfaceArray,
+ interceptorHandler);
+ }
+ else
+ {
+ return AsmProxyFactory.newProxyInstance(WebBeansUtil.getCurrentClassLoader(),
+ interceptorHandler, superClass, interfaceArray);
+ }
+
+ }
+ else
+ {
+ setHandler(result, interceptorHandler);
+ }
}
}
catch (Exception e)
@@ -220,13 +290,7 @@ public final class JavassistProxyFactory
return result;
}
- /**
- * This helper method will set the correct InterceptorHandler into our proxy.
- * @param proxyObject
- * @param bean
- * @param creationalContext
- */
- private void setInterceptorMethodHandler(ProxyObject proxyObject, OwbBean<?> bean, CreationalContext<?> creationalContext)
+ private InterceptorHandler createInterceptorHandler(OwbBean<?> bean, CreationalContext<?> creationalContext)
{
InterceptorHandler interceptorHandler = null;
String scopeClassName = bean.getScope().getName();
@@ -303,9 +367,7 @@ public final class JavassistProxyFactory
e);
}
}
-
-
- proxyObject.setHandler(interceptorHandler);
+ return interceptorHandler;
}
public Object createBuildInBeanProxy(OwbBean<?> bean)
@@ -404,7 +466,7 @@ public final class JavassistProxyFactory
result = proxyClass.newInstance();
if (!(bean instanceof WebBeansDecorator<?>) && !(bean instanceof WebBeansInterceptor<?>))
{
- ((ProxyObject)result).setHandler(new DependentScopedBeanInterceptorHandler(bean, actualInstance, creastionalContext));
+ setHandler(result, new DependentScopedBeanInterceptorHandler(bean, actualInstance, creastionalContext));
}
}
@@ -482,7 +544,7 @@ public final class JavassistProxyFactory
return fact;
}
-
+
/**
* @param o the object to check
* @return <code>true</code> if the given object is a proxy
Copied: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/ProxyGenerationException.java (from r1380956, openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/ProxyGenerationException.java)
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/ProxyGenerationException.java?p2=openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/ProxyGenerationException.java&p1=openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/ProxyGenerationException.java&r1=1380956&r2=1384168&rev=1384168&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/ProxyGenerationException.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/proxy/ProxyGenerationException.java Thu Sep 13 00:34:32 2012
@@ -1,35 +1,43 @@
/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
*/
-package org.apache.openejb.util.proxy;
+package org.apache.webbeans.proxy;
-public class ProxyGenerationException extends Exception {
+public class ProxyGenerationException
+ extends Exception
+{
- public ProxyGenerationException() {
+ public ProxyGenerationException()
+ {
}
- public ProxyGenerationException(String message) {
+ public ProxyGenerationException(String message)
+ {
super(message);
}
- public ProxyGenerationException(Throwable cause) {
+ public ProxyGenerationException(Throwable cause)
+ {
super(cause);
}
- public ProxyGenerationException(String message, Throwable cause) {
+ public ProxyGenerationException(String message, Throwable cause)
+ {
super(message, cause);
}