Java Code Examples for net.bytebuddy.implementation.Implementation#Context

The following examples show how to use net.bytebuddy.implementation.Implementation#Context . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TypeProxy.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext) {
    TypeDescription proxyType = implementationContext.register(new TypeProxy(proxiedType,
            implementationTarget,
            InvocationFactory.Default.DEFAULT_METHOD,
            true,
            serializableProxy));
    return new Compound(
            TypeCreation.of(proxyType),
            Duplication.SINGLE,
            MethodInvocation.invoke(proxyType.getDeclaredMethods().filter(isConstructor()).getOnly()),
            Duplication.SINGLE,
            MethodVariableAccess.loadThis(),
            FieldAccess.forField(proxyType.getDeclaredFields().filter((named(INSTANCE_FIELD))).getOnly()).write()
    ).apply(methodVisitor, implementationContext);
}
 
Example 2
Source File: MemberSubstitution.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new substituting method visitor.
 *
 * @param methodVisitor         The method visitor to delegate to.
 * @param instrumentedType      The instrumented type.
 * @param instrumentedMethod    The instrumented method.
 * @param methodGraphCompiler   The method graph compiler to use.
 * @param strict                {@code true} if the method processing should be strict where an exception is raised if a member cannot be found.
 * @param replacement           The replacement to use for creating substitutions.
 * @param implementationContext The implementation context to use.
 * @param typePool              The type pool to use.
 * @param virtualPrivateCalls   {@code true}, virtual method calls might target private methods in accordance to the nest mate specification.
 */
protected SubstitutingMethodVisitor(MethodVisitor methodVisitor,
                                    TypeDescription instrumentedType,
                                    MethodDescription instrumentedMethod,
                                    MethodGraph.Compiler methodGraphCompiler,
                                    boolean strict,
                                    Replacement replacement,
                                    Implementation.Context implementationContext,
                                    TypePool typePool,
                                    boolean virtualPrivateCalls) {
    super(methodVisitor, instrumentedMethod);
    this.instrumentedType = instrumentedType;
    this.instrumentedMethod = instrumentedMethod;
    this.methodGraphCompiler = methodGraphCompiler;
    this.strict = strict;
    this.replacement = replacement;
    this.implementationContext = implementationContext;
    this.typePool = typePool;
    this.virtualPrivateCalls = virtualPrivateCalls;
    stackSizeBuffer = 0;
    localVariableExtension = 0;
}
 
Example 3
Source File: ModifierAdjustment.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public ModifierAdjustingClassVisitor wrap(TypeDescription instrumentedType,
                                          ClassVisitor classVisitor,
                                          Implementation.Context implementationContext,
                                          TypePool typePool,
                                          FieldList<FieldDescription.InDefinedShape> fields,
                                          MethodList<?> methods,
                                          int writerFlags,
                                          int readerFlags) {
    Map<String, FieldDescription.InDefinedShape> mappedFields = new HashMap<String, FieldDescription.InDefinedShape>();
    for (FieldDescription.InDefinedShape fieldDescription : fields) {
        mappedFields.put(fieldDescription.getInternalName() + fieldDescription.getDescriptor(), fieldDescription);
    }
    Map<String, MethodDescription> mappedMethods = new HashMap<String, MethodDescription>();
    for (MethodDescription methodDescription : CompoundList.<MethodDescription>of(methods, new MethodDescription.Latent.TypeInitializer(instrumentedType))) {
        mappedMethods.put(methodDescription.getInternalName() + methodDescription.getDescriptor(), methodDescription);
    }
    return new ModifierAdjustingClassVisitor(classVisitor,
            typeAdjustments,
            fieldAdjustments,
            methodAdjustments,
            instrumentedType,
            mappedFields,
            mappedMethods);
}
 
Example 4
Source File: SimpleExceptionHandler.java    From jackson-modules-base with Apache License 2.0 6 votes vote down vote up
private StackManipulation preTrigger(final Label startTryBlock,
                                     final Label endTryBlock,
                                     final Label startCatchBlock) {
    return new StackManipulation() {
        @Override
        public boolean isValid() {
            return true;
        }

        @Override
        public Size apply(MethodVisitor mv, Implementation.Context ic) {
            final String name = exceptionType.getName();
            mv.visitTryCatchBlock(startTryBlock, endTryBlock, startCatchBlock, name.replace(".", "/"));
            mv.visitLabel(startTryBlock);
            return new Size(0,0);
        }
    };
}
 
