Example usage for org.objectweb.asm Opcodes INVOKEVIRTUAL

List of usage examples for org.objectweb.asm Opcodes INVOKEVIRTUAL

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes INVOKEVIRTUAL.

Prototype

int INVOKEVIRTUAL

To view the source code for org.objectweb.asm Opcodes INVOKEVIRTUAL.

Click Source Link

Usage

From source file:com.yahoo.yqlplus.engine.internal.plan.types.base.InvokeExpression.java

public InvokeExpression(Class<?> owner, String methodName, TypeWidget returnType, BytecodeExpression target,
        List<BytecodeExpression> arguments) {
    this.owner = Type.getType(owner);
    if (owner.isInterface()) {
        this.op = Opcodes.INVOKEINTERFACE;
    } else {/*from w w  w .j  ava  2  s  .  co m*/
        this.op = Opcodes.INVOKEVIRTUAL;
    }
    this.methodName = methodName;
    this.target = target;
    this.returnType = returnType;
    this.arguments = arguments;
    this.descriptor = getDescriptor(returnType, arguments);
}

From source file:com.yahoo.yqlplus.engine.internal.plan.types.base.IteratingSerializing.java

@Override
public BytecodeSequence serializeTo(final BytecodeExpression source, final BytecodeExpression generator) {
    switch (encoding) {
    case JSON:/*from  w  ww . ja v a 2  s. c  om*/
        return new BytecodeSequence() {
            @Override
            public void generate(CodeEmitter code) {
                MethodVisitor mv = code.getMethodVisitor();
                final BytecodeExpression gen = code.evaluateOnce(generator);
                gen.generate(code);
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(JsonGenerator.class),
                        "writeStartArray", Type.getMethodDescriptor(Type.VOID_TYPE), false);
                code.exec(iterateAdapter.iterate(source, new IterateAdapter.IterateLoop() {
                    @Override
                    public void item(CodeEmitter code, BytecodeExpression item, Label abortLoop,
                            Label nextItem) {
                        code.exec(valueTypeSerializer.serializeTo(item, gen));
                    }
                }));
                gen.generate(code);
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(JsonGenerator.class),
                        "writeEndArray", Type.getMethodDescriptor(Type.VOID_TYPE), false);
            }
        };
    case TBIN:
        // TODO: implement iterable TBin serialization (whoops, it requires you to know the count to write an array... hm?)
        // we may need to copy into a list
        throw new TodoException();
    default:
        throw new UnsupportedOperationException();
    }
}

From source file:com.yahoo.yqlplus.engine.internal.plan.types.base.JacksonParsePrimitive.java

@Override
public void generate(CodeEmitter code) {
    parser.generate(code);/*from www .j  av a  2 s.  c  o m*/
    MethodVisitor mv = code.getMethodVisitor();
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(JsonParser.class), "nextToken",
            Type.getMethodDescriptor(Type.getType(JsonToken.class)), false);
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(JsonToken.class), "ordinal",
            Type.getMethodDescriptor(Type.INT_TYPE), false);
    IntegerSwitchSequence seq = new IntegerSwitchSequence();
    switch (getType().getJVMType().getSort()) {
    case Type.BOOLEAN: {
        seq.put(JsonToken.VALUE_TRUE.ordinal(), new InstructionConstant(getType(), -1));
        seq.put(JsonToken.VALUE_FALSE.ordinal(), new InstructionConstant(getType(), 0));
        break;
    }
    case Type.SHORT: {
        seq.put(JsonToken.VALUE_NUMBER_INT.ordinal(), new JacksonGet(Type.SHORT_TYPE, "getShortValue"));
        seq.put(JsonToken.VALUE_NUMBER_FLOAT.ordinal(), new JacksonGet(Type.SHORT_TYPE, "getShortValue"));
        break;
    }
    case Type.INT: {
        seq.put(JsonToken.VALUE_NUMBER_INT.ordinal(), new JacksonGet(Type.INT_TYPE, "getIntValue"));
        seq.put(JsonToken.VALUE_NUMBER_FLOAT.ordinal(), new JacksonGet(Type.INT_TYPE, "getIntValue"));
        break;
    }
    case Type.FLOAT: {
        seq.put(JsonToken.VALUE_NUMBER_INT.ordinal(), new JacksonGet(Type.FLOAT_TYPE, "getFloatValue"));
        seq.put(JsonToken.VALUE_NUMBER_FLOAT.ordinal(), new JacksonGet(Type.FLOAT_TYPE, "getFloatValue"));
        break;
    }
    case Type.LONG: {
        seq.put(JsonToken.VALUE_NUMBER_INT.ordinal(), new JacksonGet(Type.LONG_TYPE, "getLongValue"));
        seq.put(JsonToken.VALUE_NUMBER_FLOAT.ordinal(), new JacksonGet(Type.LONG_TYPE, "getLongValue"));
        break;
    }
    case Type.DOUBLE: {
        seq.put(JsonToken.VALUE_NUMBER_INT.ordinal(), new JacksonGet(Type.DOUBLE_TYPE, "getDoubleValue"));
        seq.put(JsonToken.VALUE_NUMBER_FLOAT.ordinal(), new JacksonGet(Type.DOUBLE_TYPE, "getDoubleValue"));
        break;
    }
    case Type.CHAR: {
        LocalCodeChunk chunk = new LocalBatchCode(new LocalFrame());
        chunk.add(new JacksonGet(Type.getType(String.class), "getText"));
        chunk.add(new BytecodeSequence() {
            @Override
            public void generate(CodeEmitter code) {
                MethodVisitor mv = code.getMethodVisitor();
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(String.class), "charAt",
                        Type.getMethodDescriptor(Type.CHAR_TYPE, Type.INT_TYPE), false);
            }
        });
        seq.put(JsonToken.VALUE_STRING.ordinal(), chunk);
        break;
    }
    default:
        throw new UnsupportedOperationException("Unknown primitive type: " + getType().getJVMType());
    }
    seq.setDefaultSequence(new BytecodeSequence() {
        @Override
        public void generate(CodeEmitter code) {
            code.emitThrow(IllegalArgumentException.class, new StringConstantExpression(String
                    .format("JSON type cannot be parsed into expected type '%s'", getType().getTypeName())));

        }
    });
    code.exec(seq);
}

