net.bytebuddy.jar.asm.Type Java Examples

The following examples show how to use net.bytebuddy.jar.asm.Type. 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: ReactorDebugClassVisitor.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Override
public MethodVisitor visitMethod(int access, String currentMethod, String descriptor, String signature, String[] exceptions) {
	MethodVisitor visitor = super.visitMethod(access, currentMethod, descriptor, signature, exceptions);

	if (currentClassName.contains("CGLIB$$")) {
		return visitor;
	}

	String returnType = Type.getReturnType(descriptor).getInternalName();
	switch (returnType) {
		// Handle every core publisher type.
		// Note that classes like `GroupedFlux` or `ConnectableFlux` are not included,
		// because they don't have a type-preserving "checkpoint" method
		case "reactor/core/publisher/Flux":
		case "reactor/core/publisher/Mono":
		case "reactor/core/publisher/ParallelFlux":
			visitor = new ReturnHandlingMethodVisitor(visitor, returnType, currentClassName, currentMethod, currentSource,
					changed);
	}

	return new CallSiteInfoAddingMethodVisitor(visitor, currentClassName, currentMethod, currentSource, changed);
}
 
Example #2
Source File: BridgeClassVisitor.java    From kanela with Apache License 2.0 6 votes vote down vote up
private void processBridge(java.lang.reflect.Method reflectMethod, Annotation annotation) {
    val bridge = (Bridge) annotation;
    val method = Method.getMethod(reflectMethod);
    val targetMethod = Method.getMethod(bridge.value());

    val mv = cv.visitMethod(Opcodes.ACC_PUBLIC, method.getName(), method.getDescriptor(), null, null);
    mv.visitCode();
    int i = 0;
    mv.visitVarInsn(Opcodes.ALOAD, i++);

    for (Type argument : method.getArgumentTypes()) {
        mv.visitVarInsn(argument.getOpcode(Opcodes.ILOAD), i++);
    }

    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, type.getInternalName(), targetMethod.getName(), targetMethod.getDescriptor(), false);
    mv.visitInsn(method.getReturnType().getOpcode(Opcodes.IRETURN));
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
 
Example #3
Source File: FrontendClassVisitor.java    From flow with Apache License 2.0 6 votes vote down vote up
@Override
public void visitInvokeDynamicInsn(String name, String descriptor, Handle bootstrapMethodHandle, Object... bootstrapMethodArguments) {
    addSignatureToClasses(children, descriptor);
    addSignatureToClasses(children, bootstrapMethodHandle.getOwner());
    addSignatureToClasses(children, bootstrapMethodHandle.getDesc());
    for (Object obj : bootstrapMethodArguments) {
        if (obj instanceof Type) {
            addSignatureToClasses(children, obj.toString());
        } else if (obj instanceof Handle) {
            // The owner of the Handle is the reference information
            addSignatureToClasses(children, ((Handle) obj).getOwner());
            // the descriptor for the Handle won't be scanned, as it
            // adds from +10% to 40%  to the execution time and does not
            // affect the fix in itself
        }
        // the case for ConstantDynamic is also skipped for
        // performance reasons. It does not directly affect the fix
        // and slows down the execution.
    }
}
 
Example #4
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.visitMethodInsn(
			Opcodes.INVOKESPECIAL,
			managedCtClass.getSuperClass().asErasure().getInternalName(),
			EnhancerConstants.PERSISTENT_FIELD_READER_PREFIX + persistentField.getName(),
			Type.getMethodDescriptor( Type.getType( persistentField.getType().asErasure().getDescriptor() ) ),
			false
	);
	methodVisitor.visitInsn( Type.getType( persistentField.getType().asErasure().getDescriptor() ).getOpcode( Opcodes.IRETURN ) );
	return new Size( persistentField.getType().getStackSize().getSize(), instrumentedMethod.getStackSize() );
}
 
