org.codehaus.groovy.runtime.ScriptBytecodeAdapter Java Examples

The following examples show how to use org.codehaus.groovy.runtime.ScriptBytecodeAdapter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SwitchBlock.java    From groovy-cps with Apache License 2.0 6 votes vote down vote up
/**
 * Called after the case expression is evaluated to decide if we are going to run the statement.
 */
public Next matcher(Object caseValue) {
    boolean b;
    try {
        b = ScriptBytecodeAdapter.isCase(switchValue, caseValue);
    } catch (Throwable t) {
        return throwException(e, t, getCase().loc, new ReferenceStackTrace());
    }

    if (b) {
        // started executing the body
        return body(null);
    } else {
        index++;
        return matcher();
    }
}
 
Example #2
Source File: GroovyJavaMethods.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> Closure<T> castToClosure(Object o) {
    try {
        if (ScriptBytecodeAdapter.compareEqual(o, null)) {
            return (Closure<T>)ScriptBytecodeAdapter.castToType(o, Closure.class);
        } else if (safeGroovyIsCase(o, Closure.class)) {
            return (Closure<T>)ScriptBytecodeAdapter.castToType(o, Closure.class);
        } else if (o instanceof Runnable) {
            return closureFromRunnable((Runnable)ScriptBytecodeAdapter.createPojoWrapper(ScriptBytecodeAdapter.castToType(o, Runnable.class), Runnable.class));
        } else if (o instanceof Callable) {
            return closureFromCallable((Callable<T>)ScriptBytecodeAdapter.createPojoWrapper(ScriptBytecodeAdapter.castToType(o, Callable.class), Callable.class));
        } else if (o instanceof Function) {
            return closureFromFunction((Function<Object, T>)ScriptBytecodeAdapter.createPojoWrapper(ScriptBytecodeAdapter.castToType(o, Function.class), Function.class));
        } else {
            throw new IllegalArgumentException("Cannot convert to closure: o="+o+"; type="+(o != null ? o.getClass() : null));
        }
    } catch (Throwable e) {
        throw Exceptions.propagate(e);
    }
}
 
Example #3
Source File: PogoMetaClassSite.java    From groovy with Apache License 2.0 6 votes vote down vote up
public final Object call(final Object receiver, final Object[] args) throws Throwable {
    if (checkCall(receiver)) {
        try {
            try {
                return metaClass.invokeMethod(receiver, name, args);
            } catch (MissingMethodException e) {
                if (e instanceof MissingMethodExecutionFailed) {
                    throw (MissingMethodException)e.getCause();
                } else if (receiver.getClass() == e.getType() && e.getMethod().equals(name)) {
                    // in case there's nothing else, invoke the object's own invokeMethod()
                    return ((GroovyObject)receiver).invokeMethod(name, args);
                } else {
                    throw e;
                }
            }
        } catch (GroovyRuntimeException gre) {
            throw ScriptBytecodeAdapter.unwrap(gre);
        }
    } else {
        return CallSiteArray.defaultCall(this, receiver, args);
    }
}
 
Example #4
Source File: GroovyAssert.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts that the given script fails when it is evaluated
 * and that a particular type of exception is thrown.
 *
 * @param clazz the class of the expected exception
 * @param script  the script that should fail
 * @return the caught exception
 */
public static Throwable shouldFail(Class clazz, String script) {
    Throwable th = null;
    try {
        GroovyShell shell = new GroovyShell();
        shell.evaluate(script, genericScriptName());
    } catch (GroovyRuntimeException gre) {
        th = ScriptBytecodeAdapter.unwrap(gre);
    } catch (Throwable e) {
        th = e;
    }

    if (th == null) {
        fail("Script should have failed with an exception of type " + clazz.getName());
    } else if (!clazz.isInstance(th)) {
        fail("Script should have failed with an exception of type " + clazz.getName() + ", instead got Exception " + th);
    }
    return th;
}
 
Example #5
Source File: GroovyAssert.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts that the given code closure fails when it is evaluated
 * and that a particular type of exception is thrown.
 *
 * @param clazz the class of the expected exception
 * @param code  the closure that should fail
 * @return the caught exception
 */
