Search in sources :

Example 1 with StringBuilderWriter

use of org.apache.groovy.io.StringBuilderWriter in project groovy by apache.

the class AsmClassGenerator method visitConstructorOrMethod.

@Override
protected void visitConstructorOrMethod(final MethodNode node, final boolean isConstructor) {
    Parameter[] parameters = node.getParameters();
    MethodVisitor mv = classVisitor.visitMethod(node.getModifiers() | (isVargs(parameters) ? ACC_VARARGS : 0), node.getName(), BytecodeHelper.getMethodDescriptor(node.getReturnType(), parameters), BytecodeHelper.getGenericsMethodSignature(node), buildExceptions(node.getExceptions()));
    controller.setMethodVisitor(mv);
    controller.resetLineNumber();
    visitAnnotations(node, mv);
    visitTypeParameters(node, mv);
    // ideally following statement would be in visitMethod but mv not visible there
    if (!(node instanceof ConstructorNode)) {
        visitType(node.getReturnType(), mv, newTypeReference(METHOD_RETURN), "", true);
    }
    // add parameter names to the MethodVisitor (JDK8+)
    if (Optional.ofNullable(controller.getClassNode().getCompileUnit()).orElseGet(context::getCompileUnit).getConfig().getParameters()) {
        for (Parameter parameter : parameters) {
            mv.visitParameter(parameter.getName(), parameter.getModifiers());
        }
    }
    for (int i = 0, n = parameters.length; i < n; i += 1) {
        visitParameterAnnotations(parameters[i], i, mv);
        ClassNode paramType = parameters[i].getType();
        if (paramType.isGenericsPlaceHolder()) {
            visitTypeAnnotations(paramType, mv, newFormalParameterReference(i), "", true);
        } else {
            visitType(parameters[i].getType(), mv, newFormalParameterReference(i), "", true);
        }
    }
    if (node.getExceptions() != null) {
        for (int i = 0, n = node.getExceptions().length; i < n; i += 1) {
            visitTypeAnnotations(node.getExceptions()[i], mv, newExceptionReference(i), "", true);
        }
    }
    if (controller.getClassNode().isAnnotationDefinition() && !node.isStaticConstructor()) {
        visitAnnotationDefault(node, mv);
    } else if (!node.isAbstract()) {
        mv.visitCode();
        Statement code = node.getCode();
        // fast path for getters, setters, etc.
        BytecodeInstruction instruction;
        if (code instanceof BytecodeSequence && (instruction = ((BytecodeSequence) code).getBytecodeInstruction()) != null) {
            instruction.visit(mv);
        } else {
            visitStdMethod(node, isConstructor, parameters, code);
        }
        try {
            mv.visitMaxs(0, 0);
        } catch (Exception e) {
            Writer writer = null;
            if (mv instanceof TraceMethodVisitor) {
                writer = new StringBuilderWriter();
                PrintWriter p = new PrintWriter(writer);
                ((TraceMethodVisitor) mv).p.print(p);
                p.flush();
            }
            StringBuilder message = new StringBuilder(64);
            message.append("ASM reporting processing error for ");
            message.append(controller.getClassNode().toString(false)).append('#').append(node.getName());
            message.append(" with signature ").append(node.getTypeDescriptor());
            message.append(" in ").append(sourceFile).append(':').append(node.getLineNumber());
            if (writer != null) {
                message.append("\nLast known generated bytecode in last generated method or constructor:\n");
                message.append(writer);
            }
            throw new GroovyRuntimeException(message.toString(), e);
        }
    }
    mv.visitEnd();
}
Also used : InterfaceHelperClassNode(org.codehaus.groovy.ast.InterfaceHelperClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) AssertStatement(org.codehaus.groovy.ast.stmt.AssertStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BreakStatement(org.codehaus.groovy.ast.stmt.BreakStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement) WhileStatement(org.codehaus.groovy.ast.stmt.WhileStatement) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) DoWhileStatement(org.codehaus.groovy.ast.stmt.DoWhileStatement) ContinueStatement(org.codehaus.groovy.ast.stmt.ContinueStatement) SwitchStatement(org.codehaus.groovy.ast.stmt.SwitchStatement) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) TraceMethodVisitor(org.objectweb.asm.util.TraceMethodVisitor) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) RuntimeParserException(org.codehaus.groovy.syntax.RuntimeParserException) TraceMethodVisitor(org.objectweb.asm.util.TraceMethodVisitor) MethodVisitor(org.objectweb.asm.MethodVisitor) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) Parameter(org.codehaus.groovy.ast.Parameter) OptimizingStatementWriter(org.codehaus.groovy.classgen.asm.OptimizingStatementWriter) MopWriter(org.codehaus.groovy.classgen.asm.MopWriter) StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter)