Example #5
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 #6
Source File: FieldWriterAppender.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void fieldRead(MethodVisitor methodVisitor) {
	methodVisitor.visitMethodInsn(
			Opcodes.INVOKESPECIAL,
			managedCtClass.getSuperClass().asErasure().getInternalName(),
			EnhancerConstants.PERSISTENT_FIELD_READER_PREFIX + persistentFieldAsDefined.getName(),
			Type.getMethodDescriptor( Type.getType( persistentFieldAsDefined.getType().asErasure().getDescriptor() ) ),
			false
	);
}
 
Example #7
Source File: BridgeClassVisitor.java    From kanela with Apache License 2.0 5 votes vote down vote up
private void processFieldBridge(java.lang.reflect.Method reflectMethod, Annotation annotation) {
    val fieldBridge = (FieldBridge) annotation;
    val method = Method.getMethod(reflectMethod);

    val mv = cv.visitMethod(Opcodes.ACC_PUBLIC, method.getName(), method.getDescriptor(), null, null);

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitFieldInsn(Opcodes.GETFIELD, type.getInternalName(), fieldBridge.value(), method.getReturnType().getDescriptor());
    mv.visitInsn(Type.getType(type.getDescriptor()).getOpcode(Opcodes.IRETURN));
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
 
Example #8
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 #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) {
	methodVisitor.visitLdcInsn( getters.length );
	methodVisitor.visitTypeInsn( Opcodes.ANEWARRAY, Type.getInternalName( Object.class ) );
	int index = 0;
	for ( Method getter : getters ) {
		methodVisitor.visitInsn( Opcodes.DUP );
		methodVisitor.visitLdcInsn( index++ );
		methodVisitor.visitVarInsn( Opcodes.ALOAD, 1 );
		methodVisitor.visitTypeInsn( Opcodes.CHECKCAST, Type.getInternalName( clazz ) );
		methodVisitor.visitMethodInsn(
				Opcodes.INVOKEVIRTUAL,
				Type.getInternalName( clazz ),
				getter.getName(),
				Type.getMethodDescriptor( getter ),
				false
		);
		if ( getter.getReturnType().isPrimitive() ) {
			PrimitiveBoxingDelegate.forPrimitive( new TypeDescription.ForLoadedType( getter.getReturnType() ) )
					.assignBoxedTo(
							TypeDescription.Generic.OBJECT,
							ReferenceTypeAwareAssigner.INSTANCE,
							Assigner.Typing.STATIC
					)
					.apply( methodVisitor, implementationContext );
		}
		methodVisitor.visitInsn( Opcodes.AASTORE );
	}
	methodVisitor.visitInsn( Opcodes.ARETURN );
	return new Size( 6, instrumentedMethod.getStackSize() );
}
 
Example #10
Source File: FieldWriterAppender.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void fieldWrite(MethodVisitor methodVisitor) {
	methodVisitor.visitMethodInsn(
			Opcodes.INVOKESPECIAL,
			managedCtClass.getSuperClass().asErasure().getInternalName(),
			EnhancerConstants.PERSISTENT_FIELD_WRITER_PREFIX + persistentFieldAsDefined.getName(),
			Type.getMethodDescriptor( Type.getType( void.class ), Type.getType( persistentFieldAsDefined.getType().asErasure().getDescriptor() ) ),
			false
	);
}
 
Example #11
Source File: ReturnHandlingMethodVisitor.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Override
public void visitMethodInsn(int opcode, String owner, String name, String descriptor, boolean isInterface) {
    super.visitMethodInsn(opcode, owner, name, descriptor, isInterface);

    if (!checkpointed && CallSiteInfoAddingMethodVisitor.isCorePublisher(owner)) {
        String returnType = Type.getReturnType(descriptor).getInternalName();
        if (returnType.startsWith("reactor/core/publisher/")) {
            checkpointed = true;
        }
    }
}
 
