导航:首页 > 源码编译 > java注解编译器

java注解编译器

发布时间:2022-01-17 08:35:48

‘壹’ 注解的java中的注解

java.lang.annotation.Retention可以在您定义Annotation型态时,指示编译器如何对待您的自定义 Annotation,预设上编译器会将Annotation资讯留在class档案中,但不被虚拟机器读取,而仅用于编译器或工具程式运行时提供资讯。
在使用Retention型态时,需要提供java.lang.annotation.RetentionPolicy的列举型态:
package java.lang.annotation;
public enum RetentionPolicy {
SOURCE, //编译器处理完Annotation资讯后就没事了
CLASS, //编译器将Annotation储存于class档中,预设
RUNTIME //编译器将Annotation储存于class档中,可由VM读入
}
RetentionPolicy为SOURCE的例子是SuppressWarnings,这个资讯的作用仅在告知编译器抑制警讯,所以不必将这个资讯储存于class档案。
RetentionPolicy为RUNTIME的时机,可像是您使用Java设计一个程式码分析工具,您要VM读出Annotation资讯,以在分析程式中使用,搭配Reflection机制,就可以达到这个目的。
在J2SE 5.0中新增了java.lang.reflect.AnnotatedElement这个界面,当中定义有四个方法:
public Annotation getAnnotation(Class annotationType);
public Annotation[] getAnnotations();
public Annotation[] getDeclaredAnnotations();
public boolean isAnnotationPresent(Class annotationType);
Class、Constructor、Field、Method、Package等类别,都实作了AnnotatedElement这个接口,所以您可以从这些类别的实例上,分别取得标示于其上的Annotation与其资讯,如果RetentionPolicy为RUNTIME的话。
举个例子来说,假设您设计了以下的Debug Annotation:
* Debug.java
package onlyfun.caterpillar;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface Debug {
String value();
String name();
}
由于RetentionPolicy为RUNTIME,编译器在处理Debug Annotation时,会将之编译至class档中,并可以VM读出Annotation资讯,接着我们将Debug用于程式中:
* SomeObject.java
package onlyfun.caterpillar;
public class SomeObject {
@Debug(
value = unit,
name = debug1
)
public void doSomething() {
// ....
}
}
可以设计一个工具程式来读取Annotation资讯:
* DebugTool.java
package onlyfun.caterpillar;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
public class DebugTool {
public static void main(String[] args)
throws NoSuchMethodException {
Class<SomeObject> c = SomeObject.class;
Method method = c.getMethod(doSomething);
if(method.isAnnotationPresent(Debug.class)) {
System.out.println(@Debug is found.);
Debug debug = method.getAnnotation(Debug.class);
System.out.println( value = + debug.value());
System.out.println( name = + ());
}
else {
System.out.println(@Debug is not found.);
}
Annotation[] annotations = method.getAnnotations();
for(Annotation annotation : annotations) {
System.out.println(
annotation.annotationType().getName());
}
}
}
程式的执行结果如下:
@Debug is found.
value = unit
name = debug1
onlyfun.caterpillar.Debug

‘贰’ Java 什么是注解及注解原理详细介绍

1、注解是针对Java编译器的说明。

可以给Java包、类型(类、接口、枚举)、构造器、方法、域、参数和局部变量进行注解。Java编译器可以根据指令来解释注解和放弃注解,或者将注解放到编译后的生成的class文件中,运行时可用。

2、注解和注解类型

注解类型是一种特殊的接口类型,注解是注解注解类型的一个实例。

注解类型也有名称和成员,注解中包含的信息采用键值对形式,可以有0个或多个。

3、Java中定义的一些注解:

@Override 告诉编译器这个方法要覆盖一个超类方法,防止程序员覆盖出错。

@Deprecated 这个标识方法或类(接口等类型)过期,警告用户不建议使用。

@SafeVarargs JDK7新增,避免可变参数在使用泛型化时候警告”执行时期无法具体确认参数类型“,当然,也可以用@SuppressWarnings来避免检查,显然后者的抑制的范围更大。

@SuppressWarnings(value={"unchecked"}) 抑制编译警告,应用于类型、构造器、方法、域、参数以及局部变量。 value是类型数组,有效取值为:

all, to suppress all warnings

boxing, to suppress warnings relative to boxing/unboxing operations

cast, to suppress warnings relative to cast operations

dep-ann, to suppress warnings relative to deprecated annotation

deprecation, to suppress warnings relative to deprecation

fallthrough, to suppress warnings relative to missing breaks in switch statements

finally, to suppress warnings relative to finally block that don't return

hiding, to suppress warnings relative to locals that hide variable

incomplete-switch, to suppress warnings relative to missing entries in a switch statement (enum case)

javadoc, to suppress warnings relative to javadoc warnings

nls, to suppress warnings relative to non-nls string literals

null, to suppress warnings relative to null analysis

rawtypes, to suppress warnings relative to usage of raw types

restriction, to suppress warnings relative to usage of discouraged or forbidden references

serial, to suppress warnings relative to missing serialVersionUID field for a serializable class

static-access, to suppress warnings relative to incorrect static access

static-method, to suppress warnings relative to methods that could be declared as static

super, to suppress warnings relative to overriding a method without super invocations

synthetic-access, to suppress warnings relative to unoptimized access from inner classes

