diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/MethodWriter.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/MethodWriter.java index 2663fe9c717..7b7caec320e 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/MethodWriter.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/MethodWriter.java @@ -23,6 +23,7 @@ import org.elasticsearch.painless.Definition.Cast; import org.elasticsearch.painless.Definition.Sort; import org.elasticsearch.painless.Definition.Type; import org.objectweb.asm.ClassVisitor; +import org.objectweb.asm.ClassWriter; import org.objectweb.asm.Label; import org.objectweb.asm.Opcodes; import org.objectweb.asm.commons.GeneratorAdapter; @@ -87,30 +88,29 @@ import static org.elasticsearch.painless.WriterConstants.UTILITY_TYPE; * shared by the nodes of the Painless tree. */ public final class MethodWriter extends GeneratorAdapter { + private final ClassWriter parent; private final BitSet statements; private final Deque> stringConcatArgs = (INDY_STRING_CONCAT_BOOTSTRAP_HANDLE == null) ? null : new ArrayDeque<>(); - MethodWriter(int access, Method method, org.objectweb.asm.Type[] exceptions, ClassVisitor cv, BitSet statements) { - super(Opcodes.ASM5, cv.visitMethod(access, method.getName(), method.getDescriptor(), null, getInternalNames(exceptions)), + MethodWriter(int access, Method method, ClassWriter cw, BitSet statements) { + super(Opcodes.ASM5, cw.visitMethod(access, method.getName(), method.getDescriptor(), null, null), access, method.getName(), method.getDescriptor()); + + this.parent = cw; this.statements = statements; } - private static String[] getInternalNames(final org.objectweb.asm.Type[] types) { - if (types == null) { - return null; - } - String[] names = new String[types.length]; - for (int i = 0; i < names.length; ++i) { - names[i] = types[i].getInternalName(); - } - return names; + /** + * @return A new {@link MethodWriter} with the specified access and signature. + */ + MethodWriter newMethodWriter(int access, Method method) { + return new MethodWriter(access, method, parent, statements); } - /** - * Marks a new statement boundary. + /** + * Marks a new statement boundary. *

* This is invoked for each statement boundary (leaf {@code S*} nodes). */ @@ -122,7 +122,7 @@ public final class MethodWriter extends GeneratorAdapter { statements.set(offset); } - /** + /** * Encodes the offset into the line number table as {@code offset + 1}. *

* This is invoked before instructions that can hit exceptions. diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/Writer.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/Writer.java index f09bf5b87eb..4d4a778d558 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/Writer.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/Writer.java @@ -41,8 +41,7 @@ import java.util.BitSet; final class Writer { static byte[] write(CompilerSettings settings, String name, String source, Variables variables, SSource root, BitSet expressions) { - Writer writer = new Writer(settings, name, source, variables, root, expressions); - return writer.getBytes(); + return new Writer(settings, name, source, variables, root, expressions).getBytes(); } private final CompilerSettings settings; @@ -66,7 +65,7 @@ final class Writer { writeBegin(); writeConstructor(); - adapter = new MethodWriter(Opcodes.ACC_PUBLIC, EXECUTE, null, writer, expressions); + adapter = new MethodWriter(Opcodes.ACC_PUBLIC, EXECUTE, writer, expressions); writeExecute(); writeEnd(); diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/WriterConstants.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/WriterConstants.java index fce40d5a6e0..646fd515717 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/WriterConstants.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/WriterConstants.java @@ -143,7 +143,7 @@ public final class WriterConstants { public final static Method CHECKEQUALS = getAsmMethod(boolean.class, "checkEquals", Object.class, Object.class); - private static Method getAsmMethod(final Class rtype, final String name, final Class... ptypes) { + public static Method getAsmMethod(final Class rtype, final String name, final Class... ptypes) { return new Method(name, MethodType.methodType(rtype, ptypes).toMethodDescriptorString()); }