Example #12
Source File: CallSiteInfoAddingMethodVisitor.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Override
public void visitMethodInsn(int opcode, String owner, String name, String descriptor, boolean isInterface) {
    super.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
    if (isCorePublisher(owner)) {
        if ("checkpoint".equals(name)) {
            return;
        }
        String returnType = Type.getReturnType(descriptor).getInternalName();
        if (!returnType.startsWith("reactor/core/publisher/")) {
            return;
        }

        changed.set(true);

        String callSite = String.format(
                "\t%s.%s\n\t%s.%s(%s:%d)\n",
                owner.replace("/", "."), name,
                currentClassName.replace("/", "."), currentMethod, currentSource, currentLine
        );
        super.visitLdcInsn(callSite);
        super.visitMethodInsn(
                Opcodes.INVOKESTATIC,
                "reactor/core/publisher/Hooks",
                "addCallSiteInfo",
                ADD_CALLSITE_INFO_METHOD,
                false
        );
        super.visitTypeInsn(Opcodes.CHECKCAST, returnType);
    }
}
 
Example #13
Source File: FieldReaderAppender.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void fieldWrite(MethodVisitor methodVisitor) {
	methodVisitor.visitMethodInsn(
			Opcodes.INVOKESPECIAL,
			managedCtClass.getSuperClass().asErasure().getInternalName(),
			EnhancerConstants.PERSISTENT_FIELD_WRITER_PREFIX + persistentFieldAsDefined.getName(),
			Type.getMethodDescriptor( Type.getType( void.class ), Type.getType( persistentFieldAsDefined.getType().asErasure().getDescriptor() ) ),
			false
	);
}
 
Example #14
Source File: FieldReaderAppender.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void fieldRead(MethodVisitor methodVisitor) {
	methodVisitor.visitMethodInsn(
			Opcodes.INVOKESPECIAL,
			managedCtClass.getSuperClass().asErasure().getInternalName(),
			EnhancerConstants.PERSISTENT_FIELD_READER_PREFIX + persistentFieldAsDefined.getName(),
			Type.getMethodDescriptor( Type.getType( persistentFieldAsDefined.getType().asErasure().getDescriptor() ) ),
			false
	);
}
 
Example #15
Source File: BridgeClassVisitor.java    From kanela with Apache License 2.0 4 votes vote down vote up
private BridgeClassVisitor(BridgeDescription bridge, String className, ClassVisitor classVisitor) {
    super(OpenedClassReader.ASM_API, classVisitor);
    this.bridge = bridge;
    this.type = Type.getObjectType(className);
}
 
Example #16
Source File: FrontendClassVisitor.java    From flow with Apache License 2.0 4 votes vote down vote up
/**
 * Create a new {@link ClassVisitor} that will be used for visiting a
 * specific class.
 *
 * @param className
 *            the class to visit
 * @param endPoint
 *            the end-point object that will be updated during the visit
 * @param themeScope
 *            whether we are visiting from Theme
 */
