⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 beanproperty.java

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 JAVA
字号:
/*____________________________________________________________________________*\
 *

 Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.

 These sources, libraries and applications are
 FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
 as long as the following conditions are adhered to.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer. 

 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in
    the documentation and/or other materials provided with the
    distribution.

 3. The name of the author may not be used to endorse or promote products
    derived from this software without specific prior written permission. 

 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 OF THE POSSIBILITY OF SUCH DAMAGE.

 *____________________________________________________________________________*|
 *
 * $Source: /cvsroot/pi3web/Pi3Web_200/Source/Servlet/org/pi3/jsp/runtime/BeanProperty.java,v $
 * $Date: 2003/05/13 18:42:20 $
 *
 Description:
	Helper class for read/write access of bean properties.

\*____________________________________________________________________________*/
 
package org.pi3.jsp.runtime;

import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Array;
import java.lang.IllegalAccessException;
import java.beans.PropertyDescriptor;
import java.beans.IntrospectionException;
import javax.servlet.ServletRequest;
import java.util.Enumeration;

public class BeanProperty {


    private static Object convert(String s, Class t) {
        try {
            if ( t.equals(Boolean.class) || t.equals(Boolean.TYPE) ) {
			    if (s == null) s = "false";
                return new Boolean(s);
            } else if ( t.equals(Byte.class) || t.equals(Byte.TYPE) ) {
                return new Byte(s);
            } else if (t.equals(Character.class) || t.equals(Character.TYPE)) {
                return s.length() > 0 ? new Character(s.charAt(0)) : null;
            } else if ( t.equals(Short.class) || t.equals(Short.TYPE) ) {
                return new Short(s);
            } else if ( t.equals(Integer.class) || t.equals(Integer.TYPE) ) {
                return new Integer(s);
            } else if ( t.equals(Float.class) || t.equals(Float.TYPE) ) {
                return new Float(s);
            } else if ( t.equals(Long.class) || t.equals(Long.TYPE) ) {
                return new Long(s);
            } else if ( t.equals(Double.class) || t.equals(Double.TYPE) ) {
                return new Double(s);
            } else if ( t.equals(String.class) ) {
                return s;
            }
        } catch (Exception ex) {
//            throw new Exception(ex);
        }
        return s;
    }


private static void _set(Object bean, String property, String value, ServletRequest request) 
    {
	try
        {
        StringBuffer sb = new StringBuffer("set");
        sb.append( property.substring( 0, 1 ).toUpperCase() );
        sb.append( property.substring( 1 ) );
        PropertyDescriptor pd = new PropertyDescriptor( property, bean.getClass(), null, sb.toString() );
		Class type = pd.getPropertyType();
        Method m = pd.getWriteMethod();

		if ( type.isArray() )
            {
		    Class t = type.getComponentType();
		    String[] values = request.getParameterValues(property);
		    if( values == null ) return;
		    if ( t.equals(String.class) )
			    {
                m.invoke(bean, new Object[] { values });
                }
			else
                {
                Object arr = Array.newInstance(t, values.length); 
                for ( int i = 0; i < values.length; i++ ) Array.set(arr, i, values[i]);
                m.invoke(bean, new Object[] { arr } );
                }
		    }
		else
		    {
            if(value == null || value.equals("")) return;
            Object o = convert(value, type);
            if ( o != null ) m.invoke(bean, new Object[] { o });
            }
        }
    catch (Exception ex)
        {
//      throw new Exception(ex);
        }
    }
    
    public static String get( Object bean, String property )
      throws IllegalAccessException, IntrospectionException, InvocationTargetException {
        StringBuffer sb = new StringBuffer("get");
        sb.append( property.substring( 0, 1 ).toUpperCase() );
        sb.append( property.substring( 1 ) );
        PropertyDescriptor pd = new PropertyDescriptor( property, bean.getClass(), sb.toString(), null );
        Method m = pd.getReadMethod();
        Object O = m.invoke( bean, null );
        return ( O != null ) ? O.toString() : null;
    }

    public static void set( Object bean, String property, String value )
      throws IllegalAccessException, IntrospectionException, InvocationTargetException {
        StringBuffer sb = new StringBuffer("set");
        sb.append( property.substring( 0, 1 ).toUpperCase() );
        sb.append( property.substring( 1 ) );
        PropertyDescriptor pd = new PropertyDescriptor( property, bean.getClass(), null, sb.toString() );
		Class type = pd.getPropertyType();
        Method m = pd.getWriteMethod();
        m.invoke( bean, new Object[] { convert(value, type) } );
    }

    public static void set( Object bean, String property, ServletRequest request )
      throws IllegalAccessException, IntrospectionException, InvocationTargetException {
	    if ( property.equals("*") )
		    {
            Enumeration e = request.getParameterNames();
            while ( e.hasMoreElements() )
                {
                String name  = (String) e.nextElement();
                String value = request.getParameter(name);
                if (value == null || value.equals("")) continue;
                _set(bean, name, value, request);
                }
            }
		else
		    {
            String value = request.getParameter(property);
            _set(bean, property, value, request);
			}
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -