基本类
package com.user;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
@Component
@Controller
public class User {
private Long age;
private String name;
public String sex;
public String getName(){
return this.name;
}
private String getSex(){
return sex;
}
public void speakCity(String name){
System.out.println(name);
};
}
test方法
@Test
void ann() {
Class<User> userClass = User.class;
Annotation[] declaredAnnotations = userClass.getDeclaredAnnotations();
Annotation[] annotations = userClass.getAnnotations();
AnnotatedType[] annotatedInterfaces = userClass.getAnnotatedInterfaces();
System.out.println(Arrays.toString(annotatedInterfaces));
}
@Test
void methode() throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
Class<User> userClass = User.class;
User user = userClass.newInstance();
Method[] declaredMethods = userClass.getDeclaredMethods();
Method[] methods = userClass.getMethods();
Method enclosingMethod = userClass.getEnclosingMethod();
for (Method declaredMethod : declaredMethods) {
Type[] genericParameterTypes = declaredMethod.getGenericParameterTypes();
Type genericReturnType = declaredMethod.getGenericReturnType();
Class<?>[] exceptionTypes = declaredMethod.getExceptionTypes();
System.out.println("方法名称:"+ declaredMethod.getName() +"我是返回类型:"+ genericReturnType);
System.out.println(declaredMethod.getName());
}
Method speakCity = userClass.getMethod("speakCity", String.class);
speakCity.invoke(user,"你好");
System.out.println(enclosingMethod);
}
@Test
void constructors() throws InvocationTargetException, InstantiationException, IllegalAccessException {
Class<User> userClass = User.class;
Constructor<?>[] declaredConstructors = userClass.getDeclaredConstructors();
for (Constructor<?> declaredConstructor : declaredConstructors) {
Object o = declaredConstructor.newInstance();
declaredConstructor.getGenericParameterTypes();
}
}