From source file:com.yahoo.yqlplus.engine.internal.plan.types.base.JacksonSerializeObject.java

@Override
public void generate(CodeEmitter code) {
    jsonGenerator.generate(code);/*from  w  w  w .  j  av  a2 s .co  m*/
    source.generate(code);
    MethodVisitor mv = code.getMethodVisitor();
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(JsonGenerator.class), "writeObject",
            Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(Object.class)), false);
}

From source file:com.yahoo.yqlplus.engine.internal.plan.types.base.JacksonSerializePrimitive.java

@Override
public void generate(CodeEmitter code) {
    jsonGenerator.generate(code);/*from  w  w  w.j av  a  2s.co m*/
    source.generate(code);
    MethodVisitor mv = code.getMethodVisitor();
    String desc = Type.getMethodDescriptor(Type.VOID_TYPE, source.getType().getJVMType());
    String name;
    switch (source.getType().getJVMType().getSort()) {
    case Type.BOOLEAN:
        name = "writeBoolean";
        break;
    case Type.SHORT:
    case Type.INT:
    case Type.FLOAT:
    case Type.LONG:
    case Type.DOUBLE:
        name = "writeNumber";
        break;
    case Type.BYTE:
    case Type.CHAR:
        name = "writeObject";
        desc = Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(Object.class));
        code.box(source.getType());
        break;
    default:
        throw new UnsupportedOperationException("Unknown primitive type: " + source.getType().getJVMType());
    }
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(JsonGenerator.class), name, desc, false);
}

From source file:com.yahoo.yqlplus.engine.internal.plan.types.base.MethodAssignableValue.java

public MethodAssignableValue(Method method, TypeWidget type, BytecodeExpression target) {
    this.type = type;
    this.target = target;
    this.ownerName = Type.getInternalName(method.getDeclaringClass());
    this.name = method.getName();
    this.op = method.getDeclaringClass().isInterface() ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL;
    this.desc = Type.getMethodDescriptor(method);
}

From source file:com.yahoo.yqlplus.engine.internal.plan.types.base.MethodAssignableValue.java

public MethodAssignableValue(TypeWidget type, Class ownerClass, String name, BytecodeExpression target) {
    this.type = type;
    this.target = target;
    this.ownerName = Type.getInternalName(ownerClass);
    this.name = name;
    this.op = ownerClass.isInterface() ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL;
    this.desc = Type.getMethodDescriptor(type.getJVMType());
}

From source file:com.yahoo.yqlplus.engine.internal.plan.types.base.NullTestingSerializationAdapter.java

@Override
public BytecodeSequence serializeTo(final BytecodeExpression input, final BytecodeExpression generator) {
    return new BytecodeSequence() {
        @Override//from  w w  w.  ja v  a  2  s .com
        public void generate(CodeEmitter code) {
            Label isNull = new Label();
            Label done = new Label();
            BytecodeExpression source = code.evaluateOnce(input);
            code.gotoIfNull(source, isNull);
            MethodVisitor mv = code.getMethodVisitor();
            code.exec(targetAdapter.serializeTo(source, generator));
            mv.visitJumpInsn(Opcodes.GOTO, done);
            mv.visitLabel(isNull);
            switch (encoding) {
            case TBIN:
                code.exec(generator);
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(TBinEncoder.class), "writeNull",
                        Type.getMethodDescriptor(Type.VOID_TYPE), false);
                break;
            case JSON:
                code.exec(generator);
                mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(JsonGenerator.class),
                        "writeNull", Type.getMethodDescriptor(Type.VOID_TYPE), false);
                break;
            default:
                throw new UnsupportedOperationException("unknown NativeEncoding: " + encoding);
            }
            mv.visitLabel(done);
        }
    };
}

