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

JAVA 反射应用:properties2Object

时间:2016-08-05 19:26:27      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

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

 

JAVA 反射应用:properties2Object

标签:

原文地址:http://www.cnblogs.com/zno2/p/4613066.html

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