内置注解
@Override
@SuppressWarning
元注解:负责注解其他注解
@Target:用于描述注解的使用范围(即:被描述的注解可以用在什么地方)
@Retention:表示需要在什么级别保存该注释信息,用于描述注解的生命周期(source<class<runtime)
@Document:说明该注解将被包含在javadoc中
@Inherited:说明子类可以继承父类中的该注解
public class Test1 {
@MyAnnotation
public void test(){
}
}
@Target(value = ElementType.METHOD)
@Retention(value = RetentionPolicy.RUNTIME)
@Documented
@Inherited
@interface MyAnnotation{
}
自定义注解
public class Test2 {
@MyAnnotation2(name = "秦疆")
public void test(){};
@MyAnnotation3(value = "normal")
public void test2(){};
}
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2{
String name() default "";
}
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation3{
String value();
}
反射
public class Test4 {
public static void main(String[] args) throws ClassNotFoundException {
Person student = new Student();
System.out.println("此人是"+student.name);
Class<? extends Person> c1 = student.getClass();
System.out.println(c1.hashCode());
Class<?> c2 = Class.forName("com.oscar.reflection.Student");
System.out.println(c2.hashCode());
Class<Student> c3 = Student.class;
System.out.println(c3.hashCode());
Class<Integer> c4 = Integer.TYPE;
System.out.println(c4);
Class<?> c1Superclass = c1.getSuperclass();
System.out.println(c1Superclass.hashCode() );
}
}
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
Class c1 = Class.forName("com.oscar.reflection.User");
System.out.println(c1.getName());
System.out.println(c1.getSimpleName());
Field[] fields = c1.getFields();
for (Field field : fields) {
System.out.println(field);
}
fields = c1.getDeclaredFields();
for (Field field : fields) {
System.out.println(field);
}
Field name = c1.getDeclaredField("name");
System.out.println(name);
Field name1 = c1.getField("name");
System.out.println(name);
Method[] methods = c1.getMethods();
Method[] declaredMethods = c1.getDeclaredMethods();
Constructor[] declaredConstructors = c1.getDeclaredConstructors();
Constructor[] constructors = c1.getConstructors();
}
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
Class c1 = Class.forName("com.oscar.reflection.Human");
Human user = (Human) c1.newInstance();
System.out.println(user);
Constructor<?> declaredConstructor = c1.getDeclaredConstructor(String.class, int.class, int.class);
Human worker = (Human) declaredConstructor.newInstance("王凡", 1, 24);
System.out.println(worker);
Human form = (Human) c1.newInstance();
Method setName = c1.getDeclaredMethod("setName", String.class);
setName.invoke(form,"邓聪");
System.out.println(form.getName());
Human user2 = (Human) c1.newInstance();
Field field = c1.getDeclaredField("name");
field.set(user2,"司梦涛");
System.out.println(user2.getName());
}
注解*反射
public class Test8 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
Class<?> student = Class.forName("com.oscar.annotation.demo1.Student");
Annotation[] annotations = student.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
System.out.println(student.getAnnotation(TestInterface.class).value());
Field name = student.getDeclaredField("name");
Annotation[] nameAnnotations = name.getAnnotations();
for (Annotation nameAnnotation : nameAnnotations) {
System.out.println(nameAnnotation);
}
TestInterfaceField annotation = name.getAnnotation(TestInterfaceField.class);
System.out.println(annotation.columnName());
System.out.println(annotation.type());
System.out.println(annotation.length());
}
}
@TestInterface("db_student")
class Student{
@TestInterfaceField(columnName = "db_id",type = "int",length = 20)
String name;
int age;
int id;
public Student() {
}
public Student(String name, int age, int id) {
this.name = name;
this.age = age;
this.id = id;
}
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface TestInterface{
String value();
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface TestInterfaceField{
String columnName();
String type() default "Varchar";
int length() default 255;
}