You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by se...@apache.org on 2010/10/12 22:39:14 UTC

svn commit: r1021916 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/utils/ test/java/org/apache/cxf/jaxrs/ test/java/org/apache/cxf/jaxrs/fortest/

Author: sergeyb
Date: Tue Oct 12 20:39:13 2010
New Revision: 1021916

URL: http://svn.apache.org/viewvc?rev=1021916&view=rev
Log:
[CXF-3022] Some improvements to do with the generics support

Added:
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/GenericEntity2.java   (with props)
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/GenericEntityImpl2.java   (with props)
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/GenericEntityImpl3.java   (with props)
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/GenericEntityImpl4.java   (with props)
Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/SelectMethodCandidatesTest.java

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java?rev=1021916&r1=1021915&r2=1021916&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java Tue Oct 12 20:39:13 2010
@@ -116,6 +116,11 @@ public final class InjectionUtils {
             }
         }
         
+        Type[] bounds = var.getBounds();
+        if (bounds.length > pos && bounds[pos] != Object.class) {
+            return bounds[pos];
+        }
+                
         Type genericSubtype = serviceClass.getGenericSuperclass();
         if (genericSubtype == Object.class) {
             Type[] genInterfaces = serviceClass.getGenericInterfaces();
@@ -124,8 +129,9 @@ public final class InjectionUtils {
                 break;
             }
         }
-        return genericSubtype != Object.class ? InjectionUtils.getActualType(genericSubtype, pos)
+        Type result = genericSubtype != Object.class ? InjectionUtils.getActualType(genericSubtype, pos)
                                               : genericSubtype;
