Search in sources :

Example 56 with GroovyRuntimeException

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

the class ProxyGeneratorAdapter method proxy.

@SuppressWarnings("unchecked")
public GroovyObject proxy(Map<Object, Object> map, Object... constructorArgs) {
    if (constructorArgs == null && cachedNoArgConstructor != null) {
        // if there isn't any argument, we can make invocation faster using the cached constructor
        try {
            return (GroovyObject) cachedNoArgConstructor.newInstance(map);
        } catch (InstantiationException e) {
            throw new GroovyRuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new GroovyRuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new GroovyRuntimeException(e);
        }
    }
    if (constructorArgs == null)
        constructorArgs = EMPTY_ARGS;
    Object[] values = new Object[constructorArgs.length + 1];
    System.arraycopy(constructorArgs, 0, values, 0, constructorArgs.length);
    values[values.length - 1] = map;
    return DefaultGroovyMethods.<GroovyObject>newInstance(cachedClass, values);
}
Also used : GroovyRuntimeException(groovy.lang.GroovyRuntimeException) GroovyObject(groovy.lang.GroovyObject) InvocationTargetException(java.lang.reflect.InvocationTargetException) GroovyObject(groovy.lang.GroovyObject)

Example 57 with GroovyRuntimeException

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

the class WriterController method init.

public void init(AsmClassGenerator asmClassGenerator, GeneratorContext gcon, ClassVisitor cv, ClassNode cn) {
    CompilerConfiguration config = cn.getCompileUnit().getConfig();
    Map<String, Boolean> optOptions = config.getOptimizationOptions();
    boolean invokedynamic = false;
    if (optOptions.isEmpty()) {
    // IGNORE
    } else if (Boolean.FALSE.equals(optOptions.get("all"))) {
        optimizeForInt = false;
    // set other optimizations options to false here
    } else {
        if (Boolean.TRUE.equals(optOptions.get(CompilerConfiguration.INVOKEDYNAMIC)))
            invokedynamic = true;
        if (Boolean.FALSE.equals(optOptions.get("int")))
            optimizeForInt = false;
        if (invokedynamic)
            optimizeForInt = false;
    // set other optimizations options to false here
    }
    this.classNode = cn;
    this.outermostClass = null;
    this.internalClassName = BytecodeHelper.getClassInternalName(classNode);
    bytecodeVersion = chooseBytecodeVersion(invokedynamic, config.getTargetBytecode());
    if (invokedynamic) {
        try {
            this.invocationWriter = (InvocationWriter) indyWriter.newInstance(this);
            this.callSiteWriter = (CallSiteWriter) indyCallSiteWriter.newInstance(this);
            this.binaryExpHelper = (BinaryExpressionHelper) indyBinHelper.newInstance(this);
        } catch (Exception e) {
            throw new GroovyRuntimeException("Cannot use invokedynamic, indy module was excluded from this build.");
        }
    } else {
        this.callSiteWriter = new CallSiteWriter(this);
        this.invocationWriter = new InvocationWriter(this);
        this.binaryExpHelper = new BinaryExpressionHelper(this);
    }
    this.unaryExpressionHelper = new UnaryExpressionHelper(this);
    if (optimizeForInt) {
        this.fastPathBinaryExpHelper = new BinaryExpressionMultiTypeDispatcher(this);
        // todo: replace with a real fast path unary expression helper when available
        this.fastPathUnaryExpressionHelper = new UnaryExpressionHelper(this);
    } else {
        this.fastPathBinaryExpHelper = this.binaryExpHelper;
        this.fastPathUnaryExpressionHelper = new UnaryExpressionHelper(this);
    }
    this.operandStack = new OperandStack(this);
    this.assertionWriter = new AssertionWriter(this);
    this.closureWriter = new ClosureWriter(this);
    this.internalBaseClassName = BytecodeHelper.getClassInternalName(classNode.getSuperClass());
    this.acg = asmClassGenerator;
    this.sourceUnit = acg.getSourceUnit();
    this.context = gcon;
    this.compileStack = new CompileStack(this);
    this.cv = cv;
    if (optimizeForInt) {
        this.statementWriter = new OptimizingStatementWriter(this);
    } else {
        this.statementWriter = new StatementWriter(this);
    }
    this.typeChooser = new StatementMetaTypeChooser();
}
Also used : GroovyRuntimeException(groovy.lang.GroovyRuntimeException) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration)

Example 58 with GroovyRuntimeException

use of groovy.lang.GroovyRuntimeException in project intellij-community by JetBrains.

the class ScriptSupport method evaluate.

public String evaluate(MatchResult result, PsiElement context) {
    try {
        final HashMap<String, Object> variableMap = new HashMap<>();
        variableMap.put(ScriptLog.SCRIPT_LOG_VAR_NAME, myScriptLog);
        if (result != null) {
            buildVariableMap(result, variableMap);
            if (context == null) {
                context = result.getMatch();
            }
        }
        context = StructuralSearchUtil.getPresentableElement(context);
        variableMap.put(myName, context);
        variableMap.put(Configuration.CONTEXT_VAR_NAME, context);
        script.setBinding(new Binding(variableMap));
        final Object o = script.run();
        return String.valueOf(o);
    } catch (GroovyRuntimeException ex) {
        throw new StructuralSearchException(SSRBundle.message("groovy.script.error", ex.getMessage()));
    } finally {
        script.setBinding(null);
    }
}
Also used : Binding(groovy.lang.Binding) StructuralSearchException(com.intellij.structuralsearch.StructuralSearchException) HashMap(java.util.HashMap) GroovyRuntimeException(groovy.lang.GroovyRuntimeException)

Example 59 with GroovyRuntimeException

use of groovy.lang.GroovyRuntimeException in project intellij-community by JetBrains.

the class GroovyCompilerWrapper method processException.

private void processException(Throwable exception, String prefix) {
    if (exception instanceof GroovyRuntimeException) {
        addErrorMessage((GroovyRuntimeException) exception);
        return;
    }
    if (forStubs) {
        collector.add(new CompilerMessage(GroovyCompilerMessageCategories.INFORMATION, GroovyRtConstants.GROOVYC_STUB_GENERATION_FAILED, null, -1, -1));
    }
    final StringWriter writer = new StringWriter();
    writer.append(prefix);
    if (!prefix.endsWith("\n")) {
        writer.append("\n\n");
    }
    //noinspection IOResourceOpenedButNotSafelyClosed
    exception.printStackTrace(new PrintWriter(writer));
    collector.add(new CompilerMessage(forStubs ? GroovyCompilerMessageCategories.INFORMATION : GroovyCompilerMessageCategories.ERROR, writer.toString(), null, -1, -1));
}
Also used : StringWriter(java.io.StringWriter) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) PrintWriter(java.io.PrintWriter)

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