Class RenameHandler
This handles type and enum constant renames. For example, use as follows:
RenameHandler.INSTANCE.renamedType("org.joda.OldName", NewName.class);
RenameHandler.INSTANCE.renamedEnum("CORRECT", Status.VALID);
RenameHandler.INSTANCE.renamedEnum("INCORRECT", Status.INVALID);
From v2.1, renames can be stored on the classpath in configuration files.
The file location is META-INF/org/joda/convert/Renamed.ini.
All files found in this location are read and processed.
The format has two sections [types] and [enums].
The [types] section has lines of the format oldClassName = newClassName.
The [enums] section has lines of the format oldEnumConstantName = enumClassName.newEnumConstantName.
Lines starting with # are treated as comments.
The recommended usage is to edit the static singleton before using other classes.
Editing a static is acceptable because renames are driven by bytecode which is static.
For additional security, an application should lock the rename handler instance
once any types and enums have been registered using lock().
This class is thread-safe with concurrent caches.
- Since:
- 1.6
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ConcurrentHashMap<Class<?>, Map<String, Enum<?>>> The enum renames.static final RenameHandlerA mutable global instance.private booleanThe lock flag.private static final booleanErrors in class initialization are hard to debug.private final ConcurrentHashMap<String, Class<?>> The type renames. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate voidstatic RenameHandlercreate()Creates an instance.static RenameHandlercreate(boolean loadFromClasspath) Creates an instance, providing the ability to load types in config files.private static RenameHandlergetEnumRenames(Class<?> type) Gets the map of renamed for an enum type.Gets the set of enum types that have renames.Gets the map of renamed types.private voidloadRenameFile(URL url) voidlock()Locks this instance of the rename handler.<T extends Enum<T>>
TlookupEnum(Class<T> type, String name) Lookup an enum from a name, handling renames.Class<?> lookupType(String name) Lookup a type from a name, handling renames.private voidparseRenameFile(List<String> lines, URL url) voidrenamedEnum(String oldName, Enum<?> currentValue) Register the fact that an enum constant was renamed.voidrenamedType(String oldName, Class<?> currentValue) Register the fact that a type was renamed.toString()
-
Field Details
-
LOG
private static final boolean LOGErrors in class initialization are hard to debug. Set -Dorg.joda.convert.debug=true on the command line to add extra logging to System.errNOTE! This also forces
StringConvertto be loaded before callingcreateInstance()which is vital to avoid horrid ordering issues when loading Renamed.ini classes that referenceStringConvert. -
INSTANCE
A mutable global instance. This is a singleton instance which is mutated. It will be populated by the contents of theRenamed.iniconfiguration files. -
locked
private volatile boolean lockedThe lock flag. -
typeRenames
The type renames. -
enumRenames
The enum renames.
-
-
Constructor Details
-
RenameHandler
private RenameHandler()Restricted constructor.
-
-
Method Details
-
createInstance
-
create
Creates an instance.This is not normally used as the preferred option is to edit the singleton.
- Returns:
- a new instance, not null
-
create
Creates an instance, providing the ability to load types in config files.This is not normally used as the preferred option is to edit the singleton.
If the flag is set to true, the classpath config files will be used to register types and enums.
- Parameters:
loadFromClasspath- whether to load any types in classpath config files- Returns:
- a new instance, not null
-
renamedType
Register the fact that a type was renamed.This handles the use case where a class is renamed.
- Parameters:
oldName- the old name of the type including the package name, not nullcurrentValue- the current type, not null
-
getTypeRenames
Gets the map of renamed types.An empty map is returned if there are no renames.
- Returns:
- a copy of the set of enum types with renames, not null
-
lookupType
Lookup a type from a name, handling renames.- Parameters:
name- the name of the type to lookup, not null- Returns:
- the type, not null
- Throws:
ClassNotFoundException- if the name is not a valid type
-
renamedEnum
Register the fact that an enum constant was renamed.This handles the use case where an enum constant is renamed, but the enum class remains the same.
- Parameters:
oldName- the old name of the enum constant, not nullcurrentValue- the current enum constant, not null
-
getEnumTypesWithRenames
Gets the set of enum types that have renames.An empty set is returned if there are no renames.
- Returns:
- a copy of the set of enum types with renames, not null
-
getEnumRenames
Gets the map of renamed for an enum type.An empty map is returned if there are no renames.
- Parameters:
type- the enum type, not null- Returns:
- a copy of the set of enum renames, not null
-
lookupEnum
Lookup an enum from a name, handling renames.- Type Parameters:
T- the type of the desired enum- Parameters:
type- the enum type, not nullname- the name of the enum to lookup, not null- Returns:
- the enum value, not null
- Throws:
IllegalArgumentException- if the name is not a valid enum constant
-
lock
public void lock()Locks this instance of the rename handler.For additional security, an application should lock the rename handler once any types and enums have been registered.
-
checkNotLocked
private void checkNotLocked() -
loadFromClasspath
private void loadFromClasspath() -
loadRenameFile
- Throws:
IOException
-
parseRenameFile
-
toString
-