Fix backwards compat with nullable classes

Fixes #499
This commit is contained in:
Dan Mulloy 2018-08-03 22:07:25 -04:00
parent d112e9b1dc
commit 9df0dd48e4
3 changed files with 2171 additions and 2172 deletions

View File

@ -63,21 +63,16 @@ class CachedPackage {
* @return Class object.
* @throws RuntimeException If we are unable to find the given class.
*/
public Optional<Class<?>> getPackageClass(String className) {
public Optional<Class<?>> getPackageClass(final String className) {
Preconditions.checkNotNull(className, "className cannot be null!");
Optional<Class<?>> result = cache.get(className);
if (result == null) {
return cache.computeIfAbsent(className, x -> {
try {
Class<?> clazz = source.loadClass(combine(packageName, className));
result = Optional.ofNullable(clazz);
cache.put(className, result);
return Optional.ofNullable(source.loadClass(combine(packageName, className)));
} catch (ClassNotFoundException ex) {
cache.put(className, Optional.empty());
return Optional.empty();
}
}
return result;
});
}
/**

View File

@ -1,8 +1,7 @@
package com.comphenix.protocol.utility;
import static com.comphenix.protocol.utility.TestUtils.assertItemsEqual;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
@ -75,6 +74,11 @@ public class MinecraftReflectionTest {
MinecraftReflection.getBukkitEntity("Hello");
}
@Test
public void testNullable() {
assertNull(MinecraftReflection.getNullableNMS("ProtocolLib"));
}
@Test
public void testAttributeSnapshot() {
assertEquals(AttributeSnapshot.class, MinecraftReflection.getAttributeSnapshotClass());