FrontendClassVisitor(String className, EndPointData endPoint,
        boolean themeScope) { // NOSONAR
    super(Opcodes.ASM7);
    this.className = className;
    this.endPoint = endPoint;

    // Visitor for each method in the class.
    methodVisitor = new FrontendMethodVisitor();
    // Visitor for each annotation in the class.
    routeVisitor = new RepeatedAnnotationVisitor() {
        @Override
        public void visit(String name, Object value) {
            if (LAYOUT.equals(name)) {
                endPoint.layout = ((Type) value).getClassName();
                children.add(endPoint.layout);
            }
            if (VALUE.equals(name)) {
                endPoint.route = value.toString();
            }
        }
    };
    // Visitor for @Theme annotations in classes annotated with @Route
    themeRouteVisitor = new RepeatedAnnotationVisitor() {
        @Override
        public void visit(String name, Object value) {
            if (VALUE.equals(name)) {
                endPoint.theme.name = ((Type) value).getClassName();
                children.add(endPoint.theme.name);
            } else if (VARIANT.equals(name)) {
                endPoint.theme.variant = value.toString();
            }
        }
    };
    // Visitor for @Theme annotations in classes extending RouterLayout
    themeLayoutVisitor = new RepeatedAnnotationVisitor() {
        @Override
        public void visit(String name, Object value) {
            if (VALUE.equals(name) && endPoint.theme.name == null) {
                themeRouteVisitor.visit(name, value);
            } else if (VARIANT.equals(name)
                    && endPoint.theme.variant.isEmpty()) {
                themeRouteVisitor.visit(name, value);
            }
        }
    };
    // Visitor for @JsModule annotations
    jsModuleVisitor = new RepeatedAnnotationVisitor() {
        @Override
        public void visit(String name, Object value) {
            if (themeScope) {
                endPoint.themeModules.add(value.toString());
            } else {
                endPoint.modules.add(value.toString());
            }
        }
    };
    // Visitor for @JavaScript annotations
    jScriptVisitor = new RepeatedAnnotationVisitor() {
        @Override
        public void visit(String name, Object value) {
            endPoint.scripts.add(value.toString());
        }
    };
    // Visitor all other annotations
    annotationVisitor = new RepeatedAnnotationVisitor() {
        @Override
        public void visit(String name, Object value) {
            if (value != null && !value.getClass().isPrimitive()
                    && !value.getClass().equals(String.class)) {
                addSignatureToClasses(children, value.toString());
            }
        }
    };
}
 
Example #17
Source File: FrontendClassVisitor.java    From flow with Apache License 2.0 4 votes vote down vote up
@Override
public void visitLdcInsn(Object value) {
    if (value instanceof Type) {
        addSignatureToClasses(children, value.toString());
    }
}
 
Example #18
Source File: AnalyzedClass.java    From kanela with Apache License 2.0 4 votes vote down vote up
private static String getType(Type methodDescription) {
    return methodDescription
            .getInternalName()
            .replace('/', '.');
}
 
Example #19
Source File: AnalyzedClass.java    From kanela with Apache License 2.0 4 votes vote down vote up
private static Map<String, Set<String>> extractMethods(ClassNode classNode) {
    return List.ofAll(classNode.methods)
            .filter(methodNode -> (methodNode.access & Opcodes.ACC_SYNTHETIC) == 0)
            .toJavaMap(methodNode -> Tuple.of(methodNode.name, Array.of(Type.getArgumentTypes(methodNode.desc)).map(AnalyzedClass::getType).toJavaSet()));
}
 
Example #20
Source File: MixinClassVisitor.java    From kanela with Apache License 2.0 4 votes vote down vote up
private MixinClassVisitor(MixinDescription mixin, String className, ClassVisitor classVisitor) {
    super(OpenedClassReader.ASM_API, classVisitor);
    this.mixin = mixin;
    this.type = Type.getObjectType(className);
}
 
Example #21
Source File: MixinInitializer.java    From kanela with Apache License 2.0 4 votes vote down vote up
MixinInitializer(MethodVisitor mv, int access, String name, String desc, Type typeClass, MixinDescription mixinDescription) {
    super(OpenedClassReader.ASM_API, mv, access, name, desc);
    this.typeClass = typeClass;
    this.mixinDescription = mixinDescription;
}
 
