Example usage for org.objectweb.asm MethodVisitor visitLdcInsn

List of usage examples for org.objectweb.asm MethodVisitor visitLdcInsn

Introduction

In this page you can find the example usage for org.objectweb.asm MethodVisitor visitLdcInsn.

Prototype

public void visitLdcInsn(final Object value) 

Source Link

Document

Visits a LDC instruction.

Usage

From source file:org.gradle.api.internal.tasks.compile.MethodStubbingApiMemberAdapter.java

License:Apache License

/**
 * Generates an exception which is going to be thrown in each method.
 * The reason it is in a separate method is because it reduces the bytecode size.
 *///w w  w .j  a  v  a 2  s .com
private void generateUnsupportedOperationExceptionMethod() {
    MethodVisitor mv = cv.visitMethod(ACC_PRIVATE | ACC_STATIC | ACC_SYNTHETIC, UOE_METHOD,
            "()Ljava/lang/UnsupportedOperationException;", null, null);
    mv.visitCode();
    mv.visitTypeInsn(NEW, "java/lang/UnsupportedOperationException");
    mv.visitInsn(DUP);
    mv.visitLdcInsn(
            "You tried to call a method on an API class. Is the API jar on the classpath instead of the runtime jar?");
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/UnsupportedOperationException", "<init>",
            "(Ljava/lang/String;)V", false);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(3, 0);
    mv.visitEnd();
}

From source file:org.gradle.language.base.internal.tasks.apigen.StubClassWriter.java

License:Apache License

/**
 * Generates an exception which is going to be thrown in each method. The reason it is in a separate method is because it reduces the bytecode size.
 *//*  w  w  w . j a v a  2  s.c o m*/
private void generateUnsupportedOperationExceptionMethod() {
    MethodVisitor mv = cv.visitMethod(ACC_PRIVATE | ACC_STATIC | ACC_SYNTHETIC, UOE_METHOD,
            "()Ljava/lang/UnsupportedOperationException;", null, null);
    mv.visitCode();
    mv.visitTypeInsn(NEW, "java/lang/UnsupportedOperationException");
    mv.visitInsn(DUP);
    mv.visitLdcInsn(
            "You tried to call a method on an API class. You probably added the API jar on classpath instead of the implementation jar.");
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/UnsupportedOperationException", "<init>",
            "(Ljava/lang/String;)V", false);
    mv.visitInsn(ARETURN);
    mv.visitMaxs(3, 0);
    mv.visitEnd();
}

From source file:org.gradle.model.internal.manage.schema.extract.ManagedProxyClassGenerator.java

License:Apache License

private void writeManagedTypeStaticField(Type generatedType, Class<?> managedTypeClass,
        MethodVisitor constructorVisitor) {
    constructorVisitor.visitLdcInsn(Type.getType(managedTypeClass));
    constructorVisitor.visitMethodInsn(INVOKESTATIC, MODEL_TYPE_INTERNAL_NAME, "of",
            MODEL_TYPE_OF_METHOD_DESCRIPTOR, false);
    constructorVisitor.visitFieldInsn(PUTSTATIC, generatedType.getInternalName(), MANAGED_TYPE_FIELD_NAME,
            Type.getDescriptor(ModelType.class));
}

From source file:org.gradle.model.internal.manage.schema.extract.ManagedProxyClassGenerator.java

License:Apache License

private void setCanCallSettersField(MethodVisitor methodVisitor, Type generatedType, boolean canCallSetters) {
    putThisOnStack(methodVisitor);//  www  .  j a v  a2 s  .c  o m
    methodVisitor.visitLdcInsn(canCallSetters);
    methodVisitor.visitFieldInsn(PUTFIELD, generatedType.getInternalName(), CAN_CALL_SETTERS_FIELD_NAME,
            Type.BOOLEAN_TYPE.getDescriptor());
}

From source file:org.gradle.model.internal.manage.schema.extract.ManagedProxyClassGenerator.java

License:Apache License

