Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 575
Oh Chin Boon Avatar asked Aug 01 '26 10:08

Oh Chin Boon


1 Answers

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 . . .
like image 132
Andrew Thompson Avatar answered Aug 03 '26 23:08

Andrew Thompson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!