HJW
HJW

Reputation: 23443

Will JVM issue warning during runtime for using @deprecated codes?

Will JVM issue warning during runtime for using @deprecated codes?

If it doesn't, can i make it warn for using @deprecated codes?

Upvotes: 3

Views: 436

Answers (5)

Stephen C
Stephen C

Reputation: 719219

Will JVM issue warning during runtime for using @deprecated codes?

No it won't warn you.

If it doesn't, can i make it warn for using @deprecated codes?

In practice, no.

I guess it is theoretically possible to make the VVM do this by implementing your own class loader and parsing the class files by hand to extract and check the "deprecated" attributes of all of the methods used. However, that would be an awful lot of work because you'd need to replicate a large part of the parsing and checking logic that happens under the hood in a normal classloader. It would also (I think) require you to modify the JVM's bootclasspath to get your classloader to be used for everything. So even if this is possible, it is a really bad idea.


@prunge's comment suggests an alternative which would be simpler to implement ... and more "kosher" ... but still not an easy task.

Upvotes: 2

Anish Dasappan
Anish Dasappan

Reputation: 415

No, annotations are not available at runtime (no such information will be embedded into bytecode)

they are just instructions to compiler. The compiler gives warning to the programmer (if it is not suppressed) that better implementations exist & don't use methods that are deprecated (ie marked @deprecated)

Upvotes: 0

Andrew Thompson
Andrew Thompson

Reputation: 168835

Will JVM issue warning during runtime for using @deprecated codes?

No.

If it doesn't, can i make it warn for using @deprecated codes?

Not any way that I know of.


OTOH it is possible to find out in code, at runtime, if a particular member is deprecated:

import java.awt.Panel;
import java.lang.reflect.Method;
import java.lang.annotation.Annotation;

class TestAnnotations {

    @Deprecated
    public Object getStuff() {
        return null;
    }

    public static void showAnnotations(
        Object obj,
        String methodName) throws Exception {

        Class cls = obj.getClass();
        Method m = cls.getMethod(methodName);
        System.out.println(m);
        Annotation[] as = m.getDeclaredAnnotations();
        for (Annotation a : as) {
            System.out.println(a);
        }
    }

    public static void main(String[] args) throws Exception {
        Panel p = new Panel();
        showAnnotations(p, "show");

        TestAnnotations ta = new TestAnnotations();
        showAnnotations(ta, "getStuff");
    }
}

Output

public void java.awt.Component.show()
@java.lang.Deprecated()
public java.lang.Object TestAnnotations.getStuff()
@java.lang.Deprecated()
Press any key to continue . . .

Upvotes: 4

Tim Bender
Tim Bender

Reputation: 20442

No and you shouldn't do that. There is nothing wrong with @Deprecated code. This is simply a hint from the library provider that the code/method/class is no longer the recommended way of doing something and may fall out of service life. Most open source projects will have a defined rule for the schedule by which code may be marked deprecated and then removed. I've typically seen 1 major release for deprecation and removal in the subsequent major release.

Upvotes: 1

Mike
Mike

Reputation: 8100

The proper thing to do is heed the deprecation warnings that are emitted when you compile the code. They're there for a reason. Most times, you can pretty easily find out how to update the code by reading the fine manual, and then you don't have to worry about running deprecated code at all.

Upvotes: 0

Related Questions