Created
January 17, 2013 19:42
-
-
Save timw/4559000 to your computer and use it in GitHub Desktop.
Patch for http://java.net/jira/browse/JERSEY-1352 in Jersey 1.x
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/jersey/jersey-server/src/main/java/com/sun/jersey/server/impl/modelapi/annotation/IntrospectionModeller.java b/jersey/jersey-server/src/main/java/com/sun/jersey/server/impl/modelapi/annotation/IntrospectionModeller.java | |
index a141559..62c82e6 100644 | |
--- a/jersey/jersey-server/src/main/java/com/sun/jersey/server/impl/modelapi/annotation/IntrospectionModeller.java | |
+++ b/jersey/jersey-server/src/main/java/com/sun/jersey/server/impl/modelapi/annotation/IntrospectionModeller.java | |
@@ -591,8 +591,8 @@ public class IntrospectionModeller { | |
} else if (DefaultValue.class == annotation.annotationType()) { | |
paramDefault = ((DefaultValue) annotation).value(); | |
} else { | |
- // lets only clear things down if we've not found a ANOT_HELPER_MAP annotation already | |
- if (paramAnnotation == null) { | |
+ // Take latest unknown annotation, but don't override known annotation | |
+ if ((paramAnnotation == null) || (paramSource == Source.UNKNOWN)) { | |
paramAnnotation = annotation; | |
paramSource = Source.UNKNOWN; | |
paramName = getValue(annotation); | |
diff --git a/jersey/jersey-server/src/test/java/com/sun/jersey/server/impl/model/parameter/ParameterWithMultipleAnnotationsTest.java b/jersey/jersey-server/src/test/java/com/sun/jersey/server/impl/model/parameter/ParameterWithMultipleAnnotationsTest.java | |
index 84acf5e..e77cabb 100644 | |
--- a/jersey/jersey-server/src/test/java/com/sun/jersey/server/impl/model/parameter/ParameterWithMultipleAnnotationsTest.java | |
+++ b/jersey/jersey-server/src/test/java/com/sun/jersey/server/impl/model/parameter/ParameterWithMultipleAnnotationsTest.java | |
@@ -41,6 +41,7 @@ package com.sun.jersey.server.impl.model.parameter; | |
import com.sun.jersey.api.model.Parameter; | |
import com.sun.jersey.server.impl.modelapi.annotation.IntrospectionModeller; | |
+import org.junit.BeforeClass; | |
import org.junit.Test; | |
import javax.ws.rs.PathParam; | |
@@ -54,21 +55,40 @@ import static org.junit.Assert.*; | |
* Checks that Parameters work fine with multiple annotations | |
*/ | |
public class ParameterWithMultipleAnnotationsTest { | |
+ | |
+ private static Method createParameterMethod; | |
+ | |
+ @BeforeClass | |
+ public static void getCreateParameterMethod() { | |
+ Method[] methods = IntrospectionModeller.class.getDeclaredMethods(); | |
+ for (Method method : methods) { | |
+ if (method.getName().equals("createParameter")) { | |
+ createParameterMethod = method; | |
+ break; | |
+ } | |
+ } | |
+ assertNotNull("Should have found the createParameter() method on IntrospectionModeller", createParameterMethod); | |
+ createParameterMethod.setAccessible(true); | |
+ } | |
@Test | |
public void testParametersWithMultiple() throws Exception { | |
- Class<?> declaring = MyResource.class; | |
- Method processMethod = declaring.getMethod("process", String.class); | |
- | |
- // its a private method so lets use reflection to invoke it | |
- Method createParameterMethod = null; | |
- Method[] methods = IntrospectionModeller.class.getDeclaredMethods(); | |
- for (Method method : methods) { | |
- if (method.getName().equals("createParameter")) { | |
- createParameterMethod = method; | |
- } | |
- } | |
- assertNotNull("Should have found the createParameter() method on IntrospectionModeller", createParameterMethod); | |
+ checkMyResourceMethod("processTrailingUnknown"); | |
+ checkMyResourceMethod("processLeadingUnknown"); | |
+ checkMyResourceMethod("processLeadingAndTrailingUnknown"); | |
+ checkMyResourceMethod("processSingleUnknown"); | |
+ checkMyResourceMethod("processDoubleUnknown"); | |
+ } | |
+ | |
+ private void checkMyResourceMethod(String methodName) throws Exception { | |
+ Object value = invokeCreateParameterMethod(MyResource.class, methodName, String.class); | |
+ assertTrue("Should return a Parameter but found " + value, value instanceof Parameter); | |
+ Parameter parameter = (Parameter) value; | |
+ assertEquals(methodName, "correct", parameter.getSourceName()); | |
+ } | |
+ | |
+ private Object invokeCreateParameterMethod(Class<?> declaring, String methodName, Class<?>... methodParamTypes) throws Exception { | |
+ Method processMethod = declaring.getMethod(methodName, methodParamTypes); | |
/* | |
private static Parameter createParameter( | |
@@ -83,22 +103,28 @@ public class ParameterWithMultipleAnnotationsTest { | |
Class<?>[] parameterTypes = processMethod.getParameterTypes(); | |
Annotation[][] parameterAnnotations = processMethod.getParameterAnnotations(); | |
int idx = 0; | |
- createParameterMethod.setAccessible(true); | |
Object value = createParameterMethod.invoke(null, declaring, declaring, isEncoded, String.class, parameterTypes[idx], parameterAnnotations[idx]); | |
- assertTrue("Should return a Parameter but found " + value, value instanceof Parameter); | |
- Parameter parameter = (Parameter) value; | |
- assertEquals("id", parameter.getSourceName()); | |
+ return value; | |
+ } | |
+ | |
+ @java.lang.annotation.Target({java.lang.annotation.ElementType.PARAMETER, java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.FIELD}) | |
+ @java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME) | |
+ public @interface LeadAnnotation { | |
+ String value() default "lead"; | |
} | |
@java.lang.annotation.Target({java.lang.annotation.ElementType.PARAMETER, java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.FIELD}) | |
@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME) | |
- public @interface DummyAnnotation { | |
+ public @interface TrailAnnotation { | |
+ String value() default "trail"; | |
} | |
private static class MyResource { | |
- public void process(@PathParam("id") @DummyAnnotation String id) { | |
- | |
- } | |
+ public void processTrailingUnknown(@PathParam("correct") @TrailAnnotation String id) {} | |
+ public void processLeadingUnknown(@LeadAnnotation @PathParam("correct") String id) {} | |
+ public void processLeadingAndTrailingUnknown(@LeadAnnotation @PathParam("correct") @TrailAnnotation String id) {} | |
+ public void processSingleUnknown(@LeadAnnotation("correct") String id) {} | |
+ public void processDoubleUnknown(@LeadAnnotation @TrailAnnotation("correct") String id) {} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment