Author: aparfonov
Date: 2009-09-02 12:17:17 -0400 (Wed, 02 Sep 2009)
New Revision: 129
Modified:
ws/trunk/rest/core/src/main/java/org/exoplatform/services/rest/impl/FieldInjectorImpl.java
ws/trunk/rest/core/src/main/java/org/exoplatform/services/rest/impl/resource/AbstractResourceDescriptorImpl.java
ws/trunk/rest/core/src/main/java/org/exoplatform/services/rest/impl/resource/ResourceMethodDescriptorImpl.java
ws/trunk/rest/core/src/main/java/org/exoplatform/services/rest/impl/resource/SubResourceLocatorDescriptorImpl.java
ws/trunk/rest/core/src/main/java/org/exoplatform/services/rest/impl/resource/SubResourceMethodDescriptorImpl.java
ws/trunk/rest/core/src/test/java/org/exoplatform/services/rest/impl/RequestDispatcherTest.java
ws/trunk/rest/core/src/test/java/org/exoplatform/services/rest/impl/resource/ResourceDescriptorTest.java
Log:
EXOJCR-129 : get and process fields from super class
Modified:
ws/trunk/rest/core/src/main/java/org/exoplatform/services/rest/impl/FieldInjectorImpl.java
===================================================================
---
ws/trunk/rest/core/src/main/java/org/exoplatform/services/rest/impl/FieldInjectorImpl.java 2009-09-02
10:13:55 UTC (rev 128)
+++
ws/trunk/rest/core/src/main/java/org/exoplatform/services/rest/impl/FieldInjectorImpl.java 2009-09-02
16:17:17 UTC (rev 129)
@@ -64,16 +64,6 @@
private final Annotation annotation;
/**
- * Parameter type. See {@link Constructor#getGenericParameterTypes()} .
- */
- private final Type type;
-
- /**
- * Parameter class. See {@link Constructor#getParameterTypes()}
- */
- private final Class<?> clazz;
-
- /**
* Default value for this parameter, default value can be used if there is not
* found required parameter in request. See {@link javax.ws.rs.DefaultValue}.
*/
@@ -84,11 +74,10 @@
*/
private final boolean encoded;
- /**
- * Field name, see {@link java.lang.reflect.Field#getName()}.
- */
- private final String name;
+ /** See {@link java.lang.reflect.Field} . */
+ private final java.lang.reflect.Field jfield;
+
/**
* @param resourceClass class that contains field <tt>jfield</tt>
* @param jfield java.lang.reflect.Field
@@ -96,10 +85,8 @@
public FieldInjectorImpl(Class<?> resourceClass, java.lang.reflect.Field
jfield)
{
- this.name = jfield.getName();
+ this.jfield = jfield;
this.annotations = jfield.getDeclaredAnnotations();
- this.clazz = jfield.getType();
- this.type = jfield.getGenericType();
Annotation annotation = null;
String defaultValue = null;
@@ -126,8 +113,8 @@
else
{
String msg =
- "JAX-RS annotations on one of fields are equivocality.
Annotations: " + annotation.toString()
- + " and " + a.toString() + " can't be applied to
one field.";
+ "JAX-RS annotations on one of fields " + jfield.toString() +
" are equivocality. Annotations: "
+ + annotation.toString() + " and " + a.toString() + "
can't be applied to one field.";
throw new RuntimeException(msg);
}
@@ -160,6 +147,23 @@
/**
* {@inheritDoc}
*/
+ @Override
+ public boolean equals(Object other)
+ {
+ if (other == null)
+ {
+ return false;
+ }
+ if (getClass() != other.getClass())
+ {
+ return false;
+ }
+ return getName().equals(((FieldInjectorImpl)other).getName());
+ }
+
+ /**
+ * {@inheritDoc}
+ */
public Annotation getAnnotation()
{
return annotation;
@@ -186,7 +190,7 @@
*/
public Class<?> getParameterClass()
{
- return clazz;
+ return jfield.getType();
}
/**
@@ -194,7 +198,7 @@
*/
public Type getGenericType()
{
- return type;
+ return jfield.getGenericType();
}
/**
@@ -210,7 +214,7 @@
*/
public String getName()
{
- return name;
+ return jfield.getName();
}
/**
@@ -223,8 +227,6 @@
ParameterResolver<?> pr =
ParameterResolverFactory.createParameterResolver(annotation);
try
{
- java.lang.reflect.Field jfield =
resource.getClass().getDeclaredField(getName());
-
if (!Modifier.isPublic(jfield.getModifiers()))
jfield.setAccessible(true);
@@ -233,8 +235,7 @@
catch (Throwable e)
{
- // if (LOG.isDebugEnabled())
- e.printStackTrace();
+ LOG.error("Failed initialize field. ", e);
Class<?> ac = annotation.annotationType();
if (ac == MatrixParam.class || ac == QueryParam.class || ac ==
PathParam.class)
Modified:
ws/trunk/rest/core/src/main/java/org/exoplatform/services/rest/impl/resource/AbstractResourceDescriptorImpl.java
===================================================================
---
ws/trunk/rest/core/src/main/java/org/exoplatform/services/rest/impl/resource/AbstractResourceDescriptorImpl.java 2009-09-02
10:13:55 UTC (rev 128)
+++
ws/trunk/rest/core/src/main/java/org/exoplatform/services/rest/impl/resource/AbstractResourceDescriptorImpl.java 2009-09-02
16:17:17 UTC (rev 129)
@@ -193,6 +193,23 @@
{
fields.add(new FieldInjectorImpl(resourceClass, jfield));
}
+ Class<?> sc = resourceClass;
+ while (sc != Object.class)
+ {
+ for (java.lang.reflect.Field jfield : sc.getDeclaredFields())
+ {
+ int modif = jfield.getModifiers();
+ if (Modifier.isPublic(modif) || Modifier.isProtected(modif))
+ {
+ FieldInjector inj = new FieldInjectorImpl(resourceClass, jfield);
+ if (inj.getAnnotation() != null && !fields.contains(inj))
+ {
+ fields.add(new FieldInjectorImpl(resourceClass, jfield));
+ }
+ }
+ }
+ sc = sc.getSuperclass();
+ }
}
this.resourceMethods = new ResourceMethodMap<ResourceMethodDescriptor>();
@@ -459,8 +476,9 @@
else
{
String msg =
- "JAX-RS annotations on one of method parameters of resource
" + toString() + "are equivocality. "
- + "Annotations: " + annotation + " and " + a
+ " can't be applied to one parameter.";
+ "JAX-RS annotations on one of method parameters of resource
" + toString() + ", method "
+ + method.getName() + " are equivocality. " +
"Annotations: " + annotation + " and " + a
+ + " can't be applied to one parameter.";
throw new RuntimeException(msg);
}
@@ -475,8 +493,8 @@
}
else
{
- LOG.warn("Method parameter contains unknown or not valid JAX-RS
annotation " + a.toString()
- + ". It will be ignored.");
+ LOG.warn("Method parameter of resource " + toString() + ",
method " + method.getName()
+ + " contains unknown or not valid JAX-RS annotation " +
a.toString() + ". It will be ignored.");
}
}
@@ -721,8 +739,7 @@
{
StringBuffer sb = new StringBuffer("[ AbstractResourceDescriptorImpl:
");
sb.append("path: " + getPathValue()).append("; isRootResource:
" + isRootResource()).append(
- "; class: " + getObjectClass()).append(getConstructorDescriptors() +
"; ").append(getFieldInjectors()).append(
- " ]");
+ "; class: " + getObjectClass()).append(" ]");
return sb.toString();
}
Modified:
ws/trunk/rest/core/src/main/java/org/exoplatform/services/rest/impl/resource/ResourceMethodDescriptorImpl.java
===================================================================
---
ws/trunk/rest/core/src/main/java/org/exoplatform/services/rest/impl/resource/ResourceMethodDescriptorImpl.java 2009-09-02
10:13:55 UTC (rev 128)
+++
ws/trunk/rest/core/src/main/java/org/exoplatform/services/rest/impl/resource/ResourceMethodDescriptorImpl.java 2009-09-02
16:17:17 UTC (rev 129)
@@ -179,11 +179,7 @@
StringBuffer sb = new StringBuffer("[ ResourceMethodDescriptorImpl: ");
sb.append("resource: " + getParentResource() + ";
").append("HTTP method: " + getHttpMethod() + "; ").append(
"produces media type: " + produces() + ";
").append("consumes media type: " + consumes() + "; ").append(
- "return type: " + getResponseType() + ";
").append("invoker: " + getMethodInvoker() + "; ").append(
- "parameters: [ ");
- for (MethodParameter p : getMethodParameters())
- sb.append(p.toString() + " ");
- sb.append("] ]");
+ "return type: " + getResponseType() + ";
").append("invoker: " + getMethodInvoker()).append("] ]");
return sb.toString();
}
Modified:
ws/trunk/rest/core/src/main/java/org/exoplatform/services/rest/impl/resource/SubResourceLocatorDescriptorImpl.java
===================================================================
---
ws/trunk/rest/core/src/main/java/org/exoplatform/services/rest/impl/resource/SubResourceLocatorDescriptorImpl.java 2009-09-02
10:13:55 UTC (rev 128)
+++
ws/trunk/rest/core/src/main/java/org/exoplatform/services/rest/impl/resource/SubResourceLocatorDescriptorImpl.java 2009-09-02
16:17:17 UTC (rev 129)
@@ -158,11 +158,7 @@
{
StringBuffer sb = new StringBuffer("[ SubResourceMethodDescriptorImpl:
");
sb.append("resource: " + getParentResource() + ";
").append("path: " + getPathValue() + "; ").append(
- "return type: " + getResponseType() + ";
").append("invoker: " + getMethodInvoker() + "; ").append(
- "parameters: [ ");
- for (MethodParameter p : getMethodParameters())
- sb.append(p.toString() + " ");
- sb.append("] ]");
+ "return type: " + getResponseType() + ";
").append("invoker: " + getMethodInvoker()).append(" ]");
return sb.toString();
}
}
Modified:
ws/trunk/rest/core/src/main/java/org/exoplatform/services/rest/impl/resource/SubResourceMethodDescriptorImpl.java
===================================================================
---
ws/trunk/rest/core/src/main/java/org/exoplatform/services/rest/impl/resource/SubResourceMethodDescriptorImpl.java 2009-09-02
10:13:55 UTC (rev 128)
+++
ws/trunk/rest/core/src/main/java/org/exoplatform/services/rest/impl/resource/SubResourceMethodDescriptorImpl.java 2009-09-02
16:17:17 UTC (rev 129)
@@ -210,10 +210,7 @@
sb.append("resource: " + getParentResource() + ";
").append("path: " + getPathValue() + "; ").append(
"HTTP method: " + getHttpMethod() + ";
").append("produces media type: " + produces() + "; ").append(
"consumes media type: " + consumes() + ";
").append("return type: " + getResponseType() + "; ").append(
- "invoker: " + getMethodInvoker() + ";
").append("parameters: [ ");
- for (MethodParameter p : getMethodParameters())
- sb.append(p.toString() + " ");
- sb.append("] ]");
+ "invoker: " + getMethodInvoker()).append(" ]");
return sb.toString();
}
Modified:
ws/trunk/rest/core/src/test/java/org/exoplatform/services/rest/impl/RequestDispatcherTest.java
===================================================================
---
ws/trunk/rest/core/src/test/java/org/exoplatform/services/rest/impl/RequestDispatcherTest.java 2009-09-02
10:13:55 UTC (rev 128)
+++
ws/trunk/rest/core/src/test/java/org/exoplatform/services/rest/impl/RequestDispatcherTest.java 2009-09-02
16:17:17 UTC (rev 129)
@@ -29,6 +29,9 @@
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.Request;
+import javax.ws.rs.core.SecurityContext;
import javax.ws.rs.core.UriInfo;
/**
@@ -327,5 +330,46 @@
service("GET", "/a/b/c/d?q1=q1&q2=q2", "", null,
null);
unregistry(Resource5.class);
}
+
//--------------------------------------
+
+ public void testFieldSuperClass() throws Exception
+ {
+ registry(EndResource.class);
+ service("GET", "/a", "", null, null);
+ unregistry(EndResource.class);
+ }
+
+ public abstract static class AbstractResource
+ {
+ @Context
+ protected UriInfo uriInfo;
+
+ @Context
+ public Request request;
+ }
+
+ public abstract static class ExtResource extends AbstractResource
+ {
+ @Context
+ protected SecurityContext sc;
+
+ }
+
+ @Path("a")
+ public static class EndResource extends ExtResource
+ {
+ @Context
+ private HttpHeaders header;
+
+ @GET
+ public void m1()
+ {
+ assertNotNull(uriInfo);
+ assertNotNull(request);
+ assertNotNull(sc);
+ assertNotNull(header);
+ }
+ }
+
}
Modified:
ws/trunk/rest/core/src/test/java/org/exoplatform/services/rest/impl/resource/ResourceDescriptorTest.java
===================================================================
---
ws/trunk/rest/core/src/test/java/org/exoplatform/services/rest/impl/resource/ResourceDescriptorTest.java 2009-09-02
10:13:55 UTC (rev 128)
+++
ws/trunk/rest/core/src/test/java/org/exoplatform/services/rest/impl/resource/ResourceDescriptorTest.java 2009-09-02
16:17:17 UTC (rev 129)
@@ -51,7 +51,10 @@
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
+import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Request;
+import javax.ws.rs.core.SecurityContext;
import javax.ws.rs.core.UriInfo;
/**
@@ -750,4 +753,40 @@
}
+ // =========================================
+
+ public void testInitializeFieldSuperClass()
+ {
+ AbstractResourceDescriptor resource = new
AbstractResourceDescriptorImpl(EndResource.class);
+ assertEquals(4, resource.getFieldInjectors().size());
+ }
+
+ public abstract static class AbstractResource
+ {
+ @Context
+ protected UriInfo uriInfo;
+
+ @Context
+ public Request request;
+ }
+
+ public abstract static class ExtResource extends AbstractResource
+ {
+
+ @Context
+ protected SecurityContext sc;
+
+ }
+
+ public static class EndResource extends ExtResource
+ {
+ @SuppressWarnings("unused")
+ @Context
+ private HttpHeaders header;
+
+ @GET
+ public void m1()
+ {
+ }
+ }
}