Java反射
Eave
2019.11.21 12:55
package com.qianyan.reflect;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface NoLogin
{
String[] mchId() default {};
}
package com.qianyan.reflect;
@NoLogin(mchId = {"208171120117418", "208171120117419"})
public class Message
{
private Integer code;
private String msgId;
private String message;
public Integer getCode()
{
return code;
}
public void setCode(Integer code)
{
this.code = code;
}
public String getMsgId()
{
return msgId;
}
public void setMsgId(String msgId)
{
this.msgId = msgId;
}
public String getMessage()
{
return message;
}
public void setMessage(String message)
{
this.message = message;
}
}
package com.qianyan.reflect;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class App
{
@SuppressWarnings("rawtypes")
public static void main(String[] args)
{
try
{
Class> clazz = Class.forName("com.qianyan.reflect.Message");
// Class> clazz = ClassLoader.getSystemClassLoader().loadClass("com.qianyan.reflect.Message");
System.out.println("=========获取所有公用的构造方法=========");
Constructor[] constructors1 = clazz.getConstructors();
for(Constructor constructor : constructors1)
{
System.out.println(constructor);
}
System.out.print("\n");
System.out.println("=========获取所有声明的构造方法=========");
Constructor[] constructors2 = clazz.getDeclaredConstructors();
for(Constructor constructor : constructors2)
{
System.out.println(constructor);
}
System.out.print("\n");
System.out.println("=========获取所有公用的方法=========");
Method[] methods1 = clazz.getMethods();
for(Method method : methods1)
{
System.out.println(method);
}
System.out.print("\n");
System.out.println("=========获取所有声明的方法=========");
Method[] methods2 = clazz.getDeclaredMethods();
for(Method method : methods2)
{
System.out.println(method);
}
System.out.print("\n");
System.out.println("=========获取所有公用的属性=========");
Field[] fields1 = clazz.getFields();
for(Field field : fields1)
{
System.out.println(field);
}
System.out.print("\n");
System.out.println("=========获取所有声明的属性=========");
Field[] fields2 = clazz.getDeclaredFields();
for(Field field : fields2)
{
System.out.println(field);
}
System.out.print("\n");
System.out.println("=========调用方法=========");
Object object = clazz.newInstance();
Method method = clazz.getDeclaredMethod("setMsgId", String.class);
if(method != null)
{
// method.setAccessible(true);
method.invoke(object, "WEIXIN20191115124627840");
}
Message message = (Message) object;
System.out.println(message.getMsgId());
System.out.print("\n");
System.out.println("=========类上是否有NoLogin注解=========");
NoLogin noLogin = clazz.getAnnotation(NoLogin.class);
System.out.println(noLogin);
for(String mchId : noLogin.mchId())
{
System.out.println(mchId);
}
}
catch(Exception e)
{
e.printStackTrace();
} finally
{
}
}
}
=========获取所有公用的构造方法=========
public com.qianyan.reflect.Message()
=========获取所有声明的构造方法=========
public com.qianyan.reflect.Message()
=========获取所有公用的方法=========
public java.lang.String com.qianyan.reflect.Message.getMessage()
public void com.qianyan.reflect.Message.setMsgId(java.lang.String)
public java.lang.String com.qianyan.reflect.Message.getMsgId()
public void com.qianyan.reflect.Message.setMessage(java.lang.String)
public void com.qianyan.reflect.Message.setCode(java.lang.Integer)
public java.lang.Integer com.qianyan.reflect.Message.getCode()
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public boolean java.lang.Object.equals(java.lang.Object)
public java.lang.String java.lang.Object.toString()
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
=========获取所有声明的方法=========
public java.lang.String com.qianyan.reflect.Message.getMessage()
public void com.qianyan.reflect.Message.setMsgId(java.lang.String)
public java.lang.String com.qianyan.reflect.Message.getMsgId()
public void com.qianyan.reflect.Message.setMessage(java.lang.String)
public void com.qianyan.reflect.Message.setCode(java.lang.Integer)
public java.lang.Integer com.qianyan.reflect.Message.getCode()
=========获取所有公用的属性=========
=========获取所有声明的属性=========
private java.lang.Integer com.qianyan.reflect.Message.code
private java.lang.String com.qianyan.reflect.Message.msgId
private java.lang.String com.qianyan.reflect.Message.message
=========调用方法=========
WEIXIN20191115124627840
=========类上是否有NoLogin注解=========
@com.qianyan.reflect.NoLogin(mchId=[208171120117418, 208171120117419])
208171120117418
208171120117419