WO2019028989A1 - 一种代码分析方法及终端设备 - Google Patents

一种代码分析方法及终端设备 Download PDF

Info

Publication number
WO2019028989A1
WO2019028989A1 PCT/CN2017/104535 CN2017104535W WO2019028989A1 WO 2019028989 A1 WO2019028989 A1 WO 2019028989A1 CN 2017104535 W CN2017104535 W CN 2017104535W WO 2019028989 A1 WO2019028989 A1 WO 2019028989A1
Authority
WO
WIPO (PCT)
Prior art keywords
call
path
relationship
file
found
Prior art date
Application number
PCT/CN2017/104535
Other languages
English (en)
French (fr)
Inventor
李玲
Original Assignee
平安科技(深圳)有限公司
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by 平安科技(深圳)有限公司 filed Critical 平安科技(深圳)有限公司
Publication of WO2019028989A1 publication Critical patent/WO2019028989A1/zh

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Preventing errors by testing or debugging software
    • G06F11/3604Software analysis for verifying properties of programs
    • G06F11/3612Software analysis for verifying properties of programs by runtime analysis
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Preventing errors by testing or debugging software
    • G06F11/3668Software testing
    • G06F11/3672Test management
    • G06F11/3684Test management for test design, e.g. generating new test cases
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Preventing errors by testing or debugging software
    • G06F11/3668Software testing
    • G06F11/3672Test management
    • G06F11/3688Test management for test execution, e.g. scheduling of test suites