Example #22
Source File: InlineDirtyCheckingHandler.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Size apply(
		MethodVisitor methodVisitor,
		Context implementationContext,
		MethodDescription instrumentedMethod) {
	// if (arg != field) {
	methodVisitor.visitVarInsn( Type.getType( persistentField.getType().asErasure().getDescriptor() ).getOpcode( Opcodes.ILOAD ), 1 );
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	if ( persistentField.getDeclaringType().asErasure().equals( managedCtClass ) ) {
		methodVisitor.visitFieldInsn(
				Opcodes.GETFIELD,
				persistentField.getDeclaringType().asErasure().getInternalName(),
				persistentField.getName(),
				persistentField.getDescriptor()
		);
	}
	else {
		methodVisitor.visitMethodInsn(
				Opcodes.INVOKEVIRTUAL,
				persistentField.getDeclaringType().asErasure().getInternalName(),
				EnhancerConstants.PERSISTENT_FIELD_READER_PREFIX + persistentField.getName(),
				Type.getMethodDescriptor( Type.getType( persistentField.getDescriptor() ) ),
				false
		);
	}
	int branchCode;
	if ( persistentField.getType().isPrimitive() ) {
		if ( persistentField.getType().represents( long.class ) ) {
			methodVisitor.visitInsn( Opcodes.LCMP );
		}
		else if ( persistentField.getType().represents( float.class ) ) {
			methodVisitor.visitInsn( Opcodes.FCMPL );
		}
		else if ( persistentField.getType().represents( double.class ) ) {
			methodVisitor.visitInsn( Opcodes.DCMPL );
		}
		else {
			methodVisitor.visitInsn( Opcodes.ISUB );
		}
		branchCode = Opcodes.IFEQ;
	}
	else {
		methodVisitor.visitMethodInsn(
				Opcodes.INVOKESTATIC,
				Type.getInternalName( Objects.class ),
				"deepEquals",
				Type.getMethodDescriptor( Type.getType( boolean.class ), Type.getType( Object.class ), Type.getType( Object.class ) ),
				false
		);
		branchCode = Opcodes.IFNE;
	}
	Label skip = new Label();
	methodVisitor.visitJumpInsn( branchCode, skip );
	// this.$$_hibernate_trackChange(fieldName)
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	methodVisitor.visitLdcInsn( persistentField.getName() );
	methodVisitor.visitMethodInsn(
			Opcodes.INVOKEVIRTUAL,
			managedCtClass.getInternalName(),
			EnhancerConstants.TRACKER_CHANGER_NAME,
			Type.getMethodDescriptor( Type.getType( void.class ), Type.getType( String.class ) ),
			false
	);
	// }
	methodVisitor.visitLabel( skip );
	if ( implementationContext.getClassFileVersion().isAtLeast( ClassFileVersion.JAVA_V6 ) ) {
		methodVisitor.visitFrame( Opcodes.F_SAME, 0, null, 0, null );
	}
	return new Size( 1 + 2 * persistentField.getType().asErasure().getStackSize().getSize(), instrumentedMethod.getStackSize() );
}
 
