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();
}
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();
}
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);
}
}
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;
}
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();
}
Aggregations