blob: 18efe23c37e20396699dd57746d865ed65581584 [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta name="copyright" content=
"Copyright (c) IBM Corporation and others 2000, 2019. This page is made available under license. For full details see the LEGAL in the documentation book that contains this page." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="../book.css" charset="ISO-8859-1" type=
"text/css" />
<title>
Quick Fix
</title>
<script language="JavaScript" src=
"PLUGINS_ROOT/org.eclipse.help/livehelp.js" type="text/javascript">
</script>
</head>
<body>
<h1>
Quick Fix
</h1>
<p>
The Java editor offers corrections to problems found while typing and
after compiling. To show that correction proposals are available for a
problem or warning, a 'light bulb' is visible on the editor's annotation
bar.<br />
<br />
Left click on the light bulb or invoking <b>Ctrl+1 (Edit &gt; Quick
Fix)</b> brings up the proposals for the problem at the cursor
position.<br />
Each quick fix shows a preview when selected in the proposal
window.
</p>
<p>
<img src="images/c-quickfix.png" border="0" alt="Quick fix selection dialog" />
</p>
<p>
Quick fixes are also shown directly in problem hovers (but there, no preview is available).
</p>
<p>
<img src="images/quickfix-hover.png" border="0" alt="Quick fix hover" />
</p>
<br/>
<p>
<b>Usage hint:</b> Quick fixes are not only useful to fix errors that accidentally occurred.
An other common usage pattern is to inentionally write "incorrect" code, for example by referring to
a local variable that is not declared yet. Then, the <b>Create local variable</b> Quick Fix can
generate the declaration in no time, and it can even infer the variable type.
</p>
<p>
Some selected quick fixes can also be assigned with direct shortcuts. You
can configure these shortcuts on the <a class="command-link" href=
'javascript:executeCommand("org.eclipse.ui.window.preferences(preferencePageId=org.eclipse.ui.preferencePages.Keys)")'>
<img src="PLUGINS_ROOT/org.eclipse.help/command_link.png" alt=
"Opens keys preference page" /> <b>General &gt; Keys</b></a> preference
page (in the 'Source' category).<br />
</p>
<p>
Some quick fixes offer to fix all problems of the same kind in the
current file at once. The information text in the proposal window contains
this information for all applicable proposals. To fix all problems of the
same kind, press <b>Ctrl+Enter</b>.
</p>
<p>
Here's a selection of available quick fixes:
</p>
<table border="0" cellpadding="10" cellspacing="0" width="700" summary=
"List if quick fixes">
<tbody>
<tr>
<td width="20%" valign="top" align="right">
<b>Package Declaration</b>
</td>
<td width="80%" valign="top" align="left">
<ul>
<li>Add missing package declaration or correct package
declaration
</li>
<li>Move compilation unit to package that corresponds to the
package declaration
</li>
</ul>
</td>
</tr>
<tr>
<td valign="top" align="right">
<b>Imports</b>
</td>
<td valign="top" align="left">
<ul>
<li>Remove unused, unresolvable or non-visible import
</li>
<li>Invoke 'Organize imports' on problems in imports
</li>
<li>Convert static field accesses and static methods to use a static import
</li>
</ul>
</td>
</tr>
<tr>
<td valign="top" align="right">
<b>Types</b>
</td>
<td valign="top" align="left">
<ul>
<li>Create new class, interface, enum, annotation or type
variable for references to types that can not be resolved
</li>
<li>Change visibility for types that are accessed but not visible
</li>
<li>Rename to a similar type for references to types that can not
be resolved
</li>
<li>Add import statement for types that can not be resolved but
exist in the project
</li>
<li>Add explicit import statement for ambiguous type references
(two import-on-demands for the same type)
</li>
<li>If the type name is not matching with the compilation unit
name either rename the type or rename the compilation unit
</li>
<li>Remove unused private types
</li>
<li>Add missing type annotation attributes
</li>
</ul>
</td>
</tr>
<tr>
<td valign="top" align="right">
<b>Constructors</b>
</td>
<td valign="top" align="left">
<ul>
<li>Create new constructor for references to constructors that
can not be resolved (this, super or new class creation)
</li>
<li>Reorder, add or remove arguments for constructor references
that mismatch parameters
</li>
<li>Change method with constructor name to constructor (remove
return type)
</li>
<li>Change visibility for constructors that are accessed but not
visible
</li>
<li>Remove unused private constructor
</li>
<li>Create constructor when super call of the implicit default
constructor is undefined, not visible or throws an exception
</li>
<li>If type contains unimplemented methods, change type modifier
to 'abstract' or add the method to implement
</li>
</ul>
</td>
</tr>
<tr>
<td valign="top" align="right">
<b>Methods</b>
</td>
<td valign="top" align="left">
<ul>
<li>Create new method for references to methods that can not be
resolved
</li>
<li>Rename to a similar method for references to methods that can
not be resolved
</li>
<li>Reorder or remove arguments for method references that
mismatch parameters
</li>
<li>Correct access (visibility, static) of referenced methods
</li>
<li>Remove unused private methods
</li>
<li>Correct return type for methods that have a missing return
type or where the return type does not match the return statement
</li>
<li>Add return statement if missing
</li>
<li>For non-abstract methods with no body change to 'abstract' or
add body
</li>
<li>For an abstract method in a non-abstract type remove abstract
modifier of the method or make type abstract
</li>
<li>For an abstract/native method with body remove the abstract
or native modifier or remove body
</li>
<li>Change method access to 'static' if method is invoked inside
a constructor invocation (super, this)
</li>
<li>Change method access to default access to avoid emulated
method access
</li>
<li>Add 'synchronized' modifier
</li>
<li>Override hashCode()
</li>
<li>Open the 'Generate hashCode() and equals()' wizard
</li>
<li>Create abstract methods. Improved the existing quick fix to
create missing methods to create abstract method declarations in
an abstract class.
</li>
</ul>
</td>
</tr>
<tr>
<td valign="top" align="right">
<b>Fields and variables</b>
</td>
<td valign="top" align="left">
<ul>
<li>Correct access (visibility, static) of referenced fields
</li>
<li>Create new fields, parameters, local variables or constants
for references to variables that can not be resolved
</li>
<li>Rename to a variable with similar name for references that
can not be resolved
</li>
<li>Remove unused private fields
</li>
<li>Correct non-static access of static fields
</li>
<li>Add 'final' modifier to local variables accessed in outer
types
</li>
<li>Change field access to default access to avoid emulated
method access
</li>
<li>Change local variable type to fix a type mismatch
</li>
<li>Initialize a variable that has not been initialized
</li>
<li>Create getter and setters for invisible or unused fields
</li>
<li>Create loop variable to correct an incomplete enhanced 'for' loop by adding the type of the loop variable
</li>
</ul>
</td>
</tr>
<tr>
<td valign="top" align="right">
<b>Exception Handling</b>
</td>
<td valign="top" align="left">
<ul>
<li>Remove unneeded catch block</li>
<li>Remove unneeded exceptions from a multi-catch clause (1.7 or higher)</li>
<li>Handle uncaught exception by surrounding with try/catch or
adding catch block to a surrounding try block
</li>
<li>Handle uncaught exceptions by surrounding with try/multi-catch or
adding exceptions to existing catch clause (1.7 or higher)
</li>
<li>Handle uncaught exception by adding a throw declaration to
the parent method or by generalize an existing throw declaration
</li>
</ul>
</td>
</tr>
<tr>
<td valign="top" align="right">
<b>Build Path Problems</b>
</td>
<td valign="top" align="left">
<ul>
<li>Add a missing JAR or library for an unresolvable type
</li>
<li>Open the build path dialog for access restriction problems or
missing binary classes.
</li>
<li>Change project compliance and JRE to 1.5
</li>
<li>Change workspace compliance and JRE to 1.5
</li>
<li>Change project compliance and JRE to 10 on <b>var</b> type (10 or higher)
</li>
<li>Change project compliance and JRE to 11
</li>
<li>Change project compliance and JRE to 12
</li>
</ul>
</td>
</tr>
<tr>
<td valign="top" align="right">
<b>Others</b>
</td>
<td valign="top" align="left">
<ul>
<li>Add cast or change cast to fix type mismatches
</li>
<li>Let a type implement an interface to fix type mismatches
</li>
<li>Add type arguments to raw references
</li>
<li>Complete switch statements over enums
</li>
<li>Remove dead code
</li>
<li>Insert '//$FALL-THROUGH$'
</li>
<li>Insert null check
</li>
<li>For non-NLS strings open the NLS wizard or mark as non-NLS
</li>
<li>Add missing @Override, @Deprecated annotations
</li>
<li>Add missing Javadoc comments
</li>
<li>Add missing Javadoc tags
</li>
<li>Suppress a warning using @SuppressWarnings
</li>
<li>Throw the allocated object
</li>
<li>Return the allocated object
</li>
<li>Add @SafeVarargs annotation for heap pollution warnings (1.7 or higher)
</li>
<li>Remove invalid @SafeVarargs annotations (1.7 or higher)
</li>
<li>Remove redundant type arguments (1.7 or higher)
</li>
<li>Add inferred type arguments (1.5 and 1.6)
</li>
<li>Remove unused type parameter (1.5 or higher)
</li>
<li>Configure problem severity
</li>
<li>Add @NonNullByDefault annotation on package when the <b>Missing '@NonNullByDefault' annotation on package</b> warning is enabled
</li>
<li>Add missing module dependency (requires statement) on import statements (9 or higher)
</li>
<li>Add missing import statement and module dependency (requires statement) on unresolved type (9 or higher)
</li>
<li>Create new Class or Interface for an unresolved type on service provider in a provides directive (9 or higher)
</li>
<li>Move Classpath entry to Modulepath for unresolved module on requires statement (9 or higher)
</li>
<li>Create new Class or Integer or Enum or Annotation for Non-existing or empty package on exports directive (9 or higher)
</li>
<li>Open Compiler Compliance property page on JRE Compiler Compilance problem marker
</li>
<li>Create new service provider method in case of invalid service provider implementation used in module-info.java (9 or higher)
</li>
<li>Change "Enable preview features" option to true on Compiler preferences page (12 or higher)
</li>
<li>Configure problem severity of "Enable preview features" option on Compiler preferences page (12 or higher)
</li>
<li>Add 'default' case to enhanced switch statement (12 or higher)
</li>
<li>Add missing case statements for enhanced switch statement (12 or higher)
</li>
<li>Add 'default' case to switch expression (12 or higher)
</li>
<li>Add missing case statements for switch expression (12 or higher)
</li>
<li>Split switch case labels into separate case statements (12 or higher)
</li>
<li>Create a public constructor on a Service defined in module-info.java whose no-arg constructor is not visible, or is non-existent (9 or higher)
</li>
<li>Change the visibility of the constructor of a service defined in module-info.java to public (9 or higher)
</li>
</ul>
</td>
</tr>
</tbody>
</table>
<p>
Quick Assists are proposals available even if there is no problem or
warning. See the <a href='ref-java-editor-quickassist.htm'>Quick
Assist</a> page for more information.
</p>
<h3 class="related">Related concepts</h3>
<a href="../concepts/concept-java-editor.htm">Java editor</a><br />
<a href="../concepts/concept-quickfix-assist.htm">Quick fix and
assist</a>
<h3 class="related">Related references</h3>
<a href="ref-java-editor-quickassist.htm">Quick Assist</a><br />
<a href="ref-jdt-actions.htm">JDT actions</a>
</body>
</html>