集册 Java实例教程 尝试在给定对象中将属性“名称”设置为“值”。

尝试在给定对象中将属性“名称”设置为“值”。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

560
尝试在给定对象中将属性“名称”设置为“值”。

/*

 * #%L

 * Wisdom-Framework

 * %%

 * Copyright (C) 2013 - 2014 Wisdom Framework

 * %%

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 * 

 *      http://www.apache.org/licenses/LICENSE-2.0

 * 

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 * #L%

 */

//package com.nowjava;

import java.beans.Introspector;/** 来 自 N o w  J a v a  . c o m**/

import java.beans.PropertyDescriptor;

import java.lang.reflect.Method;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;


public class Main {


    /**

     * Tries to set the property 'name' to `value` in the given object. This assignation is made using a

     * <em>setter</em> method discovered and invoked using reflection.

     * @param object the object

     * @param name the property name

     * @param value the value

     * @throws SQLException if the property cannot be set. This happens if there are no setter for the given property

     * in the object or if the value cannot be wrapped to the type of the setter parameter.

     */

    public static void setProperty(Object object, String name, String value)

            throws SQLException {/* 来 自 N o w  J a v a  . c o m*/

        Class<?> type = object.getClass();


        PropertyDescriptor[] descriptors;

        try {

            descriptors = Introspector.getBeanInfo(type)

                    .getPropertyDescriptors();

        } catch (Exception ex) {

            throw new SQLException(ex);

        }

        List<String> names = new ArrayList<>();


        for (PropertyDescriptor descriptor : descriptors) {

            if (descriptor.getWriteMethod() == null) {

                continue;

            }


            if (descriptor.getName().equals(name)) {

                Method method = descriptor.getWriteMethod();

                Class<?> paramType = method.getParameterTypes()[0];

                Object param = toBasicType(value, paramType.getName());


                try {

                    method.invoke(object, param);

                } catch (Exception ex) {

                    throw new SQLException(ex);

                }

                return;

            }


            names.add(descriptor.getName());

        }

        throw new SQLException("No such property: " + name

                + ", exists.  Writable properties are: " + names);

    }


    /**

     * Transforms the given value to an instance of the given type.

     * @param value the value

     * @param type the type

     * @return an instance of the type having the wrapped value

     * @throws SQLException if the conversion is not possible.

     */

    public static Object toBasicType(String value, String type)

            throws SQLException {


        // Early return from first "if" condition that evaluates to true


        if (value == null) {

            return null;

        }


        if (type == null || type.equals(String.class.getName())) {

            return value;

        }


        if (type.equals(Integer.class.getName())

                || type.equals(int.class.getName())) {

            try {

                return Integer.valueOf(value);

            } catch (NumberFormatException e) {

                throwSQLException(e, "Integer", value);

            }

        }


        if (type.equals(Float.class.getName())

                || type.equals(float.class.getName())) {

            try {

                return Float.valueOf(value);

            } catch (NumberFormatException e) {

                throwSQLException(e, "Float", value);

            }

        }


        if (type.equals(Long.class.getName())

                || type.equals(long.class.getName())) {

            try {

                return Long.valueOf(value);

            } catch (NumberFormatException e) {

                throwSQLException(e, "Long", value);

            }

        }


        if (type.equals(Double.class.getName())

                || type.equals(double.class.getName())) {

            try {

                return Double.valueOf(value);

            } catch (NumberFormatException e) {

                throwSQLException(e, "Double", value);

            }

        }


        if (type.equals(Character.class.getName())

                || type.equals(char.class.getName())) {

            if (value.length() != 1) {

                throw new SQLException("Invalid Character value: " + value);

            }

            return value.charAt(0);

        }


        if (type.equals(Byte.class.getName())

                || type.equals(byte.class.getName())) {

            try {

                return Byte.valueOf(value);

            } catch (NumberFormatException e) {

                throwSQLException(e, "Byte", value);

            }

        }


        if (type.equals(Short.class.getName())

                || type.equals(short.class.getName())) {

            try {

                return Short.valueOf(value);

            } catch (
展开阅读全文