Search in sources :

Example 6 with GroovyRuntimeException

use of groovy.lang.GroovyRuntimeException in project groovy by apache.

the class DummyClassGenerator method visitClass.

// GroovyClassVisitor interface
//-------------------------------------------------------------------------
public void visitClass(ClassNode classNode) {
    try {
        this.classNode = classNode;
        this.internalClassName = BytecodeHelper.getClassInternalName(classNode);
        //System.out.println("Generating class: " + classNode.getName());
        this.internalBaseClassName = BytecodeHelper.getClassInternalName(classNode.getSuperClass());
        cv.visit(Opcodes.V1_3, classNode.getModifiers(), internalClassName, (String) null, internalBaseClassName, BytecodeHelper.getClassInternalNames(classNode.getInterfaces()));
        classNode.visitContents(this);
        for (Iterator iter = innerClasses.iterator(); iter.hasNext(); ) {
            ClassNode innerClass = (ClassNode) iter.next();
            ClassNode innerClassType = innerClass;
            String innerClassInternalName = BytecodeHelper.getClassInternalName(innerClassType);
            // default for inner classes
            String outerClassName = internalClassName;
            MethodNode enclosingMethod = innerClass.getEnclosingMethod();
            if (enclosingMethod != null) {
                // local inner classes do not specify the outer class name
                outerClassName = null;
            }
            cv.visitInnerClass(innerClassInternalName, outerClassName, innerClassType.getName(), innerClass.getModifiers());
        }
        cv.visitEnd();
    } catch (GroovyRuntimeException e) {
        e.setModule(classNode.getModule());
        throw e;
    }
}
Also used : Iterator(java.util.Iterator) GroovyRuntimeException(groovy.lang.GroovyRuntimeException)

Example 7 with GroovyRuntimeException

use of groovy.lang.GroovyRuntimeException in project groovy by apache.

the class AsmClassGenerator method visitConstructorOrMethod.

protected void visitConstructorOrMethod(MethodNode node, boolean isConstructor) {
    controller.resetLineNumber();
    Parameter[] parameters = node.getParameters();
    String methodType = BytecodeHelper.getMethodDescriptor(node.getReturnType(), parameters);
    String signature = BytecodeHelper.getGenericsMethodSignature(node);
    int modifiers = node.getModifiers();
    if (isVargs(node.getParameters()))
        modifiers |= Opcodes.ACC_VARARGS;
    MethodVisitor mv = cv.visitMethod(modifiers, node.getName(), methodType, signature, buildExceptions(node.getExceptions()));
    controller.setMethodVisitor(mv);
    visitAnnotations(node, mv);
    for (int i = 0; i < parameters.length; i++) {
        visitParameterAnnotations(parameters[i], i, mv);
    }
    // Add parameter names to the MethodVisitor (jdk8+ only)
    if (getCompileUnit().getConfig().getParameters()) {
        for (int i = 0; i < parameters.length; i++) {
            // TODO handle ACC_SYNTHETIC for enum method parameters?
            mv.visitParameter(parameters[i].getName(), 0);
        }
    }
    if (controller.getClassNode().isAnnotationDefinition() && !node.isStaticConstructor()) {
        visitAnnotationDefault(node, mv);
    } else if (!node.isAbstract()) {
        Statement code = node.getCode();
        mv.visitCode();
        // fast path for getter/setters etc.
        if (code instanceof BytecodeSequence && ((BytecodeSequence) code).getInstructions().size() == 1 && ((BytecodeSequence) code).getInstructions().get(0) instanceof BytecodeInstruction) {
            ((BytecodeInstruction) ((BytecodeSequence) code).getInstructions().get(0)).visit(mv);
        } else {
            visitStdMethod(node, isConstructor, parameters, code);
        }
        //mv.visitInsn(NOP);
        try {
            mv.visitMaxs(0, 0);
        } catch (Exception e) {
            throw new GroovyRuntimeException("ASM reporting processing error for " + controller.getClassNode() + "#" + node.getName() + " with signature " + node.getTypeDescriptor() + " in " + sourceFile + ":" + node.getLineNumber(), e);
        }
    }
    mv.visitEnd();
}
Also used : CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) AssertStatement(org.codehaus.groovy.ast.stmt.AssertStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) WhileStatement(org.codehaus.groovy.ast.stmt.WhileStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) DoWhileStatement(org.codehaus.groovy.ast.stmt.DoWhileStatement) ContinueStatement(org.codehaus.groovy.ast.stmt.ContinueStatement) BreakStatement(org.codehaus.groovy.ast.stmt.BreakStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement) SwitchStatement(org.codehaus.groovy.ast.stmt.SwitchStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) Parameter(org.codehaus.groovy.ast.Parameter) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) RuntimeParserException(org.codehaus.groovy.syntax.RuntimeParserException) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 8 with GroovyRuntimeException

use of groovy.lang.GroovyRuntimeException in project groovy by apache.

the class MethodFailureTest method testMethodWhichCallsTheFailingMethodInsideAClosure.

public void testMethodWhichCallsTheFailingMethodInsideAClosure() {
    MockGroovyObject object = new MockGroovyObject();
    try {
        object.invokeMethod("callClosure", new Closure(this) {

            protected Object doCall(GroovyObject object) {
                return object.invokeMethod("nonExistentMethod", "hello");
            }
        });
        fail("Should have thrown an exception");
    } catch (GroovyRuntimeException e) {
    // expected
    }
}
Also used : Closure(groovy.lang.Closure) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) GroovyObject(groovy.lang.GroovyObject) GroovyObject(groovy.lang.GroovyObject)

Example 9 with GroovyRuntimeException