Example #23
Source File: FieldWriterAppender.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Size apply(
		MethodVisitor methodVisitor,
		Implementation.Context implementationContext,
		MethodDescription instrumentedMethod) {
	TypeDescription dispatcherType = persistentFieldAsDefined.getType().isPrimitive()
			? persistentFieldAsDefined.getType().asErasure()
			: TypeDescription.OBJECT;
	// if ( this.$$_hibernate_getInterceptor() != null )
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	methodVisitor.visitMethodInsn(
			Opcodes.INVOKEVIRTUAL,
			managedCtClass.getInternalName(),
			EnhancerConstants.INTERCEPTOR_GETTER_NAME,
			Type.getMethodDescriptor( Type.getType( PersistentAttributeInterceptor.class ) ),
			false
	);
	Label noInterceptor = new Label();
	methodVisitor.visitJumpInsn( Opcodes.IFNULL, noInterceptor );
	// this (for field write)
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	// this.$$_hibernate_getInterceptor();
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	methodVisitor.visitMethodInsn(
			Opcodes.INVOKEVIRTUAL,
			managedCtClass.getInternalName(),
			EnhancerConstants.INTERCEPTOR_GETTER_NAME,
			Type.getMethodDescriptor( Type.getType( PersistentAttributeInterceptor.class ) ),
			false
	);
	// .writeXXX( self, fieldName, field, arg1 );
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	methodVisitor.visitLdcInsn( persistentFieldAsDefined.getName() );
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	fieldRead( methodVisitor );
	methodVisitor.visitVarInsn( Type.getType( dispatcherType.getDescriptor() ).getOpcode( Opcodes.ILOAD ), 1 );
	methodVisitor.visitMethodInsn(
			Opcodes.INVOKEINTERFACE,
			Type.getInternalName( PersistentAttributeInterceptor.class ),
			"write" + EnhancerImpl.capitalize( dispatcherType.getSimpleName() ),
			Type.getMethodDescriptor(
					Type.getType( dispatcherType.getDescriptor() ),
					Type.getType( Object.class ),
					Type.getType( String.class ),
					Type.getType( dispatcherType.getDescriptor() ),
					Type.getType( dispatcherType.getDescriptor() )
			),
			true
	);
	// arg1 = (cast) XXX
	if ( !dispatcherType.isPrimitive() ) {
		methodVisitor.visitTypeInsn( Opcodes.CHECKCAST, persistentFieldAsDefined.getType().asErasure().getInternalName() );
	}
	fieldWrite( methodVisitor );
	// return
	methodVisitor.visitInsn( Opcodes.RETURN );
	// else
	methodVisitor.visitLabel( noInterceptor );
	if ( implementationContext.getClassFileVersion().isAtLeast( ClassFileVersion.JAVA_V6 ) ) {
		methodVisitor.visitFrame( Opcodes.F_SAME, 0, null, 0, null );
	}
	// this (for field write)
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	// arg1 = (cast) XXX
	methodVisitor.visitVarInsn( Type.getType( dispatcherType.getDescriptor() ).getOpcode( Opcodes.ILOAD ), 1 );
	if ( !dispatcherType.isPrimitive() ) {
		methodVisitor.visitTypeInsn( Opcodes.CHECKCAST, persistentFieldAsDefined.getType().asErasure().getInternalName() );
	}
	fieldWrite( methodVisitor );
	// return
	methodVisitor.visitInsn( Opcodes.RETURN );
	return new Size( 4 + 2 * persistentFieldAsDefined.getType().getStackSize().getSize(), instrumentedMethod.getStackSize() );
}
 
Example #24
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 );
			}
		}
	};
}
 
Example #25
Source File: BiDirectionalAssociationHandler.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Size apply(
		MethodVisitor methodVisitor, Context implementationContext, MethodDescription instrumentedMethod) {
	return delegate.apply( new MethodVisitor( Opcodes.ASM5, methodVisitor ) {

		@Override
		public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
			if ( owner.startsWith( Type.getInternalName( CodeTemplates.class ) ) ) {
				if ( name.equals( "getter" ) ) {
					super.visitTypeInsn( Opcodes.CHECKCAST, targetEntity.getInternalName() );
					super.visitMethodInsn(
							Opcodes.INVOKEVIRTUAL,
							targetEntity.getInternalName(),
							EnhancerConstants.PERSISTENT_FIELD_READER_PREFIX + mappedBy,
							Type.getMethodDescriptor( Type.getType( targetType.getDescriptor() ) ),
							false
					);
				}
				else if ( name.equals( "setterSelf" ) ) {
					super.visitInsn( Opcodes.POP );
					super.visitTypeInsn( Opcodes.CHECKCAST, targetEntity.getInternalName() );
					super.visitVarInsn( Opcodes.ALOAD, 0 );
					super.visitMethodInsn(
							Opcodes.INVOKEVIRTUAL,
							targetEntity.getInternalName(),
							EnhancerConstants.PERSISTENT_FIELD_WRITER_PREFIX + mappedBy,
							Type.getMethodDescriptor( Type.getType( void.class ), Type.getType( targetType.getDescriptor() ) ),
							false
					);
				}
				else if ( name.equals( "setterNull" ) ) {
					super.visitInsn( Opcodes.POP );
					super.visitTypeInsn( Opcodes.CHECKCAST, targetEntity.getInternalName() );
					super.visitInsn( Opcodes.ACONST_NULL );
					super.visitMethodInsn(
							Opcodes.INVOKEVIRTUAL,
							targetEntity.getInternalName(),
							EnhancerConstants.PERSISTENT_FIELD_WRITER_PREFIX + mappedBy,
							Type.getMethodDescriptor( Type.getType( void.class ), Type.getType( targetType.getDescriptor() ) ),
							false
					);
				}
				else {
					throw new EnhancementException( "Unknown template method: " + name );
				}
			}
			else {
				super.visitMethodInsn( opcode, owner, name, desc, itf );
			}
		}
	}, implementationContext, instrumentedMethod );
}
 
