Get the App
SLTechnology News&Howtos  ›  Development  › 

The method of integrating asm Code insertion pile with SpringBoot Custom maven-plugin plug-in

Shulou Source: shulou.com Published: 2022-06-01 13:07:27 09月30日 Update

This article "SpringBoot custom maven-plugin plug-in integration of asm code piling method" most people do not understand, so the editor summarized the following content, detailed, clear steps, with a certain reference value, I hope you can finish reading this article can have a harvest, let's take a look at this "SpringBoot custom maven-plugin plug-in integration of asm code piling method" article it.

Background

The company's development framework adds the license authorization certificate verification module of the web system to implement one authorization certificate per machine. The preliminary plan is to increase the interceptor to intercept and verify the global request. After evaluation, it is considered that the verification method is single and important tools should be added. The verification of each method in the business service implementation is difficult because it involves a large amount of code. Therefore, we choose to insert piles in dynamic code during compilation through a custom maven plug-in.

Project configuration

Set the packaging method for a new maven project

Maven-plugin

Increase dependencies

Org.apache.maven maven-plugin-api 3.5.2 org.apache.maven.plugin-tools maven-plugin-annotations 3.5.2 provided org.apache.maven maven-project 2.2.1 Org.ow2.asm asm 9.0

Build content configuration

Org.apache.maven.plugins maven-plugin-plugin 3.5 org.apache.maven.plugins maven-compiler-plugin 3.6.1 1.8 1.8 Compiler intercept

Create the compilation operation class FramePlugin, inherit AbstractMojo and annotate with Mojo. The output parameter is the post-compilation path of the class file.

@ Mojo (name = "deepcompile", defaultPhase = LifecyclePhase.COMPILE) public class FramePlugin extends AbstractMojo {@ Parameter (name = "output", defaultValue = "${project.build.directory}") private File output; public void execute () throws MojoExecutionException {File f =; if (! f.exists ()) {f.mkdirs ();} try {insertPile (f) } catch (Exception e) {exceptioncount++; e.printStackTrace ();}} ASM piling

Create a new ClassVisitor rewrite visitMethod method to filter access to the methods that need to be inserted, and you need to exclude the init method that comes with it.

Public class MethodCoverageClassVisitor extends ClassVisitor {public MethodCoverageClassVisitor (ClassVisitor classVisitor) {super (Opcodes.ASM9, classVisitor);} @ Override public MethodVisitor visitMethod (int access, String name, String descriptor, String signature, String [] exceptions) {final MethodVisitor methodVisitor = super.visitMethod (access, name, descriptor, signature, exceptions); if (name.equals (")) {return methodVisitor } return new MethodCoverageMethodVisitor (Opcodes.ASM9, methodVisitor);}}

A new MethodVisitor rewrite visitCode method is created to customize the method's internal bytecode. Here, a static method encapsulated inside the framework is used to verify the license certificate.

Public class MethodCoverageMethodVisitor extends MethodVisitor {public MethodCoverageMethodVisitor (int api, MethodVisitor methodVisitor) {super (api, methodVisitor);} @ Override public void visitCode () {mv.visitFieldInsn (Opcodes.INVOKESTATIC, "com/xxxx/frame/common/utils/ComplieSDK", "checkLicense", "() V");}}

Finally, the file recursive lookup call is made in execute, that is, the compiled class file is read / customized and saved.

Private void insertPile (File root) throws IOException {if (root.isDirectory ()) {for (File file: root.listFiles ()) {insertPile (file);}} String className = root.getName () .replace (".class", "") If (root.getName (). EndsWith (".class") {/ / class filter boolean flag = false; / / custom class file filter condition code if (flag) {System.out.println ("[insertPile]:" + className); FileOutputStream fos = null Try {final byte [] instrumentBytes = doInsertPile (root); fos = new FileOutputStream (root); fos.write (instrumentBytes); fos.flush ();} catch (MojoExecutionException e) {System.out.println ("[insertPile-exception]:" + className) E.printStackTrace ();} finally {if (fos! = null) {fos.close ();}} Project usage

The maven-plugin project executes mvn install installation to the local warehouse

The framework project is configured with a custom maven plug-in for packaging. The declaration cycle for configuration execution is complie (compilation), where the name of the goal custom command needs to be the same as the name name specified in the mojo annotation class.

Com.xxxxx frame-maven-plugin 1.2.5 deepcompile Compile above is the content of the article on "SpringBoot Custom maven-plugin plug-ins integrate asm Code stuffing" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Tags: Methods code content compilation plug-ins projects files configurations integration frameworks certificates names that is articles methods annotations knowledge articles large and important Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Information Shulou Technology MariaDB Shulou Tech Info