How can I tell at runtime that a type or member of Java 1.4 is deprecated?

In Java 6, I can use a technique like this:

@Deprecated
public final class Test {

    public static void main(String[] args) {
        System.out.println(Test.class.isAnnotationPresent(Deprecated.class));
    }   
}

to decide if the type is obsolete. Is there any way to do this in version 1.4 with an outdated style (based on Javadoc)?

+3
source share
5 answers

You cannot do such a check in javadoc tags. Well, if you are distributing the source code, download the source file and parse it for the tag @deprecated, but this is not preferred.

A pre-Java5 way to specify something using the marker interface. You can define your own:

public interface Deprecated {
}

and use obsolete classes. Of course, you cannot use it in methods.

public final class Test implements Deprecated

, Deprecated.class.isAssignableFrom(Test.class).

, .

+4

, , JavaDoc .

+3

@deprecated , java- , .

?

+3

, .

1.5, JavaDoc @deprecated, , :

00000000  ca fe ba be 00 00 00 34  00 0a 07 00 02 01 00 02  |.......4........|
00000010  43 30 07 00 04 01 00 10  6a 61 76 61 2f 6c 61 6e  |C0......java/lan|
00000020  67 2f 4f 62 6a 65 63 74  01 00 06 3c 69 6e 69 74  |g/Object...<init|
00000030  3e 01 00 03 28 29 56 01  00 04 43 6f 64 65 0a 00  |>...()V...Code..|
00000040  03 00 09 0c 00 05 00 06  00 20 00 01 00 03 00 00  |......... ......|
00000050  00 00 00 01 00 00 00 05  00 06 00 01 00 07 00 00  |................|
00000060  00 11 00 01 00 01 00 00  00 05 2a b7 00 08 b1 00  |..........*.....|
00000070  00 00 00 00 00                                    |.....|

00000000  ca fe ba be 00 00 00 34  00 0b 07 00 02 01 00 02  |.......4........|
00000010  43 30 07 00 04 01 00 10  6a 61 76 61 2f 6c 61 6e  |C0......java/lan|
00000020  67 2f 4f 62 6a 65 63 74  01 00 06 3c 69 6e 69 74  |g/Object...<init|
00000030  3e 01 00 03 28 29 56 01  00 04 43 6f 64 65 0a 00  |>...()V...Code..|
00000040  03 00 09 0c 00 05 00 06  01 00 0a 44 65 70 72 65  |...........Depre|
00000050  63 61 74 65 64 00 20 00  01 00 03 00 00 00 00 00  |cated. .........|
00000060  01 00 00 00 05 00 06 00  01 00 07 00 00 00 11 00  |................|
00000070  01 00 01 00 00 00 05 2a  b7 00 08 b1 00 00 00 00  |.......*........|
00000080  00 01 00 0a 00 00 00 00                           |........|

JVM, 4, Deprecated (§4.7.15) ClassFile.

, , :

  • IDE
  • jad (Java decompiler), closed source, purely native code.
  • jclasslib project (GPLv2) by Ingo Kegel .

You can either implement the structure ClassFileyourself, or if you have nothing against GPLv2, look at the org.gjt.jclasslib.structures.AttributeInfoclass.

+1
source

annotations are available from version 1.5, so this is not possible for 1.4 and previous

0
source

Source: https://habr.com/ru/post/1728438/


All Articles