简体   繁体   English

如何在运行时告知Java 1.4类型或成员已弃用?

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

In Java 6 I can use a technique like this: 在Java 6中,我可以使用这样的技术:

@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? 有没有办法在1.4中使用旧样式(基于Javadoc的)弃用?

You can't make such a check on javadoc tags. 你不能对javadoc标签进行这样的检查。 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. 好吧,如果你分发你的源代码,你可以加载源文件并为@deprecated标签解析它,但这不是更好的选择。

The pre-Java5 way of indicating something is by using a marker interface. Java5之前的指示方式是使用标记接口。 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) . 然后检查Deprecated.class.isAssignableFrom(Test.class)

But deprecation is a purely indicative notion and should not be used at run-time to differentiate behaviour. 但是,弃用只是一种纯粹的指示性概念,不应在运行时用于区分行为。

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

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. @deprecated标记仅由编译器使用,但未放入编译的java字节代码中,因此在编译后无法检测到它。

What is it you want to do? 你想做什么?

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: 使用或不使用@deprecated JavaDoc标记编译的1.5之前的源代码生成的类文件有几个字节:

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 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. 如果你看一下JVM规范,第4章ClassFile结构中有一个Deprecated属性(第4.7.15节 )。

There're tools capable to determine whether a class is deprecated or not: 有些工具可以确定某个类是否被弃用:

  • any of the modern IDEs 任何现代IDE
  • jad (the Java decompiler), closed source, purely native code. jad(Java反编译器),封闭源代码,纯粹的本机代码。
  • jclasslib project (GPLv2) by Ingo Kegel . Ingo Kegel的 jclasslib项目(GPLv2)。

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. 您可以自己继续实现ClassFile结构,或者如果您对GPLv2没有任何反对意见,请查看org.gjt.jclasslib.structures.AttributeInfo类。

注释从版本1.5开始可用,因此1.4和之前的版本不可用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我怎么能一般地告诉Java类是原始类型? - How can I generically tell if a Java Class is a primitive type? 如何告诉Java两个“?”是同一类型? - How can I tell Java that two “?”s are the same type? Java:如何将类型转换为“运行时”类? - Java: How can I type cast to a "Runtime" Class? 我如何知道在内部(成员)类上使用哪个构造函数? - How can I tell which constructor to use on an inner (member) class? 如何在Java 1.4中模拟静态方法? - How can I mock static methods in Java 1.4? 如何判断Java整数是否为空? - How can I tell if a Java integer is null? 如何为 android 4.0.3 替换 type_orientation(已弃用)? - How can i replace type_orientation (is deprecated) for android 4.0.3? 不建议使用CucumberOptions类型,如何解决该问题 - The type CucumberOptions is deprecated how can i resolve the issue 如何在运行时识别对象的类型? - how can I identify the type of object at runtime? 如何设计我的 Spring 程序,以便当用户登录时,我可以判断他们是教职员工、学生还是教职员工? - How can I design my Spring program so that when a user logs in I can tell if they are a Staff member, student or faculty member?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM