Java Reference
In-Depth Information
Allowing the inspector to fix problems for you
IDEA can fix many of the problems it finds for you automatically. When it can do
so, it provides an entry in the Problem resolution field in the inspection report.
In some situations, there may be multiple recommendations, allowing you to pick
the most appropriate one. In figure 9.8, IDEA suggests removing a magic num-
ber —a literal numeric constant, compiled directly into the code—and replacing it
with a constant stored in a class variable. The fact that this recommendation is
hyperlinked tells you that IDEA will take care of the problem if you click the rec-
ommendation. If no hyperlink is present, you must fix the problem by hand.
9.3 Other advanced code analysis features
Automated analysis is well suited for the class of problems that are common and
easy to identify. If you look at the list of code inspections performed by IDEA ,
you'll see common performance problems, common encapsulation issues, com-
mon security violations, and so on. Other problems—such as replacing an ineffi-
cient code structure throughout a project, or finding functionality that's been
duplicated in more than one place—are more complex and typically need to be
addressed manually. In addition to the automated features already discussed,
IDEA provides several features to aid your manual code and design review process.
9.3.1
Using Structural Search and Replace
With IDEA 4.5, JetBrains provided a powerful new feature known as Structural
Search and Replace ( SSR ). Essentially, SSR performs the same type of operations
as the textual search and replace feature, but it takes advantage of IDEA 's intelli-
gent knowledge of Java syntax, code structure, and code dependencies. Architec-
turally, it combines elements of Find Usages , IDEA 's refactoring operations, and
live templates into a generic code analysis and refactoring tool.
This tool aims to solve the textual search's inability to restrict the scope of a
search or replace operation effectively. Rather than relying solely on text match-
ing, SSR lets you specify search conditions within the context of your code, from
something as simple as restricting a search to a certain type of element (such as
a class name or method declaration) to something as complex as a multiline
code pattern.
To access the SSR panel shown in figure 9.9, select the Search | Search Struc-
turally command ( Ctrl+Shift+S ). The text box at the top of the window is
where you specify the code structure being sought. If this were a regular search
 
 
 
 
 
 
 
 
 
Search WWH ::




Custom Search