jsr356-experts@websocket-spec.java.net

[jsr356-experts] Annotation inheritance: was: Re: Potpourri of smaller issues

From: Danny Coward <danny.coward_at_oracle.com>
Date: Fri, 11 Jan 2013 15:23:49 -0800

On 11/28/12 3:40 PM, Mark Thomas wrote:
>> 1) What are the inheritance semantics of the websocket annotations ?
>> >For example, if I subclass a class annotated with @WebSocketEndpoint,
>> >can a use @WebSocketMessage on methods on the subclass even if the
>> >subclass is not annotated with @WebSocketMessage ?
>> >
>> >My proposal is that we stick to the guidelines that JSR 250 ( Common
>> >Annotations for the Java Platform) sets out in
>> >section 2.1. The layman's version is that subclasses don't inherit any
>> >of the metadata. Annotations only affect the class (or methods of the
>> >class) they live on. The semantics start to get very tricky if we
>> >diverge from those guidelines.
> That is exactly the opposite of what the Servlet spec does with
> @HandlesTypes. This would require further filtering of the classes
> passed to the SCI to ensure they met these stricter requirements.
>
Hi Mark,

I meant to follow up on this before public draft but didn't.

My concern with allowing annotated behavior to be inherited is that we'd
also need to define a set of rules for the inheritance for a variety of
circumstances, which quickly get complicated, even in relatively simple
cases. I think even the following simple examples hint at this:-

- Which onMessage() method gets called here ?


public class Chat {
     @WebSocketMessage
     public void onMessage(String s, Session s) {
         // do I get called because I have the annotation ?
     }
}

@WebSocketEndpoint(/"chat") // btw does this have to be '/chat' or can
it be something else ?
public class SuperChat extends Chat {

     public void onMessage(String s, Session s) {
         // or do I get called because I am on the subclass that is
actually the endpont ?
     }
}

or worse

@WebSocketEndpoint(/"chat")
public class Chat {
}

@WebSocketEndpoint(/"superchat")
public class SuperChat extends Chat {
}

How many endpoints do I have in that example ?

or is the mixing of class and method level annotations up and down the
hieirarchy allowed ?

public abstract class Chat {
     @WebSocketMessage
     public void onMessage(String s) {
         this.processMessage(s);
     }

     public abstract void processMessage(String s);
}

@WebSocketEndpoint(/"actualchat")
public class ActualChat extends Chat {

     public void processMessage(String s) {
         ....
     }
}

I started writing examples with encoders and methods with custom
developer objects as parameters, and it gets even less clear. The do's
and don'ts get very complicated very quickly.

In sum, I think its much cleaner to say: annotations have to live on the
implementation class.

In terms of container implementation, are you saying that the infamous
SCI scan will return any subclasses of a class marked @WebSocketEndpoint
? We do have java.lang.Class. getDeclaredAnnotations() to help out.

- Danny


-- 
<http://www.oracle.com> 	*Danny Coward *
Java EE
Oracle Corporation