How is it implemented? Java Editor Hint: Change variable type fix
By sandipchitale on May 11, 2007
In the previous entry I talked about the Change Variable Type fix. In this entry I will talk about how it is implemented.
Registration of the factory
Like many other features in NetBeans the hints/fixes/suggestions are implemented using a factory mechanism. The factory is registered in the module's layer file like this:
The factory is registered under the org-netbeans-modules-java-hints/rules/errors folder because it creates a fix for certain compilation errors such as type mismatch of the left and right side of a vatriable declaration statement.
Implementation of the factory
As the user edits the source code, the Java editor incrementally runs the javac in the background. As the javac emits compilation errors the editor shows the red squiggly lines. Aside from that it also invokes the factories registered to handle the particular compilation errors. In this case org.netbeans.modules.javahints.ChangeTypeCreator is one such factory. The ChangeTypeCreator implements the interface org.netbeans.modules.java.hints.spi.ErrorRule. This intreface essentially has two important method:
/\*\* Get the diagnostic codes this rule should run on
public Set<String> getCodes();
/\*\* Return possible fixes for a given diagnostic report.
public List<Fix> run(CompilationInfo compilationInfo,
The getCodes() method essentially indicates the set of compilatioin error codes that are handled by the factory. The ChangeTypeCreator handles:
These codes are part of standard error codes emitted by the javac.
In the run(...) method of ChangeTypeCreator,enough information about the context in the editor is passed in. The ChangeTypeCreator uses the APIs to determine the types of expressions on the left and right side of a variable assignmenets and returns an instance of org.netbeans.modules.javahints.ChangeTypeHint. This instance is used to populate the list of fixes that appear as actions in the light bulb pop up menu in the left margin of the editor.
Implementation of the Change variable type fix
The ChangeTypeHint which implements org.netbeans.spi.editor.hints.Fix . It provides the text of menu item which is returned from:
public String getText()
The actual fix is implemented in the method:
public ChangeInfo implement()
using the Java Source APIs.