unchecked, to suppress warnings relative to unchecked operations

unqualified-field-access, to suppress warnings relative to field access unqualified

unused, to suppress warnings relative to unused code and dead code

4、注解的定义

使用 @interface 关键字声明一个注解

public @interface MyAnnotation1

注解中可以定义属性

String name default “defval”;

value是注解中的特殊属性

注解中定义的属性如果名称为 value, 此属性在使用时可以省写属性名

例如,声明一个注解:

@Retention(RetentionPolicy.RUNTIME)

public @interface MyAnno1 {

String msg();

int value();

}

‘叁’ 如何实现自定义Java编译时注解功能

自定义注解,可以应用到反射中,比如自己写个小框架。
如实现实体类某些属性不自动赋值,或者验证某个对象属性完整性等
本人自己用过的验证属性值完整性:

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface IgnoreProperty {
}
然后实体类中:
public class TarResearch implements Serializable{

@IgnoreProperty
private static final long serialVersionUID = 1L;

@IgnoreProperty
private Integer researchId;

@IgnoreProperty
private TarUser userId;

private String version;

private String grade;
....
}

然后action类中
// 验证数据完整性

Class<TarResearch > userClass = TarResearch .class;

Field[] field = userClass.getDeclaredFields();

for (int i = 0; i < field.length; i++) {

if (field[i].getAnnotation(IgnoreProperty.class) != null) {

continue;

}

String fie = field[i].getName().substring(0, 1).toUpperCase()

+ field[i].getName().substring(1);

Method method = userClass.getMethod("get" + fie);

Object obj = method.invoke(u);

if (obj == null) {

sendResponseMsg(response, "数据错误");

return null;

}

}

‘肆’ Java编译时注解和运行时注解有什么区别

重写,重载,泛型,分别是在运行时还是编译时执行的

1. 方法重载是在编译时执行的,因为,在编译的时候,如果调用了一个重载的方法,那么编译时必须确定他调用的方法是哪个。如:

当调用evaluate("hello")时候,我们在编译时就可以确定他调用的method #1.

2.
方法的重写是在运行时进行的。这个也常被称为运行时多态的体现。编译器是没有办法知道它调用的到底是那个方法,相反的,只有在jvm执行过程中,才知晓到底是父子类中的哪个方法被调用了当有如下一个接口的时候,我们是无法确定到底是调用父类还是子类的方法

3.
泛型(类型检测),这个发生在编译时。编译器会在编译时对泛型类型进行检测,并吧他重写成实际的对象类型(非泛型代码),这样就可以被JVM执行了。这个过程被称为"类型擦除"。

类型擦除的关键在于从泛型类型中清除类型参数的相关信息,并且再必要的时候添加类型检查和类型转换的方法。

类型擦除可以简单的理解为将泛型java代码转换为普通java代码,只不过编译器更直接点,将泛型java代码直接转换成普通java字节码。类型擦除的主要过程如下:

1). 将所有的泛型参数用其最左边界(最顶级的父类型)类型替换。

2). 移除所有的类型参数。

在编译后变成:

4. 注解。注解即有可能是运行时也有可能是编译时。

如java中的@Override注解就是典型的编译时注解,他会在编译时会检查一些简单的如拼写的错误(与父类方法不相同)等

同样的@Test注解是junit框架的注解,他是一个运行时注解,他可以在运行时动态的配置相关信息如timeout等。

5. 异常。异常即有可能是运行时异常,也有可能是编译时异常。

RuntimeException是一个用于指示编译器不需要检查的异常。RuntimeException
是在jvm运行过程中抛出异常的父类。对于运行时异常是不需要再方法中显示的捕获或者处理的。

已检查的异常是被编译器在编译时候已经检查过的异常,这些异常需要在try/catch块中处理的异常。

6. AOP. Aspects能够在编译时,预编译时以及运行时使用。

1).
编译时:当你拥有源码的时候,AOP编译器(AspectJ编译器)能够编译源码并生成编织后的class。这些编织进入的额外功能是在编译时放进去的。

2). 预编译时:织入过程有时候也叫二进制织入,它是用来织入到哪些已经存在的class文件或者jar中的。

3). 运行时:当被织入的对象已经被加载如jvm中后,可以动态的织入到这些类中一些信息。

7. 继承:继承是编译时执行的,它是静态的。这个过程编译后就已经确定

8. 代理(delegate):也称动态代理,是在运行时执行。

阅读全文

与java注解编译器相关的资料

热点内容
工作三年的大专程序员 浏览:728
java毕业设计文献 浏览:143
筹码集中度指标源码 浏览:482
listsortjava 浏览:186
plc闪光电路编程实例 浏览:299
socket编程试题 浏览:206
华为的服务器怎么设置从光驱启动 浏览:871
程序员真的累吗 浏览:328
学信网app为什么刷脸不了 浏览:874
天蝎vs程序员 浏览:996
单片机下载口叫什么 浏览:190
程序员的道 浏览:926
云服务器不实名违法吗 浏览:558
怎样查看文件夹图片是否重复 浏览:995
文件怎么导成pdf文件 浏览:808
打开sql表的命令 浏览:103
安卓手机如何面部支付 浏览:38
天元数学app为什么登录不上去 浏览:825
明日之后为什么有些服务器是四个字 浏览:104
安卓系统l1是什么意思 浏览:26