public static Throwable shouldFail(Class clazz, Closure code) {
    Throwable th = null;
    try {
        code.call();
    } catch (GroovyRuntimeException gre) {
        th = ScriptBytecodeAdapter.unwrap(gre);
    } catch (Throwable e) {
        th = e;
    }

    if (th == null) {
        fail("Closure " + code + " should have failed with an exception of type " + clazz.getName());
    } else if (!clazz.isInstance(th)) {
        fail("Closure " + code + " should have failed with an exception of type " + clazz.getName() + ", instead got Exception " + th);
    }
    return th;
}
 
Example #6
Source File: GroovyAssert.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts that the given code closure fails when it is evaluated
 *
 * @param code the code expected to fail
 * @return the caught exception
 */
public static Throwable shouldFail(Closure code) {
    boolean failed = false;
    Throwable th = null;
    try {
        code.call();
    } catch (GroovyRuntimeException gre) {
        failed = true;
        th = ScriptBytecodeAdapter.unwrap(gre);
    } catch (Throwable e) {
        failed = true;
        th = e;
    }
    assertTrue("Closure " + code + " should have failed", failed);
    return th;
}
 
Example #7
Source File: PogoMetaMethodSite.java    From groovy with Apache License 2.0 5 votes vote down vote up
public Object callCurrent(GroovyObject receiver, Object[] args) throws Throwable {
    if(checkCall(receiver, args)) {
        try {
            return invoke(receiver,args);
        } catch (GroovyRuntimeException gre) {
            throw ScriptBytecodeAdapter.unwrap(gre);
        }
    } else {
        return CallSiteArray.defaultCallCurrent(this, receiver, args);
    }
}
 
Example #8
Source File: ObjectRange.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static boolean areReversed(Comparable from, Comparable to) {
    try {
        return ScriptBytecodeAdapter.compareGreaterThan(from, to);
    } catch (IllegalArgumentException iae) {
        throw new IllegalArgumentException("Unable to create range due to incompatible types: " + from.getClass().getSimpleName() + ".." + to.getClass().getSimpleName() + " (possible missing brackets around range?)", iae);
    }
}
 
Example #9
Source File: ConstructorSite.java    From groovy with Apache License 2.0 5 votes vote down vote up
public final Object callConstructor(Object receiver, Object[] args) throws Throwable {
    if (checkCall(receiver, args)) {
        final Object[] newArgs = new Object[] {args[0]};
        final Object bean = constructor.invoke(newArgs);
        try {
            ((MetaClassImpl) metaClass).setProperties(bean, (Map) args[1]);
        } catch (GroovyRuntimeException gre) {
            throw ScriptBytecodeAdapter.unwrap(gre);
        }
        return bean;
    } else
        return CallSiteArray.defaultCallConstructor(this, receiver, args);
}
 
Example #10
Source File: GroovyCollections.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Selects the minimum value found in an Iterable of items.
 *
 * @param items an Iterable
 * @return the minimum value
 * @since 2.2.0
 */
public static <T> T min(Iterable<T> items) {
    T answer = null;
    for (T value : items) {
        if (value != null) {
            if (answer == null || ScriptBytecodeAdapter.compareLessThan(value, answer)) {
                answer = value;
            }
        }
    }
    return answer;
}
 
Example #11
Source File: GroovyJavaMethods.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> T fix(Object o) {
    try {
        if (safeGroovyIsCase(o, GString.class)) {
            return (T)ScriptBytecodeAdapter.asType(o, String.class);
        } else {
            return (T)o;
        }
    } catch (Throwable e) {
        throw Exceptions.propagate(e);
    }
}
 
Example #12
Source File: ConstructorSite.java    From groovy with Apache License 2.0 5 votes vote down vote up
public final Object callConstructor(Object receiver, Object[] args) throws Throwable {
    if (checkCall(receiver, args)) {
        final Object bean = constructor.invoke(NO_ARGS);
        try {
            ((MetaClassImpl) metaClass).setProperties(bean, (Map) args[0]);
        } catch (GroovyRuntimeException gre) {
            throw ScriptBytecodeAdapter.unwrap(gre);
        }
        return bean;
    } else
        return CallSiteArray.defaultCallConstructor(this, receiver, args);
}
 
Example #13
Source File: DefaultInvoker.java    From groovy-cps with Apache License 2.0 5 votes vote down vote up
public Object superCall(Class methodType, Object receiver, String method, Object[] args) throws Throwable {
    try {
        MetaClass mc = InvokerHelper.getMetaClass(receiver.getClass());
        return mc.invokeMethod(methodType.getSuperclass(), receiver, method, args, true, true);
    } catch (GroovyRuntimeException gre) {
        throw ScriptBytecodeAdapter.unwrap(gre);
    }
}
 
