JAVA 反射应用:properties2Object

时间:2022-05-05 19:39:08

MixAll.java

import java.lang.reflect.Method;
import java.util.Properties; public class MixAll { /**
* 将Properties中的值写入Object
*/
public static void properties2Object(final Properties p, final Object object) {
Method[] methods = object.getClass().getMethods();
for (Method method : methods) {
String mn = method.getName();
if (mn.startsWith("set")) {
try {
String tmp = mn.substring(4);
String first = mn.substring(3, 4); String key = first.toLowerCase() + tmp;
String property = p.getProperty(key);
if (property != null) {
Class<?>[] pt = method.getParameterTypes();
if (pt != null && pt.length > 0) {
String cn = pt[0].getSimpleName();
Object arg = null;
if (cn.equals("int")) {
arg = Integer.parseInt(property);
} else if (cn.equals("long")) {
arg = Long.parseLong(property);
} else if (cn.equals("double")) {
arg = Double.parseDouble(property);
} else if (cn.equals("boolean")) {
arg = Boolean.parseBoolean(property);
} else if (cn.equals("String")) {
arg = property;
} else {
continue;
}
method.invoke(object, new Object[] { arg });
}
}
} catch (Throwable e) {
}
}
}
} }

PersonConfig.java

public class PersonConfig {

    private String name;
private int age;
private double saving; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public double getSaving() {
return saving;
} public void setSaving(double saving) {
this.saving = saving;
} @Override
public String toString() {
return "PersonConfig [name=" + name + ", age=" + age + ", saving="
+ saving + "]";
}
}

TestCommandline.java

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties; import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException; public class TestCommandline {
public static void main(String[] args) throws ParseException, IOException {
Options options = new Options();
Option opt = new Option("c", "config", true, "config file path");
opt.setRequired(true);
options.addOption(opt); CommandLineParser parser = new DefaultParser();
CommandLine commandLine = parser.parse(options, args); String optNmae = "c";
if (commandLine.hasOption(optNmae)) {
File file = new File(commandLine.getOptionValue(optNmae)); InputStream in = new BufferedInputStream(new FileInputStream(file));
Properties properties = new Properties();
properties.load(in); PersonConfig personConfig = new PersonConfig();
System.out.println(personConfig); // 前
MixAll.properties2Object(properties, personConfig);
System.out.println(personConfig); // 后 System.out.println("load config properties file OK, " + file);
in.close();
}
}
}

E:\config\person.properties

name=Zno
age=18
saving=200.0

执行参数:

-c E:/config/person.properties

运行结果:

PersonConfig [name=null, age=, saving=0.0]
PersonConfig [name=Zno, age=, saving=200.0]
load config properties file OK, E:\config\person.properties

动态修改属性

     import org.apache.commons.beanutils.BeanUtils;

        public void clean() {
Field[] fields = this.getClass().getDeclaredFields();
for (Field field : fields) {
try {
String name = field.getName();
String property = BeanUtils.getProperty(this, name);
if ("未知".equals(property)) {
BeanUtils.setProperty(this, name, null);
}
} catch (Exception e) {
}
}
}