phil294
phil294

Reputation: 10922

Show deprecated warnings for methods of same class

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?


Note: Eclipse not showing deprecated warning? is not related

Upvotes: 4

Views: 1592

Answers (1)

Stuart Marks
Stuart Marks

Reputation: 132610

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.

Upvotes: 4

Related Questions