How to analyze Java code for modification? JDT offers several tools to help you analyze the code. This article intends to choose the simplest ISCANNER interface for demonstration, and its scope is also the most limited. This interface belongs to the JDT toolbox and can access it through the JDT ToolFactory class. Its CreateScanner method returns a scanner that simplifies the work that is tagged for a string Java code. It does not deal with any particular difficult operation, just simple resolution and classification of the returned tag. For example, it indicates that the next tag is a public keyword, and the label is an identifier, and the back mark is left population, and the like. Subsequently, this scanner is only suitable when you want to analyze a small code (you know what you want to get in this code). You will never use the scanner to analyze the entire Java source code; because you will turn it with some tools that are very familiar with the compiler: The Abstract Syntax Tree, AST framework of the JDT.
Unlike simple scanners, AST understands the relationship between language elements (they are no longer just "tags"). It can identify more than 60 different language elements such as local variables, instance variables, expressions, and IF statements. It will help you perform a particularly high-level reconstruction of a wide range of fuzzy degree of a pair of scratches. To know when to use the difference between when using the scanner and when using the AST, consider the code in Listing 10.
Listing 10. Fuzzy variable reference
Public class foo {
INT foo = 1;
PUBLIC INT FOO (Int foo) {
Return foo this.foo;
}
Public int getfoo () {
Return foo;
}
}
If you are a part of the reconstruction, you want to find a reference to the instance variable foo, then you will understand that a simple resolution will make the local reference and instance variable references become a problem. AST creates a complete analysis tree that represents each element of Java source code and distinguishes these elements. In this special case, different classes will consider the context referenced by "foo", which represents the "foo" reference representative (such as FieldDeclaration, SimpleName, and ThiseXpression), so you will easily identify them.
As mentioned earlier, this article will only discuss the simple example of our choice. For complicated modifications and analysis examples, see the reference information section. Now let's go back to the code that we skip to use the omitted number. This code will use the ISCANNER's instance to determine and replace the keywords that determine the visibility in the source code. We will handle the visibility modifiers of PUBLIC, Private, Protected and Final. By adopting a "brute force" method, we can simplify this solution, that is, two steps can be done. First remove all visual modes (or at least scan for at least scan, if you find, you can delete), then insert the desired modifier. In particular:
If you find public, private, or protected in the method characterization, you will delete them.
Insert the requested visibility modifier (for the case of the package, no operation, because this is the default operation; there is no modifier).
Final modifiers are very simple. Because the desired behavior is inserted and removed, if it exists, we remove it; otherwise insert it. The code in Listing 11 shows only one example, it unconditionally converts the visibility from Pubilc to Private. In the solution related to this article, you will see that the public code for each operation is moved to an abstract superclass. It is basically the same as the code below, but it is just a slight consolidation to avoid redundancy. Listing 11. Scanning is a Pubilc keyword
ICompiLationUnit Cu = Member.getCompiLationUnit ();
IF (cu.isworkingcopy ()) {
Ibuffer buffer = cu.getBuffer ();
Iscanner scanner =
ToolFactory.createscanner (False, False, False, False);
Scanner.setsource (buffer.getcharacters ());
IsourcesRange Sr = Member.getsourceRange ();
Scanner.resetto
Sr.Getoffset (),
Sr.getOffset () Sr.getLength () - 1);
Int token = scanner.getnextToken ();
While (token! = iterminalsymbols.tokennameeof
&& token! = oreminalsymbols.tokennamelparen)
Token = scanner.getnextToken ();
IF (token == oreminalsymbols.tokennamepublic) {
Buffer.replace
Scanner.getcurrentTokenstArtPosition (),
Scanner.getcurrentTokenndPosition (),
Scanner.getcurrentTokenStartPosition () 1,
"private");
Break;
}
}
Cu.Reconcile ();
}
Note: iterminalSymbols defines the mark name that the scan program can return, which corresponds to the standard tag of the Java syntax. You can further query the scanner to ask if the current tag starts and ends in the buffer, which row it appears, of course, there is also a marker itself (especially the example like IterminalSymbols.TokennameStringlitral and IterminalSymbols.TokenNameIdentifier, they are not Reserved keyword).
In the above code snippet, the SCANNER.SetSource method provides the complete source code of the compiler, which is all of the Java source files. As mentioned earlier, the scanner is not very suitable for large analysis, so we must limit it for only the first character of the target method, until the SETSourceRange method as the ended source code. The IMEMBER interface inherits isourcereference, isourcereference is an interface that allows you to query the source code string and source code location in the compilation unit. This makes us no need to determine the target method starts and ends in the Java source code. It was originally possible to achieve this with AST, and the ISourceReference interface made AST into extra tools. Since the Java method characteristic is easy to resolve, the resolution capacity of the ISCANNER interface matches it. What we have to do is to find the public keyword, which appears after the first character declared, the left round parentheses of the parameter declaration, replaced it with the private key. Of course, in this solution, this interface will handle all possible situations, whether the method is initially public, private, protected or package (default). What is the next step?
The goal set in this article is to provide you with a value-based extension to the Java development environment of Eclipse, which enhances the productivity of this development environment. I frankly, I jumped some details many times for the sake of simplicity. The solution itself makes some simplified assumptions, like only to modify the opened Java source code in the editor. You may want to cancel this limit in a more complete implementation.
Even so, I still hope that you can feel that it is possible and is confident that this is not particularly difficult. In this article, we discuss some of the contents of the Java Developer's Guide To Eclipse a senior chapter. There are eleven shallow chapters to discuss the basis of plugin development. Like this article, most chapters contain a documentation work solution that enhances the knowledge you have learned, most of the content is written in the same style you have seen in this article (but may not So fast rhythm discuss!).
Important: You may need to add the necessary plugins to work space, so that the solution can compile and run. Select Window> Preference> Plug-in Development> Target Platform, then select Not in Workspace. This will ensure that the base plugin depends on the solution can be used during importing and re-compiling.
Once imported, you may need to switch to the PLUG-IN Development perspective, select Plugin.xml in the com.ibm.Lab.soln.jdt.Excerpt project, then select Update ClassPath. This will modify the compilation error caused by the installation path of the Eclipse installation path and the solution.
About author