From 36b5670b3ddb5b2f7f70689f45d48538da1b0321 Mon Sep 17 00:00:00 2001 From: Emiel Tasseel Date: Sat, 26 Nov 2016 12:50:27 +0100 Subject: [PATCH] Fix performance issue where classes that don't exist in the current environment endlessly get looked up over and over again --- .../protocol/utility/CachedPackage.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/modules/API/src/main/java/com/comphenix/protocol/utility/CachedPackage.java b/modules/API/src/main/java/com/comphenix/protocol/utility/CachedPackage.java index e13fa5fd..e9e2616b 100644 --- a/modules/API/src/main/java/com/comphenix/protocol/utility/CachedPackage.java +++ b/modules/API/src/main/java/com/comphenix/protocol/utility/CachedPackage.java @@ -19,6 +19,7 @@ package com.comphenix.protocol.utility; import java.util.Map; +import com.google.common.base.Optional; import com.google.common.base.Preconditions; import com.google.common.base.Strings; import com.google.common.collect.Maps; @@ -29,7 +30,7 @@ import com.google.common.collect.Maps; * @author Kristian */ class CachedPackage { - private final Map> cache; + private final Map>> cache; private final String packageName; private final ClassSource source; @@ -50,7 +51,7 @@ class CachedPackage { * @param clazz - type of class. */ public void setPackageClass(String className, Class clazz) { - cache.put(className, clazz); + cache.put(className, Optional.>fromNullable(clazz)); } /** @@ -61,19 +62,24 @@ class CachedPackage { */ public Class getPackageClass(String className) { try { - Class result = cache.get(Preconditions.checkNotNull(className, "className cannot be NULL")); + Optional> result = cache.get(Preconditions.checkNotNull(className, "className cannot be NULL")); // Concurrency is not a problem - we don't care if we look up a class twice if (result == null) { // Look up the class dynamically - result = source.loadClass(combine(packageName, className)); - if (result == null) + result = Optional.>fromNullable(source.loadClass(combine(packageName, className))); + if (!result.isPresent()) throw new IllegalArgumentException("Source " + source + " returned NULL for " + className); cache.put(className, result); } - return result; + // Class has been looked for and hasn't been found in the past + if (!result.isPresent()) { + throw new ClassNotFoundException(); + } + + return result.get(); } catch (ClassNotFoundException e) { setPackageClass(className, null); throw new RuntimeException("Cannot find class " + combine(packageName, className), e);