Skip to content

Commit

Permalink
* ClassUtilities.addPermanentClassAlias() - add an alias that `.for…
Browse files Browse the repository at this point in the history
…Name()` can use to instantiate class (e.g. "date" for `java.util.Date`)

* `ClassUtilities.removePermanentClassAlias()` - remove an alias that `.forName()` can no longer use.
* Updated build plug-in dependencies.
  • Loading branch information
jdereg committed Sep 29, 2024
1 parent a84eda1 commit 7b57fb3
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ Both of these features ensure that our library can be seamlessly integrated into
To include in your project:
##### Gradle
```groovy
implementation 'com.cedarsoftware:java-util:2.13.0'
implementation 'com.cedarsoftware:java-util:2.14.0'
```

##### Maven
```xml
<dependency>
<groupId>com.cedarsoftware</groupId>
<artifactId>java-util</artifactId>
<version>2.13.0</version>
<version>2.14.0</version>
</dependency>
```
---
Expand Down
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
### Revision History
#### 2.14.0
> * `ClassUtilities.addPermanentClassAlias()` - add an alias that `.forName()` can use to instantiate class (e.g. "date" for `java.util.Date`)
> * `ClassUtilities.removePermanentClassAlias()` - remove an alias that `.forName()` can no longer use.
> * Updated build plug-in dependencies.
#### 2.13.0
> * `LRUCache` improved garbage collection handling to avoid [gc Nepotism](https://psy-lob-saw.blogspot.com/2016/03/gc-nepotism-and-linked-queues.html?lr=1719181314858) issues by nulling out node references upon eviction. Pointed out by [Ben Manes](https://github.com/ben-manes).
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>com.cedarsoftware</groupId>
<artifactId>java-util</artifactId>
<packaging>bundle</packaging>
<version>2.14.0-SNAPSHOT</version>
<version>2.14.0</version>
<description>Java Utilities</description>
<url>https://github.com/jdereg/java-util</url>

Expand Down
32 changes: 26 additions & 6 deletions src/main/java/com/cedarsoftware/util/ClassUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,29 @@ public class ClassUtilities
wrapperMap.put(Boolean.class, boolean.class);
}

/**
* Add alias names for classes to allow .forName() to bring the class (.class) back with the alias name.
* Because the alias to class name mappings are static, it is expected that these are set up during initialization
* and not changed later.
* @param clazz Class to add an alias for
* @param alias String alias name
*/
public static void addPermanentClassAlias(Class<?> clazz, String alias)
{
nameToClass.put(alias, clazz);
}

/**
* Remove alias name for classes to prevent .forName() from fetching the class with the alias name.
* Because the alias to class name mappings are static, it is expected that these are set up during initialization
* and not changed later.
* @param alias String alias name
*/
public static void removePermanentClassAlias(String alias)
{
nameToClass.remove(alias);
}

/**
* Computes the inheritance distance between two classes/interfaces/primitive types.
* @param source The source class, interface, or primitive type.
Expand Down Expand Up @@ -183,8 +206,7 @@ public static boolean isPrimitive(Class<?> c)
* Compare two primitives.
* @return 0 if they are the same, -1 if not. Primitive wrapper classes are consider the same as primitive classes.
*/
private static int comparePrimitiveToWrapper(Class<?> source, Class<?> destination)
{
private static int comparePrimitiveToWrapper(Class<?> source, Class<?> destination) {
try
{
return source.getField("TYPE").get(null).equals(destination) ? 0 : -1;
Expand All @@ -201,8 +223,7 @@ private static int comparePrimitiveToWrapper(Class<?> source, Class<?> destinati
* @param classLoader ClassLoader to use when searching for JVM classes.
* @return Class instance of the named JVM class or null if not found.
*/
public static Class<?> forName(String name, ClassLoader classLoader)
{
public static Class<?> forName(String name, ClassLoader classLoader) {
if (name == null || name.isEmpty()) {
return null;
}
Expand Down Expand Up @@ -246,8 +267,7 @@ private static Class<?> internalClassForName(String name, ClassLoader classLoade
/**
* loadClass() provided by: Thomas Margreiter
*/
private static Class<?> loadClass(String name, ClassLoader classLoader) throws ClassNotFoundException
{
private static Class<?> loadClass(String name, ClassLoader classLoader) throws ClassNotFoundException {
String className = name;
boolean arrayType = false;
Class<?> primitiveArray = null;
Expand Down
13 changes: 12 additions & 1 deletion src/test/java/com/cedarsoftware/util/ClassUtilitiesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,18 @@ public void testClassForNameFailOnClassLoaderErrorFalse()
assert testObjectClass == null;
}

@Test
public void testClassUtilitiesAliases()
{
ClassUtilities.addPermanentClassAlias(HashMap.class, "mapski");
Class<?> x = ClassUtilities.forName("mapski", ClassUtilities.class.getClassLoader());
assert HashMap.class == x;

ClassUtilities.removePermanentClassAlias("mapski");
x = ClassUtilities.forName("mapski", ClassUtilities.class.getClassLoader());
assert x == null;
}

private static class AlternateNameClassLoader extends ClassLoader
{
AlternateNameClassLoader(String alternateName, Class<?> clazz)
Expand Down Expand Up @@ -222,5 +234,4 @@ protected Class<?> findClass(String className)
private final String alternateName;
private final Class<?> clazz;
}

}

0 comments on commit 7b57fb3

Please sign in to comment.