读属性文件直接执行其中的方法
Posted jiminluo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了读属性文件直接执行其中的方法相关的知识,希望对你有一定的参考价值。
package com.gxnu.study.reflect.bean;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;
import java.util.Set;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MyTestTools {
Properties pro = new Properties();
private Class clazz;
private Class[] conParams;
private Class[] methodParams;
private Constructor cons;
private Method method;
private Object[] methodValues;
private Object[] consValue;
private Object target;
// private Logger loger = Logger.getLogger(this.getClass());
public MyTestTools() {
init("E:\c.txt");//调用init方法,给定指定的值
}
public MyTestTools(String fileName) {
init(fileName);//调用init方法,参数
}
private void init(String fileName){
try(InputStream is = Files.newInputStream(Paths.get(fileName));){
pro.load(is);
}catch(Exception e){
e.printStackTrace();
}
}
public Properties getPro(){
return this.pro;
}
public void run(){
Properties pro = getPro();//调用MyTestTools里的方法
Set<String> set = pro.stringPropertyNames();//获取属性名
// loger.info("rfg"+(set==null));
set.forEach((name)->{//取出集合中的属性的名字
// loger.info("fkdf"+(name==null));
process(name,pro.getProperty(name));});
}
public void process(String name, String value){
Pattern pattern1 = Pattern.compile("(.+)_\((.*)\)");//\表示转义为普通字符
Pattern pattern2 = Pattern.compile("\((.*)\)_(.+)\((.*)\)_\((.*)\)");
String className = null;
String conParamType="";
String conParamValue = null;
String methodName = null;
String methodParamType = null;
String methodValue = null;
Matcher matcher1 = pattern1.matcher(name);//匹配集合中的名字
if(matcher1.find()){
className=matcher1.group(1);
conParamType=matcher1.group(2);
}
Matcher matcher2 = pattern2.matcher(value);//匹配集合中的值
if(matcher2.find()){
conParamValue=matcher2.group(1);
methodName=matcher2.group(2);
methodParamType=matcher2.group(3);
methodValue=matcher2.group(4);
}
try {
this.clazz = Class.forName(className);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
makeCons(conParamType);
this.makeConsObject(conParamValue);
this.makeTarget();//对象创建
this.methodParams = this.getParamClasses(methodParamType);
getMethod(methodName,methodParamType);//创建Method
makeMethodParams(methodValue);//传入方法值
execute();
/*
* 执行里边的方法:
* 1.Method 2.参数值构成给的数组
* Method Class方法名字 参数列表(Class...)
*
* 执行:
* method 目标对象 参数值对象构成的数组
*/
}
public void execute(){
// System.out.println("err"+(this.method == null));
// System.out.println("err"+(this.target == null));
// System.out.println("err"+(this.methodValues == null));
try {
this.method.invoke(this.target, methodValues);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void getMethod(String methodName,String paramType){
try {
this.method = this.clazz.getMethod(methodName, getParamClasses(paramType));
System.out.println("this.method"+this.method);
} catch (NoSuchMethodException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Class[] getParamClasses(String paramType){
Class[] classes = null;
if(paramType == null || "".equals(paramType.trim())){
classes = new Class[]{};
}else{
String[] arr = paramType.split(",");
classes = new Class[arr.length];
for(int i = 0;i<arr.length;i++){
classes[i] = this.transform(arr[i]);
}
}
return classes;
}
public void makeTarget(){
try {
this.target=this.cons.newInstance(consValue);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void makeCons (String paramType){
Class[] classs = this.getParamClasses(paramType);
this.conParams = classs;
try {
this.cons = clazz.getConstructor(classs);
} catch (NoSuchMethodException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Object[] getParamValue(Class[] types,String consString){
Object[] paramValues = null;
if(consString == null || "".equals(consString.trim())){
paramValues = new Object[]{};
}else{
String[] arr = consString.split(",");
paramValues = new Object[arr.length];
for(int i = 0;i<arr.length;i++){
paramValues[i] = this.transform(types[i],arr[i]);
}
}
return paramValues;
}
public void makeMethodParams(String consString){
this.methodValues=getParamValue(this.methodParams, consString);
}
public void makeConsObject (String consString){
this.consValue= this.getParamValue(conParams, consString);
}
public Class transform(String name){
Class clz = null;
switch(name){//Stirng enum
case "double":
clz=double.class;
break;
case "float":
clz=float.class;
break;
case "char":
clz=char.class;
break;
case "byte":
clz=byte.class;
break;
case "short":
clz=short.class;
break;
case "int":
clz=int.class;
break;
case "long":
clz=long.class;
break;
case "boolean":
clz=boolean.class;
break;
default:
try {
clz=Class.forName(name);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
return clz;
}
public Object transform(Class clas, String value){
Object obj = null;
if(clas == String.class){
obj = value;
}else if(clas == boolean.class || clas == Boolean.class){
obj = value;
}else if(clas == int.class){
obj = Integer.valueOf(value);
}else if(clas == long.class){
obj = Long.valueOf(value);
}else if(clas == short.class){
obj = Short.valueOf(value);
}else if(clas == byte.class){
obj = Byte.valueOf(value);
}else if(clas == char.class){
obj = value.charAt(0);
}else if(clas == float.class){
obj = Float.valueOf(value);
}else if(clas == double.class){
obj = Double.valueOf(value);
}
return obj;
}
}
/*
public void makeCons (String paramType){
Class[] classs = this.getParamClasses(paramType);
this.conParams = classs;
if(paramType == null || "".equals(paramType.trim())){
try {
this.cons = this.clazz.getConstructor();
} catch (NoSuchMethodException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else{
String[] arr = paramType.split(",");
for(int i=0;i<arr.length;i++){
this.conParams[i] = this.transform(arr[i]);
}
try {
this.cons = clazz.getConstructor(this.conParams);
} catch (NoSuchMethodException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
*/
/*public void makeConsObject (String consString){
if(consString == null || "".equals(consString.trim())){
this.consValue = new Object[]{};
}
else{
String[] arr = consString.split(",");
this.consValue = new Class[arr.length];
for(int i=0;i<arr.length;i++){
this.consValue[i] = this.transform(this.conParams[i],arr[i]);
}
try {
this.cons = clazz.getConstructor(this.conParams);
} catch (NoSuchMethodException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
*/
//测试类
package com.gxnu.study.reflect;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import org.junit.Before;
import org.junit.Test;
import com.gxnu.study.reflect.bean.MyTestTools;
public class testProperty {
MyTestTools mtt = null;
@Before
public void init(){
mtt = new MyTestTools();
}
@Test
public void testPro(){
Properties pro = mtt.getPro();//调用MyTestTools里的方法
Set<String> set = pro.stringPropertyNames();//获取属性名
Pattern pattern1 = Pattern.compile("(.+)_\((.*)\)");//\表示转义为普通字符
Pattern pattern2 = Pattern.compile("\((.*)\)_(.+)\((.*)\)_\((.*)\)");
set.forEach((name)->{//取出集合中的属性的名字
Matcher matcher1 = pattern1.matcher(name);//匹配集合中的名字
if(matcher1.find()){
System.out.println("类名:"+matcher1.group(1)+" "+"类名的参数类型"+matcher1.group(2));
}
String value = pro.getProperty(name);//获取属性名的值
Matcher matcher2 = pattern2.matcher(value);//匹配集合中的值
if(matcher2.find()){
System.out.println(matcher2.group(1)+" "
+matcher2.group(2)+" "+matcher2.group(3)+" "
+matcher2.group(4));
}
});
// System.out.println(pro);
// System.out.println(set);
}
@Test
public void testSpit(){
String s = "aa";
String s1 = "aa,bb,cc,dd";
Stream.of(s.split(",")).forEach(System.out::println);
Stream.of(s1.split(",")).forEach(System.out::println);
}
@Test
public void testClass(){
Class clazz = null;
try {
clazz = Class.forName("double");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(clazz);
}
@Test
public void testAutoExcute(){
mtt.run();
}
}
#属性文件
com.gxnu.study.reflect.bean.Person_(java.lang.String,double)=(lisi,157)_setName(java.lang.String,int,long)_(xiaogou,7,888)
com.gxnu.study.reflect.ReflectEx_()=()_testMethod()_()
以上是关于读属性文件直接执行其中的方法的主要内容,如果未能解决你的问题,请参考以下文章