[jboss-cvs] JBossAS SVN: r95713 - in projects/interceptors/trunk/jboss-interceptor/src: main/java/org/jboss/interceptor/util and 2 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Wed Oct 28 18:17:55 EDT 2009
Author: marius.bogoevici
Date: 2009-10-28 18:17:55 -0400 (Wed, 28 Oct 2009)
New Revision: 95713
Added:
projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/util/proxy/TargetInstanceProxyMethodHandler.java
Modified:
projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/proxy/InterceptorMethodHandler.java
projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/proxy/InterceptorProxyCreatorImpl.java
projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/util/InterceptionUtils.java
projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/util/proxy/TargetInstanceProxy.java
projects/interceptors/trunk/jboss-interceptor/src/test/java/org/jboss/interceptors/proxy/InterceptionTest.java
Log:
Fix proxy unwrapping and test.
Modified: projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/proxy/InterceptorMethodHandler.java
===================================================================
--- projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/proxy/InterceptorMethodHandler.java 2009-10-28 21:49:47 UTC (rev 95712)
+++ projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/proxy/InterceptorMethodHandler.java 2009-10-28 22:17:55 UTC (rev 95713)
@@ -21,27 +21,26 @@
import org.jboss.interceptor.util.ReflectionUtils;
import org.jboss.interceptor.util.InterceptionUtils;
import org.jboss.interceptor.util.proxy.TargetInstanceProxy;
+import org.jboss.interceptor.util.proxy.TargetInstanceProxyMethodHandler;
import javassist.util.proxy.MethodHandler;
/**
* @author Marius Bogoevici
*/
-public class InterceptorMethodHandler implements Serializable, TargetInstanceProxy<Object>
+public class InterceptorMethodHandler extends TargetInstanceProxyMethodHandler implements Serializable
{
private static ThreadLocal<Set<MethodHolder>> interceptionStack = new ThreadLocal<Set<MethodHolder>>();
- private final Object target;
private Map<Object, InterceptionHandler> interceptorHandlerInstances = new HashMap<Object, InterceptionHandler>();
- private Class<?> targetClazz;
private InterceptorClassMetadata targetClassInterceptorMetadata;
private List<InterceptionModel<Class<?>, ?>> interceptionModels;
public InterceptorMethodHandler(Object target, Class<?> targetClass, List<InterceptionModel<Class<?>, ?>> interceptionModels, List<InterceptionHandlerFactory<?>> interceptionHandlerFactories)
{
-
+ super(target, targetClass != null? targetClass: target.getClass());
if (interceptionModels == null)
throw new IllegalArgumentException("Interception model must not be null");
@@ -55,16 +54,6 @@
this.interceptionModels = interceptionModels;
- if (target == null)
- this.target = this;
- else
- this.target = target;
-
- if (targetClass != null)
- this.targetClazz = targetClass;
- else
- this.targetClazz = this.target.getClass();
-
for (int i = 0; i < interceptionModels.size(); i++)
{
for (Object interceptorReference : this.interceptionModels.get(i).getAllInterceptors())
@@ -72,24 +61,14 @@
interceptorHandlerInstances.put(interceptorReference, ((InterceptionHandlerFactory) interceptionHandlerFactories.get(i)).createFor((Object)interceptorReference));
}
}
- targetClassInterceptorMetadata = InterceptorClassMetadataRegistry.getRegistry().getInterceptorClassMetadata(targetClazz);
+ targetClassInterceptorMetadata = InterceptorClassMetadataRegistry.getRegistry().getInterceptorClassMetadata(getTargetClass());
}
- public Object getTargetInstance()
+ public Object doInvoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable
{
- return target;
- }
-
- public Class<?> getTargetClass()
- {
- return targetClazz;
- }
-
- public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable
- {
ReflectionUtils.ensureAccessible(thisMethod);
if (getInterceptionStack().contains(MethodHolder.of(thisMethod, true)))
- return thisMethod.invoke(target, args);
+ return thisMethod.invoke(getTargetInstance(), args);
try
{
getInterceptionStack().add(MethodHolder.of(thisMethod, true));
@@ -97,7 +76,7 @@
if (!thisMethod.getDeclaringClass().equals(LifecycleMixin.class))
{
if (!org.jboss.interceptor.util.InterceptionUtils.isInterceptionCandidate(thisMethod))
- return thisMethod.invoke(target, args);
+ return thisMethod.invoke(getTargetInstance(), args);
if (InterceptionTypeRegistry.supportsTimeoutMethods() && thisMethod.isAnnotationPresent(InterceptionTypeRegistry.TIMEOUT_ANNOTATION_CLASS))
return executeInterception(thisMethod, args, InterceptionType.AROUND_TIMEOUT);
else
@@ -144,11 +123,11 @@
if (targetClassInterceptorMetadata.getInterceptorMethods(interceptionType) != null && !targetClassInterceptorMetadata.getInterceptorMethods(interceptionType).isEmpty())
{
- interceptionHandlers.add(new DirectClassInterceptionHandler<Class<?>>(target, targetClazz));
+ interceptionHandlers.add(new DirectClassInterceptionHandler<Class<?>>(getTargetInstance(), getTargetClass()));
}
- InterceptionChain chain = new InterceptionChain(interceptionHandlers, interceptionType, target, thisMethod, args);
- return chain.invokeNext(new InterceptorInvocationContext(chain, target, thisMethod, args));
+ InterceptionChain chain = new InterceptionChain(interceptionHandlers, interceptionType, getTargetInstance(), thisMethod, args);
+ return chain.invokeNext(new InterceptorInvocationContext(chain, getTargetInstance(), thisMethod, args));
}
private void writeObject(ObjectOutputStream objectOutputStream) throws IOException
Modified: projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/proxy/InterceptorProxyCreatorImpl.java
===================================================================
--- projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/proxy/InterceptorProxyCreatorImpl.java 2009-10-28 21:49:47 UTC (rev 95712)
+++ projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/proxy/InterceptorProxyCreatorImpl.java 2009-10-28 22:17:55 UTC (rev 95713)
@@ -27,6 +27,7 @@
import org.jboss.interceptor.registry.InterceptorClassMetadataRegistry;
import org.jboss.interceptor.InterceptorException;
import org.jboss.interceptor.util.InterceptionUtils;
+import org.jboss.interceptor.util.proxy.TargetInstanceProxy;
import javax.interceptor.AroundInvoke;
import java.lang.reflect.Method;
@@ -64,7 +65,7 @@
if (proxyClass != null)
proxyFactory.setSuperclass(proxyClass);
- proxyFactory.setInterfaces(new Class<?>[]{LifecycleMixin.class});
+ proxyFactory.setInterfaces(new Class<?>[]{LifecycleMixin.class, TargetInstanceProxy.class});
InterceptorMethodHandler interceptorMethodHandler = new InterceptorMethodHandler(target, proxyClass, getModelsFor(proxyClass), interceptionHandlerFactories);
proxyFactory.setHandler(interceptorMethodHandler);
Modified: projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/util/InterceptionUtils.java
===================================================================
--- projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/util/InterceptionUtils.java 2009-10-28 21:49:47 UTC (rev 95712)
+++ projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/util/InterceptionUtils.java 2009-10-28 22:17:55 UTC (rev 95713)
@@ -38,6 +38,8 @@
import java.util.List;
import java.util.Collections;
+import javassist.util.proxy.ProxyObject;
+
/**
* @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
*/
Modified: projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/util/proxy/TargetInstanceProxy.java
===================================================================
--- projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/util/proxy/TargetInstanceProxy.java 2009-10-28 21:49:47 UTC (rev 95712)
+++ projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/util/proxy/TargetInstanceProxy.java 2009-10-28 22:17:55 UTC (rev 95713)
@@ -5,7 +5,7 @@
/**
* @author <a href="mailto:mariusb at redhat.com">Marius Bogoevici</a>
*/
-public interface TargetInstanceProxy<T> extends MethodHandler
+public interface TargetInstanceProxy<T>
{
T getTargetInstance();
Added: projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/util/proxy/TargetInstanceProxyMethodHandler.java
===================================================================
--- projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/util/proxy/TargetInstanceProxyMethodHandler.java (rev 0)
+++ projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/util/proxy/TargetInstanceProxyMethodHandler.java 2009-10-28 22:17:55 UTC (rev 95713)
@@ -0,0 +1,76 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat, Inc. and/or its affiliates, and individual
+ * contributors by the @authors tag. See the copyright.txt in the
+ * distribution for a full listing of individual contributors.
+ *
+ * Licensed 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
+ * 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.jboss.interceptor.util.proxy;
+
+import java.lang.reflect.Method;
+import java.io.Serializable;
+
+import javassist.util.proxy.MethodHandler;
+
+/**
+ * @author Marius Bogoevici
+ */
+public abstract class TargetInstanceProxyMethodHandler<T> implements MethodHandler, Serializable
+{
+ private T targetInstance;
+
+ private Class<? extends T> targetClass;
+
+ public TargetInstanceProxyMethodHandler(T targetInstance, Class<? extends T> targetClass)
+ {
+ this.targetInstance = targetInstance;
+ this.targetClass = targetClass;
+ }
+
+ public final Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable
+ {
+ if (thisMethod.getDeclaringClass().equals(TargetInstanceProxy.class))
+ {
+ if (thisMethod.getName().equals("getTargetInstance"))
+ {
+ return this.getTargetInstance();
+ }
+ else if (thisMethod.getName().equals("getTargetClass"))
+ {
+ return this.getTargetClass();
+ }
+ else
+ {
+ // we shouldn't arrive here
+ return null;
+ }
+ }
+ else
+ {
+ return doInvoke(self, thisMethod, proceed, args);
+ }
+ }
+
+ protected abstract Object doInvoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable;
+
+ public T getTargetInstance()
+ {
+ return targetInstance;
+ }
+
+ public Class<? extends T> getTargetClass()
+ {
+ return targetClass;
+ }
+}
+
Modified: projects/interceptors/trunk/jboss-interceptor/src/test/java/org/jboss/interceptors/proxy/InterceptionTest.java
===================================================================
--- projects/interceptors/trunk/jboss-interceptor/src/test/java/org/jboss/interceptors/proxy/InterceptionTest.java 2009-10-28 21:49:47 UTC (rev 95712)
+++ projects/interceptors/trunk/jboss-interceptor/src/test/java/org/jboss/interceptors/proxy/InterceptionTest.java 2009-10-28 22:17:55 UTC (rev 95713)
@@ -77,6 +77,13 @@
"org.jboss.interceptors.proxy.InterceptionTest$MySecondInterceptor_aroundInvokeAfter",
"org.jboss.interceptors.proxy.InterceptionTest$MyFirstInterceptor_aroundInvokeAfter",
};
+
+ private String[] expectedLoggedValuesWhenRaw = {
+ "org.jboss.interceptors.proxy.FootballTeam_getName",
+ };
+
+
+
private InterceptionModel<Class<?>, Class<?>> interceptionModel;
private InterceptorRegistry<Class<?>, Class<?>> interceptorRegistry;
@@ -146,7 +153,8 @@
Assert.assertEquals(TEAM_NAME, proxy.getName());
InterceptionUtils.executePredestroy(proxy);
Object[] logValues = InterceptorTestLogger.getLog().toArray();
- Assert.assertArrayEquals(iterateAndDisplay(logValues), expectedLoggedValues, logValues);
+ Assert.assertArrayEquals(iterateAndDisplay(logValues), expectedLoggedValues, logValues);
+ assertRawObject(proxy);
}
@Test
@@ -157,6 +165,7 @@
InterceptionUtils.executePostConstruct(proxy);
Assert.assertEquals(TEAM_NAME, proxy.getName());
InterceptionUtils.executePredestroy(proxy);
+ assertRawObject(proxy);
}
@Test
@@ -169,7 +178,7 @@
InterceptionUtils.executePredestroy(proxy);
Object[] logValues = InterceptorTestLogger.getLog().toArray();
Assert.assertArrayEquals(iterateAndDisplay(logValues), expectedLoggedValues, logValues);
-
+ assertRawObject(proxy);
}
@Test
@@ -182,6 +191,7 @@
InterceptionUtils.executePredestroy(proxy);
Object[] logValues = InterceptorTestLogger.getLog().toArray();
Assert.assertArrayEquals(iterateAndDisplay(logValues), expectedLoggedValuesWithGlobalsIgnored, logValues);
+ assertRawObject(proxy);
}
@@ -196,8 +206,18 @@
Assert.assertEquals(TEAM_NAME, proxy.getName());
Object[] logValues = InterceptorTestLogger.getLog().toArray();
Assert.assertArrayEquals(iterateAndDisplay(logValues), expectedLoggedValuesOnSerialization, logValues);
+ assertRawObject(proxy);
}
+ public void assertRawObject(FootballTeam proxy)
+ {
+ InterceptorTestLogger.reset();
+ FootballTeam rawInstance = InterceptionUtils.getRawInstance(proxy);
+ Assert.assertEquals(TEAM_NAME, rawInstance.getName());
+ Object[] logValues = InterceptorTestLogger.getLog().toArray();
+ Assert.assertArrayEquals(iterateAndDisplay(logValues), expectedLoggedValuesWhenRaw, logValues);
+ }
+
private String iterateAndDisplay(Object[] logValues)
{
StringBuffer buffer = new StringBuffer();
More information about the jboss-cvs-commits
mailing list