Hello,

Unless I'm missing something, Plastic prevents an
InstructionBuilderCallback from accessing the underlying ASM method
visitor, and provides no other way to add arbitrary byte code?

Plastic is very useful, and way more pleasant than raw ASM, however I've
found I need to go off the tracks occasionally. E.g.

    new InstructionBuilderCallback() {

        @Override
        public void doBuild(final InstructionBuilder builder) {

            final MethodVisitor mv = getMethodVisitor(builder);

            builder
                .loadConstant("%s -> %s")
                .loadConstant(2);
            mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
           
    builder.dupe().loadConstant(0).loadThis().getField(delegateField);
            mv.visitInsn(Opcodes.AASTORE);
            builder.dupe().loadConstant(1).loadThis().getField(nextField);
            mv.visitInsn(Opcodes.AASTORE);

            builder
                .invokeStatic(String.class, String.class, "format",
    String.class, Object[].class)
                .returnResult();
        }
    };

where

    // hack hack hack
    private static MethodVisitor getMethodVisitor(InstructionBuilder
    builder) {
            try {
                final Field stateField =
    builder.getClass().getDeclaredField("v");
                stateField.setAccessible(true);
                return (MethodVisitor) stateField.get(builder);
            }
            catch (NoSuchFieldException e) {
                throw new AssertionError(e);
            }
            catch (IllegalAccessException e) {
                throw new AssertionError(e);
            }
    }

Am I missing something? Would you be open to an enhancement request to
provide InstructionBuilder.methodVisitor()?

Thanks,

- Phil

Reply via email to