Example #26
Source File: FieldReaderAppender.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Size apply(
		MethodVisitor methodVisitor,
		Implementation.Context implementationContext,
		MethodDescription instrumentedMethod) {
	TypeDescription dispatcherType = persistentFieldAsDefined.getType().isPrimitive()
			? persistentFieldAsDefined.getType().asErasure()
			: TypeDescription.OBJECT;
	// if ( this.$$_hibernate_getInterceptor() != null )
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	methodVisitor.visitMethodInsn(
			Opcodes.INVOKEVIRTUAL,
			managedCtClass.getInternalName(),
			EnhancerConstants.INTERCEPTOR_GETTER_NAME,
			Type.getMethodDescriptor( Type.getType( PersistentAttributeInterceptor.class ) ),
			false
	);
	Label skip = new Label();
	methodVisitor.visitJumpInsn( Opcodes.IFNULL, skip );
	// this (for field write)
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	// this.$$_hibernate_getInterceptor();
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	methodVisitor.visitMethodInsn(
			Opcodes.INVOKEVIRTUAL,
			managedCtClass.getInternalName(),
			EnhancerConstants.INTERCEPTOR_GETTER_NAME,
			Type.getMethodDescriptor( Type.getType( PersistentAttributeInterceptor.class ) ),
			false
	);
	// .readXXX( self, fieldName, field );
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	methodVisitor.visitLdcInsn( persistentFieldAsDefined.getName() );
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	fieldRead( methodVisitor );
	methodVisitor.visitMethodInsn(
			Opcodes.INVOKEINTERFACE,
			Type.getInternalName( PersistentAttributeInterceptor.class ),
			"read" + EnhancerImpl.capitalize( dispatcherType.getSimpleName() ),
			Type.getMethodDescriptor(
					Type.getType( dispatcherType.getDescriptor() ),
					Type.getType( Object.class ),
					Type.getType( String.class ),
					Type.getType( dispatcherType.getDescriptor() )
			),
			true
	);
	// field = (cast) XXX
	if ( !dispatcherType.isPrimitive() ) {
		methodVisitor.visitTypeInsn( Opcodes.CHECKCAST, persistentFieldAsDefined.getType().asErasure().getInternalName() );
	}
	fieldWrite( methodVisitor );
	// end if
	methodVisitor.visitLabel( skip );
	if ( implementationContext.getClassFileVersion().isAtLeast( ClassFileVersion.JAVA_V6 ) ) {
		methodVisitor.visitFrame( Opcodes.F_SAME, 0, null, 0, null );
	}
	// return field
	methodVisitor.visitVarInsn( Opcodes.ALOAD, 0 );
	fieldRead( methodVisitor );
	if ( !persistentField.getType().isPrimitive()
			&& !persistentField.getType().asErasure().getInternalName().equals( persistentFieldAsDefined.getType().asErasure().getInternalName() ) ) {
		methodVisitor.visitTypeInsn( Opcodes.CHECKCAST, persistentField.getType().asErasure().getInternalName() );
	}
	methodVisitor.visitInsn( Type.getType( persistentFieldAsDefined.getType().asErasure().getDescriptor() ).getOpcode( Opcodes.IRETURN ) );
	return new Size( 4 + persistentFieldAsDefined.getType().getStackSize().getSize(), instrumentedMethod.getStackSize() );
}