码迷,mamicode.com
首页 > 编程语言 > 详细

java反射实例

时间:2014-12-24 09:52:26      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

 1 package com.test;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.lang.reflect.InvocationTargetException;
 6 import java.lang.reflect.Method;
 7 import java.util.Properties;
 8  
 9 public class Test {
10  
11     /**
12      * 
13      * @param b 参数列表
14      * @param a
15      * @return
16      */
17     public String a(int b,int a){
18         String bb=b+2+"";
19         System.out.println(a);
20         System.out.println("通过反色");
21         return bb;
22     }
23  
24 
25     public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
26         
27         //读取配置文件获得签名用到的key值
28         InputStream inputStream = new Test().getClass().getClassLoader().getResourceAsStream("log4j.properties");
29         Properties p = new Properties();
30         try{
31             p.load(inputStream);
32         } catch (IOException e1){
33             e1.printStackTrace();
34         }
35         String a = p.getProperty("234234234"); //方法的字符串变量
36         Test test=new Test();
37         Class ss = int.class;
38         Class sa = int.class;
39         Class b[] = new Class[2]; //参数类型数组
40         
41         b[0] = ss;//第一个参数的类型
42         b[1]= sa; //第二个参数的类型
43         Method get = test.getClass().getMethod(a,b);
44         
45                                         //调用方法的类,参数列表
46         String aa = (String)get.invoke(test,new Integer(3),new Integer(3));
47         System.out.println(aa);
48 }
49 
50 
51 log4j.properties
52 
53 
54 234234234=a
55 
56 
57 
58 
59 
60 
61     /**
62      * 读取路径下的properties文件,获得Properties对象
63      * @param path
64      * @return Properties
65      * @throws IOException
66      */
67     public static Properties getProperties(String fileName) throws IOException{
68         String filePath =  ContentsMethod.class.getResource("/").getPath()+ fileName;
69         FileInputStream in = new FileInputStream(new File(filePath));
70         Properties p = new Properties();
71         p.load(in);
72         logger.info("ContentsMethod.getProperties =>加载配置文件"+filePath);
73         return p;
74     }

 

java反射实例

标签:

原文地址:http://www.cnblogs.com/w731478725/p/4181618.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!