From source file:cuchaz.enigma.CompiledSourceTypeLoader.java

License:Open Source License

private void removeRedundantClassCalls(ClassNode node) {
    // remove <obj>.getClass() calls that are seemingly injected
    //   DUP//from  ww  w  .  j  ava2s. c o  m
    //   INVOKEVIRTUAL java/lang/Object.getClass ()Ljava/lang/Class;
    //   POP
    for (MethodNode methodNode : node.methods) {
        AbstractInsnNode insnNode = methodNode.instructions.getFirst();
        while (insnNode != null) {
            if (insnNode instanceof MethodInsnNode && insnNode.getOpcode() == Opcodes.INVOKEVIRTUAL) {
                MethodInsnNode methodInsnNode = (MethodInsnNode) insnNode;
                if (methodInsnNode.name.equals("getClass") && methodInsnNode.owner.equals("java/lang/Object")
                        && methodInsnNode.desc.equals("()Ljava/lang/Class;")) {
                    AbstractInsnNode previous = methodInsnNode.getPrevious();
                    AbstractInsnNode next = methodInsnNode.getNext();
                    if (previous.getOpcode() == Opcodes.DUP && next.getOpcode() == Opcodes.POP) {
                        insnNode = previous.getPrevious();//reset the iterator so it gets the new next instruction
                        methodNode.instructions.remove(previous);
                        methodNode.instructions.remove(methodInsnNode);
                        methodNode.instructions.remove(next);
                    }
                }
            }
            insnNode = insnNode.getNext();
        }
    }
}

From source file:cuchaz.enigma.TranslatingTypeLoader.java

License:Open Source License

private byte[] loadType(String className) {

    // NOTE: don't know if class name is obf or deobf
    ClassEntry classEntry = new ClassEntry(className);
    ClassEntry obfClassEntry = this.obfuscatingTranslator.getTranslatedClass(classEntry);

    // is this an inner class referenced directly? (ie trying to load b instead of a$b)
    if (!obfClassEntry.isInnerClass()) {
        List<ClassEntry> classChain = this.jarIndex.getObfClassChain(obfClassEntry);
        if (classChain.size() > 1) {
            System.err.println(String.format("WARNING: no class %s after inner class reconstruction. Try %s",
                    className, obfClassEntry.buildClassEntry(classChain)));
            return null;
        }//from w ww.  j  av a2  s  .  c  o m
    }

    // is this a class we should even know about?
    if (!this.jarIndex.containsObfClass(obfClassEntry)) {
        return null;
    }

    // DEBUG
    //System.out.println(String.format("Looking for %s (obf: %s)", classEntry.getName(), obfClassEntry.getName()));

    // find the class in the jar
    ClassNode node = findClassInJar(obfClassEntry);
    if (node == null) {
        // couldn't find it
        return null;
    }

    // remove <obj>.getClass() calls that are seemingly injected
    //   DUP
    //   INVOKEVIRTUAL java/lang/Object.getClass ()Ljava/lang/Class;
    //   POP
    for (MethodNode methodNode : node.methods) {
        AbstractInsnNode insnNode = methodNode.instructions.getFirst();
        while (insnNode != null) {
            if (insnNode instanceof MethodInsnNode && insnNode.getOpcode() == Opcodes.INVOKEVIRTUAL) {
                MethodInsnNode methodInsnNode = (MethodInsnNode) insnNode;
                if (methodInsnNode.name.equals("getClass") && methodInsnNode.owner.equals("java/lang/Object")
                        && methodInsnNode.desc.equals("()Ljava/lang/Class;")) {
                    AbstractInsnNode previous = methodInsnNode.getPrevious();
                    AbstractInsnNode next = methodInsnNode.getNext();
                    if (previous.getOpcode() == Opcodes.DUP && next.getOpcode() == Opcodes.POP) {
                        insnNode = previous.getPrevious();//reset the iterator so it gets the new next instruction
                        methodNode.instructions.remove(previous);
                        methodNode.instructions.remove(methodInsnNode);
                        methodNode.instructions.remove(next);
                    }
                }
            }
            insnNode = insnNode.getNext();
        }
    }

    ClassWriter writer = new ClassWriter(0);
    transformInto(node, writer);

    // we have a transformed class!
    return writer.toByteArray();
}