Skip to content

Commit

Permalink
updated to work well with JDK's through 21. Removed many warnings fro…
Browse files Browse the repository at this point in the history
…m source files.
  • Loading branch information
jdereg committed Oct 13, 2023
1 parent 040f5a9 commit f9e8a15
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 828 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ Rare, hard-to-write utilities that are thoroughly tested (> 98% code coverage vi
To include in your project:
##### Gradle
```
implementation 'com.cedarsoftware:java-util:2.1.0'
implementation 'com.cedarsoftware:java-util:2.1.1'
```

##### Maven
```
<dependency>
<groupId>com.cedarsoftware</groupId>
<artifactId>java-util</artifactId>
<version>2.1.0</version>
<version>2.1.1</version>
</dependency>
```

Expand Down
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
### Revision History
* 2.1.1
* ReflectionUtils skips static fields, speeding it up and remove runtime warning (field SerialVersionUID). Supports JDK's up through 21.
* 2.1.0
* `DeepEquals.deepEquals(a, b)` compares Sets and Maps without regards to order per the equality spec.
* Updated all dependent libraries to latest versions as of 16 Sept 2023.
Expand Down
7 changes: 2 additions & 5 deletions 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>jar</packaging>
<version>2.1.0</version>
<version>2.1.1</version>
<description>Java Utilities</description>
<url>https://github.com/jdereg/java-util</url>

Expand Down Expand Up @@ -184,9 +184,6 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${version.plugin.surefire}</version>
<configuration>
<forkCount>0</forkCount>
</configuration>
</plugin>

</plugins>
Expand Down Expand Up @@ -224,7 +221,7 @@
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.24.2</version>
<version>${version.assertj}</version>
<scope>test</scope>
</dependency>

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/cedarsoftware/util/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
/**
* Handy conversion utilities. Convert from primitive to other primitives, plus support for Date, TimeStamp SQL Date,
* and the Atomic's.
*
* <p>
* `Converter.convert2*()` methods: If `null` passed in, primitive 'logical zero' is returned.
* Example: `Converter.convert(null, boolean.class)` returns `false`.
*
* <p>
* `Converter.convertTo*()` methods: if `null` passed in, `null` is returned. Allows "tri-state" Boolean.
* Example: `Converter.convert(null, Boolean.class)` returns `null`.
*
* <p>
* `Converter.convert()` converts using `convertTo*()` methods for primitive wrappers, and
* `convert2*()` methods for primitives.
*
* <p>
* @author John DeRegnaucourt ([email protected])
* <br>
* Copyright (c) Cedar Software LLC
Expand Down Expand Up @@ -58,8 +58,8 @@ public final class Converter
public static final Double DOUBLE_ONE = 1.0d;
public static final BigDecimal BIG_DECIMAL_ZERO = BigDecimal.ZERO;
public static final BigInteger BIG_INTEGER_ZERO = BigInteger.ZERO;
private static final Map<Class<?>, Work> conversion = new HashMap<>();
private static final Map<Class<?>, Work> conversionToString = new HashMap<>();
private static final Map<Class<?>, Work<?>> conversion = new HashMap<>();
private static final Map<Class<?>, Work<?>> conversionToString = new HashMap<>();

protected interface Work<T>
{
Expand Down
45 changes: 30 additions & 15 deletions src/main/java/com/cedarsoftware/util/ReflectionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public final class ReflectionUtils
private static final ConcurrentMap<String, Method> METHOD_MAP = new ConcurrentHashMap<>();
private static final ConcurrentMap<String, Method> METHOD_MAP2 = new ConcurrentHashMap<>();
private static final ConcurrentMap<String, Method> METHOD_MAP3 = new ConcurrentHashMap<>();
private static final ConcurrentMap<String, Constructor> CONSTRUCTORS = new ConcurrentHashMap<>();
private static final ConcurrentMap<String, Constructor<?>> CONSTRUCTORS = new ConcurrentHashMap<>();

private ReflectionUtils()
{
Expand Down Expand Up @@ -130,7 +130,7 @@ public static Method getMethod(Class<?> c, String methodName, Class<?>...types)
builder.append('.');
builder.append(methodName);
builder.append(makeParamKey(types));

// methodKey is in form ClassName.methodName:arg1.class|arg2.class|...
String methodKey = builder.toString();
Method method = METHOD_MAP.get(methodKey);
Expand Down Expand Up @@ -200,21 +200,36 @@ public static void getDeclaredFields(Class<?> c, Collection<Field> fields) {

for (Field field : local)
{
if (field.trySetAccessible())
int modifiers = field.getModifiers();
if (Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers))
{ // skip static and transient fields
continue;
}
String fieldName = field.getName();
if ("metaClass".equals(fieldName) && "groovy.lang.MetaClass".equals(field.getType().getName()))
{ // skip Groovy metaClass field if present (without tying this project to Groovy in any way).
continue;
}

if (fieldName.startsWith("this$"))
{ // Skip field in nested class pointing to enclosing outer class instance
continue;
}

if (Modifier.isPublic(modifiers))
{
fields.add(field);
}
else
{
int modifiers = field.getModifiers();
if (!Modifier.isStatic(modifiers) &&
!field.getName().startsWith("this$") &&
!Modifier.isTransient(modifiers))
{ // speed up: do not count static fields, do not go back up to enclosing object in nested case, do not consider transients
fields.add(field);
}
field.trySetAccessible();
fields.add(field);
}
}
}
catch (Throwable ignored)
catch (Throwable ignore)
{
ExceptionUtilities.safelyIgnoreException(ignored);
ExceptionUtilities.safelyIgnoreException(ignore);
}
}

Expand Down Expand Up @@ -337,7 +352,7 @@ public static Method getMethod(Object bean, String methodName, int argCount)
{
throw new IllegalArgumentException("Attempted to call getMethod() with a null method name on an instance of: " + bean.getClass().getName());
}
Class beanClass = bean.getClass();
Class<?> beanClass = bean.getClass();
StringBuilder builder = new StringBuilder(getClassLoaderName(beanClass));
builder.append('.');
builder.append(beanClass.getName());
Expand Down Expand Up @@ -366,7 +381,7 @@ public static Method getMethod(Object bean, String methodName, int argCount)
/**
* Reflectively find the requested method on the requested class, only matching on argument count.
*/
private static Method getMethodWithArgs(Class c, String methodName, int argc)
private static Method getMethodWithArgs(Class<?> c, String methodName, int argc)
{
Method[] methods = c.getMethods();
for (Method method : methods)
Expand Down Expand Up @@ -432,7 +447,7 @@ private static String makeParamKey(Class<?>... parameterTypes)
* @return Method instance found on the passed in class, or an IllegalArgumentException is thrown.
* @throws IllegalArgumentException
*/
public static Method getNonOverloadedMethod(Class clazz, String methodName)
public static Method getNonOverloadedMethod(Class<?> clazz, String methodName)
{
if (clazz == null)
{
Expand Down
Loading

0 comments on commit f9e8a15

Please sign in to comment.