FeaturesPluginsDocs & SupportCommunityPartners

Use Field Instead of Literal Refactoring Specification

Author: Hugues Ferland
$Id: usefieldinsteadofliteral.html,v 1.2 2005/11/09 04:13:26 hferland Exp $ (see full CVS history)

This refactoring goal is to provide an easy way to replace occurences of a literal in a class (or in classes) by a class field (static final) created or not at the same time.

Pre-Conditions

No pre-condition. It should be possible to enter every parameters in the refactoring dialog.

Pre-Checks

No pre-check.

Parameters

  1. The literal to replace (the type must be unambiguous). Default to the currently selected literal if any.
  2. The name of the field to create/use. No default.
    1. The name can be fully qualified to create/use the field in a specific class.
    2. The name can be the field name alone to create/use the field in the currently selected class, if any.
  3. Access
    1. If the field does not exist, the user choose one from "private", "protected", default or "public".
      Defaults to "private" if the field is to be created in the current class, "public" if in another class.
    2. If the field already exist, this parameter is disabled.
  4. Classes to apply the modification to. Default to the current class if any.


Fast Parameters Check

  1. Error: The field already exist but is not "static final".
  2. Error: The field does not exist, and the class where to create it is not in one of the openned project.
  3. Error: The field does not exist, and the class where to create the field is read only.
  4. Error: The field is not fully qualified, and no class is currently selected.

Parameters Check

No specific checks

Changes To Be Made

  1. If the field does not exist, create it in the specified class.
  2. For every selected class, find every occurence of the literal and replace them with a reference to the specified field.

Checks During Changes Preparation

  1. Only classes that has access to the field can be refactored.


Refactoring UI

Menu item: Use field instead of literal
Action title:
If a literal is under current cursor position
Use field instead of ...
If no literal is under current cursor position
Use field instead of literal
+-----------------------------------------------+
| Literal : [                         ]         |
| Field :   [                         ]         |
| Access :  [ public       |v]                  |
|           | protected      |                  |
|           | default        |                  |
|           | private        |                  |
|           +----------------+                  |
| Apply refactoring to :                        |
| +-------------------------------------------+ |
| | ( ) All opened projects                   | |
| | (o) Project: [Project 1               [v] | |
| | ( ) Package: [org.netbeans            [v] | |
| | ( ) Class:   <currently selected class>   | |
| +-------------------------------------------+ |
|                                               |
+-----------------------------------------------+

NOTES

  • When the access is changed, elements of the "apply refactoring to" options should be enabled/disabled accordingly.
  • Even though we may start the refactoring when the cursor is on a literal, the literal may still be changed from the original selected one. In which case, the original literal will not be modified.
  • If the specified field already exist (and is "static final", the Access field will be disabled and changed to reflect the current access of the field.
Companion
Projects:
MySQL Database Server   Open JDK: an Open SourceJDK   GlassFish Community: an Open Source Application Server    Mobile & Embedded Community    Open Solaris   java.net - The Source for Java Technology Collaboration   Virtual Box - full virtualizer  Open ESB - The Open Enterprise Service Bus Powered by