Example #14
Source File: NewArrayBlock.java    From groovy-cps with Apache License 2.0 5 votes vote down vote up
public Next fixArg(Object v) {
    try {
        dimensions[idx++] = (Integer)ScriptBytecodeAdapter.castToType(v,int.class);
    } catch (Throwable t) {
        return throwException(e, t, loc, new ReferenceStackTrace());
    }
    return dispatchOrArg();
}
 
Example #15
Source File: ForInLoopBlock.java    From groovy-cps with Apache License 2.0 5 votes vote down vote up
public Next loopHead(Object col) {
    try {
        itr = (Iterator) ScriptBytecodeAdapter.invokeMethod0(null/*unused*/, col, "iterator");
    } catch (Throwable t) {
        return throwException(e, t, loc, new ReferenceStackTrace());
    }

    return increment(null);
}
 
Example #16
Source File: LocalVariableBlock.java    From groovy-cps with Apache License 2.0 5 votes vote down vote up
public Next set(Object v, Continuation k) {
    Class type = e.getLocalVariableType(name);
    try {
        e.setLocalVariable(name, (type == null) ? v : ScriptBytecodeAdapter.castToType(v, type));
    } catch (Throwable t) {
        return throwException(e, t, loc, new ReferenceStackTrace());
    }

    return k.receive(null);
}
 
Example #17
Source File: AssertBlock.java    From groovy-cps with Apache License 2.0 5 votes vote down vote up
public Next fail(Object msg) {
    try {
        ScriptBytecodeAdapter.assertFailed(sourceText, msg);
        throw new IllegalStateException();  // assertFailed will throw an exception
    } catch (Throwable t) {
        return e.getExceptionHandler(t.getClass()).receive(t);
    }
}
 
Example #18
Source File: GroovyJavaMethods.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> Callable<T> callableFromRunnable(final Runnable job) {
    try {
        if (safeGroovyIsCase(job, Callable.class)) {
            return (Callable<T>)ScriptBytecodeAdapter.asType(job, Callable.class);
        } else {
            return CallableFromRunnable.newInstance(job, null);
        }
    } catch (Throwable e) {
        throw Exceptions.propagate(e);
    }
}
 
Example #19
Source File: ConstructorSite.java    From groovy with Apache License 2.0 5 votes vote down vote up
public Object callConstructor(Object receiver, Object[] args) throws Throwable {
    if (checkCall(receiver, args)) {
        try {
            return constructor.invoke(args);
        } catch (GroovyRuntimeException gre) {
            throw ScriptBytecodeAdapter.unwrap(gre);
        }
    } else
        return CallSiteArray.defaultCallConstructor(this, receiver, args);
}
 
Example #20
Source File: ConstructorSite.java    From groovy with Apache License 2.0 5 votes vote down vote up
public final Object callConstructor(Object receiver, Object[] args) throws Throwable {
    if (checkCall(receiver, args)) {
        try {
            return constructor.doConstructorInvoke(args);
        } catch (GroovyRuntimeException gre) {
            throw ScriptBytecodeAdapter.unwrap(gre);
        }
    } else
        return CallSiteArray.defaultCallConstructor(this, receiver, args);
}
 
Example #21
Source File: ConstructorSite.java    From groovy with Apache License 2.0 5 votes vote down vote up
public Object callConstructor(Object receiver, Object[] args) throws Throwable {
    if (checkCall(receiver, args)) {
        MetaClassHelper.unwrap(args);
        try {
            return constructor.doConstructorInvoke(args);
        } catch (GroovyRuntimeException gre) {
            throw ScriptBytecodeAdapter.unwrap(gre);
        }
    } else
        return CallSiteArray.defaultCallConstructor(this, receiver, args);
}
 
Example #22
Source File: GetEffectivePogoPropertySite.java    From groovy with Apache License 2.0 5 votes vote down vote up
public final Object getProperty(Object receiver) throws Throwable {
    try {
        return effective.getProperty(receiver);
    } catch (GroovyRuntimeException gre) {
        throw ScriptBytecodeAdapter.unwrap(gre);
    }
}
 