+        return result == null ? Object.class : result;
     }
     
     public static boolean invokeBooleanGetter(Object o, String name) {
@@ -202,16 +208,22 @@ public final class InjectionUtils {
             return null;
         }
         if (!ParameterizedType.class.isAssignableFrom(genericType.getClass())) {
-            Class<?> cls =  (Class<?>)genericType;
+            if (genericType instanceof TypeVariable) {
+                genericType = getType(((TypeVariable)genericType).getBounds(), pos);
+            }
+            Class<?> cls = (Class<?>)genericType;
             return cls.isArray() ? cls.getComponentType() : cls;
         }
         ParameterizedType paramType = (ParameterizedType)genericType;
-        Type[] types = paramType.getActualTypeArguments();
+        Type t = getType(paramType.getActualTypeArguments(), pos);
+        return t instanceof Class ? (Class<?>)t : getActualType(t, pos);
+    }
+    
+    public static Type getType(Type[] types, int pos) {
         if (pos >= types.length) {
             throw new RuntimeException("No type can be found at position " + pos);
         }
-        Type t = types[pos];
-        return t instanceof Class ? (Class<?>)t : getActualType(t, pos);
+        return types[pos];    
     }
     
     public static Class<?> getRawType(Type genericType) {

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java?rev=1021916&r1=1021915&r2=1021916&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java Tue Oct 12 20:39:13 2010
@@ -532,12 +532,17 @@ public final class JAXRSUtils {
         for (int i = 0; i < parameterTypes.length; i++) {
             Class<?> param = parameterTypes[i]; 
             Type genericParam = genericParameterTypes[i];
-            if (param == Object.class && genericParam instanceof TypeVariable) {
+            if (genericParam instanceof TypeVariable) {
                 genericParam = InjectionUtils.getSuperType(ori.getClassResourceInfo().getServiceClass(), 
                                                            (TypeVariable)genericParam);
+            }
+            if (param == Object.class) {
                 param = (Class)genericParam; 
+            } else if (genericParam == Object.class) {
+                genericParam = param;
             }
             
+            
             Object paramValue = processParameter(param, 
                                                  genericParam,
                                                  anns == null ? new Annotation[0] : anns[i],

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/SelectMethodCandidatesTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/SelectMethodCandidatesTest.java?rev=1021916&r1=1021915&r2=1021916&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/SelectMethodCandidatesTest.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/SelectMethodCandidatesTest.java Tue Oct 12 20:39:13 2010
@@ -30,6 +30,9 @@ import org.apache.cxf.endpoint.Endpoint;
 import org.apache.cxf.jaxrs.fortest.BookEntity;
 import org.apache.cxf.jaxrs.fortest.BookEntity2;
 import org.apache.cxf.jaxrs.fortest.GenericEntityImpl;
+import org.apache.cxf.jaxrs.fortest.GenericEntityImpl2;
+import org.apache.cxf.jaxrs.fortest.GenericEntityImpl3;
+import org.apache.cxf.jaxrs.fortest.GenericEntityImpl4;
 import org.apache.cxf.jaxrs.impl.MetadataMap;
 import org.apache.cxf.jaxrs.model.ClassResourceInfo;
 import org.apache.cxf.jaxrs.model.OperationResourceInfo;
@@ -64,6 +67,62 @@ public class SelectMethodCandidatesTest 
     }
     
     @Test
+    public void testFindFromAbstractGenericInterface2() throws Exception {
+        doTestGenericSuperType(GenericEntityImpl2.class, "POST");
+    }
+    
+    @Test
+    public void testFindFromAbstractGenericImpl3() throws Exception {
+        doTestGenericSuperType(GenericEntityImpl3.class, "POST");
+    }
+    
+    @Test
+    public void testFindFromAbstractGenericImpl4() throws Exception {
+        JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
+        sf.setResourceClasses(GenericEntityImpl4.class);
+        sf.create();
+        List<ClassResourceInfo> resources = ((JAXRSServiceImpl)sf.getService()).getClassResourceInfos();
+        String contentTypes = "text/xml";
+        String acceptContentTypes = "text/xml";
+        
+        Message m = new MessageImpl();
+        m.put(Message.CONTENT_TYPE, "text/xml");
+        Exchange ex = new ExchangeImpl();
+        ex.setInMessage(m);
+        m.setExchange(ex);
+        Endpoint e = EasyMock.createMock(Endpoint.class);
+        e.get(ProviderFactory.class.getName());
+        EasyMock.expectLastCall().andReturn(ProviderFactory.getInstance()).times(2);
+        e.get("org.apache.cxf.jaxrs.comparator");
+        EasyMock.expectLastCall().andReturn(null);
+        EasyMock.replay(e);
+        ex.put(Endpoint.class, e);
+        
+        MetadataMap<String, String> values = new MetadataMap<String, String>();
+        ClassResourceInfo resource = JAXRSUtils.selectResourceClass(resources, "/books", values,
+                                                                    m);
+        OperationResourceInfo ori = JAXRSUtils.findTargetMethod(resource, 
+                                                                m, 
+                                    "POST", values, contentTypes, 
+                                    JAXRSUtils.sortMediaTypes(acceptContentTypes),
+                                    true);
+        assertNotNull(ori);
+        assertEquals("resourceMethod needs to be selected", "postEntity",
+                     ori.getMethodToInvoke().getName());
+        
+        String value = "<Books><Book><name>The Book</name><id>2</id></Book></Books>";
+        m.setContent(InputStream.class, new ByteArrayInputStream(value.getBytes()));
+        List<Object> params = JAXRSUtils.processParameters(ori, values, m);
+        assertEquals(1, params.size());
+        List<?> books = (List<?>)params.get(0);
+        assertEquals(1, books.size());
+        Book book = (Book)books.get(0);
+        assertNotNull(book);
+        assertEquals(2L, book.getId());
+        assertEquals("The Book", book.getName());
+    }
+    
+    @Test
     public void testFindFromAbstractGenericClass3() throws Exception {
         JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
         sf.setResourceClasses(BookEntity.class);

Added: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/GenericEntity2.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/GenericEntity2.java?rev=1021916&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/GenericEntity2.java (added)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/GenericEntity2.java Tue Oct 12 20:39:13 2010
@@ -0,0 +1,26 @@
+/**
+ * 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
+ *
+ * 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.cxf.jaxrs.fortest;
+
+import org.apache.cxf.jaxrs.resources.Book;
+
+
+public interface GenericEntity2<T extends Book> {
+}

Propchange: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/GenericEntity2.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/GenericEntity2.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/GenericEntityImpl2.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/GenericEntityImpl2.java?rev=1021916&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/GenericEntityImpl2.java (added)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/GenericEntityImpl2.java Tue Oct 12 20:39:13 2010
@@ -0,0 +1,39 @@
+/**
+ * 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
+ *
+ * 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.cxf.jaxrs.fortest;
+
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+
+import org.apache.cxf.jaxrs.resources.Book;
+
+@Path("/books")
+public class GenericEntityImpl2<T extends Book> implements GenericEntity2 {
+
+    private T book;
+    
+    @POST
+    public void postEntity(T object) {
+        book = object;
+    }
+    
+    public T getEntity() {
+        return book;
+    }
+}

Propchange: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/GenericEntityImpl2.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/GenericEntityImpl2.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/GenericEntityImpl3.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/GenericEntityImpl3.java?rev=1021916&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/GenericEntityImpl3.java (added)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/GenericEntityImpl3.java Tue Oct 12 20:39:13 2010
@@ -0,0 +1,39 @@
+/**
+ * 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
+ *
+ * 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.cxf.jaxrs.fortest;
+
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+
+import org.apache.cxf.jaxrs.resources.Book;
+
+@Path("/books")
+public class GenericEntityImpl3<T extends Book> {
+
+    private T book;
+    
+    @POST
+    public void postEntity(T object) {
+        book = object;
+    }
+    
+    public T getEntity() {
+        return book;
+    }
+}

Propchange: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/GenericEntityImpl3.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/GenericEntityImpl3.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/GenericEntityImpl4.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/GenericEntityImpl4.java?rev=1021916&view=auto
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/GenericEntityImpl4.java (added)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/GenericEntityImpl4.java Tue Oct 12 20:39:13 2010
@@ -0,0 +1,41 @@
+/**
+ * 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
+ *
+ * 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.cxf.jaxrs.fortest;
+
+import java.util.List;
+
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+
+import org.apache.cxf.jaxrs.resources.Book;
+
+@Path("/books")
+public class GenericEntityImpl4<T extends Book> {
+
+    private List<T> books;
+    
+    @POST
+    public void postEntity(List<T> object) {
+        books = object;
+    }
+    
+    public List<T> getEntity() {
+        return books;
+    }
+}

Propchange: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/GenericEntityImpl4.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/fortest/GenericEntityImpl4.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date