Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

Using Editor Hints to Generate Missing Code

When the IDE detects an error for which it has identified a possible fix, a lightbulb ( keegan_05_01.jpg ) icon appears in the left margin of that line. You can click the lightbulb or press Alt-Enter to display a list of possible fixes. If one of those fixes suits you, you can select it and press Enter to have the fix generated in your code.

Often, the "error" is not a coding mistake but a reflection of the fact that you have not gotten around to filling in the missing code. In those cases, the editor hints simply automate the entry of certain types of code.

For example, assume you have just typed the following code, but x is not defined anywhere in the class.

   int newIntegerTransformer () {
       return x;

   }

If your cursor is still resting on the line of the return statement, the keegan_05_01.jpg icon will appear. If you click the icon or press Alt-Enter, you will be offered three possible solutions as shown in Figure 5-6. You can select one of those hints to generate the code.

keegan_fig05_06.jpg

Figure 5-6 Display of editor hints

The IDE is able to provide hints for and generate the following solutions to common coding errors:

  • Add a missing import statement
  • Insert abstract methods that are declared in a class' implemented interfaces and abstract superclasses
  • Insert a method parameter
  • Create a missing method
  • Create a missing field
  • Create a missing local variable
  • Initialize a variable
  • Insert a cast
  • Add a throws clause with the appropriate exception
  • Surround the code with a try-catch block including the appropriate exception
  • + Share This
  • 🔖 Save To Your Account