private void writeTypeConvertingSetter(ClassVisitor visitor, Type generatedType, Class<?> viewClass,
        ModelProperty<?> property) {
    WeaklyTypeReferencingMethod<?, ?> weakSetter = property.getAccessor(SETTER);
    // There is no setter for this property
    if (weakSetter == null) {
        return;//from   ww  w  .j  a va  2  s. c om
    }
    if (!(property.getSchema() instanceof ScalarValueSchema)) {
        return;
    }

    Class<?> propertyClass = property.getType().getConcreteClass();
    Type propertyType = Type.getType(propertyClass);
    Class<?> boxedClass = propertyClass.isPrimitive() ? BOXED_TYPES.get(propertyClass) : propertyClass;
    Type boxedType = Type.getType(boxedClass);

    Method setter = weakSetter.getMethod();
    MethodVisitor methodVisitor = declareMethod(visitor, setter.getName(), SET_OBJECT_PROPERTY_DESCRIPTOR,
            SET_OBJECT_PROPERTY_DESCRIPTOR);

    putThisOnStack(methodVisitor);
    putTypeConverterFieldValueOnStack(methodVisitor, generatedType);

    // Object converted = $typeConverter.convert(foo, Float.class, false);
    methodVisitor.visitVarInsn(ALOAD, 1); // put var #1 ('foo') on the stack
    methodVisitor.visitLdcInsn(boxedType); // push the constant Class onto the stack
    methodVisitor.visitInsn(propertyClass.isPrimitive() ? ICONST_1 : ICONST_0); // push int 1 or 0 (interpreted as true or false) onto the stack
    Label startTry = new Label();
    methodVisitor.visitLabel(startTry);
    methodVisitor.visitMethodInsn(INVOKEINTERFACE, TYPE_CONVERTER_TYPE.getInternalName(), "convert",
            COERCE_TO_SCALAR_DESCRIPTOR, true);
    Label endTry = new Label();
    methodVisitor.visitLabel(endTry);
    methodVisitor.visitTypeInsn(CHECKCAST, boxedType.getInternalName());

    if (propertyClass.isPrimitive()) {
        unboxType(methodVisitor, propertyClass);
    }

    // invoke the typed setter
    methodVisitor.visitMethodInsn(INVOKEVIRTUAL, generatedType.getInternalName(), setter.getName(),
            Type.getMethodDescriptor(Type.VOID_TYPE, propertyType), false);
    methodVisitor.visitInsn(RETURN);

    // catch(TypeConversionException e) { throw ... }
    Label startCatch = new Label();
    methodVisitor.visitLabel(startCatch);
    methodVisitor.visitTryCatchBlock(startTry, endTry, startCatch,
            TYPE_CONVERSION_EXCEPTION_TYPE.getInternalName());
    methodVisitor.visitVarInsn(ASTORE, 2); // store thrown exception
    putClassOnStack(methodVisitor, viewClass);
    methodVisitor.visitLdcInsn(property.getName());
    putFirstMethodArgumentOnStack(methodVisitor);
    methodVisitor.visitVarInsn(ALOAD, 2);
    methodVisitor.visitMethodInsn(INVOKESTATIC, Type.getInternalName(ManagedProxyClassGenerator.class),
            "propertyValueConvertFailure", Type.getMethodDescriptor(Type.VOID_TYPE, CLASS_TYPE, STRING_TYPE,
                    OBJECT_TYPE, TYPE_CONVERSION_EXCEPTION_TYPE),
            false);
    finishVisitingMethod(methodVisitor);
}

From source file:org.gradle.model.internal.manage.schema.extract.ManagedProxyClassGenerator.java

License:Apache License

private void putConstantOnStack(MethodVisitor methodVisitor, Object value) {
    methodVisitor.visitLdcInsn(value);
}

From source file:org.iobserve.mobile.instrument.bytecode.ActivitiyBytecodeInstrumenter.java

License:Apache License

/**
 * {@inheritDoc}//from  w w  w . j ava  2 s.c  o m
 */