use of groovy.lang.GroovyRuntimeException in project groovy by apache.

the class ConversionHandler method invoke.

/**
     * This method is a default implementation for the invoke method given in
     * InvocationHandler. Any call to a method with a declaring class that is
     * not Object, excluding toString() and default methods is redirected to invokeCustom.
     * <p>
     * Methods like equals and hashcode are called on the class itself instead
     * of the delegate because they are considered fundamental methods that should
     * not be overwritten. The toString() method gets special treatment as it is
     * deemed to be a method that you might wish to override when called from Groovy.
     * Interface default methods from Java 8 on the other hand are considered being
     * default implementations you don't normally want to change. So they are called
     * directly too
     * </p><p>
     * In many scenarios, it is better to overwrite the invokeCustom method where
     * the core Object related methods are filtered out.
     *</p>
     * @param proxy  the proxy
     * @param method the method
     * @param args   the arguments
     * @return the result of the invocation by method or delegate
     * @throws Throwable if caused by the delegate or the method
     * @see #invokeCustom(Object, Method, Object[])
     * @see InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
     */
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    if (handleCache != null && isDefaultMethod(method)) {
        VMPlugin plugin = VMPluginFactory.getPlugin();
        Object handle = handleCache.get(method);
        if (handle == null) {
            handle = plugin.getInvokeSpecialHandle(method, proxy);
            handleCache.put(method, handle);
        }
        return plugin.invokeHandle(handle, args);
    }
    if (!checkMethod(method)) {
        try {
            if (method.getDeclaringClass() == GroovyObject.class) {
                if ("getMetaClass".equals(method.getName())) {
                    return getMetaClass(proxy);
                } else if ("setMetaClass".equals(method.getName())) {
                    return setMetaClass((MetaClass) args[0]);
                }
            }
            return invokeCustom(proxy, method, args);
        } catch (GroovyRuntimeException gre) {
            throw ScriptBytecodeAdapter.unwrap(gre);
        }
    }
    try {
        return method.invoke(this, args);
    } catch (InvocationTargetException ite) {
        throw ite.getTargetException();
    }
}
Also used : MetaClass(groovy.lang.MetaClass) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) GroovyObject(groovy.lang.GroovyObject) VMPlugin(org.codehaus.groovy.vmplugin.VMPlugin) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 10 with GroovyRuntimeException

use of groovy.lang.GroovyRuntimeException in project groovy by apache.

the class DefaultTypeTransformation method continueCastOnSAM.

private static Object continueCastOnSAM(Object object, Class type) {
    if (object instanceof Closure) {
        Method m = CachedSAMClass.getSAMMethod(type);
        if (m != null) {
            return CachedSAMClass.coerceToSAM((Closure) object, m, type, type.isInterface());
        }
    }
    Object[] args = null;
    if (object instanceof Collection) {
        // let's try invoke the constructor with the list as arguments
        // such as for creating a Dimension, Point, Color etc.
        Collection collection = (Collection) object;
        args = collection.toArray();
    } else if (object instanceof Object[]) {
        args = (Object[]) object;
    } else if (object instanceof Map) {
        // emulate named params constructor
        args = new Object[1];
        args[0] = object;
    }
    Exception nested = null;
    if (args != null) {
        try {
            return InvokerHelper.invokeConstructorOf(type, args);
        } catch (InvokerInvocationException iie) {
            throw iie;
        } catch (GroovyRuntimeException e) {
            if (e.getMessage().contains("Could not find matching constructor for")) {
                try {
                    return InvokerHelper.invokeConstructorOf(type, object);
                } catch (InvokerInvocationException iie) {
                    throw iie;
                } catch (Exception ex) {
                    // let's ignore exception and return the original object
                    // as the caller has more context to be able to throw a more
                    // meaningful exception (but stash to get message later)
                    nested = e;
                }
            } else {
                nested = e;
            }
        } catch (Exception e) {
            // let's ignore exception and return the original object
            // as the caller has more context to be able to throw a more
            // meaningful exception (but stash to get message later)
            nested = e;
        }
    }
    GroovyCastException gce;
    if (nested != null) {
        gce = new GroovyCastException(object, type, nested);
    } else {
        gce = new GroovyCastException(object, type);
    }
    throw gce;
}
Also used : MethodClosure(org.codehaus.groovy.runtime.MethodClosure) Closure(groovy.lang.Closure) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) Collection(java.util.Collection) InvokerInvocationException(org.codehaus.groovy.runtime.InvokerInvocationException) NullObject(org.codehaus.groovy.runtime.NullObject) Method(java.lang.reflect.Method) Map(java.util.Map) InvokerInvocationException(org.codehaus.groovy.runtime.InvokerInvocationException) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) IOException(java.io.IOException)

Aggregations

GroovyRuntimeException (groovy.lang.GroovyRuntimeException)59 IOException (java.io.IOException)15 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 GroovyObject (groovy.lang.GroovyObject)8 ArrayList (java.util.ArrayList)6 Closure (groovy.lang.Closure)5 InputStream (java.io.InputStream)5 List (java.util.List)5 GroovyShell (groovy.lang.GroovyShell)4 MetaClass (groovy.lang.MetaClass)4 Map (java.util.Map)4 ClassNode (org.codehaus.groovy.ast.ClassNode)4 MethodNode (org.codehaus.groovy.ast.MethodNode)4 MetaMethod (groovy.lang.MetaMethod)3 Script (groovy.lang.Script)3 PrintWriter (java.io.PrintWriter)3 StringWriter (java.io.StringWriter)3 URL (java.net.URL)3 DelegatingMetaClass (groovy.lang.DelegatingMetaClass)2 ExpandoMetaClass (groovy.lang.ExpandoMetaClass)2