javaannotation类型(Java annotation)

今天给各位分享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类型的二维数组

javaannotation类型(Java 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的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

本文来自投稿,不代表【】观点,发布者:【

本文地址: ,如若转载,请注明出处!

举报投诉邮箱:253000106@qq.com

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2024年4月2日 19:58:03
下一篇 2024年4月2日 20:05:12

相关推荐

  • java8种基本类型范围的简单介绍

    java中常用的数据类型有哪些 1、java数据类型分为基本数据类型和引用数据类型,基本数据类型有boolean 、long 、int 、char、byte、short、double、float。引用数据类型有类类型、接口类型和数组类型。 2、java中包含的基本数据类型介绍:\x0d\x0aJava共支持8种内置数据类型。内置类型由Java语言预先定义好,…

    2024年5月23日
    4700
  • c语言用string定义字符串,c语言中用string类型来处理字符串类型

    C++怎样定义定义字符串 1、第一是字符数组来表示字符串。用下面的语句声明:char a[10];C语言中字符数组与字符串的唯一区别是字符串末尾有一个结束符\0,而字符数组不需要。 2、在C中定义字符串有下列几种形式:字符串常量,char数组,char指针 字符串常量 即:位于一对双括号中的任何字符。双引号里的字符加上编译器自动提供的结束标志\0字符,作为 …

    2024年5月23日
    4300
  • 在excel中没有的图表类型是,excel2010图表中可以没有图例

    excel图表类型中不包括那一种类型的图表 1、在excel表格图表中没有的图形类型是扇形图。Excel中图表的基本类型包括:柱形图:表示某一时间段内数据的变化情况或比较各项数据之间的差异。分类在水平方向组织,而数据在垂直方向的组织,以强调时间的变化。 2、在excel表格图表中不存在的图表类型是扇形图。根据查询相关公开信息显示,在excel表格图表中的图表…

    2024年5月23日
    5600
  • java中时间戳类型,java时间戳类型格式

    postgresql的timestamp对应java什么类型 Timestamp是 java.sql.Date的子类, Timestamp 也提供支持时间戳值的 JDBC 转义语法的格式化和解析操作的能力。 对应java中的java.sql.Timestamp类型(注意命名空间)。保存到数据库这样做:Timestamp.valueOf(时间);注意时间的格式…

    2024年5月23日
    4000
  • java判断数值类型,java判断是否数字类型

    java中如何判断输入的是int还是double? 1、你可以用正则表达式判断输入的数据是否输入的字符可以转化为你需要的类型,当然也可以用异常处理机制或其他方法判断是否可转。 2、首先,前提是不知道有哪些对象类型,以及其表现形式。其次,2楼说的对,Java输入都是String。因此,我们设计一下要做的事情: 提供一组对象类型名称和判断方法的注册列表。 3、类…

    2024年5月23日
    3900
  • java中string类型的数组的声明,java定义一个string类型的数组

    java中如何创建字符串数组? 字符串数组的创建:String a =new String(Java);不过推荐用ArrayList strArray = new ArrayList (); 比较灵活。 在Java核心API中,有许多应用final的例子,例如java.lang.String,整个类都是final的。为类指定final修饰符可以让类不可以被继…

    2024年5月23日
    3900
  • c语言隐式转换运算转换还有什么,c语言的隐式类型转换

    C语言数据类型的转换 不同类型的数据混合运算,自动转换成最复杂的类型。强制类型转换。float fA = 0.2; int iB = (int)fA;指针强制类型转换。 类型转化的原则是从低级向高级自动转化(除非人为的加以控制)。 第二个是5和2匙整形的先计算,等于2,然后再强制转换成float赋给x。 因此需要转换数据类型来解决,方法是:float c=f…

    2024年5月23日
    4700
  • c语言char*a,c语言char类型的数字转为int数字

    C语言中的char具体用法 C语言中char用于定义字符类型变量或字符指针变量,例如“char a;”则定义了变量a是字符类型,“char *a;”则定义了变量a是字符指针类型。 char是C语言关键字之一,用来表示基础类型字符型。每个char变量占一个字节,表示范围为-128到127。char用来存储字符时,实际存储值为对应的ASCII码值。char类型可…

    2024年5月23日
    5400
  • java基本类型内存,java基本类型内存占用

    java基本类型之间的共同点是什么 1、简单性: Java语言继承了C++语言的优点,去掉了C++中学习起来比较难的多继承、指针等概念,所以Java语言学习起来更简单,使用起来也更方便。面向对象: Java是一种面向对象的编程语言。 2、JavaServlet是一种开发Web应用的理想构架。JSP以Servlet技术为基础,又在许多方面作了改进。JSP页面看…

    2024年5月23日
    4000
  • java判断类,java判断类类型

    java判断数字类型(小数和整数) 1、用Double 解析判断是否为数字,如果抛出异常退出,正确接着解析是否为整数.value.indexOf(.)-1表示为小数/*哪怕像00这样的数同样是小数*/,==-1表示为整数。接着用value.indexOf(-)判断是否为负数。 2、正负好判断,这个还要交就等于没入门。小数整数不就是,多个精度。你用double…

    2024年5月22日
    4200

发表回复

登录后才能评论



关注微信