4

Using eclipse, I want to see a warning as soon as any of my used methods is marked as @Deprecated. Method calls are correctly crossed out if so, but eclipse does not give out a warning if the method origins from the same class. See the screenshot below.

Eclipse_warnings

For better reproducing, I'll also provide the code in textform:

/* MainClass.java */
public class MainClass {
    public static void main(String[] args) {
        MainClass.foo();
        MemberClass.foo();
        OtherClass.foo();
    }
    @Deprecated
    public static void foo() {
        return;
    }
    private static class MemberClass {
        @Deprecated
        protected static void foo() {
            return;
        }
    }
}

/* OtherClass.java */
public class OtherClass {
    @Deprecated
    public static void foo() {
        return;
    }
}

Whereas only OtherClass.foo() generates a warning. Why? How can I fix this?

  • Enable project specific settings is deactivated
  • Window->Preferences->Java->Compiler->Errors/Warnings->Deprecated and restricted API if fully set to Warning, as can be seen from the picture above.

Note: Eclipse not showing deprecated warning? is not related

Community
  • 1
  • 1
phil294
  • 10,038
  • 8
  • 65
  • 98

1 Answers1

4

The exact definition of the @Deprecated annotation is not in the javadoc, but is actually in the Java Language Specification, section 9.6.4.6.

Essentially it says that uses of a deprecated element will generate a warning unless

The use and declaration are both within the same outermost class.

Since MainClass.foo() and MemberClass.foo() are both within MainClass, and your calls to them are also in MainClass, no warnings are generated.

The call to OtherClass.foo() is not within the same outermost class, so that one generates a warning.

I believe the reason for this rule is that the code within an outermost or top-level class can be presumed to be maintained and evolved together. Making an element deprecated is usually a signal to code "outside," possibly maintained by other people, that something is going to happen to that element. Often different rules apply within a class. Internal use of deprecated elements, like private elements, might be perfectly fine.

It's probably legal for a compiler or an IDE to issue additional warnings beyond those required by the JLS, such as for uses of a deprecated element within the same outermost class. I'm not aware of any system that does this, though.

But you're using Eclipse, and I'm sure that it, as well other IDEs, can easily find all usages of some element within a class.

EDOLLing
  • 5
  • 4
Stuart Marks
  • 127,867
  • 37
  • 205
  • 259
  • thanks for the detailed and understandable answer! so your advice would be to live with it or work around? – phil294 Mar 14 '15 at 19:45
  • @Blauhirn Yes, basically live with it. Or if you want to quickly find all uses of a deprecated element in the same file where it's declared, change its spelling at the point of definition and look for all the red squigglies. :-) – Stuart Marks Mar 14 '15 at 20:36
  • @Blauhirn The behavior of the `Deprecated` annotation is mandated by the JLS and is probably baked into the compiler. I don't think there's a way you can change it. It seems pretty unlikely to me that we would change the JLS; it just doesn't seem important, and there are reasonably good reasons for the way it is. – Stuart Marks Mar 15 '15 at 06:11