Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does @Deprecated mean on method parameters and local variables?

DISCLAIMER: I know the meaning and purpose of @Deprecated.

The definition of the @Deprecated annotation looks like this in the source code of Java:

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})
public @interface Deprecated {
}

I understand the purpose of having target values of CONSTRUCTOR, FIELD, TYPE, METHOD and PACKAGE.

However, what does it mean to mark a method parameter or a local variable as @Deprecated?

Strangely the below example compiles without any warnings.

interface Doable {
    void doIt(@Deprecated Object input);
}

class Action implements Doable {

    @Override
    public void doIt(@Deprecated Object input) {

        String string = String.valueOf(input); // no warning!

        @Deprecated
        String localVariable = "hello";

        System.out.println("Am I using a deprecated variable?" + localVariable); // no warning!
    }
}

Is this something they might intend to implement in the future?


FWIW, I use JDK 1.7.0_11 on Ubuntu 12.04 64bit. The result is the same whether I run the program from Eclipse or command line.

The compiler does spit out warnings for normal usage of @Deprecated, such as using one of the deprecated constructors of the java.util.Date class. Just to prove that I don't have a faulty terminal or set up, here is the output:

$ javac com/adarshr/Test.java -Xlint:deprecation
com/adarshr/Test.java:12: warning: [deprecation] Date(int,int,int) in Date has been deprecated
        new Date(2013, 1, 31);
        ^
1 warning
$ 
$ javac -version
javac 1.7.0_11
like image 641
adarshr Avatar asked Jan 31 '13 13:01

adarshr


2 Answers

What does it mean to mark a method parameter or a local variable as @Deprecated?

It has the same meaning as when applied to any other element:

A program element annotated @Deprecated is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists. Compilers warn when a deprecated program element is used or overridden in non-deprecated code.


Why doesn't the compiler omit warnings for deprecated parameters and fields in Java 7?
Because that's exactly what the JLS (§ 9.6.3.6) dictates.

A Java compiler must produce a deprecation warning when a type, method, field, or constructor whose declaration is annotated with the annotation @Deprecated is used (i.e. overridden, invoked, or referenced by name), unless:

  • The use is within an entity that is itself annotated with the annotation @Deprecated; or

  • The use is within an entity that is annotated to suppress the warning with the annotation @SuppressWarnings("deprecation"); or

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

Use of the @Deprecated annotation on a local variable declaration or on a parameter declaration has no effect.

(emphasis added)

like image 96
Matt Ball Avatar answered Sep 23 '22 02:09

Matt Ball


The JLS expressly states that the @Deprecation annotation is ignored on local variables. See Matt Ball's answer.

Is this something they might intend to implement in the future?

I very much doubt it.

  • What could it mean ... apart from its current meaning as an informal reminder to the implementor (and maybe style checkers / PMD / FindBugs / etc) that the local variable needs to be removed.

  • Any material change is likely to break source compatibility for people who currently use the annotation as above. The Java maintainers try very hard to avoid breaking old code.

like image 28
Stephen C Avatar answered Sep 22 '22 02:09

Stephen C