scriptbytecodeadapter.java

来自「Groovy动态语言 运行在JVM中的动态语言 可以方便的处理业务逻辑变化大的业」· Java 代码 · 共 834 行 · 第 1/3 页

JAVA
834
字号
    //  --------------------------------------------------------       

    public static Object getGroovyObjectProperty(Class senderClass, GroovyObject receiver, String messageName) throws Throwable{
        return receiver.getProperty(messageName);
    }
    
    public static Object getGroovyObjectPropertySafe(Class senderClass, GroovyObject receiver, String messageName) throws Throwable{
        if (receiver==null) return null;
        return receiver.getProperty(messageName);
    }
    
    public static Object getGroovyObjectPropertySpreadSafe(Class senderClass, GroovyObject receiver, String messageName) throws Throwable{
        if (receiver==null) return null;
        if (! (receiver instanceof List)) return getGroovyObjectProperty(senderClass,receiver,messageName);
        
        List list = (List) receiver;
        List answer = new ArrayList();
        for (Iterator it = list.iterator(); it.hasNext();) {
            answer.add(getPropertySafe(senderClass, it.next(), messageName));
        }
        return answer;
    }

    //  --------------------------------------------------------
    //              normal GroovyObject Property handling : set
    //  --------------------------------------------------------       

    public static void setGroovyObjectProperty(Object messageArgument, Class senderClass, GroovyObject receiver, String messageName) throws Throwable{
        receiver.setProperty(messageName,messageArgument);
    }
    
    public static void setGroovyObjectPropertySafe(Object messageArgument, Class senderClass, GroovyObject receiver, String messageName) throws Throwable{
        if (receiver==null) return;
        receiver.setProperty(messageName,messageArgument);
    }
    
    public static void setGroovyObjectPropertySpreadSafe(Object messageArgument, Class senderClass, GroovyObject receiver, String messageName) throws Throwable{
        if (receiver==null) return;
        if (! (receiver instanceof List)) {
            setProperty(messageArgument, senderClass, receiver, messageName);
            return;
        }
        
        List list = (List) receiver;
        List answer = new ArrayList();
        for (Iterator it = list.iterator(); it.hasNext();) {
            setPropertySafe(messageArgument, senderClass, it.next(), messageName);
        }
    }
    
    //  **********************************************************************************
    //  **********************************************************************************
    //  **************          methods not covered by the new MOP          **************
    //  **********************************************************************************
    //  **********************************************************************************
    
    //  --------------------------------------------------------
    //                     Closures
    //  --------------------------------------------------------           
    
    /**
     * Returns the method pointer for the given object name
     */
    public static Closure getMethodPointer(Object object, String methodName) {
        return InvokerHelper.getMethodPointer(object, methodName);
    }
    
    // TODO: set sender class
    public static Object invokeClosure(Object closure, Object[] arguments) throws Throwable {
        return invokeMethodN(closure.getClass(), closure, "doCall", arguments);
    } 
        

    //  --------------------------------------------------------
    //                     type conversion
    //  --------------------------------------------------------           
          
    /**
     * 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
     * @throws Throwable 
     */
    public static Object asType(Object object, Class type) throws Throwable {
        return invokeMethodN(object.getClass(),object,"asType",new Object[]{type});
    }
    
    /**
     * Provides a hook for type casting 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
     * @throws Throwable 
     */
    public static Object castToType(Object object, Class type) throws Throwable{
        try {
            return DefaultTypeTransformation.castToType(object,type);
        } catch (GroovyRuntimeException gre) {
            return (Matcher) unwrap(gre);
        }
    }    

    public static Tuple createTuple(Object[] array) {
        return new Tuple(array);
    }

    public static List createList(Object[] values) {
        return InvokerHelper.createList(values);
    }
    
    public static Wrapper createPojoWrapper(Object val, Class clazz) {
        return new PojoWrapper(val,clazz);
    }

    public static Wrapper createGroovyObjectWrapper(GroovyObject val, Class clazz) {
        return new GroovyObjectWrapper(val,clazz);
    }
    
    public static Map createMap(Object[] values) {
        return InvokerHelper.createMap(values);
    }
    

    //TODO: refactor
    public static List createRange(Object from, Object to, boolean inclusive) throws Throwable {
        if (!inclusive) {
            if (compareEqual(from,to)){
                return new EmptyRange((Comparable)from);
            }
            if (compareGreaterThan(from, to)) {
                to = invokeMethod0(ScriptBytecodeAdapter.class, to, "next");
            }
            else {
                to = invokeMethod0(ScriptBytecodeAdapter.class, to, "previous");
            }
        }
        if (from instanceof Integer && to instanceof Integer) {
            return new IntRange(DefaultTypeTransformation.intUnbox(from), DefaultTypeTransformation.intUnbox(to));
        }
        else {
            return new ObjectRange((Comparable) from, (Comparable) to);
        }
    }
    
    //assert
    public static void assertFailed(Object expression, Object message) {
        InvokerHelper.assertFailed(expression,message);
    }

    //isCase
    //TODO: set sender class
    public static boolean isCase(Object switchValue, Object caseExpression) throws Throwable{
        if (caseExpression == null) {
            return switchValue == null;
        }
        return DefaultTypeTransformation.castToBoolean(invokeMethodN(caseExpression.getClass(), caseExpression, "isCase", new Object[]{switchValue}));
    }
    
    //compare
    public static boolean compareIdentical(Object left, Object right) {
        return left == right;
    }
    
    public static boolean compareEqual(Object left, Object right) {
        return DefaultTypeTransformation.compareEqual(left, right);
    }
    
    public static boolean compareNotEqual(Object left, Object right) {
        return !compareEqual(left, right);
    }
    
    public static Integer compareTo(Object left, Object right) {
        int answer = DefaultTypeTransformation.compareTo(left, right);
        if (answer == 0) {
            return ZERO;
        }
        else {
            return answer > 0 ? ONE : MINUS_ONE;
        }
    }    

    public static boolean compareLessThan(Object left, Object right) {
        return compareTo(left, right).intValue() < 0;
    }
    
    public static boolean compareLessThanEqual(Object left, Object right){
        return compareTo(left, right).intValue() <= 0;
    }
    
    public static boolean compareGreaterThan(Object left, Object right){
        return compareTo(left, right).intValue() > 0;
    }

    public static boolean compareGreaterThanEqual(Object left, Object right){
        return compareTo(left, right).intValue() >= 0;
    }

    //regexpr
    public static Pattern regexPattern(Object regex) {
        return DefaultGroovyMethods.negate(regex.toString());
    }
    
    public static Matcher findRegex(Object left, Object right) throws Throwable{
        try {
            return InvokerHelper.findRegex(left, right);
        } catch (GroovyRuntimeException gre) {
            return (Matcher) unwrap(gre);
        }
    }
    
    public static boolean matchRegex(Object left, Object right) {
        return InvokerHelper.matchRegex(left, right);
    }
    
    
    //spread expressions
    public static Object[] despreadList(Object[] args, Object[] spreads, int[] positions) {
        ArrayList ret = new ArrayList();
        int argsPos = 0;
        int spreadPos = 0;
        for (int pos = 0; pos<positions.length; pos++) {
            for (;argsPos<positions[pos];argsPos++) {
                ret.add(args[argsPos]);
            }
            Object value = spreads[spreadPos];
            if (value==null) {
                ret.add(null);
            } else if (value instanceof List) {
                ret.addAll((List) value);
            } else if (value.getClass().isArray()) {
                ret.addAll(DefaultTypeTransformation.primitiveArrayToList(value));
            } else {
                throw new IllegalArgumentException("connot spread the type "+ value.getClass().getName()+" with value "+value);
            }
            spreadPos++;
        }
        for (;argsPos<args.length;argsPos++) {
            ret.add(args[argsPos]);
        }
        return ret.toArray();
    }
    
    public static Object spreadMap(Object value) {
        return InvokerHelper.spreadMap(value);
    }
    
    //negation
    public static Object negate(Object value) throws Throwable {
        try {
            return InvokerHelper.negate(value);
        } catch (GroovyRuntimeException gre) {
            return unwrap(gre);
        }
    }
    
    public static Object bitNegate(Object value) {
        return InvokerHelper.bitNegate(value);
    }

    public static MetaClass initMetaClass(Object object) {
        return InvokerHelper.getMetaClass(object);
    }
    
    private static MetaClass getMetaClassObjectNotNull(Object object) {
        if (!(object instanceof GroovyObject)) {
            return initMetaClass(object);
        } else {
            return ((GroovyObject) object).getMetaClass();
        }        
    }
    
    

}

⌨️ 快捷键说明

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