Example 2 with StringBuilderWriter

use of org.apache.groovy.io.StringBuilderWriter in project groovy by apache.

the class MultipleCompilationErrorsException method getMessage.

@Override
public String getMessage() {
    Writer data = new StringBuilderWriter();
    PrintWriter writer = new PrintWriter(data);
    Janitor janitor = new Janitor();
    writer.write(super.getMessage());
    writer.println(":");
    try {
        collector.write(writer, janitor);
    } finally {
        janitor.cleanup();
    }
    return data.toString();
}
Also used : StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) PrintWriter(java.io.PrintWriter) StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter)

Example 3 with StringBuilderWriter

use of org.apache.groovy.io.StringBuilderWriter in project groovy by apache.

the class JavaShell method doCompile.

private void doCompile(String className, String src, Iterable<String> options) throws IOException {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    try (BytesJavaFileManager bjfm = new BytesJavaFileManager(compiler.getStandardFileManager(null, locale, charset))) {
        StringBuilderWriter out = new StringBuilderWriter();
        JavaCompiler.CompilationTask task = compiler.getTask(out, bjfm, null, options, Collections.emptyList(), Collections.singletonList(new MemJavaFileObject(className, src)));
        task.call();
        if (bjfm.isEmpty()) {
            throw new JavaShellCompilationException(out.toString());
        }
        final Map<String, byte[]> classMap = bjfm.getClassMap();
        jscl.setClassMap(classMap);
    }
}
Also used : StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) JavaCompiler(javax.tools.JavaCompiler) MemJavaFileObject(org.codehaus.groovy.tools.javac.MemJavaFileObject)

Example 4 with StringBuilderWriter

use of org.apache.groovy.io.StringBuilderWriter in project groovy by apache.

the class SignatureCodecVersion1 method encode.

@Override
public String encode(final ClassNode node) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(128);
    DataOutputStream dos = new DataOutputStream(baos);
    Writer wrt = new StringBuilderWriter();
    String encoded = null;
    try {
        doEncode(node, dos);
        EncodingGroovyMethods.encodeBase64(baos.toByteArray()).writeTo(wrt);
        encoded = wrt.toString();
    } catch (IOException e) {
        throw new GroovyRuntimeException("Unable to serialize type information", e);
    }
    return encoded;
}
Also used : StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) DataOutputStream(java.io.DataOutputStream) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) Writer(java.io.Writer)

Example 5 with StringBuilderWriter

use of org.apache.groovy.io.StringBuilderWriter in project groovy by apache.

the class CSTNode method toString.

// ---------------------------------------------------------------------------
// STRING CONVERSION
/**
 * Formats the node as a <code>String</code> and returns it.
 */
@Override
public String toString() {
    Writer string = new StringBuilderWriter();
    write(new PrintWriter(string));
    return string.toString();
}
Also used : StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) PrintWriter(java.io.PrintWriter) StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter)

Aggregations

StringBuilderWriter (org.apache.groovy.io.StringBuilderWriter)25 Writer (java.io.Writer)22 PrintWriter (java.io.PrintWriter)15 IOException (java.io.IOException)11 OutputStreamWriter (java.io.OutputStreamWriter)6 BuildException (org.apache.tools.ant.BuildException)5 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)4 Writable (groovy.lang.Writable)4 ErrorReporter (org.codehaus.groovy.tools.ErrorReporter)4 StringWriterIOException (groovy.lang.StringWriterIOException)3 GroovyClassLoader (groovy.lang.GroovyClassLoader)2 BufferedWriter (java.io.BufferedWriter)2 URISyntaxException (java.net.URISyntaxException)2 AntBuilder (groovy.ant.AntBuilder)1 GroovyPrintWriter (groovy.io.GroovyPrintWriter)1 Binding (groovy.lang.Binding)1 GString (groovy.lang.GString)1 GroovyShell (groovy.lang.GroovyShell)1 MissingMethodException (groovy.lang.MissingMethodException)1 Script (groovy.lang.Script)1