Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit dce250e

Browse files
author
Adam Lindenthal
committed
Merge pull request #90 from ccronemberger/master
Make the method really search for the @path annotation in the ancestor h...
2 parents 7163b4f + 96bed1c commit dce250e

File tree

2 files changed

+102
-8
lines changed

2 files changed

+102
-8
lines changed

core-server/src/main/java/org/glassfish/jersey/server/model/internal/ModelHelper.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2013-2014 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -46,6 +46,7 @@
4646
* Common model helper methods.
4747
*
4848
* @author Michal Gajdos (michal.gajdos at oracle.com)
49+
* @author Constantino Cronemberger (ccronemberger at yahoo.com.br)
4950
*/
5051
public final class ModelHelper {
5152

@@ -58,15 +59,20 @@ public final class ModelHelper {
5859
* annotation.
5960
*/
6061
public static Class<?> getAnnotatedResourceClass(Class<?> resourceClass) {
61-
if (resourceClass.isAnnotationPresent(Path.class)) {
62-
return resourceClass;
63-
}
6462

65-
for (Class<?> i : resourceClass.getInterfaces()) {
66-
if (i.isAnnotationPresent(Path.class)) {
67-
return i;
63+
// traverse the class hierarchy to find the annotation
64+
Class<?> cls = resourceClass;
65+
do {
66+
if (cls.isAnnotationPresent(Path.class)) {
67+
return cls;
6868
}
69-
}
69+
70+
for (Class<?> i : cls.getInterfaces()) {
71+
if (i.isAnnotationPresent(Path.class)) {
72+
return i;
73+
}
74+
}
75+
} while ((cls = cls.getSuperclass()) != null);
7076

7177
return resourceClass;
7278
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013-2014 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.glassfish.jersey.server.model.internal;
41+
42+
import org.junit.Assert;
43+
import org.junit.Test;
44+
45+
import javax.ws.rs.Path;
46+
import java.lang.reflect.InvocationHandler;
47+
import java.lang.reflect.Method;
48+
import java.lang.reflect.Proxy;
49+
50+
/**
51+
* @author Constantino Cronemberger (ccronemberger at yahoo.com.br)
52+
*/
53+
public class ModelHelperTest {
54+
55+
@Test
56+
public void testClass() {
57+
Class cls = ModelHelper.getAnnotatedResourceClass(MyAnnotatedClass.class);
58+
Assert.assertSame(MyAnnotatedClass.class, cls);
59+
}
60+
61+
@Test
62+
public void testSubClass() {
63+
// Spring with CGLIB proxies creates sub-classes
64+
Object obj = new MyAnnotatedClass() {};
65+
Assert.assertNotSame(MyAnnotatedClass.class,obj.getClass());
66+
Class cls = ModelHelper.getAnnotatedResourceClass(obj.getClass());
67+
Assert.assertSame(MyAnnotatedClass.class, cls);
68+
}
69+
70+
@Test
71+
public void testProxyClass() throws Exception {
72+
// Spring can also create proxies for beans
73+
Object obj = Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] {MyServiceInterface.class}, new InvocationHandler() {
74+
@Override
75+
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
76+
return null;
77+
}
78+
});
79+
Class cls = ModelHelper.getAnnotatedResourceClass(obj.getClass());
80+
Assert.assertSame(MyServiceInterface.class, cls);
81+
}
82+
83+
@Path("test")
84+
public static interface MyServiceInterface {}
85+
86+
@Path("test")
87+
public static class MyAnnotatedClass implements MyServiceInterface {}
88+
}

0 commit comments

Comments
 (0)