JAVA 反射应用:properties2Object

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=0, saving=0.0]
PersonConfig [name=Zno, age=18, 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) {
                }
            }
        }

 

转载于:https://www.cnblogs.com/zno2/p/4613066.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值