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

📄 invoker.java

📁 大名鼎鼎的java动态脚本语言。已经通过了sun的认证
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    protected Class loadClass(String type) {        try {            return getClass().getClassLoader().loadClass(type);        }        catch (ClassNotFoundException e) {            try {                return Thread.currentThread().getContextClassLoader().loadClass(type);            }            catch (ClassNotFoundException e2) {                try {                    return Class.forName(type);                }                catch (ClassNotFoundException e3) {                }            }            throw new GroovyRuntimeException("Could not load type: " + type, e);        }    }    /**     * Find the right hand regex within the left hand string and return a matcher.     *     * @param left  string to compare     * @param right regular expression to compare the string to     * @return     */    public Matcher objectFindRegex(Object left, Object right) {        String stringToCompare;        if (left instanceof String) {            stringToCompare = (String) left;        }        else {            stringToCompare = toString(left);        }        String regexToCompareTo;        if (right instanceof String) {            regexToCompareTo = (String) right;        }        else if (right instanceof Pattern) {            Pattern pattern = (Pattern) right;            return pattern.matcher(stringToCompare);        }        else {            regexToCompareTo = toString(right);        }        Matcher matcher = Pattern.compile(regexToCompareTo).matcher(stringToCompare);        return matcher;    }    /**     * Find the right hand regex within the left hand string and return a matcher.     *     * @param left  string to compare     * @param right regular expression to compare the string to     * @return     */    public boolean objectMatchRegex(Object left, Object right) {        Pattern pattern;        if (right instanceof Pattern) {            pattern = (Pattern) right;        }        else {            pattern = Pattern.compile(toString(right));        }        String stringToCompare = toString(left);        Matcher matcher = pattern.matcher(stringToCompare);        RegexSupport.setLastMatcher(matcher);        return matcher.matches();    }    /**     * Compile a regular expression from a string.     *     * @param regex     * @return     */    public Pattern regexPattern(Object regex) {        return Pattern.compile(regex.toString());    }    public Object asType(Object object, Class type) {        if (object == null) {            return null;        }        // TODO we should move these methods to groovy method, like g$asType() so that        // we can use operator overloading to customize on a per-type basis        if (type.isArray()) {            return asArray(object, type);        }        if (type.isInstance(object)) {            return object;        }        if (type.isAssignableFrom(Collection.class)) {            if (object.getClass().isArray()) {                // lets call the collections constructor                // passing in the list wrapper                Collection answer = null;                try {                    answer = (Collection) type.newInstance();                }                catch (Exception e) {                    throw new ClassCastException("Could not instantiate instance of: " + type.getName() + ". Reason: " + e);                }                // we cannot just wrap in a List as we support primitive type arrays                int length = Array.getLength(object);                for (int i = 0; i < length; i++) {                    Object element = Array.get(object, i);                    answer.add(element);                }                return answer;            }        }        if (type.equals(String.class)) {            return object.toString();        }        if (type.equals(Character.class)) {            if (object instanceof Number) {                return asCharacter((Number) object);            }            else {                String text = object.toString();                if (text.length() == 1) {                    return new Character(text.charAt(0));                }                else {                    throw new ClassCastException("Cannot cast: " + text + " to a Character");                }            }        }        if (Number.class.isAssignableFrom(type)) {            if (object instanceof Character) {                return new Integer(((Character) object).charValue());            }            else if (object instanceof String) {                String c = (String) object;                if (c.length() == 1) {                    return new Integer(c.charAt(0));                }                else {                    throw new ClassCastException("Cannot cast: '" + c + "' to an Integer");                }            }        }        if (object instanceof Number) {            Number n = (Number) object;            if (type.isPrimitive()) {                if (type == byte.class) {                    return new Byte(n.byteValue());                }                if (type == char.class) {                    return new Character((char) n.intValue());                }                if (type == short.class) {                    return new Short(n.shortValue());                }                if (type == int.class) {                    return new Integer(n.intValue());                }                if (type == long.class) {                    return new Long(n.longValue());                }                if (type == float.class) {                    return new Float(n.floatValue());                }                if (type == double.class) {                    Double answer = new Double(n.doubleValue());                    //throw a runtime exception if conversion would be out-of-range for the type.                    if (!(n instanceof Double) && (answer.doubleValue() == Double.NEGATIVE_INFINITY                            || answer.doubleValue() == Double.POSITIVE_INFINITY)) {                        throw new GroovyRuntimeException("Automatic coercion of " + n.getClass().getName()                                + " value " + n + " to double failed.  Value is out of range.");                    }                    return answer;                }            }            else {                if (Number.class.isAssignableFrom(type)) {                    if (type == Byte.class) {                        return new Byte(n.byteValue());                    }                    if (type == Character.class) {                        return new Character((char) n.intValue());                    }                    if (type == Short.class) {                        return new Short(n.shortValue());                    }                    if (type == Integer.class) {                        return new Integer(n.intValue());                    }                    if (type == Long.class) {                        return new Long(n.longValue());                    }                    if (type == Float.class) {                        return new Float(n.floatValue());                    }                    if (type == Double.class) {                        Double answer = new Double(n.doubleValue());                        //throw a runtime exception if conversion would be out-of-range for the type.                        if (!(n instanceof Double) && (answer.doubleValue() == Double.NEGATIVE_INFINITY                                || answer.doubleValue() == Double.POSITIVE_INFINITY)) {                            throw new GroovyRuntimeException("Automatic coercion of " + n.getClass().getName()                                    + " value " + n + " to double failed.  Value is out of range.");                        }                        return answer;                    }                }            }        }        if (type == Boolean.class) {            return asBool(object) ? Boolean.TRUE : Boolean.FALSE;        }        Object[] args = null;        if (object instanceof Collection) {            Collection list = (Collection) object;            args = list.toArray();        }        else if (object instanceof Object[]) {            args = (Object[]) object;        }        if (args != null) {            // lets try invoke the constructor with the list as arguments            // such as for creating a Dimension, Point, Color etc.            try {                return invokeConstructorOf(type, args);            }            catch (Exception e) {                // lets ignore exception and return the original object                // as the caller has more context to be able to throw a more                // meaningful exception            }        }        return object;    }    public Object asArray(Object object, Class type) {        Collection list = asCollection(object);        int size = list.size();        Class elementType = type.getComponentType();        Object array = Array.newInstance(elementType, size);        int idx = 0;        if (boolean.class.equals(elementType)) {            for (Iterator iter = list.iterator(); iter.hasNext(); idx++) {                Object element = iter.next();                Array.setBoolean(array, idx, asBool(element));            }        }        else if (byte.class.equals(elementType)) {            for (Iterator iter = list.iterator(); iter.hasNext(); idx++) {                Object element = iter.next();                Array.setByte(array, idx, asByte(element));            }        }        else if (char.class.equals(elementType)) {            for (Iterator iter = list.iterator(); iter.hasNext(); idx++) {                Object element = iter.next();                Array.setChar(array, idx, asChar(element));            }        }        else if (double.class.equals(elementType)) {            for (Iterator iter = list.iterator(); iter.hasNext(); idx++) {                Object element = iter.next();                Array.setDouble(array, idx, asDouble(element));            }        }        else if (float.class.equals(elementType)) {            for (Iterator iter = list.iterator(); iter.hasNext(); idx++) {                Object element = iter.next();                Array.setFloat(array, idx, asFloat(element));            }        }        else if (int.class.equals(elementType)) {            for (Iterator iter = list.iterator(); iter.hasNext(); idx++) {                Object element = iter.next();                Array.setInt(array, idx, asInt(element));            }        }        else if (long.class.equals(elementType)) {            for (Iterator iter = list.iterator(); iter.hasNext(); idx++) {                Object element = iter.next();                Array.setLong(array, idx, asLong(element));            }        }        else if (short.class.equals(elementType)) {            for (Iterator iter = list.iterator(); iter.hasNext(); idx++) {                Object element = iter.next();                Array.setShort(array, idx, asShort(element));            }        }        else {            for (Iterator iter = list.iterator(); iter.hasNext(); idx++) {                Object element = iter.next();                Object coercedElement = asType(element, elementType);                Array.set(array, idx, coercedElement);            }        }        return array;    }    public Number asNumber(Object value) {        if (value instanceof Number) {            return (Number) value;        }        else if (value instanceof String) {            String s = (String) value;            if (s.length() == 1) {                return new Integer(s.charAt(0));            }            else {                return new BigDecimal(s);            }        }        else if (value instanceof Character) {            return new Integer(((Character) value).charValue());        }        else {            throw new GroovyRuntimeException("Could not convert object: " + value + " into a Number");        }    }    public byte asByte(Object element) {        return asNumber(element).byteValue();    }    public char asChar(Object element) {        if (element instanceof String) {            return asCharacter((String) element).charValue();        }        return asCharacter(asNumber(element)).charValue();    }    public float asFloat(Object element) {        return asNumber(element).floatValue();    }    public double asDouble(Object element) {        return asNumber(element).doubleValue();    }    public short asShort(Object element) {        return asNumber(element).shortValue();    }    public int asInt(Object element) {        return asNumber(element).intValue();    }    public long asLong(Object element) {        return asNumber(element).longValue();    }    public boolean asBool(Object object) {        if (object instanceof Boolean) {            Boolean booleanValue = (Boolean) object;            return booleanValue.booleanValue();        }        else if (object instanceof Matcher) {            Matcher matcher = (Matcher) object;            RegexSupport.setLastMatcher(matcher);            return matcher.find();        }        else if (object instanceof Collection) {            Collection collection = (Collection) object;            return !collection.isEmpty();        }        else if (object instanceof Map) {            Map map = (Map) object;            return !map.isEmpty();        }        else if (object instanceof String) {            String string = (String) object;            return string.length() > 0;        }        else if (object instanceof Number) {            Number n = (Number) object;            return n.doubleValue() != 0;        }        else {            return object != null;        }    }    protected Character asCharacter(Number value) {        return new Character((char) value.intValue());    }    protected Character asCharacter(String text) {        return new Character(text.charAt(0));    }    /**     * @return true if the given value is a valid character string (i.e. has length of 1)     */    protected boolean isValidCharacterString(Object value) {        if (value instanceof String) {            String s = (String) value;            if (s.length() == 1) {                return true;            }        }        return false;    }    public void removeMetaClass(Class clazz) {        getMetaRegistry().removeMetaClass(clazz);    }}

⌨️ 快捷键说明

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