Java source code analysis Executor (2009-5-1) (English version)
I. Functions and modules: // sorry, although this project is interesting, it is unorganized, easy to read, and stuck.
1. Identify the syntax structure of the Java source code and generate a node tree with the node type as the specific source code component.
2. parse the Java source code and start running from the specified method entrance. During the running process, you can execute the code logic that is inserted externally. Similar to AOP, but the intercepted target can be method or any fine-grained code.
3. parse the Java source code and execute it from the specified method entrance. During the execution, warnings are given for unreasonable code. The warning function is my interest and will be greatly enhanced.
4. Static processing:
① Check the legitimacy of the source code to ensure that it meets the requirements of specific projects. Similar to the Java code defect automatic analysis tool "findbugs ".
② Execute some complicated refactoring tasks, such as changing the array to Java. util. list to change the order of parameters in a method call. For example, rewrite the count (int A, string B) method to count (string B, int ).
Ii. main concepts:
1. syntax tree: the syntax tree of Java source code is composed of several small-scale syntax trees. The abstract syntax tree is essentially a graph rather than a tree.
2. Matching tree: it is a strict tree. The matching engine identifies the source code by referring to the syntax tree and generates the code.
3. an of the matching tree: the matching tree describes the static form of the source code. The bean-based process generates corresponding jvabean for different source code components, implements targeted running logic in bean, and implements Dynamic Interpretation and execution of Java code on this basis. (See the source code package bugsfind. Bean .*)
4. node type: Typical node types include ifelse, for, dowhile, mehtodd, block, expression, and so on.
5. Source bean and passive class. In actual projects, not all classes have source code, and the interaction between the source system and the passive system needs to be solved ....... Generate corresponding source bean for classes with source code ...... In the process of reading and executing the source code step by step, when encountering a passive class (a class without source code exists), ensure that the source bean and the passive class share the data they work together.
The source bean has a corresponding passive object, which allows the passive class to call the source bean.
The source bean can directly call the passive class. Because the calling details of the source bean are transparent, you can use the specific method for specific computing details:
① Operator calculation, for example, A + = 2, B! = C + D,... can be equivalent through string analysis.
② System function calls, such as file read/write, screen output,... can be achieved through the reflection mechanism.
③ Data transfer between different data structures. For example, list. toarray (),... can be equivalent through the reflection mechanism.
Iii. Current Development Status vs prospects:
The structure and details of this project have basically taken shape and need to be fully tested and further expanded in terms of functions.
See the test case demo. testcase_all.java, demo. testcase_typical.java.
By Rewriting the above test cases and related test objects, you can learn more about this project.
The reflection mechanism has limited capabilities. To ensure the proper running of large programs, some changes must be made to the source code, but such changes can be lossless or reversible. Major changes include explicitly generating constructors for non-public internal classes in other packages. Otherwise, the reflection mechanism cannot run the constructors.
Enum (jdk5.0) and some operators cannot be identified yet (for details, see bugsfinder. Exp. operatorsj. Java ).
This project provides a reference for the following tasks as needed:
① JavaScript source code analysis and JS editor.
② Analysis of various scripts (such as SQL), automatic construction, and standardized source code detection.
Iv. Test Case: // Java version: jdk1.5.0 _ 04
After extracting the project, click startdemo. bat in the project root directory to run a simple test case.
The bugsfind. Demo and bugsfind. Test packages have more detailed test cases. -- Import the project to eclipse before running, modify the configuration file bugsfind. _ config. config. properties, and set the sourcecodelocation parameter value to the source code root directory path of the current project.
5. Download
This Java project can be downloaded from: http://code.google.com/p/java-parser-executor/downloads/list
Author: weilai2@163.com, China Shanghai (Pudong ),
(Keywords: JAVA Source Code Analyzer, JAVA Source Code Analyzer, source code tree, Java source code error detector, Java source code interpretation executor, compilation technology)
Java source-code parser & Executor (2009-5-1)
* ** 1. Function and module: // sorry, this project is interesting but source code is a mess
1.1. Recognize the structure of Java source-code, produce a node-tree.
1.2. parse the source, execute Form specified method. custom execute logic can be inserted into this process. AOP can intercept a method, via this way, we can intercept any clause in a method, that is to say, more fine-grained.
1.3 parse the source, execute Form specified method. And try to find out illogical code. This finding-out is my interest, and hopes to be enhanced in the future.
1.4 static process:
1.4.1. Check the source-code, assure it fits the rule of concrete project. Just like "findbugs". (http://findbugs.sourceforge.net /).
1.4.2. fulfill some complex refactor task. e.g. refactor array to Java. util. list, refactor the order of method parameter. e.g. change Method count (int A, string B) to method count (string B, int ).
* ** 2. terms:
2.1. Grammar tree: grammar of Java code is merged from some more smaller grammar. In essential, abstract grammar tree is a digoal, no a tree.
2.2. Match-tree: is a strict tree. After parsing a certain Java code-fragment, the matching-engine will create a tree as a result.
2.3. match-tree to JavaBeans: Match-tree is certain Java code's static form. after JavaBean-lize, I add corresponding execution-logic to every grammar-element, based on this, we can run some java-code not via bytecode but via source-code. (Please see source-package bugsfind. bean. *)
2.4. Type of node. Typical node type is: ifelse, for, dowhile, method, block, expression,... etc.
2.5. source bean and no-source class: In a practical project, some cod has source, some hasn' t, we must face such problem: source-class interactive-with no-source-class. in some way, I realize it (not strong, but useful ).
* ** 3. Current progress:
Structure and details has basically formed, it is to be fully-test and waiting for more extension in function.
Test Case demo. testcase_all.java, demo. testcase_typical.java is useful to get more conception on this project.
* ** 4. Test Case & running: // Java version: jdk1.5.0 _ 04
Click batch-file startdemo. bat, a simple test will be start.
There are more test under source-package bugsfind. demo * And bugsfind. test. *. but before first running, shocould import this project into eclipse, then, config file "bugsfind. _ config. config. properties "shocould be modified: Set sourcecodelocation =" root directory path of source-code in current project ".
* ** 5. Download:
This Java project can be downloaded from
Http://code.google.com/p/java-parser-executor/downloads/list
Your opinion is welcome!
Author: weilai2@163.com, Shanghai (Pudong), China.
A.m.
~~~~~~~~~~~ End.