Application of ASM in Spring

background

Very rough spring to see the source code, under the application of ASM repeatedly found in the source code, a simple record.
My job is JavaAgent, for ASM is quite familiar.
ASM can be read only during the operation of the code corresponding information, you can also modify the bytecode is a pretty good frame.
ASM was beautiful, but still add new logic is bound to consume performance, it can not, then do not use, and such as we are more familiar with @RequestParam ( "name"), reducing the need for ASM reading method parameters operation name (parameter type of reflection is obtained, not the name of the parameter)

The following applications ASM look in the Spring of how to find the source code (ASM only find clues, not the ASM usage)

Registration notes BeanDefinition

See ClassPathScanningCandidateComponentProvider this class addCandidateComponentsFromIndex () method, find AnnotatedGenericBeanDefinition sbd = new AnnotatedGenericBeanDefinition( metadataReader.getAnnotationMetadata());the code.
Here Insert Picture Description
getAnnotationMetadata () method, was understood from the literal meaning of the meta information Bean.
To SimpleMetadataReader class

final class SimpleMetadataReader implements MetadataReader {
	private final AnnotationMetadata annotationMetadata;
	@Override
	public AnnotationMetadata getAnnotationMetadata() {
		return this.annotationMetadata;
	}
}

The return is annotationMetadata this field, which is an interface, we see one of the implementation class AnnotationMetadataReadingVisitor
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
fact ASM is simply integrated ClassVisitor, then rewrite a series of visit *** method inside, by directly modifying the byte code command manner, to modify bytecodes at runtime technology.

Well, not only found AnnotationMetadataReadingVisitor, also found ClassMetadataReadingVisitor, made a big profit, when they saw the suffix with Visitorbasically the way out.
Spring can be seen in the ASM with quite a few, rewrite them visit, visitInnerClass, visitMethod, visitFieldetc.

Published 525 original articles · won praise 2337 · Views 2.17 million +

Guess you like

Origin blog.csdn.net/dataiyangu/article/details/104443839
ASM