@Override
public void onMethodEnter(final String owner, final String name, final String desc, final AdviceAdapter parent,
        final MethodVisitor mv) {
    if (ACTIVITY_ONCREATE.equals(name)) {
        final String belAgentPoint = activityPointMapping.get("onCreate");

        // INSERT CONFIG CALLS
        mv.visitLdcInsn(connectionConfig.getBeaconUrl());
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, EXTCONFIGURATION_TYPE.getInternalName(),
                setBeaconUrlMethod.getName(), setBeaconUrlType.getDescriptor(), false);

        mv.visitLdcInsn(connectionConfig.getHelloUrl());
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, EXTCONFIGURATION_TYPE.getInternalName(),
                setHelloUrlMethod.getName(), setHelloUrlType.getDescriptor(), false);

        // INIT AGENT
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, ANDROIDAGENT_TYPE.getInternalName(), belAgentPoint,
                "(Landroid/app/Activity;)V", false);
    } else if (ACTIVITY_ONDESTROY.equals(name)) {
        final String belAgentPoint = activityPointMapping.get("onDestroy");

        mv.visitMethodInsn(Opcodes.INVOKESTATIC, ANDROIDAGENT_TYPE.getInternalName(), belAgentPoint, "()V",
                false);
    } else if (ACTIVITY_ONSTART.equals(name)) {
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, ANDROIDAGENT_TYPE.getInternalName(),
                onStartActivityMethod.getName(), onStartActivityType.getDescriptor(), false);
    } else if (ACTIVITY_ONSTOP.equals(name)) {
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, ANDROIDAGENT_TYPE.getInternalName(),
                onStopActivityMethod.getName(), onStopActivityType.getDescriptor(), false);
    }
}

From source file:org.iobserve.mobile.instrument.bytecode.SensorBytecodeInstrumenter.java

License:Apache License

/**
 * {@inheritDoc}/*ww w  . j a v a2s  . c  o m*/
 */
@Override
public void onMethodEnter(final String owner, final String name, final String desc, final AdviceAdapter parent,
        final MethodVisitor mv) {
    // add sensor call
    mv.visitLdcInsn(sensor);
    mv.visitLdcInsn(name + desc);
    mv.visitLdcInsn(owner);
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, ANDROIDAGENT_TYPE.getInternalName(), enterBodyMethod.getName(),
            enterBodyType.getDescriptor(), false);
    index = parent.newLocal(Type.LONG_TYPE);
    mv.visitVarInsn(Opcodes.LSTORE, index);
}

From source file:org.jacoco.core.internal.instr.CondyProbeArrayStrategy.java

License:Open Source License

public int storeInstance(final MethodVisitor mv, final boolean clinit, final int variable) {
    final Handle bootstrapMethod = new Handle(Opcodes.H_INVOKESTATIC, className, InstrSupport.INITMETHOD_NAME,
            B_DESC, isInterface);//from   w  w  w .  j ava 2s .  c  om
    // As a workaround for https://bugs.openjdk.java.net/browse/JDK-8216970
    // constant should have type Object
    mv.visitLdcInsn(new ConstantDynamic(InstrSupport.DATAFIELD_NAME, "Ljava/lang/Object;", bootstrapMethod));
    mv.visitTypeInsn(Opcodes.CHECKCAST, "[Z");
    mv.visitVarInsn(Opcodes.ASTORE, variable);
    return 1;
}

From source file:org.jacoco.core.internal.instr.ProbeInserterTest.java

License:Open Source License

@Before
public void setup() {
    actual = new MethodRecorder();
    actualVisitor = actual.getVisitor();
    expected = new MethodRecorder();
    expectedVisitor = expected.getVisitor();
    arrayStrategy = new IProbeArrayStrategy() {

        public int storeInstance(MethodVisitor mv, int variable) {
            mv.visitLdcInsn("init");
            return 5;
        }//  ww  w . j a v  a  2s .  c o  m

        public void addMembers(ClassVisitor delegate) {
        }
    };
}