Example 5
Source File: SerializedConstant.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext) {
    try {
        return new StackManipulation.Compound(
                TypeCreation.of(TypeDescription.ForLoadedType.of(ObjectInputStream.class)),
                Duplication.SINGLE,
                TypeCreation.of(TypeDescription.ForLoadedType.of(ByteArrayInputStream.class)),
                Duplication.SINGLE,
                new TextConstant(serialization),
                new TextConstant(CHARSET),
                MethodInvocation.invoke(new MethodDescription.ForLoadedMethod(String.class.getMethod("getBytes", String.class))),
                MethodInvocation.invoke(new MethodDescription.ForLoadedConstructor(ByteArrayInputStream.class.getConstructor(byte[].class))),
                MethodInvocation.invoke(new MethodDescription.ForLoadedConstructor(ObjectInputStream.class.getConstructor(InputStream.class))),
                MethodInvocation.invoke(new MethodDescription.ForLoadedMethod(ObjectInputStream.class.getMethod("readObject")))
        ).apply(methodVisitor, implementationContext);
    } catch (NoSuchMethodException exception) {
        throw new IllegalStateException("Could not locate Java API method", exception);
    }
}
 
Example 6
Source File: SimpleExceptionHandler.java    From jackson-modules-base with Apache License 2.0 6 votes vote down vote up
private StackManipulation postTrigger(final Label endTryBlock, final Label startCatchBlock) {
    return new StackManipulation() {
        @Override
        public boolean isValid() {
            return true;
        }

        @Override
        public Size apply(MethodVisitor mv, Implementation.Context ic) {
            mv.visitLabel(endTryBlock);
            // and then do catch block
            mv.visitLabel(startCatchBlock);

            //although this StackManipulation does not alter the stack on it's own
            //however when an exception is caught
            //the exception will be on the top of the stack (being placed there by the JVM)
            //we need to increment the stack size
            return new Size(1, 0);
        }
    };
}
 
Example 7
Source File: PersistentAttributeTransformer.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Size apply(
		MethodVisitor methodVisitor,
		Implementation.Context implementationContext,
		MethodDescription instrumentedMethod
) {
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	methodVisitor.visitVarInsn( Type.getType( persistentField.getType().asErasure().getDescriptor() ).getOpcode( Opcodes.ILOAD ), 1 );
	methodVisitor.visitMethodInsn(
			Opcodes.INVOKESPECIAL,
			managedCtClass.getSuperClass().asErasure().getInternalName(),
			EnhancerConstants.PERSISTENT_FIELD_WRITER_PREFIX + persistentField.getName(),
			Type.getMethodDescriptor( Type.getType( void.class ), Type.getType( persistentField.getType().asErasure().getDescriptor() ) ),
			false
	);
	methodVisitor.visitInsn( Opcodes.RETURN );
	return new Size( 1 + persistentField.getType().getStackSize().getSize(), instrumentedMethod.getStackSize() );
}
 
Example 8
Source File: MemberSubstitution.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public MethodVisitor wrap(TypeDescription instrumentedType,
                          MethodDescription instrumentedMethod,
                          MethodVisitor methodVisitor,
                          Implementation.Context implementationContext,
                          TypePool typePool,
                          int writerFlags,
                          int readerFlags) {
    typePool = typePoolResolver.resolve(instrumentedType, instrumentedMethod, typePool);
    return new SubstitutingMethodVisitor(methodVisitor,
            instrumentedType,
            instrumentedMethod,
            methodGraphCompiler,
            strict,
            replacementFactory.make(instrumentedType, instrumentedMethod, typePool),
            implementationContext,
            typePool,
            implementationContext.getClassFileVersion().isAtLeast(ClassFileVersion.JAVA_V11));
}
 
Example 9
Source File: BytecodeProviderImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Size apply(
		MethodVisitor methodVisitor,
		Implementation.Context implementationContext,
		MethodDescription instrumentedMethod) {
	int index = 0;
	for ( Method setter : setters ) {
		methodVisitor.visitVarInsn( Opcodes.ALOAD, 1 );
		methodVisitor.visitTypeInsn( Opcodes.CHECKCAST, Type.getInternalName( clazz ) );
		methodVisitor.visitVarInsn( Opcodes.ALOAD, 2 );
		methodVisitor.visitLdcInsn( index++ );
		methodVisitor.visitInsn( Opcodes.AALOAD );
		if ( setter.getParameterTypes()[0].isPrimitive() ) {
			PrimitiveUnboxingDelegate.forReferenceType( TypeDescription.Generic.OBJECT )
					.assignUnboxedTo(
							new TypeDescription.Generic.OfNonGenericType.ForLoadedType( setter.getParameterTypes()[0] ),
							ReferenceTypeAwareAssigner.INSTANCE,
							Assigner.Typing.DYNAMIC
					)
					.apply( methodVisitor, implementationContext );
		}
		else {
			methodVisitor.visitTypeInsn( Opcodes.CHECKCAST, Type.getInternalName( setter.getParameterTypes()[0] ) );
		}
		methodVisitor.visitMethodInsn(
				Opcodes.INVOKEVIRTUAL,
				Type.getInternalName( clazz ),
				setter.getName(),
				Type.getMethodDescriptor( setter ),
				false
		);
	}
	methodVisitor.visitInsn( Opcodes.RETURN );
	return new Size( 4, instrumentedMethod.getStackSize() );
}
 
