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

📄 invokerhelper.java

📁 大名鼎鼎的java动态脚本语言。已经通过了sun的认证
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* $Id: InvokerHelper.java,v 1.73 2005/10/03 18:07:36 tug Exp $ Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved. Redistribution and use of this software and associated documentation ("Software"), with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain copyright    statements and notices.  Redistributions must also contain a    copy of this document. 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 "groovy" must not be used to endorse or promote    products derived from this Software without prior written    permission of The Codehaus.  For written permission,    please contact info@codehaus.org. 4. Products derived from this Software may not be called "groovy"    nor may "groovy" appear in their names without prior written    permission of The Codehaus. "groovy" is a registered    trademark of The Codehaus. 5. Due credit should be given to The Codehaus -    http://groovy.codehaus.org/ THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS ``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 CODEHAUS 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. */package org.codehaus.groovy.runtime;import groovy.lang.*;import java.beans.Introspector;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.Reader;import java.io.Writer;import java.lang.reflect.Array;import java.math.BigDecimal;import java.math.BigInteger;import java.util.ArrayList;import java.util.Collection;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * A static helper class to make bytecode generation easier and act as a facade over the Invoker * * @author <a href="mailto:james@coredevelopers.net">James Strachan</a> * @version $Revision: 1.73 $ */public class InvokerHelper {    public static final Object[] EMPTY_ARGS = {    };    private static final Object[] EMPTY_MAIN_ARGS = new Object[]{new String[0]};    private static final Invoker singleton = new Invoker();    private static final Integer ZERO = new Integer(0);    private static final Integer MINUS_ONE = new Integer(-1);    private static final Integer ONE = new Integer(1);    public static MetaClass getMetaClass(Object object) {        return getInstance().getMetaClass(object);    }    public static void removeClass(Class clazz) {        getInstance().removeMetaClass(clazz);        Introspector.flushFromCaches(clazz);    }    public static Invoker getInstance() {        return singleton;    }    public static Object invokeNoArgumentsMethod(Object object, String methodName) {        return getInstance().invokeMethod(object, methodName, EMPTY_ARGS);    }    public static Object invokeMethod(Object object, String methodName, Object arguments) {        return getInstance().invokeMethod(object, methodName, arguments);    }    public static Object invokeSuperMethod(Object object, String methodName, Object arguments) {        return getInstance().invokeSuperMethod(object, methodName, arguments);    }    public static Object invokeMethodSafe(Object object, String methodName, Object arguments) {        if (object != null) {            return getInstance().invokeMethod(object, methodName, arguments);        }        return null;    }    public static Object invokeStaticMethod(String type, String methodName, Object arguments) {        return getInstance().invokeStaticMethod(type, methodName, arguments);    }    public static Object invokeStaticNoArgumentsMethod(String type, String methodName) {        return getInstance().invokeStaticMethod(type, methodName, EMPTY_ARGS);    }    public static Object invokeConstructorAt(Class at, Class type, Object arguments) {        return getInstance().invokeConstructorAt(at, type, arguments);    }    public static Object invokeConstructorAt(Class at, String type, Object arguments) {        return getInstance().invokeConstructorAt(at, type, arguments);    }    public static Object invokeNoArgumentsConstructorAt(Class at, Class type) {        return getInstance().invokeConstructorAt(at, type, EMPTY_ARGS);    }    public static Object invokeConstructorOf(String type, Object arguments) {        return getInstance().invokeConstructorOf(type, arguments);    }    public static Object invokeConstructorOf(Class type, Object arguments) {        return getInstance().invokeConstructorOf(type, arguments);    }    public static Object invokeNoArgumentsConstructorOf(Class type) {        return getInstance().invokeConstructorOf(type, EMPTY_ARGS);    }    public static Object invokeClosure(Object closure, Object arguments) {        return getInstance().invokeMethod(closure, "doCall", arguments);    }    public static Iterator asIterator(Object collection) {        return getInstance().asIterator(collection);    }    public static Collection asCollection(Object collection) {        return getInstance().asCollection(collection);    }    public static List asList(Object args) {        return getInstance().asList(args);    }    public static String toString(Object arguments) {        if (arguments instanceof Object[])            return getInstance().toArrayString((Object[])arguments);        else if (arguments instanceof Collection)            return getInstance().toListString((Collection)arguments);        else if (arguments instanceof Map)            return getInstance().toMapString((Map)arguments);        else            return getInstance().toString(arguments);    }    public static String toTypeString(Object[] arguments) {        return getInstance().toTypeString(arguments);    }    public static String toMapString(Map arg) {        return getInstance().toMapString(arg);    }    public static String toListString(Collection arg) {        return getInstance().toListString(arg);    }    public static String toArrayString(Object[] arguments) {        return getInstance().toArrayString(arguments);    }    public static String inspect(Object self) {        return getInstance().inspect(self);    }    public static Object getAttribute(Object object, String attribute) {        return getInstance().getAttribute(object, attribute);    }    public static void setAttribute(Object object, String attribute, Object newValue) {        getInstance().setAttribute(object, attribute, newValue);    }    public static Object getProperty(Object object, String property) {        return getInstance().getProperty(object, property);    }    public static Object getPropertySafe(Object object, String property) {        if (object != null) {            return getInstance().getProperty(object, property);        }        return null;    }    public static void setProperty(Object object, String property, Object newValue) {        getInstance().setProperty(object, property, newValue);    }    /**     * This is so we don't have to reorder the stack when we call this method.     * At some point a better name might be in order.     */    public static void setProperty2(Object newValue, Object object, String property) {        getInstance().setProperty(object, property, newValue);    }    /**     * This is so we don't have to reorder the stack when we call this method.     * At some point a better name might be in order.     */    public static void setGroovyObjectProperty(Object newValue, GroovyObject object, String property) {        object.setProperty(property, newValue);    }    public static Object getGroovyObjectProperty(GroovyObject object, String property) {        return object.getProperty(property);    }    /**     * This is so we don't have to reorder the stack when we call this method.     * At some point a better name might be in order.     */    public static void setPropertySafe2(Object newValue, Object object, String property) {        if (object != null) {            setProperty2(newValue, object, property);        }    }    /**     * Returns the method pointer for the given object name     */    public static Closure getMethodPointer(Object object, String methodName) {        return getInstance().getMethodPointer(object, methodName);    }    /**     * Provides a hook for type coercion of the given object to the required type     *     * @param type   of object to convert the given object to     * @param object the object to be converted     * @return the original object or a new converted value     */    public static Object asType(Object object, Class type) {        return getInstance().asType(object, type);    }    public static boolean asBool(Object object) {        return getInstance().asBool(object);    }    public static boolean notObject(Object object) {        return !asBool(object);    }    public static boolean notBoolean(boolean bool) {        return !bool;    }    public static Object negate(Object value) {        if (value instanceof Integer) {            Integer number = (Integer) value;            return integerValue(-number.intValue());        }        else if (value instanceof Long) {            Long number = (Long) value;            return new Long(-number.longValue());        }        else if (value instanceof BigInteger) {            return ((BigInteger) value).negate();        }        else if (value instanceof BigDecimal) {            return ((BigDecimal) value).negate();        }        else if (value instanceof Double) {            Double number = (Double) value;            return new Double(-number.doubleValue());        }        else if (value instanceof Float) {            Float number = (Float) value;            return new Float(-number.floatValue());        }        else if (value instanceof ArrayList) {            // value is an list.            ArrayList newlist = new ArrayList();            Iterator it = ((ArrayList) value).iterator();            for (; it.hasNext();) {                newlist.add(negate(it.next()));            }            return newlist;        }        else {            throw new GroovyRuntimeException("Cannot negate type " + value.getClass().getName() + ", value " + value);        }    }    public static Object bitNegate(Object value) {        if (value instanceof Integer) {            Integer number = (Integer) value;            return integerValue(~number.intValue());        }        else if (value instanceof Long) {            Long number = (Long) value;            return new Long(~number.longValue());        }        else if (value instanceof BigInteger) {            return ((BigInteger) value).not();        }        else if (value instanceof String) {            // value is a regular expression.            return getInstance().regexPattern(value);        }        else if (value instanceof GString) {            // value is a regular expression.            return getInstance().regexPattern(value.toString());        }        else if (value instanceof ArrayList) {            // value is an list.            ArrayList newlist = new ArrayList();            Iterator it = ((ArrayList) value).iterator();            for (; it.hasNext();) {                newlist.add(bitNegate(it.next()));

⌨️ 快捷键说明

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