1

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.

  • 1
    The warning is actually telling you that nobody is using your `echo1()` method, therefore you can as well remove it and obtain the same result. What are you trying to accomplish? – Flavio Jul 23 '12 at 12:05
  • Hi @Flavio, thanks for commenting! I agree with what you say at all. And, I have additional explanation... Please hear, for it might be interesting. I shall post details devided into 3 parts: (1)PURPOSE, (2)PROBLEM, (3)QUESTION. for each comment are expected to be short. – lang.and.engine Jul 24 '12 at 00:14
  • ** (1)PURPOSE ** What I'm trying to accomplish is, TO CONNECT Java (a static language) BETWEEN JavaScript (a dynamic language). This is really often done, when developing Android apps. We can now CALL JAVA METHODS FROM HTML & JS. ---- cf. "call java function from javascript over android webview": http://stackoverflow.com/questions/10389572/call-java-function-from-javascript-over-android-webview explanation I wrote (sorry auto-translation) http://translate.google.co.jp/translate?hl=ja&sl=ja&tl=en&u=http://d.hatena.ne.jp/language_and_engineering/20120710/CreateAndroidAppByHtml5JavaScript – lang.and.engine Jul 24 '12 at 00:15
  • But such java methods that callable from JS, should be all public. This is just a specification of Android API. Then, my situation happens. (continues to the next comment) ** (2)PROBLEM ** In the world of Java & Eclipse & statically typed programs, they never know that public Java method "echo1" would be called somewhere. Actually, those methods are never called inside Java world. For they are coded in order to be used in JavaScript world, which Eclipse can never know. So, warnings of Eclipse is just right, kindly telling "these methods are not used in Java world". That's OK... – lang.and.engine Jul 24 '12 at 00:16
  • ** (3)QUESTION ** So my question arises. Can I easily delete or suppress such Eclipse's warning messages, in this situation? Method by method, I have to write @SuppressWarnings("unused") annotation, if I want to add one java public method for using in JS. I wish to decrease this cost, and enable simpler&less coding style. If I could "extend" SuppressWarnings annotation to be inherited, I would write that only once. But I can not extend Java's standard annotations, nor can I adjust SuppressWarnings to be inherited. – lang.and.engine Jul 24 '12 at 00:18
  • So, I asked this question... I understand, this may sound strange, for this topic is bridging over normal theory of Java and also non-static scripting of JS. Sorry if confused. And any ideas, will be welcome very much. (end of my one set of comments.) – lang.and.engine Jul 24 '12 at 00:19
  • Maybe you can just tell Eclipse to disable that kind of warning for the entire project? See http://stackoverflow.com/questions/3624756/how-can-i-disable-compiler-warnings-in-eclipse-on-a-file-specific-basis – Flavio Jul 24 '12 at 07:17
  • Flavio, Thanks for replying though above is nicely complicated. I just could as you taught. (Window -> preferrences -> Java -> Compiler -> Error&Warnings -> unused local or private members -> ignore) It's true this operation removed successfully all this kind of warnings... But sadly, it's not easy to apply this way for reasons: I'm developing libraries for common use, so I can only provide codes, can't force users to change IDE's settings. I wish if I could enable such settings only by code (such as giving annotations to class) as solution. Anyway, I appreciate for your helping information:-) – lang.and.engine Jul 24 '12 at 08:31
  • @Flavio Doing it for the entire project is IMHO too generous. Annotating the enclosing class should do. Converting the anonymous class into normal one would be even more specific (and it costs just a few lines more). – maaartinus Jun 12 '14 at 14:30
  • [Can I make a Java Annotation which “extends” @SuppressWarnings?](http://stackoverflow.com/questions/8680207/can-i-make-a-java-annotation-which-extends-suppresswarnings) and [Why is not possible to extend annotations in Java?](http://stackoverflow.com/questions/1624084/why-is-not-possible-to-extend-annotations-in-java) state that extending annotations is not currently possible. – Vadzim Jul 27 '15 at 18:15

0 Answers0