Example #23
Source File: GetEffectivePogoPropertySite.java    From groovy with Apache License 2.0 5 votes vote down vote up
public final Object callGroovyObjectGetProperty (Object receiver) throws Throwable {
    if (GroovyCategorySupport.hasCategoryInCurrentThread() || !(receiver instanceof GroovyObject) || ((GroovyObject) receiver).getMetaClass() != metaClass) {
        return createGetPropertySite(receiver).getProperty(receiver);
    } else {
        try {
            return effective.getProperty(receiver);
        } catch (GroovyRuntimeException gre) {
            throw ScriptBytecodeAdapter.unwrap(gre);
        }
    }
}
 
Example #24
Source File: PogoGetPropertySite.java    From groovy with Apache License 2.0 5 votes vote down vote up
public Object getProperty(Object receiver) throws Throwable {
    try{
        return ((GroovyObject)receiver).getProperty(name);
    } catch (GroovyRuntimeException gre) {
        throw ScriptBytecodeAdapter.unwrap(gre);
    }
}
 
Example #25
Source File: ClassMetaClassGetPropertySite.java    From groovy with Apache License 2.0 5 votes vote down vote up
public final Object getProperty(Object receiver) throws Throwable{
    try {
        return metaClass.getProperty(aClass, name);
    } catch (GroovyRuntimeException gre) {
        throw ScriptBytecodeAdapter.unwrap(gre);
    }
}
 
Example #26
Source File: PogoMetaClassSite.java    From groovy with Apache License 2.0 5 votes vote down vote up
public final Object callCurrent(final GroovyObject receiver, final Object[] args) throws Throwable {
    if (checkCall(receiver)) {
        try {
            try {
                return metaClass.invokeMethod(array.owner, receiver, name, args, false, true);
            } catch (MissingMethodException e) {
                if (e instanceof MissingMethodExecutionFailed) {
                    throw (MissingMethodException) e.getCause();
                } else if (receiver.getClass() == e.getType() && e.getMethod().equals(name)) {
                    // in case there's nothing else, invoke the receiver's own invokeMethod()
                    try {
                        return receiver.invokeMethod(name, args);
                    } catch (MissingMethodException mme) {
                        if (mme instanceof MissingMethodExecutionFailed)
                            throw (MissingMethodException) mme.getCause();
                        // GROOVY-9387: in rare cases, this form still works
                        return metaClass.invokeMethod(receiver, name, args);
                    }
                } else {
                    throw e;
                }
            }
        } catch (GroovyRuntimeException gre) {
            throw ScriptBytecodeAdapter.unwrap(gre);
        }
    } else {
        return CallSiteArray.defaultCallCurrent(this, receiver, args);
    }
}
 
Example #27
Source File: PogoMetaMethodSite.java    From groovy with Apache License 2.0 5 votes vote down vote up
public final Object invoke(Object receiver, Object[] args) throws Throwable {
    try {
        return metaMethod.invoke(receiver,  args);
    } catch (GroovyRuntimeException gre) {
        throw ScriptBytecodeAdapter.unwrap(gre);
    }
}
 
Example #28
Source File: PogoMetaMethodSite.java    From groovy with Apache License 2.0 5 votes vote down vote up
public final Object invoke(Object receiver, Object[] args) throws Throwable {
    try {
        return metaMethod.doMethodInvoke(receiver,  args);
    } catch (GroovyRuntimeException gre) {
        throw ScriptBytecodeAdapter.unwrap(gre);
    }
}
 
Example #29
Source File: PogoMetaMethodSite.java    From groovy with Apache License 2.0 5 votes vote down vote up
public Object call(Object receiver, Object[] args) throws Throwable {
    if(checkCall(receiver, args)) {
        try {
            return invoke(receiver,args);
        } catch (GroovyRuntimeException gre) {
            throw ScriptBytecodeAdapter.unwrap(gre);
        }
    } else {
        return CallSiteArray.defaultCall(this, receiver, args);
    }
}
 
Example #30
Source File: GetEffectivePogoPropertySite.java    From groovy with Apache License 2.0 5 votes vote down vote up
public final Object callGetProperty (Object receiver) throws Throwable {
    if (GroovyCategorySupport.hasCategoryInCurrentThread() || !(receiver instanceof GroovyObject) || ((GroovyObject) receiver).getMetaClass() != metaClass) {
        return createGetPropertySite(receiver).getProperty(receiver);
    } else {
        try {
            return effective.getProperty(receiver);
        } catch (GroovyRuntimeException gre) {
            throw ScriptBytecodeAdapter.unwrap(gre);
        }
    }
}