I'm struggling with smarter coding style in usage of anonymous classes.
How can I use anonymous inner class WITHOUT @SuppressWarnings in the case below?
When using anonymous classes like this way:
someMethod( new Y(){
// non-overriding public method
public void echo1(){
// do some stuff
}
} );
...
public abstract class Y
{
// use me as an anonymous class,
// and define any methods when defining me
}
This code makes warning.
public method "echo1" needs to be annotated @SuppressWarnings("unused") .
If I code additionally echo2, echo3, echo4,... all methods needs to be annotated one by one.
In order to avoid writing annotations frequently, I tried to code this way for propagating annotation effects over all child classes:
@Inherited // <- This is what I want this annotation to be!
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface SuppressWarningsPropagate extends SuppressWarnings
{
}
then I tried to annotate class Y with this annotation. This would be useful and simple enough to annotate only one base class.
...and I found myself stupid.
Annotations like SuppressWarnings can't be "extended" nor "implemented".
Instead, this code works and has no warnings, because SuppressWarnings goes well for this only one concrete class:
@SuppressWarnings("unused")
public class X
{
private int i;
private int j;
}
In summary, how can I...
- use anonymous inner class (for good readability of code)
- and also defining public methods on new (must be public, for the matter of addJavascriptInterface() of Android SDK)
- and also avoid writing @SuppressWarnings("unused") for all the methods one by one (for good writability)
...possible? or am I just daydreaming?
Thanks a lot in advance.