Example 10
Source File: TypeReferenceAdjustment.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public ClassVisitor wrap(TypeDescription instrumentedType,
                         ClassVisitor classVisitor,
                         Implementation.Context implementationContext,
                         TypePool typePool,
                         FieldList<FieldDescription.InDefinedShape> fields,
                         MethodList<?> methods,
                         int writerFlags,
                         int readerFlags) {
    return new TypeReferenceClassVisitor(classVisitor, strict, filter, typePool);
}
 
Example 11
Source File: FieldConstant.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext) {
    try {
        return new Compound(
                ClassConstant.of(fieldDescription.getDeclaringType()),
                new TextConstant(fieldDescription.getInternalName()),
                MethodInvocation.invoke(new MethodDescription.ForLoadedMethod(Class.class.getMethod("getDeclaredField", String.class)))
        ).apply(methodVisitor, implementationContext);
    } catch (NoSuchMethodException exception) {
        throw new IllegalStateException("Cannot locate Class::getDeclaredField", exception);
    }
}
 
Example 12
Source File: AbstractCreateLocalVarStackManipulation.java    From jackson-modules-base with Apache License 2.0 5 votes vote down vote up
@Override
public Size apply(MethodVisitor methodVisitor,
                  Implementation.Context implementationContext) {

    final List<StackManipulation> operations = new ArrayList<StackManipulation>();

    operations.add(MethodVariableAccess.REFERENCE.loadFrom(beanArgIndex())); //load the bean
    operations.add(TypeCasting.to(beanClassDescription));

    operations.add(MethodVariableAccess.REFERENCE.storeAt(localVarIndexCalculator.calculate()));

    final StackManipulation.Compound compound = new StackManipulation.Compound(operations);
    return compound.apply(methodVisitor, implementationContext);
}
 
Example 13
Source File: AsmVisitorWrapper.java    From byte-buddy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public ClassVisitor wrap(TypeDescription instrumentedType,
                         ClassVisitor classVisitor,
                         Implementation.Context implementationContext,
                         TypePool typePool,
                         FieldList<FieldDescription.InDefinedShape> fields,
                         MethodList<?> methods,
                         int writerFlags,
                         int readerFlags) {
    return classVisitor;
}
 
Example 14
Source File: RebaseImplementationTarget.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext) {
    return stackManipulation.apply(methodVisitor, implementationContext);
}
 
Example 15
Source File: InstanceCheck.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext) {
    methodVisitor.visitTypeInsn(Opcodes.INSTANCEOF, typeDescription.getInternalName());
    return new Size(0, 0);
}
 
Example 16
Source File: IntegerConstant.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext) {
    methodVisitor.visitIntInsn(Opcodes.SIPUSH, value);
    return SIZE;
}
 
Example 17
Source File: IntegerConstant.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext) {
    methodVisitor.visitLdcInsn(value);
    return SIZE;
}
 
Example 18
Source File: MethodDelegationBinder.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext) {
    return delegate.apply(methodVisitor, implementationContext);
}
 
Example 19
Source File: JumpStackManipulation.java    From jackson-modules-base with Apache License 2.0 4 votes vote down vote up
@Override
public Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext) {
    methodVisitor.visitJumpInsn(opcde, label);
    return new Size(stackImpact, 0);
}
 
Example 20
Source File: AsmVisitorWrapper.java    From byte-buddy with Apache License 2.0 3 votes vote down vote up
/**
 * Wraps a method visitor.
 *
 * @param instrumentedType      The instrumented type.
 * @param instrumentedMethod    The method that is currently being defined.
 * @param methodVisitor         The original field visitor that defines the given method.
 * @param implementationContext The implementation context to use.
 * @param typePool              The type pool to use.
 * @param writerFlags           The ASM {@link org.objectweb.asm.ClassWriter} reader flags to consider.
 * @param readerFlags           The ASM {@link org.objectweb.asm.ClassReader} reader flags to consider.
 * @return The wrapped method visitor.
 */
MethodVisitor wrap(TypeDescription instrumentedType,
                   MethodDescription instrumentedMethod,
                   MethodVisitor methodVisitor,
                   Implementation.Context implementationContext,
                   TypePool typePool,
                   int writerFlags,
                   int readerFlags);