Java Reference
In-Depth Information
example, if your class name doesn't match the name of its containing file, a Quick-
fix intention appears and offers to rename one or the other into compliance.
3.8.2
Why and when IDEA suggests intention actions
When IDEA encounters a syntax error or other
portion of code that it believes it can assist you
with, it alerts you with the intention action
alert symbol, which looks like a little light bulb,
flush left on the page. An example is shown in
figure 3.26, where we're trying to return a long
value from a method with a return type of int .
Because any given line of source code may trigger several different intention
action alerts, the alert icon appears only when your cursor is on the affected region.
In our example, the alert is displayed only when the cursor is on the line with the
return keyword. Note that although this alert is centered on a syntax error, in some
situations IDEA offers intention actions for code that is syntactically correct as well;
it does so if it has a suggestion about how to optimize the code or improve its struc-
ture or can logically deduce a common operation you might wish to perform.
Figure 3.26 The light bulb icon
suggests that IDEA has an idea: an
intention, a way to solve the bad code.
3.8.3
Using intention actions to fix errors
The primary purpose of intention actions is to
fix problems and oversights in your code. For
example, in figure 3.27, when we try to return a
long value from a method with a declared
return type of int , IDEA not only flags the syn-
tax error before the compiler catches it, but also
offers two possible suggestions based on what
we may have intended to do. In our example,
IDEA suggests that we either cast the returned
value to an int , thereby satisfying the method signature, or change the method sig-
nature to return a long value, as shown. Whichever action we take, IDEA imple-
ments the change. You'll learn how to invoke these actions a little later.
Figure 3.27 When a problem
highlighted by an intention has multiple
solutions, IDEA presents them in a
short drop-down list for your review.
3.8.4
Using intention actions for code completion
Intention actions do more than fix mistakes. Once you're familiar with the types of
intention actions IDEA provides, you can use them to perform code completion or
act as programming shortcuts. For instance, if you make a call to a method that
doesn't yet exist, IDEA 's intention actions will offer to create the method for you.
 
 
 
 
 
Search WWH ::




Custom Search