关闭

java的反射机制

标签: java
230人阅读 评论(0) 收藏 举报
分类:

一直不太明白java的反射机制,今天稍微看了下,写了一个例子:

public static void main(String[] args) {
		try {
			Class clazz = Class.forName("cn.com.mysite.sink.ScalaSink");
			Field[] fields = clazz.getDeclaredFields();    
            for(int i = 0; i < fields.length; i++) {    
            	 // 权限修饰符

                int mo = fields[i].getModifiers();

                String priv = Modifier.toString(mo);

                // 属性类型

                Class<?> type = fields[i].getType();

                System.out.println(priv + " " + type.getName() + " "

                        + fields[i].getName() + ";");
            }    
            System.out.println();
            System.out.println("Public fields: ");    
            fields = clazz.getFields();    
            for(int i = 0; i < fields.length; i++) {    
                int mo = fields[i].getModifiers();

                String priv = Modifier.toString(mo);

                // 属性类型

                Class<?> type = fields[i].getType();

                System.out.println(priv + " " + type.getName() + " "

                        + fields[i].getName() + ";");
            }  
            
            
            Method method=clazz.getDeclaredMethod("isEmpty",String.class);
            method.setAccessible(true);
            Object o = method.invoke((cn.com.mysite.sink.ScalaSink)clazz.newInstance(),"");
            System.out.println(o);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

打印结果:

private static final org.slf4j.Logger logger;
public static final java.lang.String TOPIC_HDR;
private java.lang.String isWhite;
private java.util.Properties kafkaProps;
private java.lang.String topic;
private int batchSize;
private java.lang.String hostname;
private java.lang.String uripath;
private java.lang.String field;
private java.lang.String dataSource;
private [Ljava.lang.String; f;
private java.util.List messageList;
private org.apache.flume.sink.SinkCounter counter;
private com.ibm.tivoli.unity.common.rest.RestClient restClient;
private com.ibm.tivoli.unity.common.rest.RequestResult result;
private java.text.SimpleDateFormat df;
private java.text.SimpleDateFormat format;
private java.util.Map map;
private com.ibm.json.java.JSONObject batch;
private java.lang.StringBuilder sb;
private com.ibm.json.java.JSONObject job;
private static java.security.MessageDigest digest;
private java.util.concurrent.ExecutorService threadPool;
private long bytes;
private int _count;
private java.io.File timeFile;
private long lastSendTime;
private long sendTimeInterval;
private boolean isWrite;
private java.io.FileWriter writer;
private boolean countFlowEnable;

Public fields: 
public static final java.lang.String TOPIC_HDR;
true

class里面有几个方法:getDeclaredFields();getFields();   getDeclaredMethod();getMethod();

取值不太一样,getFields()与getMethod()只能取修饰符为public的属性跟方法,而其他两个则可以取到任意的属性和方法。



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:62039次
    • 积分:679
    • 等级:
    • 排名:千里之外
    • 原创:44篇
    • 转载:50篇
    • 译文:0篇
    • 评论:8条
    最新评论