[jboss-cvs] JBossAS SVN: r96154 - in projects/interceptors/trunk/jboss-interceptor/src: test/java/org/jboss/interceptors/proxy and 1 other directory.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Mon Nov 9 02:22:13 EST 2009
Author: marius.bogoevici
Date: 2009-11-09 02:22:12 -0500 (Mon, 09 Nov 2009)
New Revision: 96154
Added:
projects/interceptors/trunk/jboss-interceptor/src/test/java/org/jboss/interceptors/proxy/ParameterOverridingInterceptorWithInteger.java
projects/interceptors/trunk/jboss-interceptor/src/test/java/org/jboss/interceptors/proxy/ParameterOverridingInterceptorWithLong.java
projects/interceptors/trunk/jboss-interceptor/src/test/java/org/jboss/interceptors/proxy/ParameterOverridingInterceptorWithLongArray.java
Modified:
projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/proxy/InterceptorInvocationContext.java
projects/interceptors/trunk/jboss-interceptor/src/test/java/org/jboss/interceptors/proxy/FootballTeam.java
projects/interceptors/trunk/jboss-interceptor/src/test/java/org/jboss/interceptors/proxy/InterceptionTest.java
Log:
fix method parameter conversion rules
Modified: projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/proxy/InterceptorInvocationContext.java
===================================================================
--- projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/proxy/InterceptorInvocationContext.java 2009-11-09 07:16:30 UTC (rev 96153)
+++ projects/interceptors/trunk/jboss-interceptor/src/main/java/org/jboss/interceptor/proxy/InterceptorInvocationContext.java 2009-11-09 07:22:12 UTC (rev 96154)
@@ -20,6 +20,10 @@
import org.jboss.interceptor.InterceptorException;
import javax.interceptor.InvocationContext;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.lang.reflect.Method;
@@ -41,7 +45,34 @@
private InterceptionChain interceptionChain;
private Object timer;
+ private static List<Class<?>> WIDENING_SEQUENCE = Arrays.<Class<?>>asList(byte.class, short.class, int.class, long.class, float.class, double.class);
+ private static Map<Class<?>, Class<?>> WRAPPER_CLASSES;
+ private static Map<Class<?>, Class<?>> REVERSE_WRAPPER_CLASSES;
+
+ static
+ {
+ WRAPPER_CLASSES = new HashMap<Class<?>, Class<?>>();
+ WRAPPER_CLASSES.put(boolean.class, Boolean.class);
+ WRAPPER_CLASSES.put(byte.class, Byte.class);
+ WRAPPER_CLASSES.put(char.class, Character.class);
+ WRAPPER_CLASSES.put(short.class, Short.class);
+ WRAPPER_CLASSES.put(int.class, Integer.class);
+ WRAPPER_CLASSES.put(long.class, Long.class);
+ WRAPPER_CLASSES.put(float.class, Float.class);
+ WRAPPER_CLASSES.put(double.class, Double.class);
+
+ WRAPPER_CLASSES = Collections.unmodifiableMap(WRAPPER_CLASSES);
+
+ REVERSE_WRAPPER_CLASSES = new HashMap<Class<?>, Class<?>>();
+ for (Map.Entry<Class<?>, Class<?>> classEntry: WRAPPER_CLASSES.entrySet())
+ {
+ REVERSE_WRAPPER_CLASSES.put(classEntry.getValue(), classEntry.getKey());
+ }
+
+ REVERSE_WRAPPER_CLASSES = Collections.unmodifiableMap(REVERSE_WRAPPER_CLASSES);
+ }
+
public InterceptorInvocationContext(InterceptionChain interceptionChain, Object target, Method targetMethod, Object[] parameters)
{
this.interceptionChain = interceptionChain;
@@ -96,6 +127,36 @@
}
}
+ private static boolean isWideningPrimitive(Class argumentClass, Class targetClass)
+ {
+ int argumentClassIndex = WIDENING_SEQUENCE.indexOf(argumentClass);
+ return argumentClassIndex >= 0 && WIDENING_SEQUENCE.indexOf(targetClass) >= argumentClassIndex;
+ }
+
+ private static Class<?> getWrapperClass(Class<?> primitiveClass)
+ {
+ if (!WRAPPER_CLASSES.containsKey(primitiveClass))
+ {
+ return primitiveClass;
+ }
+ else
+ {
+ return WRAPPER_CLASSES.get(primitiveClass);
+ }
+ }
+
+ private static Class<?> getPrimitiveClass(Class<?> wrapperClass)
+ {
+ if (!REVERSE_WRAPPER_CLASSES.containsKey(wrapperClass))
+ {
+ return wrapperClass;
+ }
+ else
+ {
+ return REVERSE_WRAPPER_CLASSES.get(wrapperClass);
+ }
+ }
+
public void setParameters(Object[] params)
{
if (method != null)
@@ -110,16 +171,59 @@
{
for (int i=0; i<params.length; i++)
{
- Class<?> parameterClass = method.getParameterTypes()[i];
+ Class<?> methodParameterClass = method.getParameterTypes()[i];
if (params[i] != null)
{
- if (!method.getParameterTypes()[i].isAssignableFrom(params[i].getClass()))
+ //identity ok
+ Class<? extends Object> newArgumentClass = params[i].getClass();
+ if (newArgumentClass.equals(methodParameterClass))
+ break;
+ if (newArgumentClass.isPrimitive())
{
- throw new IllegalArgumentException("Incompatible parameter: " + params[i] + " (expected type was " + method.getParameterTypes()[i].getName() + ")");
+ // argument is primitive - never actually a case for interceptors
+ if (methodParameterClass.isPrimitive())
+ {
+ //widening primitive
+ if (!isWideningPrimitive(newArgumentClass, methodParameterClass))
+ {
+ throw new IllegalArgumentException("Incompatible parameter type on position: " + i + " :" + newArgumentClass + " (expected type was " + methodParameterClass.getName() + ")");
+ }
+ }
+ else
+ {
+ //boxing+widening reference
+ Class<?> boxedArgumentClass = getWrapperClass(newArgumentClass);
+ if (!methodParameterClass.isAssignableFrom(boxedArgumentClass))
+ {
+ throw new IllegalArgumentException("Incompatible parameter type on position: " + i + " :" + newArgumentClass + " (expected type was " + methodParameterClass.getName() + ")");
+ }
+ }
}
+ else
+ {
+ // argument is non-primitive
+ if (methodParameterClass.isPrimitive())
+ {
+ // unboxing+widening primitive
+ Class<?> unboxedClass = getPrimitiveClass(newArgumentClass);
+ if (!isWideningPrimitive(unboxedClass, methodParameterClass))
+ {
+ throw new IllegalArgumentException("Incompatible parameter type on position: " + i + " :" + newArgumentClass + " (expected type was " + methodParameterClass.getName() + ")");
+ }
+ }
+ else
+ {
+ //widening reference
+ if (!methodParameterClass.isAssignableFrom(newArgumentClass))
+ {
+ throw new IllegalArgumentException("Incompatible parameter type on position: " + i + " :" + newArgumentClass + " (expected type was " + methodParameterClass.getName() + ")");
+ }
+ }
+ }
}
else
{
+ // null is never acceptable on a primitive type
if (method.getParameterTypes()[i].isPrimitive())
{
throw new IllegalArgumentException("Trying to set a null value on a " + method.getParameterTypes()[i].getName());
Modified: projects/interceptors/trunk/jboss-interceptor/src/test/java/org/jboss/interceptors/proxy/FootballTeam.java
===================================================================
--- projects/interceptors/trunk/jboss-interceptor/src/test/java/org/jboss/interceptors/proxy/FootballTeam.java 2009-11-09 07:16:30 UTC (rev 96153)
+++ projects/interceptors/trunk/jboss-interceptor/src/test/java/org/jboss/interceptors/proxy/FootballTeam.java 2009-11-09 07:22:12 UTC (rev 96154)
@@ -58,6 +58,31 @@
return vb.getValue();
}
+ public int echoInt(int i)
+ {
+ return i;
+ }
+
+ public long echoLong(long i)
+ {
+ return i;
+ }
+
+ public Long echoLongAsObject(Long i)
+ {
+ return i;
+ }
+
+ public Object[] echoObjectArray(Object[] i)
+ {
+ return i;
+ }
+
+ public String[] echoStringArray(String[] i)
+ {
+ return i;
+ }
+
@PrePassivate
public void beforePassivating()
{
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-11-09 07:16:30 UTC (rev 96153)
+++ projects/interceptors/trunk/jboss-interceptor/src/test/java/org/jboss/interceptors/proxy/InterceptionTest.java 2009-11-09 07:22:12 UTC (rev 96154)
@@ -21,6 +21,7 @@
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.ByteArrayInputStream;
+import java.lang.reflect.Array;
import org.jboss.interceptor.model.InterceptionModelBuilder;
import org.jboss.interceptor.model.InterceptionModel;
@@ -221,6 +222,118 @@
}
@Test
+ public void testMethodParameterOverridingWithPrimitive() throws Exception
+ {
+ InterceptorTestLogger.reset();
+
+ InterceptionModelBuilder<Class<?>, Class<?>> builder = InterceptionModelBuilder.newBuilderFor(FootballTeam.class, (Class) Class.class);
+
+ builder.interceptAroundInvoke(FootballTeam.class.getMethod("echoInt", int.class)).with(ParameterOverridingInterceptorWithInteger.class);
+ interceptionModel = builder.build();
+ this.interceptorRegistry = new InterceptorRegistry<Class<?>, Class<?>>();
+ this.interceptorRegistry.registerInterceptionModel(FootballTeam.class, interceptionModel);
+
+ FootballTeam proxy = InterceptionUtils.proxifyInstance(new FootballTeam(TEAM_NAME), FootballTeam.class, interceptorRegistry, new DirectClassInterceptionHandlerFactory());
+ Assert.assertEquals(42, proxy.echoInt(1));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testMethodParameterOverridingWithObject() throws Exception
+ {
+ InterceptorTestLogger.reset();
+
+ InterceptionModelBuilder<Class<?>, Class<?>> builder = InterceptionModelBuilder.newBuilderFor(FootballTeam.class, (Class) Class.class);
+
+ builder.interceptAroundInvoke(FootballTeam.class.getMethod("echoLongAsObject", Long.class)).with(ParameterOverridingInterceptorWithInteger.class);
+ interceptionModel = builder.build();
+ this.interceptorRegistry = new InterceptorRegistry<Class<?>, Class<?>>();
+ this.interceptorRegistry.registerInterceptionModel(FootballTeam.class, interceptionModel);
+
+ FootballTeam proxy = InterceptionUtils.proxifyInstance(new FootballTeam(TEAM_NAME), FootballTeam.class, interceptorRegistry, new DirectClassInterceptionHandlerFactory());
+ Assert.assertEquals(new Long(42), proxy.echoLongAsObject(1l));
+ }
+
+ @Test
+ public void testMethodParameterOverridingWithObjectSucceed() throws Exception
+ {
+ InterceptorTestLogger.reset();
+
+ InterceptionModelBuilder<Class<?>, Class<?>> builder = InterceptionModelBuilder.newBuilderFor(FootballTeam.class, (Class) Class.class);
+
+ builder.interceptAroundInvoke(FootballTeam.class.getMethod("echoLongAsObject", Long.class)).with(ParameterOverridingInterceptorWithLong.class);
+ interceptionModel = builder.build();
+ this.interceptorRegistry = new InterceptorRegistry<Class<?>, Class<?>>();
+ this.interceptorRegistry.registerInterceptionModel(FootballTeam.class, interceptionModel);
+
+ FootballTeam proxy = InterceptionUtils.proxifyInstance(new FootballTeam(TEAM_NAME), FootballTeam.class, interceptorRegistry, new DirectClassInterceptionHandlerFactory());
+ Assert.assertEquals(new Long(42), proxy.echoLongAsObject(1l));
+ }
+
+ @Test
+ public void testMethodParameterOverridingWithPrimitiveWidening() throws Exception
+ {
+ InterceptorTestLogger.reset();
+
+ InterceptionModelBuilder<Class<?>, Class<?>> builder = InterceptionModelBuilder.newBuilderFor(FootballTeam.class, (Class) Class.class);
+
+ builder.interceptAroundInvoke(FootballTeam.class.getMethod("echoLong", long.class)).with(ParameterOverridingInterceptorWithInteger.class);
+ interceptionModel = builder.build();
+ this.interceptorRegistry = new InterceptorRegistry<Class<?>, Class<?>>();
+ this.interceptorRegistry.registerInterceptionModel(FootballTeam.class, interceptionModel);
+
+ FootballTeam proxy = InterceptionUtils.proxifyInstance(new FootballTeam(TEAM_NAME), FootballTeam.class, interceptorRegistry, new DirectClassInterceptionHandlerFactory());
+ Assert.assertEquals(42, proxy.echoLong(1));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testMethodParameterOverridingWithPrimitiveNarrowing() throws Exception
+ {
+ InterceptorTestLogger.reset();
+
+ InterceptionModelBuilder<Class<?>, Class<?>> builder = InterceptionModelBuilder.newBuilderFor(FootballTeam.class, (Class) Class.class);
+
+ builder.interceptAroundInvoke(FootballTeam.class.getMethod("echoInt", int.class)).with(ParameterOverridingInterceptorWithLong.class);
+ interceptionModel = builder.build();
+ this.interceptorRegistry = new InterceptorRegistry<Class<?>, Class<?>>();
+ this.interceptorRegistry.registerInterceptionModel(FootballTeam.class, interceptionModel);
+
+ FootballTeam proxy = InterceptionUtils.proxifyInstance(new FootballTeam(TEAM_NAME), FootballTeam.class, interceptorRegistry, new DirectClassInterceptionHandlerFactory());
+ Assert.assertEquals(42, proxy.echoInt(1));
+ }
+
+ @Test
+ public void testMethodParameterOverridingWithArray() throws Exception
+ {
+ InterceptorTestLogger.reset();
+
+ InterceptionModelBuilder<Class<?>, Class<?>> builder = InterceptionModelBuilder.newBuilderFor(FootballTeam.class, (Class) Class.class);
+
+ builder.interceptAroundInvoke(FootballTeam.class.getMethod("echoObjectArray", Object[].class)).with(ParameterOverridingInterceptorWithLongArray.class);
+ interceptionModel = builder.build();
+ this.interceptorRegistry = new InterceptorRegistry<Class<?>, Class<?>>();
+ this.interceptorRegistry.registerInterceptionModel(FootballTeam.class, interceptionModel);
+
+ FootballTeam proxy = InterceptionUtils.proxifyInstance(new FootballTeam(TEAM_NAME), FootballTeam.class, interceptorRegistry, new DirectClassInterceptionHandlerFactory());
+ Assert.assertEquals(new Long[]{42l}, proxy.echoObjectArray(new Object[]{}));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testMethodParameterOverridingWithArrayOnString() throws Exception
+ {
+ InterceptorTestLogger.reset();
+
+ InterceptionModelBuilder<Class<?>, Class<?>> builder = InterceptionModelBuilder.newBuilderFor(FootballTeam.class, (Class) Class.class);
+
+ builder.interceptAroundInvoke(FootballTeam.class.getMethod("echoStringArray", String[].class)).with(ParameterOverridingInterceptorWithLongArray.class);
+ interceptionModel = builder.build();
+ this.interceptorRegistry = new InterceptorRegistry<Class<?>, Class<?>>();
+ this.interceptorRegistry.registerInterceptionModel(FootballTeam.class, interceptionModel);
+
+ FootballTeam proxy = InterceptionUtils.proxifyInstance(new FootballTeam(TEAM_NAME), FootballTeam.class, interceptorRegistry, new DirectClassInterceptionHandlerFactory());
+ Assert.assertEquals(new Long[]{42l}, proxy.echoStringArray(new String[]{}));
+ }
+
+ @Test
public void testMethodParameterOverridingWithSubclass() throws Exception
{
InterceptorTestLogger.reset();
Copied: projects/interceptors/trunk/jboss-interceptor/src/test/java/org/jboss/interceptors/proxy/ParameterOverridingInterceptorWithInteger.java (from rev 96145, projects/interceptors/trunk/jboss-interceptor/src/test/java/org/jboss/interceptors/proxy/ParameterOverridingInterceptor2.java)
===================================================================
--- projects/interceptors/trunk/jboss-interceptor/src/test/java/org/jboss/interceptors/proxy/ParameterOverridingInterceptorWithInteger.java (rev 0)
+++ projects/interceptors/trunk/jboss-interceptor/src/test/java/org/jboss/interceptors/proxy/ParameterOverridingInterceptorWithInteger.java 2009-11-09 07:22:12 UTC (rev 96154)
@@ -0,0 +1,34 @@
+/*
+ * 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.interceptors.proxy;
+
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.InvocationContext;
+
+/**
+ * @author Marius Bogoevici
+ */
+public class ParameterOverridingInterceptorWithInteger
+{
+ @AroundInvoke
+ public Object overrideParameters(InvocationContext invocationContext) throws Exception
+ {
+ invocationContext.setParameters(new Integer[]{42});
+ return invocationContext.proceed();
+ }
+}
\ No newline at end of file
Added: projects/interceptors/trunk/jboss-interceptor/src/test/java/org/jboss/interceptors/proxy/ParameterOverridingInterceptorWithLong.java
===================================================================
--- projects/interceptors/trunk/jboss-interceptor/src/test/java/org/jboss/interceptors/proxy/ParameterOverridingInterceptorWithLong.java (rev 0)
+++ projects/interceptors/trunk/jboss-interceptor/src/test/java/org/jboss/interceptors/proxy/ParameterOverridingInterceptorWithLong.java 2009-11-09 07:22:12 UTC (rev 96154)
@@ -0,0 +1,34 @@
+/*
+ * 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.interceptors.proxy;
+
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.InvocationContext;
+
+/**
+ * @author Marius Bogoevici
+ */
+public class ParameterOverridingInterceptorWithLong
+{
+ @AroundInvoke
+ public Object overrideParameters(InvocationContext invocationContext) throws Exception
+ {
+ invocationContext.setParameters(new Long[]{42l});
+ return invocationContext.proceed();
+ }
+}
\ No newline at end of file
Added: projects/interceptors/trunk/jboss-interceptor/src/test/java/org/jboss/interceptors/proxy/ParameterOverridingInterceptorWithLongArray.java
===================================================================
--- projects/interceptors/trunk/jboss-interceptor/src/test/java/org/jboss/interceptors/proxy/ParameterOverridingInterceptorWithLongArray.java (rev 0)
+++ projects/interceptors/trunk/jboss-interceptor/src/test/java/org/jboss/interceptors/proxy/ParameterOverridingInterceptorWithLongArray.java 2009-11-09 07:22:12 UTC (rev 96154)
@@ -0,0 +1,34 @@
+/*
+ * 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.interceptors.proxy;
+
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.InvocationContext;
+
+/**
+ * @author Marius Bogoevici
+ */
+public class ParameterOverridingInterceptorWithLongArray
+{
+ @AroundInvoke
+ public Object overrideParameters(InvocationContext invocationContext) throws Exception
+ {
+ invocationContext.setParameters(new Long[][]{new Long[]{42l}});
+ return invocationContext.proceed();
+ }
+}
\ No newline at end of file
More information about the jboss-cvs-commits
mailing list