Java Code Examples for net.bytebuddy.jar.asm.Opcodes#GETFIELD

The following examples show how to use net.bytebuddy.jar.asm.Opcodes#GETFIELD . 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: PersistentAttributeTransformer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public MethodVisitor wrap(
		TypeDescription instrumentedType,
		MethodDescription instrumentedMethod,
		MethodVisitor methodVisitor,
		Implementation.Context implementationContext,
		TypePool typePool,
		int writerFlags,
		int readerFlags) {
	return new MethodVisitor( Opcodes.ASM5, methodVisitor ) {
		@Override
		public void visitFieldInsn(int opcode, String owner, String name, String desc) {
			if ( isEnhanced( owner, name, desc ) ) {
				switch ( opcode ) {
					case Opcodes.GETFIELD:
						methodVisitor.visitMethodInsn(
								Opcodes.INVOKEVIRTUAL,
								owner,
								EnhancerConstants.PERSISTENT_FIELD_READER_PREFIX + name,
								"()" + desc,
								false
						);
						return;
					case Opcodes.PUTFIELD:
						methodVisitor.visitMethodInsn(
								Opcodes.INVOKEVIRTUAL,
								owner,
								EnhancerConstants.PERSISTENT_FIELD_WRITER_PREFIX + name,
								"(" + desc + ")V",
								false
						);
						return;
				}
			}
			super.visitFieldInsn( opcode, owner, name, desc );
		}
	};
}
 
Example 2
Source File: FieldAccessEnhancer.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public MethodVisitor wrap(
		TypeDescription instrumentedType,
		MethodDescription instrumentedMethod,
		MethodVisitor methodVisitor,
		Implementation.Context implementationContext,
		TypePool typePool,
		int writerFlags,
		int readerFlags) {
	return new MethodVisitor( Opcodes.ASM5, methodVisitor ) {
		@Override
		public void visitFieldInsn(int opcode, String owner, String name, String desc) {
			if ( opcode != Opcodes.GETFIELD && opcode != Opcodes.PUTFIELD ) {
				super.visitFieldInsn( opcode, owner, name, desc );
				return;
			}

			FieldDescription field = findField( owner, name, desc );

			if ( ( enhancementContext.isEntityClass( field.getDeclaringType().asErasure() )
					|| enhancementContext.isCompositeClass( field.getDeclaringType().asErasure() ) )
					&& !field.getType().asErasure().equals( managedCtClass )
					&& enhancementContext.isPersistentField( field )
					&& !EnhancerImpl.isAnnotationPresent( field, Id.class )
					&& !field.getName().equals( "this$0" ) ) {

				log.debugf(
						"Extended enhancement: Transforming access to field [%s.%s] from method [%s#%s]",
						field.getType().asErasure(),
						field.getName(),
						field.getName(),
						name
				);

				switch ( opcode ) {
					case Opcodes.GETFIELD:
						methodVisitor.visitMethodInsn(
								Opcodes.INVOKEVIRTUAL,
								owner,
								EnhancerConstants.PERSISTENT_FIELD_READER_PREFIX + name,
								Type.getMethodDescriptor( Type.getType( desc ) ),
								false
						);
						return;
					case Opcodes.PUTFIELD:
						methodVisitor.visitMethodInsn(
								Opcodes.INVOKEVIRTUAL,
								owner,
								EnhancerConstants.PERSISTENT_FIELD_WRITER_PREFIX + name,
								Type.getMethodDescriptor( Type.getType( void.class ), Type.getType( desc ) ),
								false
						);
						return;
					default:
						throw new EnhancementException( "Unexpected opcode: " + opcode );
				}
			}
			else {
				super.visitFieldInsn( opcode, owner, name, desc );
			}
		}
	};
}