
本文共 15064 字,大约阅读时间需要 50 分钟。
���������������������������,���������������������,���������������������.������ElementType���������������������������,���������������?������������������������������������.
������������������
package com.datang.pet.data.anno;public @interface NotNull {}
���������������������������@NotNull������.���������������������������������������������.
package com.datang.pet.data.anno;@NotNullpublic class TestBean { @NotNull public String name; @NotNull public TestBean(){} @NotNull public void show(@NotNull int age){ @NotNull int s = 12; }}
���������������������������@NotNull������,������������,���������������������������������������������������������������������.
������������������ @Target
������������������������������������������,���������������������JDK������������������,������������������������������������������������.
@Target���������������������.���������������������������������@NotNull������������������.@Target���������value���������������������������,������������������������ElementType������������
/* * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * * * * * * * * * * * * * * * * * * * * */package java.lang.annotation;/** * Indicates the contexts in which an annotation type is applicable. The * declaration contexts and type contexts in which an annotation type may be * applicable are specified in JLS 9.6.4.1, and denoted in source code by enum * constants of {@link ElementType java.lang.annotation.ElementType}. * *If an {@code @Target} meta-annotation is not present on an annotation type * {@code T} , then an annotation of type {@code T} may be written as a * modifier for any declaration except a type parameter declaration. * *
If an {@code @Target} meta-annotation is present, the compiler will enforce * the usage restrictions indicated by {@code ElementType} * enum constants, in line with JLS 9.7.4. * *
For example, this {@code @Target} meta-annotation indicates that the * declared type is itself a meta-annotation type. It can only be used on * annotation type declarations: *
* @Target(ElementType.ANNOTATION_TYPE) * public @interface MetaAnnotationType { * ... * } ** *This {@code @Target} meta-annotation indicates that the declared type is * intended solely for use as a member type in complex annotation type * declarations. It cannot be used to annotate anything directly: *
* @Target({}) * public @interface MemberType { * ... * } ** *It is a compile-time error for a single {@code ElementType} constant to * appear more than once in an {@code @Target} annotation. For example, the * following {@code @Target} meta-annotation is illegal: *
* @Target({ElementType.FIELD, ElementType.METHOD, ElementType.FIELD}) * public @interface Bogus { * ... * } ** * @since 1.5 * @jls 9.6.4.1 @Target * @jls 9.7.4 Where Annotations May Appear */@Documented@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.ANNOTATION_TYPE)public @interface Target { /** * Returns an array of the kinds of elements an annotation type * can be applied to. * @return an array of the kinds of elements an annotation type * can be applied to */ ElementType[] value();}
/* * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * * * * * * * * * * * * * * * * * * * * */package java.lang.annotation;/** * The constants of this enumerated type provide a simple classification of the * syntactic locations where annotations may appear in a Java program. These * constants are used in {@link Target java.lang.annotation.Target} * meta-annotations to specify where it is legal to write annotations of a * given type. * *The syntactic locations where annotations may appear are split into * declaration contexts , where annotations apply to declarations, and * type contexts , where annotations apply to types used in * declarations and expressions. * *
The constants {@link #ANNOTATION_TYPE} , {@link #CONSTRUCTOR} , {@link * #FIELD} , {@link #LOCAL_VARIABLE} , {@link #METHOD} , {@link #PACKAGE} , * {@link #PARAMETER} , {@link #TYPE} , and {@link #TYPE_PARAMETER} correspond * to the declaration contexts in JLS 9.6.4.1. * *
For example, an annotation whose type is meta-annotated with * {@code @Target(ElementType.FIELD)} may only be written as a modifier for a * field declaration. * *
The constant {@link #TYPE_USE} corresponds to the 15 type contexts in JLS * 4.11, as well as to two declaration contexts: type declarations (including * annotation type declarations) and type parameter declarations. * *
For example, an annotation whose type is meta-annotated with * {@code @Target(ElementType.TYPE_USE)} may be written on the type of a field * (or within the type of the field, if it is a nested, parameterized, or array * type), and may also appear as a modifier for, say, a class declaration. * *
The {@code TYPE_USE} constant includes type declarations and type * parameter declarations as a convenience for designers of type checkers which * give semantics to annotation types. For example, if the annotation type * {@code NonNull} is meta-annotated with * {@code @Target(ElementType.TYPE_USE)}, then {@code @NonNull} * {@code class C {...}} could be treated by a type checker as indicating that * all variables of class {@code C} are non-null, while still allowing * variables of other classes to be non-null or not non-null based on whether * {@code @NonNull} appears at the variable's declaration. * * @author Joshua Bloch * @since 1.5 * @jls 9.6.4.1 @Target * @jls 4.1 The Kinds of Types and Values */public enum ElementType { /** Class, interface (including annotation type), or enum declaration */ TYPE, /** Field declaration (includes enum constants) */ FIELD, /** Method declaration */ METHOD, /** Formal parameter declaration */ PARAMETER, /** Constructor declaration */ CONSTRUCTOR, /** Local variable declaration */ LOCAL_VARIABLE, /** Annotation type declaration */ ANNOTATION_TYPE, /** Package declaration */ PACKAGE, /** * Type parameter declaration * * @since 1.8 */ TYPE_PARAMETER, /** * Use of a type * * @since 1.8 */ TYPE_USE}
���������������@NotNull������������������������������.
package com.datang.pet.data.anno;import java.lang.annotation.ElementType;import java.lang.annotation.Target;@Target(value = {ElementType.TYPE})public @interface NotNull {}
������������@Target������������value���������ElementType.TYPE���,������������@NotNull���������TestBean������������.������@NotNull���������������������������,���������������.������������,������������������������������������@Target���������,���������������������������,���������������@Target������������������������������@Target���������value���.
���������@NotNull������������������������������������.
package com.datang.pet.data.anno;import java.lang.annotation.ElementType;import java.lang.annotation.Target;@Target(value = {ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.CONSTRUCTOR, ElementType.LOCAL_VARIABLE})public @interface NotNull {}
@Target������������������6������������������,������������,������,������������,���������,������������,������������.���������������������������.������JDK������1.8������������10������������������.
���������������
���������@Target���������value,������������������������������������������.
import java.lang.annotation.ElementType;import java.lang.annotation.Target;@Target(value = {ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.CONSTRUCTOR, ElementType.LOCAL_VARIABLE})public @interface NotNull { String value();}
������������������������@NotNull���������������value������.������������@NotNull���������������������,���������,���������������value������,���������������������.
@NotNull(value = "a")public class TestBean { @NotNull(value = "a") public String name; @NotNull(value = "a") public TestBean() { } @NotNull("a") public void show(@NotNull("a") int age) { @NotNull("a") int s = 12; }}
���������������������,���������������������value������������������������������������������.
���������������������,���������������value���������.
import java.lang.annotation.ElementType;import java.lang.annotation.Target;@Target(value = {ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.CONSTRUCTOR, ElementType.LOCAL_VARIABLE})public @interface NotNull { String value(); String name();}
������������������������������,������������������������,������������������������,������������������������������������.
import java.lang.annotation.ElementType;import java.lang.annotation.Target;@Target(value = {ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.CONSTRUCTOR, ElementType.LOCAL_VARIABLE})public @interface NotNull { String value(); String name(); int age() default 100;}
������������
���������������������������������,���������������������������.���������������������������������������������������������������,������.
������������������������������������������������?���������������������������������������������@Retention������.������������value������,������������������RetentionPolicy
���������������������������.
/* * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * * * * * * * * * * * * * * * * * * * * */package java.lang.annotation;/** * Annotation retention policy. The constants of this enumerated type * describe the various policies for retaining annotations. They are used * in conjunction with the {@link Retention} meta-annotation type to specify * how long annotations are to be retained. * * @author Joshua Bloch * @since 1.5 */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���������class���������,���������������,���������������class���������������������������������,���������������������������������.RUNTIME���������������VM���������������������������������������������������
package com.datang.pet.data.anno;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target(value = {ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.CONSTRUCTOR, ElementType.LOCAL_VARIABLE})@Retention(RetentionPolicy.RUNTIME)public @interface NotNull { String value(); String name(); int age() default 100;}
������������������������
package com.datang.pet.data.anno;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.lang.reflect.Parameter;public class Main { public static void main(String[] args) throws Exception { TestBean testBean = new TestBean(); Class aClass = testBean.getClass(); NotNull annotation = aClass.getAnnotation(NotNull.class); if (annotation != null) { System.out.println("TestBean���������@NotNull������"); String value = annotation.value(); String name = annotation.name(); int age = annotation.age(); System.out.println(value + "--" + name + "--" + age); } else { System.out.println("TestBean������������@NotNull������"); } Field nameField = aClass.getField("name"); NotNull annotation1 = nameField.getAnnotation(NotNull.class); if (annotation1 != null) { System.out.println("name������@NotNull������"); String value = annotation1.value(); String name = annotation1.name(); int age = annotation1.age(); System.out.println(value + "--" + name + "--" + age); } else { System.out.println("name���������@NotNull������"); } Constructor declaredConstructor = aClass.getDeclaredConstructor(); NotNull annotation2 = declaredConstructor.getAnnotation(NotNull.class); if (annotation2 != null) { System.out.println("���������������@NotNull������"); String value = annotation2.value(); String name = annotation2.name(); int age = annotation2.age(); System.out.println(value + "--" + name + "--" + age); } else { System.out.println("������������������@NotNull������"); } Method showMethod = aClass.getMethod("show",int.class); NotNull annotation3 = showMethod.getAnnotation(NotNull.class); if (annotation3 != null) { System.out.println("show������������@NotNull������"); String value = annotation3.value(); String name = annotation3.name(); int age = annotation3.age(); System.out.println(value + "--" + name + "--" + age); } else { System.out.println("show���������������@NotNull������"); } Parameter[] parameters = showMethod.getParameters(); for (Parameter parameter:parameters){ String name1 = parameter.getName(); if (parameter.getName().equals("age")){ NotNull annotation4 = parameter.getAnnotation(NotNull.class); System.out.println("age������������@NotNull������"); String value = annotation4.value(); String name = annotation4.name(); int age = annotation4.age(); System.out.println(value + "--" + name + "--" + age); } } }}
������������������������������������?
package com.datang.pet.data.anno;public class TestBean2 extends TestBean{}
���������������,������������������������������������������.
@Inherited������������������������������������,������������������������������.���������������class,������������������,������������������.
package com.datang.pet.data.anno;import java.lang.annotation.*;@Target(value = {ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.CONSTRUCTOR, ElementType.LOCAL_VARIABLE})@Retention(RetentionPolicy.RUNTIME)@Inheritedpublic @interface NotNull { String value(); String name(); int age() default 100;}
发表评论
最新留言
关于作者