Definitions

  • This application belongs to the field of computer technology, and in particular, to a code analysis method and a terminal device.
  • the embodiment of the present application provides a code analysis method and a terminal device, so as to solve the problem that the existing caller manually combs the call relationship, and the efficiency is low and the error rate is high.
  • a first aspect of the embodiments of the present application provides a code analysis method, which may include:
  • a second aspect of the embodiments of the present application provides a computer readable storage medium storing computer readable instructions, the computer readable instructions being executed by a processor, and implementing the following steps:
  • a third aspect of the embodiments of the present application provides a code analysis terminal device, including a memory, a processor, and computer readable instructions stored in the memory and operable on the processor, the processing The computer executes the computer readable instructions to implement the following steps:
  • a fourth aspect of the embodiments of the present application provides a computer readable instruction, the computer readable instructions being applied to a code analysis terminal device, where the code analysis terminal device includes a memory, a processor, and a storage device
  • the computer readable instructions in a memory and operative on the processor, the computer readable instructions comprising the following modules:
  • a path obtaining module configured to acquire an absolute path of a source file of the target Java project
  • a search module configured to find a first method corresponding to the specified URL from the class file in the absolute path;
  • calling a lookup module configured to search for a call to the second method in the method body of the first method, where the second method is a method different from the first method;
  • a second method configured to: if the calling of the second method is found in the method body of the first method, searching for the second method in a class file;
  • a call relationship record module configured to record a call relationship between the first method and the second method if the second method is found in a class file
  • a first method update module configured to determine the second method as a new first method, and return to perform the step of searching for a call to the second method in the method body of the first method;
  • a call relationship summary module configured to: if the call to the second method is not found in the method body of the first method, end code analysis, and summarize the recorded call relationship;
  • calling a relationship drawing module configured to draw a corresponding effect of the corresponding call relationship diagram according to the summarized calling relationship Beneficial effect
  • the beneficial effects of the embodiment of the present application compared with the prior art are as follows: The embodiment of the present application first obtains an absolute path of a source file of a target Java project, and searches for a class file from the absolute path. The first method corresponding to the specified URL, and then through recursion, find the call to the next level method layer by layer until there is no next level method, and finally summarize all the call relationships of the record and draw corresponding Call graph. The issuer only needs to specify the URL that needs to be analyzed, and then automatically analyzes the call relationship and draws the call relationship graph, which greatly improves the analysis efficiency, and avoids the occurrence of human error, effectively reducing the error rate.
  • FIG. 1 is a flow chart of an embodiment of a code analysis method in an embodiment of the present application.
  • FIG. 2 is a schematic diagram of generating a drawing script according to a script template according to a code analysis method in an embodiment of the present application
  • FIG. 3 is a schematic diagram of an effect of a call relationship diagram generated by a code analysis method according to an embodiment of the present application
  • FIG. 4 is a schematic block diagram of a code analysis terminal device according to an embodiment of the present application
  • FIG. 5 is a functional block diagram of a code analysis program according to an embodiment of the present application.
  • the embodiment of the present invention mainly analyzes the Java code and generates a call relationship diagram.
  • a lot of technical terms related to Java code are involved. For the sake of understanding, these technical terms are first explained briefly.
  • Absolute path The path to the complete description file location is the absolute path, that is, the path from the root directory at the top of the tree directory structure to a directory or file, consisting of a series of consecutive directories, with a diagonal Line separated, until the directory or file to be specified, the last name in the path is the directory or file to point to, such as C: ⁇ windows ⁇ system32 ⁇ cmcLexe.
  • the relative path is the path starting from the current path. For example, the current path is C: ⁇ windows. Using the relative path, the path should be . ⁇ system32 ⁇ cmd.exe.
  • URL Uniform Resource Locator, which is a uniform resource locator.
  • a URL is a compact representation of the location and access method of a resource that can be obtained from the Internet, and is the address of a standard resource on the Internet.
  • Method In Java code, a method is a set of code blocks for implementing a specific function, which acts like a function in C code.
  • Class In Java code, a class is a collection of objects with the same characteristics and behavior, in a nutshell
  • the class is the template for creating Java objects.
  • An object is an instance of a class that has state and behavior.
  • a dog is an object whose status is: color, name, breed; behaviors are: wagtail, call, eat, etc.
  • Database operation class The class provided by the system to operate on the database can access various types of databases, establish database connections, execute SQL statements, and access data.
  • PlantUML PlantUML is a rapid creation of Unified Modeling (Unified Modeling)
  • UML A component of graphics, where UML is a general-purpose, unified graphical model language for object-oriented software bursting. It is an effective tool for software system stats, visualization, and modeling.
  • An embodiment of a code analysis method in the embodiment of the present application may include:
  • Step S101 Obtain an absolute path of the source file of the target Java project.
  • Classpath In Java, the role of Classpath is to tell the Java execution environment, in which directories you can find the classes or packages needed for the Java program to be executed. Here you can enter the Java execution environment and the operating system. Row analogy, if the path variable is set to let the operating system find the specified utility program (in Windows, it is to find the .exe file), then the purpose of setting the Classpath is to let the Java execution environment find the specified Java program (that is, the .class file). ).
  • the source path and the compiled path are parsed from the environment variable.
  • a source code path such as a Java code path, a Class file path, and a Jar package file path is provided, and a compilation path of the Java code is provided, where the path information needs to be parsed therefrom.
  • an absolute path of the source file of the target Java project is determined according to the source code path and the compilation path.
  • the class file in the absolute path is loaded into the virtual machine memory through the Classloader, so as to be used for subsequent lookup.
  • the Classloader is a class loader for loading Java classes into the Java virtual machine. Unlike ordinary programs, Java class files are not native executables. When running a Java program, run the Java virtual machine first, then load the class file into the virtual machine.
  • Step S102 Find a first method corresponding to the specified URL from the class file in the absolute path.
  • the user may input one or more URLs in advance.
  • multiple URLs may be processed simultaneously to generate corresponding multiple call relationship diagrams.
  • the following descriptions are performed by using one URL.
  • the case of the URL can be pushed by analogy.
  • the corresponding method may be found in the class file pabrBaseSetController.java in the absolute path, that is, the first method: pabrBaseSetController°
  • Step S103 searching for a call to the second method in the method body of the first method.
  • the second method is a method different from the first method.
  • the second method is a method for injecting attributes by spring marked by @Au towired. If the calling of the second method is found in the method body of the first method, step S104 to step S106 are performed, if the second method is not found in the method body of the first method If the call is made, step S107 is performed.
  • Step S104 searching for the second method in a class file.
  • Step S105 Record a calling relationship between the first method and the second method.
  • Step S106 determining the second method as a new first method.
  • step S103 After determining the new first method, it is necessary to return to step S103, that is, to find the call of the second method in the method body of the new first method. It is easy to understand that this is a recursive process that finds the calls to the next level of methods one level at a time.
  • a call to the new method is found in the method body of the first method pabrBaseSetController. If a call to the second method pabrBaseSetWsa is found in the method body of the first method pabrBaseSetController, the class file corresponding thereto is first determined. The file path, and then get the class file pabrBaseSetWsa.java under the path, and finally find the second method pabrBaseSetWsa in the class file, recording the first method pabrBaseSetController called the second method pabrBaseSetWsa.
  • the call to the new method is found. If the call to the third method pabrBaseSetService can be found in the method body of the second method pabrBaseSetWsa, the method of the third method is first determined. Name the file path of the corresponding class file, and then obtain the class file pabrBaseSetService.java under the path, and finally find the third method pa brBaseSetService in the class file, record the second method pabrBaseSetWsa calls the third method pabrBaseSetSe rvice°
  • the method may further include:
  • the class to which the current method belongs is a subclass of the database operation class provided by the framework, the call to the parameter of the string type is found in the method body of the current method;
  • the string is The parameter of the type is determined to be the database identifier corresponding to the current method.
  • the class to which the current method pabrBaseSetDao belongs is a subclass of the database operation class provided by the framework
  • the method body of the current method pabrBaseSetDao Find the call to the parameter of the string type. If the call to the parameter "GETPABRCRITICALISSUELIST" of the string type is found in its method body, the database identifier corresponding to the current method pabrBaseSetDao can be determined, that is, SQL.
  • the ID is "GETPABRCRITICALISSUELIST,,.
  • Step S107 ending the code analysis, and summarizing the recorded call relationship.
  • Method pabrBaseSetController calls the method pabrBaseSetWsa
  • the method pabrBaseSetWsa calls the method pabrBaseSetService
  • Method pabrBaseSetService calls the method pabrBaseSetDao
  • pabrBaseSetController is a network layer method
  • pabrBaseSetWsa and pabrBaseSetSer vice are application layer methods
  • pabrBaseSetDao is a corresponding database layer method
  • Step S108 Draw a corresponding call relationship graph according to the summarized call relationship.
  • PlantUML for graphic drawing, it is generally scripted manually, and for each graphic, the corresponding PlantUML script is written, which is obviously laborious and laborious, and Error-prone.
  • a new scripting method is adopted, that is, a general PlantUML script template for drawing a call relationship diagram is set in advance, and the script template is formally related to a normal drawing call relationship diagram.
  • the PlantUML script is no different, but it leaves up the location of the script that fills in each specific call relationship. Record and mark the locations of these scripts that fill the call relationship, for example, record them as: script location 1, script location 2, script location 3, script location 4, etc.
  • the number of these script locations can be According to the actual situation, it is guaranteed that it can accommodate the complete calling relationship in the code. For example, if you know that the calling relationship of a Java code is within 10 layers, you can set the number of script positions to 10
  • the call relationships summarized in step S107 are also numbered, for example, the call relationship 1 is p abrBaseSetController--l>pabrBaseSetWsa (ie, the method pabrBaseSetController calls the method pabrBaseSetWsa), and the call relationship 2 is pabrBaseSetWsa-pabrBaseSetService ( That is, the method p abrBaseSetWsa calls the method pabrBaseSetService) , the calling relationship 3 is pabrBaseSetService --bpabrBaseSetDao (that is, the method pabrBaseSetService calls the method pabrBaseSetDao), and the calling relationship 4 is pabrBaseSetDao — l>SQL
  • GETPABRCRITICALISSUELIST ie the SQL ID corresponding to the method pabrBaseSetDao is "GETPABRCRITICALISSUELIST"
  • these call relationships are respectively filled in the corresponding script positions, and the script pabrBaseSetController-pabrBaseSetWsa calling the relationship 1 is filled in the script position 1, and the script pabrBaseSetWsa pabrBaseSetService calling the relationship 2 is filled in.
  • the script position 2 and so on, until all the call relationship scripts are filled into the corresponding script position, the script position remaining in the script template is removed, and the drawing script of the call relationship graph is obtained, and the operation script is executed.
  • the call graph can be generated by generating a script.
  • FIG. 3 is a call relationship diagram generated according to a preset script template
  • a call relationship diagram of different presentation modes can be generated by using different script templates
  • FIG. 3 Only one of the effects is indicated.
  • the user can select an appropriate script template according to the actual situation, which is not specifically limited in this embodiment.
  • the embodiment of the present application first obtains an absolute path of a source file of a target Java project, and searches for a first method corresponding to the specified URL from the class file in the absolute path, and then passes Recursive Find the call to the next level method layer by layer until there is no next level method, and finally summarize all the call relationships recorded and draw the corresponding call relationship graph.
  • the issuer only needs to specify the URL to be analyzed, and then automatically completes the analysis of the call relationship and draws the call relationship diagram, which greatly improves the analysis efficiency, and avoids the occurrence of human error, effectively reducing the error rate.
  • FIG. 4 is a schematic block diagram of the code analysis terminal device provided by the embodiment of the present application. For the convenience of description, only the related to the embodiment of the present application is shown. section.
  • the code analysis terminal device may be a computing device such as a desktop computer, a notebook, a palmtop computer, or a cloud server.
  • the code analysis terminal device can include: a processor 40, a memory 41, and computer readable instructions 42 stored in the memory 41 and executable on the processor 40.
  • the processor 40 may be a central processing unit (CPU), or may be another general-purpose processor, a digital signal processor (DSP), or an application specific integrated circuit (Application Specific Integrated Circuit, ASIC), field programmable gate array
  • CPU central processing unit
  • DSP digital signal processor
  • ASIC Application Specific Integrated Circuit
  • the general purpose processor may be a microprocessor or the processor or any conventional processor or the like.
  • the memory 41 may be an internal storage unit of the code analysis terminal device 4, such as a hard disk or a memory of the code analysis terminal device 4.
  • the memory 41 may also be an external storage device of the code analysis terminal device 4, such as a plug-in hard disk equipped with the code analysis terminal device 4, a smart memory card (SMC), and a secure digital (Secure) Digital, SD) cards, flash cards, etc.
  • the memory 41 may also include both an internal storage unit of the code analysis terminal device 4 and an external storage device.
  • the memory 41 is for storing the computer readable instructions and other programs and data required by the code analysis terminal device 4.
  • the memory 41 can also be used to temporarily store data that has been output or is about to be output.
  • FIG. 5 is a functional block diagram of computer readable instructions 42 provided by an embodiment of the present application.
  • the computer readable instructions 42 may be partitioned into one or more modules, the one or more modules being stored in the memory 41 and executed by the processor 40 to complete This application.
  • the computer readable instructions 42 may be divided into a path acquisition module 501, a first method lookup module 502, a call lookup module 503, a second method lookup module 504, a call relationship record module 505, A method update module 506, a call relationship summary module 507, and a call relationship graph drawing module 508.
  • a module as referred to in this application refers to a series of computer readable instruction instructions that are capable of performing a particular function, and are more suitable than the program to describe the execution of the computer readable instructions 42 in the code analysis terminal. The following description will specifically describe the functions of the modules 501-508.
  • the path obtaining module 501 is configured to obtain an absolute path of the source file of the target Java project.
  • the first method searching module 502 is configured to find a first method corresponding to the specified UR L from the class file in the absolute path.
  • the calling search module 503 is configured to search for a call to the second method in the method body of the first method, where the second method is a method different from the first method;
  • the second method searching module 504 is configured to: if the calling of the second method is found in the method body of the first method, searching for the second method in a class file;
  • the call relationship record module 505 is configured to: if the second method is found in the class file, record a call relationship between the first method and the second method;
  • the first method update module 506 is configured to determine the second method as a new first method, and return to perform the step of searching for a call to the second method in the method body of the first method. ;
  • the call relationship summary module 507 is configured to: if the call to the second method is not found in the method body of the first method, end code analysis, and summarize the recorded call relationship;
  • the call graph drawing module 508 is configured to draw a corresponding call relationship graph according to the summarized call relationship.
  • the path obtaining module 501 may include:
  • an environment variable obtaining unit configured to acquire an environment variable of the target Java project
  • a parsing unit configured to parse the source path and the compile path from the environment variable
  • an absolute path determining unit configured to determine an absolute path of the source file of the target Ja va project according to the source path and the compiled path.
  • the second method searching module 504 may include:
  • a file path determining unit configured to determine a file path of the class file corresponding to the method name of the second method
  • a class file obtaining unit configured to acquire a class file in the file path
  • the second method searching unit is configured to search the second method in the class file.
  • the call graph drawing module 508 may include:
  • a drawing script generating unit configured to import the summarized calling relationship into a preset PlantUML script template, and generate a drawing script of the calling relationship graph;
  • a call relationship generating unit configured to run the drawing script, and generate the calling relationship diagram corresponding to the summarized calling relationship.
  • the computer readable instructions 42 may further include:
  • the parameter calling the search module is configured to: if the class to which the first method belongs is a subclass of the database operation class provided by the framework, look for a call to the parameter of the string type in the method body of the first method. ;
  • a database identifier determining module configured to: if a call to a parameter of a string type is found in a method body of the first method, determine a parameter of the string type to correspond to the first method Database ID.
  • modules, units, and/or method steps of various embodiments described in connection with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. achieve. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the solution. A person skilled in the art can use different methods for implementing the described functions for each particular application, but such implementation should not be considered to be beyond the scope of the present application.
  • the disclosed system, apparatus, and method may be implemented in other manners.
  • the device embodiments described above are merely illustrative,
  • the division of the unit is only a logical function division, and the actual implementation may have another division manner, for example, multiple units or components may be combined or may be integrated into another system, or some features may be ignored, or not. carried out.
  • the mutual coupling or direct coupling or communication connection shown or discussed may be an indirect coupling or communication connection through some interface, device or unit, and may be in an electrical, mechanical or other form.
  • the unit described as a separate component may or may not be physically distributed, and the component displayed as a unit may or may not be a physical unit, that is, may be located in one place, or may be distributed to multiple On the network unit. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of the embodiment.
  • each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.
  • the above integrated unit can be implemented in the form of hardware or in the form of a software functional unit.
  • the integrated unit if implemented in the form of a software functional unit and sold or used as a standalone product, may be stored in a computer readable storage medium.
  • a computer readable storage medium A number of instructions are included to cause a computer device (which may be a personal computer, server, or network device, etc.) to perform all or part of the steps of the methods described in various embodiments of the present application.
  • the foregoing storage medium includes: a U disk, a removable hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, and the like, which can store program codes. .

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Computer Hardware Design (AREA)
  • Quality & Reliability (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Software Systems (AREA)
  • Stored Programmes (AREA)

Abstract

一种代码分析方法及终端设备,所述方法首先获取目标Java工程的源文件的绝对路径(S101),并从所述绝对路径下的类文件中查找出与指定的URL对应的第一方法(S102),然后通过递归,一层层地查找出对下一级方法的调用,直至不存在下一级方法为止,最后将记录的所有调用关系进行汇总并绘制出对应的调用关系图。开发人员仅需指定需要进行分析的URL,即可自动完成调用关系的分析并绘制出调用关系图,极大提高了分析效率,而且规避了人为失误的发生,有效降低了错误率。

Description

一种代码分析方法及终端设备
[0001] 本申请申明享有 2017年 8月 9日递交的申请号为 201710675458.0、 名称为"一种代 码分析方法及终端设备"中国专利申请的优先权, 该中国专利申请的整体内容以 参考的方式结合在本申请中。
技术领域
[0002] 本申请属于计算机技术领域, 尤其涉及一种代码分析方法及终端设备。
背景技术
[0003] 随着软件系统的不断地向着复杂化的方向发展, 一般的软件系统的代码量也越 来越大, 很难直观的了解到该软件系统代码的调用关系, 在对代码进行修改维 护以及测试吋, 通常需要借助于代码的调用关系图来辅助相关技术人员进行理 解, 但目前一般由幵发人员对调用关系进行手工梳理, 效率低下且错误率高。 技术问题
[0004] 有鉴于此, 本申请实施例提供了一种代码分析方法及终端设备, 以解决现有的 由幵发人员对调用关系进行手工梳理, 效率低下且错误率高的问题。
问题的解决方案
技术解决方案
[0005] 本申请实施例的第一方面提供了一种代码分析方法, 可以包括:
[0006] 获取目标 Java工程的源文件的绝对路径;
[0007] 从所述绝对路径下的类文件中査找出与指定的 URL对应的第一方法;
[0008] 在所述第一方法的方法体中査找对第二方法的调用, 所述第二方法为与所述第 一方法不同的方法;
[0009] 若在所述第一方法的方法体中査找到对所述第二方法的调用, 且在类文件中査 找到所述第二方法, 则记录所述第一方法与所述第二方法的调用关系;
[0010] 在所述第二方法的方法体中査找对第三方法的调用, 所述第三方法为与所述第 一、 第二方法不同的方法;
[0011] 若在所述第二方法的方法体中査找到对所述第三方法的调用, 且在类文件中査 找到所述第三方法, 则记录所述第二方法与所述第三方法的调用关系;
[0012] 重复上述流程, 直到当前方法中无法找到对新方法的调用吋, 则结束代码分析
, 并汇总记录的所述调用关系;
[0013] 根据汇总的所述调用关系绘制出对应的调用关系图。
[0014] 本申请实施例的第二方面提供了一种计算机可读存储介质, 所述计算机可读存 储介质存储有计算机可读指令, 所述计算机可读指令被处理器执行吋实现如下 步骤:
[0015] 获取目标 Java工程的源文件的绝对路径;
[0016] 从所述绝对路径下的类文件中査找出与指定的 URL对应的第一方法;
[0017] 在所述第一方法的方法体中査找对第二方法的调用, 所述第二方法为与所述第 一方法不同的方法;
[0018] 若在所述第一方法的方法体中査找到对所述第二方法的调用, 且在类文件中査 找到所述第二方法, 则记录所述第一方法与所述第二方法的调用关系;
[0019] 在所述第二方法的方法体中査找对第三方法的调用, 所述第三方法为与所述第 一、 第二方法不同的方法;
[0020] 若在所述第二方法的方法体中査找到对所述第三方法的调用, 且在类文件中査 找到所述第三方法, 则记录所述第二方法与所述第三方法的调用关系;
[0021] 重复上述流程, 直到当前方法中无法找到对新方法的调用吋, 则结束代码分析
, 并汇总记录的所述调用关系;
[0022] 根据汇总的所述调用关系绘制出对应的调用关系图。
[0023] 本申请实施例的第三方面提供了一种代码分析终端设备, 包括存储器、 处理器 以及存储在所述存储器中并可在所述处理器上运行的计算机可读指令, 所述处 理器执行所述计算机可读指令吋实现如下步骤:
[0024] 获取目标 Java工程的源文件的绝对路径;
[0025] 从所述绝对路径下的类文件中査找出与指定的 URL对应的第一方法;
[0026] 在所述第一方法的方法体中査找对第二方法的调用, 所述第二方法为与所述第 一方法不同的方法;
[0027] 若在所述第一方法的方法体中査找到对所述第二方法的调用, 且在类文件中査 找到所述第二方法, 则记录所述第一方法与所述第二方法的调用关系;
[0028] 在所述第二方法的方法体中査找对第三方法的调用, 所述第三方法为与所述第 一、 第二方法不同的方法;
[0029] 若在所述第二方法的方法体中査找到对所述第三方法的调用, 且在类文件中査 找到所述第三方法, 则记录所述第二方法与所述第三方法的调用关系;
[0030] 重复上述流程, 直到当前方法中无法找到对新方法的调用吋, 则结束代码分析
, 并汇总记录的所述调用关系;
[0031 ] 根据汇总的所述调用关系绘制出对应的调用关系图。
[0032] 本申请实施例的第四方面提供了一种计算机可读指令, 所述计算机可读指令应 用于一种代码分析终端设备, 所述代码分析终端设备包括存储器、 处理器以及 存储在所述存储器中并可在所述处理器上运行的所述计算机可读指令, 所述计 算机可读指令包括以下模块:
[0033] 路径获取模块, 用于获取目标 Java工程的源文件的绝对路径;
[0034] 第一方法査找模块, 用于从所述绝对路径下的类文件中査找出与指定的 URL对 应的第一方法;
[0035] 调用査找模块, 用于在所述第一方法的方法体中査找对第二方法的调用, 所述 第二方法为与所述第一方法不同的方法;
[0036] 第二方法査找模块, 用于若在所述第一方法的方法体中査找到对所述第二方法 的调用, 则在类文件中査找所述第二方法;
[0037] 调用关系记录模块, 用于若在类文件中査找到所述第二方法, 则记录所述第一 方法与所述第二方法的调用关系;
[0038] 第一方法更新模块, 用于将所述第二方法确定为新的第一方法, 并返回执行所 述在所述第一方法的方法体中査找对第二方法的调用的步骤;
[0039] 调用关系汇总模块, 用于若在所述第一方法的方法体中未査找到对所述第二方 法的调用, 则结束代码分析, 并汇总记录的所述调用关系;
[0040] 调用关系图绘制模块, 用于根据汇总的所述调用关系绘制出对应的调用关系图 发明的有益效果 有益效果
[0041] 本申请实施例与现有技术相比存在的有益效果是: 本申请实施例首先获取目标 Java工程的源文件的绝对路径, 并从所述绝对路径下的类文件中査找出与指定的 URL对应的第一方法, 然后通过递归, 一层层地査找出对下一级方法的调用, 直至不存在下一级方法为止, 最后将记录的所有调用关系进行汇总并绘制出对 应的调用关系图。 幵发人员仅需指定需要进行分析的 URL, 即可自动完成调用 关系的分析并绘制出调用关系图, 极大提高了分析效率, 而且规避了人为失误 的发生, 有效降低了错误率。
对附图的简要说明
附图说明
[0042] 为了更清楚地说明本申请实施例中的技术方案, 下面将对实施例或现有技术描 述中所需要使用的附图作简单地介绍, 显而易见地, 下面描述中的附图仅仅是 本申请的一些实施例, 对于本领域普通技术人员来讲, 在不付出创造性劳动性 的前提下, 还可以根据这些附图获得其他的附图。
[0043] 图 1为本申请实施例中一种代码分析方法的一个实施例流程图;
[0044] 图 2为本申请实施例中一种代码分析方法的根据脚本模板生成绘制脚本的示意 图;
[0045] 图 3为本申请实施例中一种代码分析方法所生成的调用关系图的效果示意; [0046] 图 4为本申请实施例提供的代码分析终端设备的示意框图;
[0047] 图 5为本申请实施例提供的代码分析程序的功能模块图。
本发明的实施方式
[0048] 为使得本申请的发明目的、 特征、 优点能够更加的明显和易懂, 下面将结合本 申请实施例中的附图, 对本申请实施例中的技术方案进行清楚、 完整地描述, 显然, 下面所描述的实施例仅仅是本申请一部分实施例, 而非全部的实施例。 基于本申请中的实施例, 本领域普通技术人员在没有做出创造性劳动前提下所 获得的所有其它实施例, 都属于本申请保护的范围。
[0049] 本方面实施例主要是对 Java代码进行分析并生成调用关系图, 在叙述过程中, 涉及了许多与 Java代码相关的专业术语, 为了便于理解, 在此首先对这些专业术 语进行简单的解释。
[0050] 绝对路径: 完整的描述文件位置的路径就是绝对路径, 也就是从树型目录结构 顶部的根目录幵始到某个目录或文件的路径, 由一系列连续的目录组成, 中间 用斜线分隔, 直到要指定的目录或文件, 路径中的最后一个名称即为要指向的 目录或文件, 例如 C:\windows\system32\cmcLexe。 与之对应的, 相对路径是从当 前路径幵始的路径, 如当前路径为 C:\windows, 使用相对路径描述上述路径应为 .\system32\cmd.exe。
[0051] URL: Uniform Resource Locator, 即统一资源定位符。 URL是对可以从互联网 上得到的资源的位置和访问方法的一种简洁的表示, 是互联网上标准资源的地 址。
[0052] 方法: 在 Java代码中, 方法是一组为了实现特定功能的代码块的集合, 其作用 类似于 C代码中的函数。
[0053] 类: 在 Java代码中, 类 (Class) 是具有相同特性和行为的对象集合, 通俗的说
, 类就是创建 Java对象的模板。 对象是类的一个实例, 有状态和行为。 例如, 一 条狗是一个对象, 它的状态有: 颜色、 名字、 品种; 行为有: 摇尾巴、 叫、 吃 等。
[0054] 数据库操作类: 系统提供的对数据库进行操作的类, 可以访问各种不同类型的 数据库, 进行建立数据库连接、 执行 SQL语句进行数据的存取操作等。
[0055] PlantUML: PlantUML是一个快速创建统一建模语言 (Unified Modeling
Language, UML) 图形的组件, 其中 UML是面向对象软件幵发中的一种通用、 统一的图形模型语言, 是用于软件系统规约化、 可视化构造和建模的有效工具
[0056] 请参阅图 1, 本申请实施例中一种代码分析方法的一个实施例可以包括:
[0057] 步骤 S101, 获取目标 Java工程的源文件的绝对路径。
[0058] 首先, 获取所述目标 Java工程的环境变量, 也即 ClasSpath。
[0059] 在 Java中, Classpath的作用, 在于告诉 Java执行环境, 在哪些目录下可以找到 所要执行的 Java程序所需要的类或者包。 这里可以将 Java执行环境与操作系统进 行类比, 如果设置路径变量是为了让操作系统找到指定的工具程序 (以 Windows 来说就是找到 .exe文件), 那么设置 Classpath的目的就是让 Java执行环境找到指定 的 Java程序 (也就是. class文件)。
[0060] 然后, 从所述环境变量中解析出源码路径和编译路径。
[0061] 在所述 Classpath中, 提供了 Java代码路径、 Class文件路径以及 Jar包文件路径等 源码路径, 还提供了 Java代码的编译路径, 此处需要从中解析出这些路径信息。
[0062] 最后, 根据所述源码路径和所述编译路径确定出所述目标 Java工程的源文件的 绝对路径。
[0063] 优选地, 在本实施例中, 将所述绝对路径下的类文件通过 Classloader加载到虚 拟机内存中, 以备后续査找使用。 所述 Classloader为类加载器, 用来加载 Java类 到 Java虚拟机中。 与普通程序不同, Java程序的类文件并不是本地的可执行程序 。 当运行 Java程序吋, 首先运行 Java虚拟机, 然后再把类文件加载到虚拟机里运 行。
[0064] 步骤 S102, 从所述绝对路径下的类文件中査找出与指定的 URL对应的第一方法
[0065] 用户可以预先输入一个或多个 URL, 本实施中可以同吋对多个 URL进行处理并 生成对应的多个调用关系图, 为了简便起见, 以下均以一个 URL来进行说明, 多个 URL的情况据此类推即可。
[0066] 例如, 若所述指定的 URL为: /do/pabr/pabrBaseSetController, 则可在所述绝对 路径下的类文件 pabrBaseSetController.java中査找出对应的方法, 也即所述第一 方法: pabrBaseSetController°
[0067] 步骤 S103, 在所述第一方法的方法体中査找对第二方法的调用。
[0068] 所述第二方法为与所述第一方法不同的方法, 优选地, 所述第二方法为由 @Au towired标注的 spring注入属性的方法。 若在所述第一方法的方法体中査找到对所 述第二方法的调用, 则执行步骤 S104至步骤 S106, 若在所述第一方法的方法体 中未査找到对所述第二方法的调用, 则执行步骤 S107。
[0069] 步骤 S104, 在类文件中査找所述第二方法。
[0070] 具体地, 首先确定与所述第二方法的方法名对应的类文件的文件路径, 然后获 取所述文件路径下的类文件, 最后在所述类文件中査找所述第二方法。
[0071] 步骤 S105, 记录所述第一方法与所述第二方法的调用关系。
[0072] 步骤 S106, 将所述第二方法确定为新的第一方法。
[0073] 在确定出了新的第一方法后, 则需要返回执行步骤 S103, 也即在新的第一方法 的方法体中査找到第二方法的调用。 容易理解地, 这是一个递归的过程, 即一 层层地査找出对下一级方法的调用。
[0074] 例如, 首先在第一方法 pabrBaseSetController的方法体中査找对新方法的调用, 若在第一方法 pabrBaseSetController的方法体中査找到对第二方法 pabrBaseSetWsa 的调用, 则首先确定与其对应的类文件的文件路径, 然后获取到该路径下的类 文件 pabrBaseSetWsa.java, 最后在所述类文件中査找到第二方法 pabrBaseSetWsa , 记录下第一方法 pabrBaseSetController调用了第二方法 pabrBaseSetWsa。
[0075] 然后在第二方法 pabrBaseSetWsa的方法体中査找对新方法的调用, 若在第二方 法 pabrBaseSetWsa的方法体中可以査找到对第三方法 pabrBaseSetService的调用, 则首先确定与第三方法的方法名对应的类文件的文件路径, 然后获取到该路径 下的类文件 pabrBaseSetService.java, 最后在所述类文件中査找到所述第三方法 pa brBaseSetService, 记录下第二方法 pabrBaseSetWsa调用了第三方法 pabrBaseSetSe rvice°
[0076] 继续进行递归过程, 在第三方法 pabrBaseSetService方法体中査找对新方法的调 用, 若在第三方法 pabrBaseSetService的方法体中可以査找到对第四方法 pabrBase SetDao的调用, 则首先确定与第四方法的方法名对应的类文件的文件路径, 然后 获取到该路径下的类文件 pabrBaseSetDao.java, 最后在所述类文件中査找到所述 第四方法 pabrBaseSetDao。 记录下第三方法 pabrBaseSetService调用了第四方法 pa brBaseSetDao。
[0077] 重复上述流程, 直到当前方法中无法找到对新方法的调用为止。
[0078] 优选地, 在执行步骤 S107之前, 还可以包括:
[0079] 若当前方法所属的类为框架提供的数据库操作类的子类, 则在当前方法的方法 体中査找对字符串类型的参数的调用;
[0080] 若在当前方法的方法体中査找到对字符串类型的参数的调用, 则将所述字符串 类型的参数确定为与当前方法对应的数据库标识。
[0081] 例如, 若在当前方法 pabrBaseSetDao的方法体中未査找到对新方法的调用, 但 当前方法 pabrBaseSetDao所属的类为框架提供的数据库操作类的子类, 则在当前 方法 pabrBaseSetDao的方法体中査找对字符串类型的参数的调用, 若在其方法体 中査找到对字符串类型的参数" GETPABRCRITICALISSUELIST"的调用, 则可确 定当前方法 pabrBaseSetDao对应的数据库标识, 也即 SQL
ID为" GETPABRCRITICALISSUELIST,,。
[0082] 步骤 S107, 结束代码分析, 并汇总记录的所述调用关系。
[0083] 例如, 在上述的例子中, 可以汇总出以下调用关系:
[0084] 方法 pabrBaseSetController调用了方法 pabrBaseSetWsa;
[0085] 方法 pabrBaseSetWsa调用了方法 pabrBaseSetService;
[0086] 方法 pabrBaseSetService调用了方法 pabrBaseSetDao;
[0087] 以及方法 pabrBaseSetDao对应的 SQL ID为" GETPABRCRITICALISSUELIST"。
[0088] 其中, pabrBaseSetController为网络层的方法, pabrBaseSetWsa和 pabrBaseSetSer vice为应用层的方法, 而 pabrBaseSetDao为对应的数据库层的方法, 可见汇总后 的调用关系的顺序依次为网络层 (WEB层) 、 应用层 (APP层) 以及数据库层
[0089] 步骤 S108, 根据汇总的所述调用关系绘制出对应的调用关系图。
[0090] 首先, 将汇总的所述调用关系导入到预设的 PlantUML脚本模板中, 生成所述 调用关系图的绘制脚本;
[0091] 然后, 运行所述绘制脚本, 生成与汇总的所述调用关系对应的所述调用关系图
[0092] 需要注意的是, 现有技术中在使用 PlantUML进行图形绘制吋, 一般都是人工 编写脚本的, 对每一个图形, 都要编写对应的 PlantUML脚本, 这种方式显然费 吋费力, 且容易出错。
[0093] 在本实施例中, 采用了一种新的脚本编写方式, 即预先设置一个通用的用于绘 制调用关系图的 PlantUML脚本模板, 该脚本模板从形式上来看与普通的绘制调 用关系图的 PlantUML脚本无异, 但空出了填写每个具体调用关系的脚本位置, 对这些填写调用关系的脚本的位置进行记录并对其进行编号, 例如, 将其分别 记为: 脚本位置 1、 脚本位置 2、 脚本位置 3、 脚本位置 4等等, 这些脚本位置的 个数可以根据实际情况来定, 保证其可以容纳代码中完整的调用关系。 例如, 若已知某 Java代码的调用关系都在 10层以内, 则可以将脚本位置的个数设置为 10
[0094] 然后, 将在步骤 S107中汇总得到的调用关系也进行编号, 例如, 调用关系 1为 p abrBaseSetController--l>pabrBaseSetWsa (即方法 pabrBaseSetController调用了方法 pabrBaseSetWsa) , 调用关系 2为 pabrBaseSetWsa— pabrBaseSetService (即方法 p abrBaseSetWsa调用了方法 pabrBaseSetService) , 调用关系 3为 pabrBaseSetService --bpabrBaseSetDao (即方法 pabrBaseSetService调用了方法 pabrBaseSetDao) , 调 用关系 4为 pabrBaseSetDao— l>SQL
ID: GETPABRCRITICALISSUELIST (即方法 pabrBaseSetDao对应的 SQL ID为" GETPABRCRITICALISSUELIST") 。
[0095] 最后, 如图 2所示, 将这些调用关系分别填入对应的脚本位置中, 即将调用关 系 1的脚本 pabrBaseSetController- pabrBaseSetWsa填入脚本位置 1中, 将调用关 系 2的脚本 pabrBaseSetWsa pabrBaseSetService填入脚本位置 2中, 以此类推, 直至将所有的调用关系脚本均填入到对应的脚本位置中, 剔除掉脚本模板中剩 余的脚本位置, 即可得到所述调用关系图的绘制脚本, 运行该绘制脚本, 就可 以生成所述调用关系图。
[0096] 如图 3所示, 即为根据预置的一种脚本模板生成的一种调用关系图, 需要注意 的是, 通过使用不同的脚本模板可以生成不同呈现方式的调用关系图, 图 3仅为 其中的一种效果示意。 用户可以根据实际情况选择合适的脚本模板, 本实施例 对此不作具体限定。
[0097] 以上叙述均为以对一个 URL进行处理来进行说明, 若用户预先输入了多个 URL , 则可以分别对多个 URL进行并行或者串行处理并生成对应的多个调用关系图 , 具体过程与以上叙述相同, 根据以上过程类推即可。
[0098] 综上所述, 本申请实施例首先获取目标 Java工程的源文件的绝对路径, 并从所 述绝对路径下的类文件中査找出与指定的 URL对应的第一方法, 然后通过递归 , 一层层地査找出对下一级方法的调用, 直至不存在下一级方法为止, 最后将 记录的所有调用关系进行汇总并绘制出对应的调用关系图。 幵发人员仅需指定 需要进行分析的 URL, 即可自动完成调用关系的分析并绘制出调用关系图, 极 大提高了分析效率, 而且规避了人为失误的发生, 有效降低了错误率。 应理解 , 上述实施例中各步骤的序号的大小并不意味着执行顺序的先后, 各过程的执 行顺序应以其功能和内在逻辑确定, 而不应对本申请实施例的实施过程构成任 何限定。
[0099] 对应于上文实施例所述的代码分析方法, 图 4示出了本申请实施例提供的代码 分析终端设备的示意框图, 为了便于说明, 仅示出了与本申请实施例相关的部 分。
[0100] 在本实施例中, 所述代码分析终端设备可以是桌上型计算机、 笔记本、 掌上电 脑及云端服务器等计算设备。 该代码分析终端设备可包括: 处理器 40、 存储器 4 1以及存储在所述存储器 41中并可在所述处理器 40上运行的计算机可读指令 42。
[0101] 所述处理器 40可以是中央处理单元 (Central Processing Unit, CPU) , 还可以是其 他通用处理器、 数字信号处理器(Digital Signal Processor, DSP)、 专用集成电路 (Application Specific Integrated Circuit, ASIC)、 现场可编程门阵列
(Field-Programmable Gate Array , FPGA)或者其他可编程逻辑器件、 分立门或者 晶体管逻辑器件、 分立硬件组件等。 通用处理器可以是微处理器或者该处理器 也可以是任何常规的处理器等。
[0102] 所述存储器 41可以是所述代码分析终端设备 4的内部存储单元, 例如代码分析 终端设备 4的硬盘或内存。 所述存储器 41也可以是所述代码分析终端设备 4的外 部存储设备, 例如所述代码分析终端设备 4上配备的插接式硬盘, 智能存储卡 ( Smart Media Card, SMC) , 安全数字 (Secure Digital, SD) 卡, 闪存卡 (Flash Card) 等。 进一步地, 所述存储器 41还可以既包括所述代码分析终端设备 4的内 部存储单元也包括外部存储设备。 所述存储器 41用于存储所述计算机可读指令 以及所述代码分析终端设备 4所需的其它程序和数据。 所述存储器 41还可以用于 暂吋地存储已经输出或者将要输出的数据。
[0103] 请参阅图 5, 是本申请实施例提供的计算机可读指令 42的功能模块图。 在本实 施例中, 所述的计算机可读指令 42可以被分割成一个或多个模块, 所述一个或 者多个模块被存储于所述存储器 41中, 并由所述处理器 40所执行, 以完成本申 请。 例如, 在图 5中, 所述的计算机可读指令 42可以被分割成路径获取模块 501 、 第一方法査找模块 502、 调用査找模块 503、 第二方法査找模块 504、 调用关系 记录模块 505、 第一方法更新模块 506、 调用关系汇总模块 507和调用关系图绘制 模块 508。 本申请所称的模块是指能够完成特定功能的一系列计算机可读指令指 令段, 比程序更适合于描述所述计算机可读指令 42在所述代码分析终端设备中 的执行过程。 以下描述将具体介绍所述模块 501-508的功能。
[0104] 路径获取模块 501, 用于获取目标 Java工程的源文件的绝对路径;
[0105] 第一方法査找模块 502, 用于从所述绝对路径下的类文件中査找出与指定的 UR L对应的第一方法;
[0106] 调用査找模块 503, 用于在所述第一方法的方法体中査找对第二方法的调用, 所述第二方法为与所述第一方法不同的方法;
[0107] 第二方法査找模块 504, 用于若在所述第一方法的方法体中査找到对所述第二 方法的调用, 则在类文件中査找所述第二方法;
[0108] 调用关系记录模块 505, 用于若在类文件中査找到所述第二方法, 则记录所述 第一方法与所述第二方法的调用关系;
[0109] 第一方法更新模块 506, 用于将所述第二方法确定为新的第一方法, 并返回执 行所述在所述第一方法的方法体中査找对第二方法的调用的步骤;
[0110] 调用关系汇总模块 507, 用于若在所述第一方法的方法体中未査找到对所述第 二方法的调用, 则结束代码分析, 并汇总记录的所述调用关系;
[0111] 调用关系图绘制模块 508, 用于根据汇总的所述调用关系绘制出对应的调用关 系图。
[0112] 进一步地, 所述路径获取模块 501可以包括:
[0113] 环境变量获取单元, 用于获取所述目标 Java工程的环境变量;
[0114] 解析单元, 用于从所述环境变量中解析出源码路径和编译路径;
[0115] 绝对路径确定单元, 用于根据所述源码路径和所述编译路径确定出所述目标 Ja va工程的源文件的绝对路径。 [0116] 进一步地, 所述第二方法査找模块 504可以包括:
[0117] 文件路径确定单元, 用于确定与所述第二方法的方法名对应的类文件的文件路 径;
[0118] 类文件获取单元, 用于获取所述文件路径下的类文件;
[0119] 第二方法査找单元, 用于在所述类文件中査找所述第二方法。
[0120] 进一步地, 所述调用关系图绘制模块 508可以包括:
[0121] 绘制脚本生成单元, 用于将汇总的所述调用关系导入到预设的 PlantUML脚本 模板中, 生成所述调用关系图的绘制脚本;
[0122] 调用关系图生成单元, 用于运行所述绘制脚本, 生成与汇总的所述调用关系对 应的所述调用关系图。
[0123] 进一步地, 所述计算机可读指令 42中还可以包括:
[0124] 参数调用査找模块, 用于若所述第一方法所属的类为框架提供的数据库操作类 的子类, 则在所述第一方法的方法体中査找对字符串类型的参数的调用;
[0125] 数据库标识确定模块, 用于若在所述第一方法的方法体中査找到对字符串类型 的参数的调用, 则将所述字符串类型的参数确定为与所述第一方法对应的数据 库标识。
[0126] 所属领域的技术人员可以清楚地了解到, 为描述的方便和简洁, 上述描述的系 统, 装置和单元的具体工作过程, 可以参考前述方法实施例中的对应过程, 在 此不再赘述。
[0127] 在上述实施例中, 对各个实施例的描述都各有侧重, 某个实施例中没有详述或 记载的部分, 可以参见其它实施例的相关描述。
[0128] 本领域普通技术人员可以意识到, 结合本文中所公幵的实施例描述的各实施例 的模块、 单元和 /或方法步骤, 能够以电子硬件、 或者计算机软件和电子硬件的 结合来实现。 这些功能究竟以硬件还是软件方式来执行, 取决于技术方案的特 定应用和设计约束条件。 专业技术人员可以对每个特定的应用来使用不同方法 来实现所描述的功能, 但是这种实现不应认为超出本申请的范围。
[0129] 在本申请所提供的几个实施例中, 应该理解到, 所揭露的系统, 装置和方法, 可以通过其它的方式实现。 例如, 以上所描述的装置实施例仅仅是示意性的, 例如, 所述单元的划分, 仅仅为一种逻辑功能划分, 实际实现吋可以有另外的 划分方式, 例如多个单元或组件可以结合或者可以集成到另一个系统, 或一些 特征可以忽略, 或不执行。 另一点, 所显示或讨论的相互之间的耦合或直接耦 合或通信连接可以是通过一些接口, 装置或单元的间接耦合或通信连接, 可以 是电性, 机械或其它的形式。
[0130] 所述作为分离部件说明的单元可以是或者也可以不是物理上分幵的, 作为单元 显示的部件可以是或者也可以不是物理单元, 即可以位于一个地方, 或者也可 以分布到多个网络单元上。 可以根据实际的需要选择其中的部分或者全部单元 来实现本实施例方案的目的。
[0131] 另外, 在本申请各个实施例中的各功能单元可以集成在一个处理单元中, 也可 以是各个单元单独物理存在, 也可以两个或两个以上单元集成在一个单元中。 上述集成的单元既可以采用硬件的形式实现, 也可以采用软件功能单元的形式 实现。
[0132] 所述集成的单元如果以软件功能单元的形式实现并作为独立的产品销售或使用 吋, 可以存储在一个计算机可读取存储介质中。 基于这样的理解, 本申请的技 术方案本质上或者说对现有技术做出贡献的部分或者该技术方案的全部或部分 可以以软件产品的形式体现出来, 该计算机软件产品存储在一个存储介质中, 包括若干指令用以使得一台计算机设备 (可以是个人计算机, 服务器, 或者网 络设备等) 执行本申请各个实施例所述方法的全部或部分步骤。 而前述的存储 介质包括: U盘、 移动硬盘、 只读存储器 (ROM, Read-Only Memory) 、 随机 存取存储器 (RAM, Random Access Memory) 、 磁碟或者光盘等各种可以存储 程序代码的介质。
[0133] 以上所述, 以上实施例仅用以说明本申请的技术方案, 而非对其限制; 尽管参 照前述实施例对本申请进行了详细的说明, 本领域的普通技术人员应当理解: 其依然可以对前述各实施例所记载的技术方案进行修改, 或者对其中部分技术 特征进行等同替换; 而这些修改或者替换, 并不使相应技术方案的本质脱离本 申请各实施例技术方案的精神和范围。

Claims

权利要求书
[权利要求 1] 一种代码分析方法, 其特征在于, 包括:
获取目标 Java工程的源文件的绝对路径;
从所述绝对路径下的类文件中査找出与指定的 URL对应的第一方法; 在所述第一方法的方法体中査找对第二方法的调用, 所述第二方法为 与所述第一方法不同的方法;
若在所述第一方法的方法体中査找到对所述第二方法的调用, 且在类 文件中査找到所述第二方法, 则记录所述第一方法与所述第二方法的 调用关系;
在所述第二方法的方法体中査找对第三方法的调用, 所述第三方法为 与所述第一、 第二方法不同的方法;
若在所述第二方法的方法体中査找到对所述第三方法的调用, 且在类 文件中査找到所述第三方法, 则记录所述第二方法与所述第三方法的 调用关系;
重复上述流程, 直到当前方法中无法找到对新方法的调用吋, 则结束 代码分析, 并汇总记录的所述调用关系;
根据汇总的所述调用关系绘制出对应的调用关系图。
[权利要求 2] 根据权利要求 1所述的代码分析方法, 其特征在于, 所述获取目标 Jav a工程的源文件的绝对路径包括:
获取所述目标 Java工程的环境变量;
从所述环境变量中解析出源码路径和编译路径; 根据所述源码路径和所述编译路径确定出所述目标 Java工程的源文件 的绝对路径。
[权利要求 3] 根据权利要求 1所述的代码分析方法, 其特征在于, 所述在类文件中 査找所述第二方法包括:
确定与所述第二方法的方法名对应的类文件的文件路径;
获取所述文件路径下的类文件;
在所述类文件中査找所述第二方法。 [权利要求 4] 根据权利要求 1所述的代码分析方法, 其特征在于, 在结束代码分析 之前, 还包括:
若当前方法所属的类为框架提供的数据库操作类的子类, 则在当前方 法的方法体中査找对字符串类型的参数的调用; 若在当前方法的方法体中査找到对字符串类型的参数的调用, 则将所 述字符串类型的参数确定为与当前方法对应的数据库标识。
[权利要求 5] 根据权利要求 1至 4中任一项所述的代码分析方法, 所述根据汇总的所 述调用关系绘制出对应的调用关系图包括:
将汇总的所述调用关系导入到预设的 PlantUML脚本模板中, 生成所 述调用关系图的绘制脚本;
运行所述绘制脚本, 生成与汇总的所述调用关系对应的所述调用关系 图。
[权利要求 6] —种计算机可读存储介质, 所述计算机可读存储介质存储有计算机可 读指令, 其特征在于, 所述计算机可读指令被处理器执行吋实现如下 步骤:
获取目标 Java工程的源文件的绝对路径;
从所述绝对路径下的类文件中査找出与指定的 URL对应的第一方法; 在所述第一方法的方法体中査找对第二方法的调用, 所述第二方法为 与所述第一方法不同的方法;
若在所述第一方法的方法体中査找到对所述第二方法的调用, 且在类 文件中査找到所述第二方法, 则记录所述第一方法与所述第二方法的 调用关系;
在所述第二方法的方法体中査找对第三方法的调用, 所述第三方法为 与所述第一、 第二方法不同的方法;
若在所述第二方法的方法体中査找到对所述第三方法的调用, 且在类 文件中査找到所述第三方法, 则记录所述第二方法与所述第三方法的 调用关系;
重复上述流程, 直到当前方法中无法找到对新方法的调用吋, 则结束 代码分析, 并汇总记录的所述调用关系;
根据汇总的所述调用关系绘制出对应的调用关系图。
根据权利要求 6所述的计算机可读存储介质, 其特征在于, 所述获取 目标 Java工程的源文件的绝对路径包括:
获取所述目标 Java工程的环境变量;
从所述环境变量中解析出源码路径和编译路径;
根据所述源码路径和所述编译路径确定出所述目标 Java工程的源文件 的绝对路径。
根据权利要求 6所述的计算机可读存储介质, 其特征在于, 所述在类 文件中査找所述第二方法包括:
确定与所述第二方法的方法名对应的类文件的文件路径;
获取所述文件路径下的类文件;
在所述类文件中査找所述第二方法。
根据权利要求 6所述的计算机可读存储介质, 其特征在于, 在结束代 码分析之前, 还包括:
若当前方法所属的类为框架提供的数据库操作类的子类, 则在当前方 法的方法体中査找对字符串类型的参数的调用;
若在当前方法的方法体中査找到对字符串类型的参数的调用, 则将所 述字符串类型的参数确定为与当前方法对应的数据库标识。
根据权利要求 6至 9中任一项所述的计算机可读存储介质, 所述根据汇 总的所述调用关系绘制出对应的调用关系图包括:
将汇总的所述调用关系导入到预设的 PlantUML脚本模板中, 生成所 述调用关系图的绘制脚本;
运行所述绘制脚本, 生成与汇总的所述调用关系对应的所述调用关系 图。
一种代码分析终端设备, 包括存储器、 处理器以及存储在所述存储器 中并可在所述处理器上运行的计算机可读指令, 其特征在于, 所述处 理器执行所述计算机可读指令吋实现如下步骤: 获取目标 Java工程的源文件的绝对路径;
从所述绝对路径下的类文件中査找出与指定的 URL对应的第一方法; 在所述第一方法的方法体中査找对第二方法的调用, 所述第二方法为 与所述第一方法不同的方法;
若在所述第一方法的方法体中査找到对所述第二方法的调用, 且在类 文件中査找到所述第二方法, 则记录所述第一方法与所述第二方法的 调用关系;
在所述第二方法的方法体中査找对第三方法的调用, 所述第三方法为 与所述第一、 第二方法不同的方法;
若在所述第二方法的方法体中査找到对所述第三方法的调用, 且在类 文件中査找到所述第三方法, 则记录所述第二方法与所述第三方法的 调用关系;
重复上述流程, 直到当前方法中无法找到对新方法的调用吋, 则结束 代码分析, 并汇总记录的所述调用关系;
根据汇总的所述调用关系绘制出对应的调用关系图。
[权利要求 12] 根据权利要求 11所述的代码分析终端设备, 其特征在于, 所述获取目 标 Java工程的源文件的绝对路径包括:
获取所述目标 Java工程的环境变量;
从所述环境变量中解析出源码路径和编译路径; 根据所述源码路径和所述编译路径确定出所述目标 Java工程的源文件 的绝对路径。
[权利要求 13] 根据权利要求 11所述的代码分析终端设备, 其特征在于, 所述在类文 件中査找所述第二方法包括:
确定与所述第二方法的方法名对应的类文件的文件路径;
获取所述文件路径下的类文件;
在所述类文件中査找所述第二方法。
[权利要求 14] 根据权利要求 11所述的代码分析终端设备, 其特征在于, 在结束代码 分析之前, 还包括: 若当前方法所属的类为框架提供的数据库操作类的子类, 则在当前方 法的方法体中査找对字符串类型的参数的调用; 若在当前方法的方法体中査找到对字符串类型的参数的调用, 则将所 述字符串类型的参数确定为与当前方法对应的数据库标识。
[权利要求 15] 根据权利要求 11至 14中任一项所述的代码分析终端设备, 其特征在于 , 所述根据汇总的所述调用关系绘制出对应的调用关系图包括: 将汇总的所述调用关系导入到预设的 PlantUML脚本模板中, 生成所 述调用关系图的绘制脚本;
运行所述绘制脚本, 生成与汇总的所述调用关系对应的所述调用关系 图。
[权利要求 16] —种计算机可读指令, 所述计算机可读指令应用于一种代码分析终端 设备, 所述代码分析终端设备包括存储器、 处理器以及存储在所述存 储器中并可在所述处理器上运行的所述计算机可读指令, 其特征在于 , 所述计算机可读指令包括以下模块:
路径获取模块, 用于获取目标 Java工程的源文件的绝对路径; 第一方法査找模块, 用于从所述绝对路径下的类文件中査找出与指定 的 URL对应的第一方法;
调用査找模块, 用于在所述第一方法的方法体中査找对第二方法的调 用, 所述第二方法为与所述第一方法不同的方法; 第二方法査找模块, 用于若在所述第一方法的方法体中査找到对所述 第二方法的调用, 则在类文件中査找所述第二方法;
调用关系记录模块, 用于若在类文件中査找到所述第二方法, 则记录 所述第一方法与所述第二方法的调用关系;
第一方法更新模块, 用于将所述第二方法确定为新的第一方法, 并返 回执行所述在所述第一方法的方法体中査找对第二方法的调用的步骤 调用关系汇总模块, 用于若在所述第一方法的方法体中未査找到对所 述第二方法的调用, 则结束代码分析, 并汇总记录的所述调用关系; 调用关系图绘制模块, 用于根据汇总的所述调用关系绘制出对应的调 用关系图。
根据权利要求 16所述的计算机可读指令, 其特征在于, 所述路径获取 模块包括:
环境变量获取单元, 用于获取所述目标 Java工程的环境变量; 解析单元, 用于从所述环境变量中解析出源码路径和编译路径; 绝对路径确定单元, 用于根据所述源码路径和所述编译路径确定出所 述目标 Java工程的源文件的绝对路径。
根据权利要求 16所述的计算机可读指令, 其特征在于, 所述第二方法 査找模块包括:
文件路径确定单元, 用于确定与所述第二方法的方法名对应的类文件 的文件路径;
类文件获取单元, 用于获取所述文件路径下的类文件;
第二方法査找单元, 用于在所述类文件中査找所述第二方法。
根据权利要求 16所述的计算机可读指令, 其特征在于, 所述调用关系 图绘制模块包括:
绘制脚本生成单元, 用于将汇总的所述调用关系导入到预设的 PlantU ML脚本模板中, 生成所述调用关系图的绘制脚本;
调用关系图生成单元, 用于运行所述绘制脚本, 生成与汇总的所述调 用关系对应的所述调用关系图。
根据权利要求 16至 19中任一项所述的计算机可读指令, 其特征在于, 所述计算机可读指令中还包括:
参数调用査找模块, 用于若所述第一方法所属的类为框架提供的数据 库操作类的子类, 则在所述第一方法的方法体中査找对字符串类型的 参数的调用;
数据库标识确定模块, 用于若在所述第一方法的方法体中査找到对字 符串类型的参数的调用, 则将所述字符串类型的参数确定为与所述第 一方法对应的数据库标识。
PCT/CN2017/104535 2017-08-09 2017-09-29 一种代码分析方法及终端设备 WO2019028989A1 (zh)

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN201710675458.0A CN107506299B (zh) 2017-08-09 2017-08-09 一种代码分析方法及终端设备
CN201710675458.0 2017-08-09

Publications (1)

Publication Number Publication Date
WO2019028989A1 true WO2019028989A1 (zh) 2019-02-14

Family

ID=60690618

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2017/104535 WO2019028989A1 (zh) 2017-08-09 2017-09-29 一种代码分析方法及终端设备

Country Status (2)

Country Link
CN (1) CN107506299B (zh)
WO (1) WO2019028989A1 (zh)

Families Citing this family (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111008022B (zh) * 2019-12-04 2023-12-12 浙江大搜车软件技术有限公司 关系图生成方法、装置、计算机设备和存储介质

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US5802367A (en) * 1995-07-07 1998-09-01 Microsoft Corporation Method and system for transparently executing code using a surrogate process
CN101482847A (zh) * 2009-01-19 2009-07-15 北京邮电大学 一种基于安全漏洞缺陷模式的检测方法
CN103678097A (zh) * 2012-09-05 2014-03-26 百度在线网络技术(北京)有限公司 一种选择回归测试用例的方法和装置
CN106020848A (zh) * 2016-06-07 2016-10-12 北京信息科技大学 面向c#的函数调用路径生成方法

Family Cites Families (11)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN100576172C (zh) * 2008-05-27 2009-12-30 华耀环宇科技(北京)有限公司 一种通过分析代码变化确定功能点变化的方法
CA2687672A1 (en) * 2009-12-07 2011-06-07 Ibm Canada Limited - Ibm Canada Limitee Intelligent call graph generation
US8954929B2 (en) * 2010-03-29 2015-02-10 Microsoft Corporation Automatically redirecting method calls for unit testing
US20110321013A1 (en) * 2010-06-23 2011-12-29 Quickunit Ltd Interactive environment for test case generation associated with a computer code
CN102012833A (zh) * 2010-11-09 2011-04-13 北京神舟航天软件技术有限公司 基于编译过程中间结果的静态堆栈检测方法
CN103186406B (zh) * 2011-12-30 2016-08-17 国际商业机器公司 用于控制流分析的方法和装置
CN104536895B (zh) * 2015-01-12 2017-06-09 牟永敏 一种面向Java函数调用路径的测试过程跟踪方法及系统
CN104899147B (zh) * 2015-06-19 2017-11-28 北京理工大学 一种面向安全检查的代码静态分析方法
CN105095092A (zh) * 2015-09-25 2015-11-25 南京大学 基于静态分析和动态运行的Web应用JavaScript代码原子性违反检测
CN105808252A (zh) * 2016-03-04 2016-07-27 北京理工大学 一种Windows操作系统内核函数遍历方法
CN106528422B (zh) * 2016-11-10 2019-01-29 中国银联股份有限公司 一种检测Java程序页面扰动的方法和装置

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US5802367A (en) * 1995-07-07 1998-09-01 Microsoft Corporation Method and system for transparently executing code using a surrogate process
CN101482847A (zh) * 2009-01-19 2009-07-15 北京邮电大学 一种基于安全漏洞缺陷模式的检测方法
CN103678097A (zh) * 2012-09-05 2014-03-26 百度在线网络技术(北京)有限公司 一种选择回归测试用例的方法和装置
CN106020848A (zh) * 2016-06-07 2016-10-12 北京信息科技大学 面向c#的函数调用路径生成方法

Also Published As

Publication number Publication date
CN107506299A (zh) 2017-12-22
CN107506299B (zh) 2021-04-02

Similar Documents

Publication Publication Date Title
US10169471B2 (en) Generating and executing query language statements from natural language
WO2019029155A1 (zh) 数据对比方法、装置、计算机设备和存储介质
CN108459962A (zh) 代码规范性检测方法、装置、终端设备及存储介质
WO2019169723A1 (zh) 测试用例选择方法、装置、设备以及计算机可读存储介质
CN111177113B (zh) 数据迁移方法、装置、计算机设备和存储介质
CN110825430A (zh) 一种api文档生成方法、装置、设备及存储介质
US11366704B2 (en) Configurable analytics for microservices performance analysis
US10693962B1 (en) Language and mechanism for modeling and exporting storage platform topologies, attributes, and behaviors
CN112187713B (zh) 报文转换的方法、装置、计算机设备和存储介质
CN114138748A (zh) 数据库映射文件生成方法、装置、设备及存储介质
US11645234B2 (en) Rule-based collections of subset(s) of metadata in response to a trigger event occurring
CN112286557B (zh) 一种非覆盖式更新代码内容的方法和装置
CN111158665B (zh) 代码生成方法及装置、电子设备和存储介质
CN110633162B (zh) 远程调用实现方法、装置、计算机设备及存储介质
KR102132449B1 (ko) 자바스크립트 파일 재작성을 통한 자바스크립트 해석 엔진 테스트 방법 및 장치
WO2019028989A1 (zh) 一种代码分析方法及终端设备
CN114115900B (zh) 一种脚本编译方法、装置及电子设备
CN116400914A (zh) 一种基于数据模型快速构建web应用的方法
US9201937B2 (en) Rapid provisioning of information for business analytics
CN113495723B (zh) 一种调用功能组件的方法、装置及存储介质
CN111159203B (zh) 一种数据关联分析的方法、平台、电子设备及存储介质
CN111124386B (zh) 基于Unity的动画事件处理方法、装置、设备和存储介质
WO2022105494A1 (zh) 数据自动回填方法、装置、电子设备及计算机存储介质
CN114218261A (zh) 数据查询方法、装置、存储介质以及电子设备
CN113761040A (zh) 数据库与应用程序双向映射方法、设备、介质及程序产品

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 17920725

Country of ref document: EP

Kind code of ref document: A1

NENP Non-entry into the national phase

Ref country code: DE

122 Ep: pct application non-entry in european phase

Ref document number: 17920725

Country of ref document: EP

Kind code of ref document: A1

32PN Ep: public notification in the ep bulletin as address of the adressee cannot be established

Free format text: NOTING OF LOSS OF RIGHTS PURSUANT TO RULE 112(1) EPC (EPO FORM 1205A DATED 03.08.2020)

122 Ep: pct application non-entry in european phase

Ref document number: 17920725

Country of ref document: EP

Kind code of ref document: A1