Adding support for discovering annotations from superclass chain

This commit is contained in:
Joakim Erdfelt 2012-06-28 10:53:57 -07:00
parent 106db485ca
commit 324431a072
4 changed files with 99 additions and 58 deletions

View File

@ -269,25 +269,6 @@ public class EventMethodsCache
return methods;
}
private boolean isNotPublicVoid(Method method)
{
// validate modifiers
int mods = method.getModifiers();
if (!Modifier.isPublic(mods) || Modifier.isStatic(mods))
{
return true;
}
// validate return
if (!Void.TYPE.isAssignableFrom(method.getReturnType()))
{
return true;
}
// we have a public void method
return true;
}
private boolean isSameParameters(Class<?>[] actual, Class<?>[] params)
{
if(actual.length != params.length) {
@ -320,9 +301,13 @@ public class EventMethodsCache
private EventMethods scanAnnotatedMethods(Class<?> pojo)
{
Class<?> clazz = pojo;
EventMethods events = new EventMethods(pojo,true);
for (Method method : pojo.getDeclaredMethods())
clazz = pojo;
while (clazz.getAnnotation(WebSocket.class) != null)
{
for (Method method : clazz.getDeclaredMethods())
{
if (method.getAnnotation(OnWebSocketConnect.class) != null)
{
@ -367,12 +352,17 @@ public class EventMethodsCache
// Not a tagged method we are interested in, ignore
}
// try superclass now
clazz = clazz.getSuperclass();
}
return events;
}
private EventMethods scanListenerMethods(Class<?> pojo)
{
EventMethods events = new EventMethods(pojo,false);
// This is a WebSocketListener object
events.onConnect = new EventMethod(pojo,"onWebSocketConnect",WebSocketConnection.class);
events.onClose = new EventMethod(pojo,"onWebSocketClose",Integer.TYPE,String.class);

View File

@ -101,6 +101,28 @@ public class EventMethodsCacheTest
}
}
/**
* Test Case for no exceptions and 4 methods (3 methods from parent)
*/
@Test
public void testDiscoverMyEchoBinarySocket()
{
EventMethodsCache cache = new EventMethodsCache();
EventMethods methods = cache.getMethods(MyEchoBinarySocket.class);
String classId = MyEchoBinarySocket.class.getSimpleName();
Assert.assertThat("EventMethods for " + classId,methods,notNullValue());
assertHasEventMethod(classId + ".onBinary",methods.onBinary);
assertHasEventMethod(classId + ".onClose",methods.onClose);
assertHasEventMethod(classId + ".onConnect",methods.onConnect);
assertNoEventMethod(classId + ".onException",methods.onException);
assertHasEventMethod(classId + ".onText",methods.onText);
Assert.assertThat(".getOnFrames()",methods.getOnFrames().size(),is(0));
}
/**
* Test Case for no exceptions and 3 methods
*/

View File

@ -0,0 +1,24 @@
package org.eclipse.jetty.websocket.annotations;
import java.io.IOException;
import java.nio.ByteBuffer;
/**
* Test of constructing a new WebSocket based on a base class
*/
@WebSocket
public class MyEchoBinarySocket extends MyEchoSocket
{
@OnWebSocketBinary
public void echoBin(ByteBuffer payload)
{
try
{
getConnection().write(payload);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}

View File

@ -14,6 +14,11 @@ public class MyEchoSocket
{
private WebSocketConnection conn;
public WebSocketConnection getConnection()
{
return conn;
}
@OnWebSocketClose
public void onClose(int statusCode, String reason)
{