今天给各位分享javaannotation类型的知识,其中也会对Java annotation进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
1、java 类后面跟[][]是什么意思,比如Annotation[][]2、JAVA中Annotation是什么,有什么用3、java.lang.annotation 是哪个包4、在JAVA中,定义方法的时候,带参数的方法这个概念怎么说?5、java怎么查看annotation 实现6、java中的 Annotation类。希望高手能够简单明了解释下用法和作用
java 类后面跟[][]是什么意思,比如Annotation[][]
Annotation[][],它是代表的一个Annotation类型的二维数组;
public Annotation[][] getParameterAnnotations(),Annotation[][]表示的是getParameterAnnotations()这个方法的返回值的类型是:Annotation类型的二维数组
JAVA中Annotation是什么,有什么用
请输入你Annotation提供了一条与程序元素关联任何或者任何元数据(metadata)的途径。从某些方面看,annotation就像修饰符一样被使用,并应用于包、类型、构造方法、方法、成员变量、参数、本地变量的声明中。这些被存储在annotation的“name=value”结构对中。annotation类型是一种接口,能够通过反射API的方式提供对其的访问。annotation能被用来为某个程序元素(类、方法、成员变量等)关联任何的。需要注意的是,这里存在着一个基本的潜规则:annotaion不能影响程序代码的执行,无论增加、删除annotation,代码都始终如一的执行。另外,尽管一些annotation通过java的反射api方法在运行时被访问,而java语言解释器在工作时忽略了这些annotation。正是由于忽略了annotation,导致了annotation类型在代码中是“不起作用”的;只有通过某种配套的工具才会对annotation类型中的进行访问和处理。本文中将涵盖标准的annotation和meta-annotation类型,陪伴这些annotation类型的工具是java编译器(当然要以某种特殊的方式处理它们)。由于上述原因,annotation在使用时十分简便。一个本地变量可以被一个以NonNull命名的annotation类型所标注,来作为对这个本地变量不能被赋予null值的断言。而我们可以编写与之配套的一个annotation代码,使用它来对具有前面变量的代码进行解析,并且尝试验证这个断言。当然这些代码并不必自己编写。在JDK安装后,在JDK/bin目录中可以找到名为“apt”的工具,它提供了处理annotation的框架:它启动后扫描源代码中的annotation,并调用我们定义好的annotation处理器完成我们所要完成的工作(比如验证前面例子中的断言)。说到这里,annotation的强大功能似乎可以替代XDoclet这类的工具了,随着我们的深入,大家会更加坚信这一点的答案… 拿别人的,希望可以帮到你~
java.lang.annotation 是哪个包
1、先看一下annotation包下的类,作者的jdk是1.6版本。
Annotation是接口,the common interface extended by all annotation types,意思就就是说Annotation是所有注解类型的父类型,个人感觉就像是注解’界‘的Object,Annotation有四个方法,但是自定义的注解是不可以重写Annotation这四个方法的。这与Object中的不同之处。
2、@interface Retention
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
RetentionPolicy value();
}
Retention:“保留,拘留”的意思。在注解中用于标识注解被保留多久(由于注解的保留的属性不同,在jvm中驻留的时间也不同)如果在注解上没加@Retention注解,则默认的保留策略是ClASS。该注解作用范围就是在注解上,用于属性和方法是不起作用的。
3、RetentionPolicy
接上面的策略说:
public enum RetentionPolicy {
/**
* Annotations are to be discarded by the compiler.
*/
SOURCE,
/**
* Annotations are to be recorded in the class file by the compiler
* but need not be retained by the VM at run time. This is the default
* behavior.
*/
CLASS,
/**
* 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.
*
* @see java.lang.reflect.AnnotatedElement
*/
RUNTIME
}
SOURCE:注解将在编译的时候被忽略;
ClASS:编译之后注解将被保留在字节码文件中,但是在不会在vm运行时保留。默认策略;
RUNTIME:注解会保留在字节码文件中,同时在vm运行时始终存在,而且可以通过反射获取相对应的信息。
在JAVA中,定义方法的时候,带参数的方法这个概念怎么说?
Java中注解(Annotation)的根本概念啥是注解(Annotation):
Annotation(注解)即是Java供给了一种元程序中的元素相关 任何信息和着任何元数据(metadata)的途径和办法。Annotion(注解)是一个接口,程序能够通过反射 来获取指定程序元素的Annotion目标,然后通过Annotion目标来获取注解里边的元数据。
Annotation(注解)是JDK5.0及今后版别引进的。它能够用于创立文档,盯梢代码中的依赖性, 乃至履行根本编译时查看。
从某些方面看,annotation就像润饰符相同被运用,并应用于包、类 型、 布局办法、办法、成员变量、参数、本地变量的声明中。这些信息被存储在Annotation的“name=value ”布局对中。
Annotation的成员在Annotation类型中以无参数的办法的办法被声明。其办法名 和返回值界说了该成员的姓名和类型。
在此有一个特定的默许语法:答应声明任何Annotation成员的默 认值:一个Annotation能够将name=value对作为没有界说默许值的Annotation成员的值,当然也能够使 用name=value对来掩盖其它成员默许值。
这一点有些近似类的承继特性,父类的布局函数能够作为子类 的默许布局函数,可是也能够被子类掩盖。
Annotation能被用来为某个程序元素(类、办法、 成员变量等)相关任何的信息。需求注意的是,这里存在着一个根本的规矩:Annotation不能影响程序 代码的履行,无论添加、删去。
Annotation,代码都始终如一的履行。
另外,尽管一些annotation通过 java的反射api办法在运转时被拜访,而java言语解说器在作业时疏忽了这些annotation。
正是因为 java虚拟机疏忽了Annotation,致使了annotation类型在代码中是“不起效果”的;
需通过某种配套的东西才会对annotation类型中的信息进行拜访和处置。
这篇文章中将包括规范的Annotation和meta- annotation类型,陪同这些annotation类型的东西是java编译器(当然要以某种特另外办法处置它们) 。
java怎么查看annotation 实现
深入理解Java:注解(Annotation)自定义注解入门
要深入学习注解,我们就必须能定义自己的注解,并使用注解,在定义自己的注解之前,我们就必须要了解Java为我们提供的元注解和相关定义注解的语法。
元注解:
元注解的作用就是负责注解其他注解。Java5.0定义了4个标准的meta-annotation类型,它们被用来提供对其它 annotation类型作说明。Java5.0定义的元注解:
1.@Target,
2.@Retention,
3.@Documented,
4.@Inherited
这些类型和它们所支持的类在java.lang.annotation包中可以找到。下面我们看一下每个元注解的作用和相应分参数的使用说明。
@Target:
@Target说明了Annotation所修饰的对象范围:Annotation可被用于 packages、types(类、接口、枚举、Annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数)。在Annotation类型的声明中使用了target可更加明晰其修饰的目标。
作用:用于描述注解的使用范围(即:被描述的注解可以用在什么地方)
取值(ElementType)有:
1.CONSTRUCTOR:用于描述构造器
2.FIELD:用于描述域
3.LOCAL_VARIABLE:用于描述局部变量
4.METHOD:用于描述方法
5.PACKAGE:用于描述包
6.PARAMETER:用于描述参数
7.TYPE:用于描述类、接口(包括注解类型) 或enum声明
使用实例:
@Target(ElementType.TYPE)
public @interface Table {
/**
* 数据表名称注解,默认值为类名称
* @return
*/
public String tableName() default “className”;
}
@Target(ElementType.FIELD)
public @interface NoDBColumn {
}
注解Table 可以用于注解类、接口(包括注解类型) 或enum声明,而注解NoDBColumn仅可用于注解类的成员变量。
@Retention:
@Retention定义了该Annotation被保留的时间长短:某些Annotation仅出现在源代码中,而被编译器丢弃;而另一些却被编译在class文件中;编译在class文件中的Annotation可能会被虚拟机忽略,而另一些在class被装载时将被读取(请注意并不影响class的执行,因为Annotation与class在使用上是被分离的)。使用这个meta-Annotation可以对 Annotation的“生命周期”限制。
作用:表示需要在什么级别保存该注释信息,用于描述注解的生命周期(即:被描述的注解在什么范围内有效)
取值(RetentionPoicy)有:
1.SOURCE:在源文件中有效(即源文件保留)
2.CLASS:在class文件中有效(即class保留)
3.RUNTIME:在运行时有效(即运行时保留)
Retention meta-annotation类型有唯一的value作为成员,它的取值来自java.lang.annotation.RetentionPolicy的枚举类型值。具体实例如下:
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Column {
public String name() default “fieldName”;
public String setFuncName() default “setField”;
public String getFuncName() default “getField”;
public boolean defaultDBValue() default false;
}
Column注解的的RetentionPolicy的属性值是RUTIME,这样注解处理器可以通过反射,获取到该注解的属性值,从而去做一些运行时的逻辑处理
@Documented:
@Documented用于描述其它类型的annotation应该被作为被标注的程序成员的公共API,因此可以被例如javadoc此类的工具文档化。Documented是一个标记注解,没有成员。
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Column {
public String name() default “fieldName”;
public String setFuncName() default “setField”;
public String getFuncName() default “getField”;
public boolean defaultDBValue() default false;
}
@Inherited:
@Inherited 元注解是一个标记注解,@Inherited阐述了某个被标注的类型是被继承的。如果一个使用了@Inherited修饰的annotation类型被用于一个class,则这个annotation将被用于该class的子类。
注意:@Inherited annotation类型是被标注过的class的子类所继承。类并不从它所实现的接口继承annotation,方法并不从它所重载的方法继承annotation。
当@Inherited annotation类型标注的annotation的Retention是RetentionPolicy.RUNTIME,则反射API增强了这种继承性。如果我们使用java.lang.reflect去查询一个@Inherited annotation类型的annotation时,反射代码检查将展开工作:检查class和其父类,直到发现指定的annotation类型被发现,或者到达类继承结构的顶层。
java中的 Annotation类。希望高手能够简单明了解释下用法和作用
java.lang.annotation,接口 Annotation。对于Annotation,是Java5的新特性,JDK5引入了Metedata(元数据)很容易的就能够调用Annotations.Annotations提供一些本来不属于程序的数据,比如:一段代码的作者或者告诉编译器禁止一些特殊的错误。An annotation 对代码的执行没有什么影响。Annotations使用@annotation的形势应用于代码:类(class),属性(field),方法(method)等等。一个Annotation出现在上面提到的开始位置,而且一般只有一行,也可以包含有任意的参数。
public interface Annotation所有 annotation 类型都要扩展的公共接口。注意,手动扩展该公共接口的接口不 定义 annotation 类型。还要注意此接口本身不定义 annotation 类型。 对于Annotation,是Java5的新特性,下面是Sun的Tutorial的描述,因为是英文,这里我翻译下,希望能够比较清晰的描述一下Annotation的语法以及思想。Annotation:Release 5.0 of the JDK introduced a metadata facility called annotations. Annotations provide data about a program that is not part of the program, such as naming the author of a piece of code or instructing the compiler to suppress specific errors. An annotation has no effect on how the code performs. Annotations use the form @annotation and may be applied to a program’s declarations: its classes, fields, methods, and so on. The annotation appears first and often (by convention) on its own line, and may include optional arguments: JDK5引入了Metedata(元数据)很容易的就能够调用Annotations.Annotations提供一些本来不属于程序的数据,比如:一段代码的作者或者告诉编译器禁止一些特殊的错误。An annotation 对代码的执行没有什么影响。Annotations使用@annotation的形势应用于代码:类(class),属性(field),方法(method)等等。一个Annotation出现在上面提到的开始位置,而且一般只有一行,也可以包含有任意的参数。@Author(“MyName”)class myClass() { } or @SuppressWarnings(“unchecked”)void MyMethod() { } Defining your own annotation is an advanced technique that won’t be described here, but there are three built-in annotations that every Java programmer should know: @Deprecated, @Override, and @SuppressWarnings. The following example illustrates all three annotation types, applied to methods: 定义自己的Annotation是一个比较高级的技巧,这里我们不做讨论,这里我们仅仅讨论每一个Java programer都应该知道的内置的annotations:@Deprecated, @Override, and @SuppressWarnings。下面的程序阐述了这三种annotation如何应用于methods。import java.util.List; class Food {} class Hay extends Food {} class Animal { Food getPreferredFood() { return null; } /** * @deprecated document why the method was deprecated */ @Deprecated static void deprecatedMethod() { } } class Horse extends Animal { Horse() { return; } @Override Hay getPreferredFood() { return new Hay(); } @SuppressWarnings(“deprecation”) void useDeprecatedMethod() { Animal.deprecateMethod(); //deprecation warning – suppressed }} } } @DeprecatedThe @Deprecated annotation indicates that the marked method should no longer be used. The compiler generates a warning whenever a program uses a deprecated method, class, or variable. When an element is deprecated, it should be documented using the corresponding @deprecated tag, as shown in the preceding example. Notice that the tag starts with a lowercase “d” and the annotation starts with an uppercase “D”. In general, you should avoid using deprecated methods — consult the documentation to see what to use instead. @Deprecated@Deprecated annotation标注一个method不再被使用。编译器在一个program(程序?)使用了不赞成的方法,类,变量的时候会产生警告(warning)。如果一个元素(element:method, class, or variable)不赞成被使用,应该像前面的例子里使用相应的@deprecated 标签,并且注意标签的首字母是小写的”d”,而annotation时大写的”D”。一般情况下,我们应该避免使用不赞成使用的方法(deprecated methods),而应该考虑替代的方法。 @OverrideThe @Override annotation informs the compiler that the element is meant to override an element declared in a superclass. In the preceding example, the override annotation is used to indicate that the getPreferredFood method in the Horse class overrides the same method in the Animal class. If a method marked with @Override fails to override a method in one of its superclasses, the compiler generates an error. While it’s not required to use this annotation when overriding a method, it can be useful to call the fact out explicitly, especially when the method returns a subtype of the return type of the overridden method. This practice, called covariant return types, is used in the previous example: Animal.getPreferredFood returns a Food instance. Horse.getPreferredFood (Horse is a subclass of Animal) returns an instance of Hay (a subclass of Food). For more information, see Overriding and Hiding Methods. @Override@Override annotation 告诉编译器当前元素是重写(override)自父类的一个元素。在前面的例子中,override annotation用来说明Horse类中的getPreferredFood这个方法重写(override)自Animal类中相同的方法。如果一个方法被标注了@Override,但是其父类中没有这个方法时,编译器将会报错。但是并不是说我们一定要使用这个annotation,但是它能够很明显的给出实际行为,尤其是在方法返回一个被重写的方法返回类型的子类型的时候。上面的例子中,Animal.getPreferredFood 返回一个 Food实例,Horse.getPreferredFood 返回一个Hay实例,这里Horse是Animal的子类,Hay是Food的子类。 @SuppressWarningsThe @SuppressWarnings annotation tells the compiler to suppress specific warnings that it would otherwise generate. In the previous example, the useDeprecatedMethod calls a deprecated method of Animal. Normally, the compiler generates a warning but, in this case, it is suppressed. Every compiler warning belongs to a category. The Java Language Specification lists two categories: “deprecation” and “unchecked”. The “unchecked” warning can occur when interfacing with legacy code written before the advent of generics. To suppress more than one category of warnings, use the following syntax: @SuppressWarnings@SuppressWarnings annotation 告诉编译器禁止别的元素产生的特殊的警告(warnings),在前面的例子里,useDeprecatedMethod调用了Animal的不赞成使用的一个方法。一般情况下,编译器会给出一个警告(warning),但是在这种情况下,不会产生这个警告,也就是说被suppress。每个编译器的警告都属于一个类型。Java Language Specification列出了两种类型:”deprecation” 和 “unchecked”。”unchecked” warning 发生在使用非generic的旧代码交互的generic collection类时。为了禁止不止一种的警告时,使用下面的语法:@SuppressWarnings({“unchecked”, “deprecation”})
关于javaannotation类型和Java annotation的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。