English 中文(简体)
Java Annotations - search for an example of RetentionPolicy. CLASS
原标题:Java Annotations - looking for an example of RetentionPolicy.CLASS

根据Java Annotation 页: 1

  • RetentionPolicy.CLASS Annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time.

  • RetentionPolicy.RUNTIME Annotations are to be recorded in the class file by the compiler and retained by the VM at run time, so they may be read reflectively.

我正在寻找“CLASS”保留政策的样本。 当我们需要利用这一政策,而不是使用RUNTMIS政策时。

最佳回答
问题回答

CLASS Annotations are used in obfuscator tools like http://proguard.sourceforge.net . For example annotation @KeepName disables name mangling when you need to have your class name unchanged to be able to call methods like Class.forName().

留任政策. CLASS在按规范进行后处理时是有用的。

<>>Example:

<>明文>

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.CLASS)
@interface RetentionClass {}

@Retention(RetentionPolicy.RUNTIME)
@interface RetentionRuntime {}

public static void main(String[] args) {
    @RetentionClass
    class C {}
    assert C.class.getAnnotations().length == 0;

    @RetentionRuntime
    class D {}
    assert D.class.getAnnotations().length == 1;
}

如果我们使用附加说明的班级的<代码>javap,我们看到<代码>保留。 CLASS annotatedsessions a 等级:

#14 = Utf8               LRetentionClass;
[...]
RuntimeInvisibleAnnotations:
  0: #14()

<代码>保留。 RUNTIME annotation have a RuntimeVisible 等级:

#14 = Utf8               LRetentionRuntime;
[...]
RuntimeVisibleAnnotations:
  0: #14()

因此,有关两案的资料都载于《星号》。

因此,<代码>Runtime.CLASS可用于将任意元数据与可使用密码操纵工具的类别联系起来,而不干扰可操作的行为。

Examples on IX,请你与你合作。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签