Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize automatic analysis of R & D delivery quality based on IDEA plug-in development and bytecode pile insertion technology

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to realize automatic analysis of R & D delivery quality based on IDEA plug-in development and bytecode insertion technology". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. Preface

How to ensure code quality?

Business demand, product plan, R & D and implementation, testing and testing process. The cooperation of the four roles is a necessary condition to ensure that a requirement is online. In the division of the delivery quality level of the whole requirement, R & D and testing is a very important link. If the quality of the code tested by R & D is not high, there will be different levels of risk of BUG repair, rework and even redo.

So, how to improve the quality of the code? In general, we will require R & D to write unit tests in the process of developing code to verify the logic of their own code. If the final unit test coverage is insufficient, the test can refuse to develop and raise the test.

However, the code for the implementation of the entire requirements is tested after all the development is completed, that is, near the last link on the line, we all know whether the implementation of a certain functional domain of a certain research and development has the conditions of raising and testing. If the quality of the code is not high at this time, then it is time for project risk. Press the test time, adjust the online time, in short, the illness dragged on and finally became a serious illness!

Of course, you can regularly review the code during project development, or provide progress feedback during daily meetings, and so on. But this requires a lot of time-consuming development inspection method is difficult to meet the complex process of the larger project development, and for the project risk control is also unpredictable.

Therefore, we hope to collect the implementation actions of R & D in the development process and make the risk judgment in advance. For example, when you develop an interface and start testing, our plug-in can collect all the information about the interface, including: interface name, input parameter type and content, output parameter type and content, exception information, call relationship chain, etc. Then the information is summarized and submitted to the server to generate all the interface actions under this requirement code branch, as well as the relationship links between the systems, with the latest interface documents and one-click test verification function generated at any time. When the later testers intervene, they can refer to all the test cases in the coding process of the research and development, and also check the coverage of the whole function. In addition, the data of the testers during the testing process will also be retained. Now with these data and information, a complete set of R & D test quality delivery panorama can be generated, which makes the whole project development and delivery quality evaluation transparent.

Then we will follow the above descriptive content, practice to develop a case experience. Let's go!

II. Preparation for technical implementation

Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community

Bytecode stub, because we need to collect interface execution information, then we need to use bytecode stud component to enhance the interface method. This implementation is somewhat similar to Google's Dapper, a large-scale distributed architecture of non-intrusive monitoring. It's just that we need to collect more descriptive information. For bytecode stuffing, you can learn about ASM, Javassist, and Byte-Buddy, all of which can do this job.

IDEA plug-in development, because we need to collect in the development process of R & D personnel, and do not break the operating habits of R & D. Then the best way is to embed it into the startup operation, and as long as there is an action to run the code in the development process, the corresponding interface information is collected.

Finally, there is the transmission and processing of data, which can be transmitted using MQ or directly using Netty. The process of processing data will be relatively complex, in this process, we need to analyze valuable data, similar data, merge the data of an execution link, and generate relevant interface documents and engineering service maps.

Third, inserting piles for bytecode

The bytecode stuffing component we use here is Byte-buddy, a code generation and manipulation library that creates and modifies Java classes while the Java application is running without the help of a compiler. In addition to the code generation utility included with the Java class library, Byte Buddy allows you to create arbitrary classes and is not limited to implementing interfaces for creating run-time proxies. In addition, Byte Buddy provides a convenient API where you can use the Java proxy or manually change the class during the build process.

Without understanding bytecode instructions, you can easily manipulate bytecode, control classes, and methods using a simple API.

Java 11 is supported and the library is lightweight, depending only on API, the visitor to the Java bytecode parser library ASM, which does not require any other dependencies on its own.

Compared with JDK dynamic agent, cglib and Javassist,Byte Buddy, it has some advantages in performance.

1. Method entrance

Public static void premain (String agentArgs, Instrumentation inst) {AgentBuilder.Transformer transformer = (builder, typeDescription, classLoader, javaModule)-> {return builder .method (ElementMatchers.any ()) / / intercept any method .intercept (MethodDelegation.to (MonitorMethod.class));} New AgentBuilder .default () .type (ElementMatchers.nameStartsWith (agentArgs)) .transform (transformer) .installOn (inst);}

If you have been exposed to Javaagent development, then you will be familiar with premain. If it's not clear that you can understand it as the method entry when the program starts, you can intercept the method you need from this entry, and then bytecode enhance it. In fact, it is to write code dynamically and add your code to the method to collect method information.

two。 Collect information

@ RuntimeType public static Object intercept (@ Origin Method method, @ SuperCall Callable callable, @ AllArguments Object [] args) throws Exception {long start = System.currentTimeMillis (); Object resObj = null; try {resObj = callable.call (); return resObj;} finally {System.out.println ("method name:" + method.getName ()); System.out.println ("number of input parameters:" + method.getParameterCount ()) For (int I = 0; I < method.getParameterCount ()) {System.out.println ("input parameter Idx:" + (I + 1) + "type:" + method.getParameterTypes () [I] .getTypeName () + "content:" + args [I]);} System.out.println ("output parameter type:" + method.getReturnType (). GetName () System.out.println ("output result:" + resObj); System.out.println ("method time:" + (System.currentTimeMillis ()-start) + "ms");}}

This is the information that can be collected using Byte-Buddy, and you can get all the information about a method by annotating the input parameters. The name of the method, the number of input parameters, the type and content of input parameters, the type and result of output parameters, and the execution time of the calculation method.

IV. IDEA plug-in development

There is a lot of knowledge about IDEA plug-in development. You can search for some information from GitHub and consult the official document: https://plugins.jetbrains.com/docs/intellij/gradle-build-system.html?from=jetbrains.org

The content of the demonstration case about plug-in development here is relatively simple, mainly inheriting the com.intellij.execution.impl.DefaultJavaProgramRunner,Override doExecute method and adding what you need.

The core of the content added in this section is to add our bytecode stuffing program when the program starts, as follows:

@ Override protected RunContentDescriptor doExecute (@ NotNull RunProfileState state, @ NotNull ExecutionEnvironment env) throws ExecutionException {JavaParameters parameters = ((JavaCommandLine) state) .getJavaParameters (); / / Information acquisition PsiFile psiFile = env.getDataContext () .getData (LangDataKeys.PSI_FILE); String packageName = ((PsiJavaFileImpl) psiFile) .getPackageName (); / / add bytecode instrumentation ParametersList parametersList = parameters.getVMParametersList () ParametersList.add ("- javaagent:" + this.getClass (). GetResource ("/"). GetPath (). Substring (1) + "ProjectProbe.jar=" + packageName); return super.doExecute (state, env);}

The core here is that-javaagent loads the Jar package of the ProjectProbe.jar engineering probe program into it. The rest is about the use of PsiFile API. If you are interested, you can read the introduction in the official documentation.

5. Demonstration of effect

Install the plug-in

Installing the plug-in is the same as our normal installation, but this plug-in is currently under development, so it needs to be installed locally.

Running effect

The above figure is a case study of the running effect, and we output the complete information of the runtime interface to the console.

In the process of actual use, this part of the information will be transmitted back to the server, analyzed and processed by the server, and displayed on the page.

This is the end of the content of "how to realize automatic analysis of R & D delivery quality based on IDEA plug-in development and bytecode pile insertion technology". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report