basictypeconverter.java

来自「JXPath」· Java 代码 · 共 513 行 · 第 1/2 页

JAVA
513
字号
/*
 * Copyright 1999-2004 The Apache Software Foundation
 *
 * 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.
 */
package org.apache.commons.jxpath.util;

import java.lang.reflect.Array;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.jxpath.JXPathException;
import org.apache.commons.jxpath.NodeSet;
import org.apache.commons.jxpath.Pointer;

/**
 * The default implementation of TypeConverter.
 *
 * @author Dmitri Plotnikov
 * @version $Revision: 1.15 $ $Date: 2004/07/25 13:16:04 $
 */
public class BasicTypeConverter implements TypeConverter {

    /**
     * Returns true if it can convert the supplied
     * object to the specified class.
     */
    public boolean canConvert(Object object, Class toType) {
        if (object == null) {
            return true;
        }

        if (toType == Object.class) {
            return true;
        }

        Class fromType = object.getClass();
        if (fromType.equals(toType)) {
            return true;
        }

        if (toType.isAssignableFrom(fromType)) {
            return true;
        }

        if (toType == String.class) {
            return true;
        }

        if (object instanceof Boolean) {
            if (toType == boolean.class
                || Number.class.isAssignableFrom(toType)) {
                return true;
            }
        }
        else if (object instanceof Number) {
            if (toType.isPrimitive()
                || Number.class.isAssignableFrom(toType)) {
                return true;
            }
        }
        else if (object instanceof Character) {
            if (toType == char.class) {
                return true;
            }
        }
        else if (object instanceof String) {
            if (toType.isPrimitive()) {
                return true;
            }
            if (toType == Boolean.class
                || toType == Character.class
                || toType == Byte.class
                || toType == Short.class
                || toType == Integer.class
                || toType == Long.class
                || toType == Float.class
                || toType == Double.class) {
                return true;
            }
        }
        else if (fromType.isArray()) {
            // Collection -> array
            if (toType.isArray()) {
                Class cType = toType.getComponentType();
                int length = Array.getLength(object);
                for (int i = 0; i < length; i++) {
                    Object value = Array.get(object, i);
                    if (!canConvert(value, cType)) {
                        return false;
                    }
                }
                return true;
            }
            else if (Collection.class.isAssignableFrom(toType)) {
                return canCreateCollection(toType);
            }
            else {
                if (Array.getLength(object) > 0) {
                    Object value = Array.get(object, 0);
                    return canConvert(value, toType);
                }
                else {
                    return canConvert("", toType);
                }
            }
        }
        else if (object instanceof Collection) {
            // Collection -> array
            if (toType.isArray()) {
                Class cType = toType.getComponentType();
                Iterator it = ((Collection) object).iterator();
                while (it.hasNext()) {
                    Object value = it.next();
                    if (!canConvert(value, cType)) {
                        return false;
                    }
                }
                return true;
            }
            else if (Collection.class.isAssignableFrom(toType)) {
                return canCreateCollection(toType);
            }
            else {
                if (((Collection) object).size() > 0) {
                    Object value;
                    if (object instanceof List) {
                        value = ((List) object).get(0);
                    }
                    else {
                        Iterator it = ((Collection) object).iterator();
                        value = it.next();
                    }
                    return canConvert(value, toType);
                }
                else {
                    return canConvert("", toType);
                }
            }
        }
        else if (object instanceof NodeSet) {
            return canConvert(((NodeSet) object).getValues(), toType);
        }
        else if (object instanceof Pointer) {
            return canConvert(((Pointer) object).getValue(), toType);
        }
        return ConvertUtils.lookup(toType) != null;
    }

    /**
     * Converts the supplied object to the specified
     * type. Throws a runtime exception if the conversion is
     * not possible.
     */
    public Object convert(Object object, Class toType) {
        if (object == null) {
            if (toType.isPrimitive()) {
                return convertNullToPrimitive(toType);
            }
            return null;
        }

        if (toType == Object.class) {
            if (object instanceof NodeSet) {
                return convert(((NodeSet) object).getValues(), toType);
            }
            else if (object instanceof Pointer) {
                return convert(((Pointer) object).getValue(), toType);
            }
            return object;
        }

        Class fromType = object.getClass();
        if (fromType.equals(toType) || toType.isAssignableFrom(fromType)) {
            return object;
        }

        if (fromType.isArray()) {
            int length = Array.getLength(object);
            if (toType.isArray()) {
                Class cType = toType.getComponentType();

                Object array = Array.newInstance(cType, length);
                for (int i = 0; i < length; i++) {
                    Object value = Array.get(object, i);
                    Array.set(array, i, convert(value, cType));
                }
                return array;
            }
            else if (Collection.class.isAssignableFrom(toType)) {
                Collection collection = allocateCollection(toType);
                for (int i = 0; i < length; i++) {
                    collection.add(Array.get(object, i));
                }
                return unmodifiableCollection(collection);
            }
            else {
                if (length > 0) { 
                    Object value = Array.get(object, 0);
                    return convert(value, toType);
                }
                else {
                    return convert("", toType);
                }
            }
        }
        else if (object instanceof Collection) {
            int length = ((Collection) object).size();
            if (toType.isArray()) {
                Class cType = toType.getComponentType();
                Object array = Array.newInstance(cType, length);
                Iterator it = ((Collection) object).iterator();
                for (int i = 0; i < length; i++) {
                    Object value = it.next();
                    Array.set(array, i, convert(value, cType));
                }
                return array;
            }
            else if (Collection.class.isAssignableFrom(toType)) {
                Collection collection = allocateCollection(toType);
                collection.addAll((Collection) object);
                return unmodifiableCollection(collection);
            }
            else {
                if (length > 0) {
                    Object value;
                    if (object instanceof List) {
                        value = ((List) object).get(0);
                    }
                    else {
                        Iterator it = ((Collection) object).iterator();
                        value = it.next();
                    }
                    return convert(value, toType);
                }
                else {
                    return convert("", toType);
                }
            }

⌨️ 快捷键说明

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