如何在运行时判断 Java 1.4 类型或成员已被弃用?

发布于 2024-08-17 10:45:59 字数 290 浏览 2 评论 0原文

在 Java 6 中,我可以使用这样的技术:

@Deprecated
public final class Test {

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

确定某个类型是否已弃用。在 1.4 中是否有任何方法可以通过旧式(基于 Javadoc)弃用来做到这一点?

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 a type is deprecated. Is there any way at all to do this in 1.4 with the old style (Javadoc-based) deprecation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

扮仙女 2024-08-24 10:45:59

您无法对 javadoc 标签进行此类检查。好吧,如果您分发源代码,则可以加载源文件并解析它的 @deprecated 标记,但这并不是更好的选择。

Java5 之前的指示方式是使用标记接口。您可以定义自己的:

public interface Deprecated {
}

并让已弃用的类实现它。当然,您不能在方法上使用它。

public final class Test implements Deprecated

然后检查是否Deprecated.class.isAssignableFrom(Test.class)

但弃用纯粹是指示性概念,不应在运行时用于区分行为。

You can't make such a check on javadoc tags. Well, you can, if you distribute your source code, load the source file and parse it for the @deprecated tag, but this is not preferable.

The pre-Java5 way of indicating something is by using a marker interface. You can define your own:

public interface Deprecated {
}

and make deprecated classes implement it. You cannot use it on methods, of course.

public final class Test implements Deprecated

And then check whether Deprecated.class.isAssignableFrom(Test.class).

But deprecation is a purely indicative notion and should not be used at run-time to differentiate behaviour.

轻拂→两袖风尘 2024-08-24 10:45:59

不,没有,因为 JavaDoc 是注释,因此在运行时不可用。

No there is not, as JavaDoc is a comment and as such not available at runtime.

∞觅青森が 2024-08-24 10:45:59

@deprecated标签只被编译器使用,不会放入编译后的java字节码中,所以编译后你无法检测到它。

你想做什么?

The @deprecated tag is only used by the compiler but not put in the compiled java byte code, so you cannot detect it after compilation.

What is it you want to do?

情丝乱 2024-08-24 10:45:59

事实上,你可以。

之前的源代码,无论是否使用 @deprecated JavaDoc 标记进行编译,都会生成几个字节不同的类文件

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                           |........|

相同的 1.5版本 com/javase/specs/jvms/se7/html/jvms-4.html" rel="nofollow noreferrer">JVM 规范,第 4 章,有一个 Deprecated 属性 (§4.7.15)在 ClassFile 结构中。

有一些工具能够确定一个类是否已被弃用:

您可以继续自己实现 ClassFile 结构,或者,如果您不反对 GPLv2,请查看 org.gjt.jclasslib.structs.AttributeInfo 类。

Actually, you can.

The same pre-1.5 source code, compiled with or without the @deprecated JavaDoc tag, produces class files which differ in a couple of bytes:

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                                    |.....|

vs

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                           |........|

If you take a look at the JVM Specification, Chapter 4, there's a Deprecated attribute (§4.7.15) in the ClassFile structure.

There're tools capable to determine whether a class is deprecated or not:

  • any of the modern IDEs
  • jad (the Java decompiler), closed source, purely native code.
  • jclasslib project (GPLv2) by Ingo Kegel.

You can either go ahead and implement the ClassFile structure yourself or, if you have nothing against GPLv2, take a look at org.gjt.jclasslib.structures.AttributeInfo class.

夏夜暖风 2024-08-24 10:45:59

注释从 1.5 版本开始可用,因此对于 1.4 及之前的版本来说这是不可能的

annotations are available since version 1.5, so that is not possible for 1.4 and previous

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文