Add support for Java 11 to deprecated Timings API

In Java 11 the internal reflection method used to determine the calling
class in the legacy and deprecated Timings API, that no one should be
using anymore, was removed.

This means plugins that try and use it will all fail to enable and we
can't compile the server with JDK 11.

The solution to the removal of this internal reflection class is
to just use reflection to call it if its available and fall back to
the unnamed handler system if it isnt.

Update to the newer API if you use Timings already...
This commit is contained in:
Zach Brown 2018-09-29 14:05:24 -04:00
parent 9285b9cf1c
commit cfc7baeefb
No known key found for this signature in database
GPG key ID: CC9DA35FC5450B76

View file

@ -1,4 +1,4 @@
From 22b1908d5e4f9388dd422f0b2648cbc8b31851a8 Mon Sep 17 00:00:00 2001
From 4c6f902ec2700b7b46e6010142d22d0625a5a720 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Mon, 29 Feb 2016 18:48:17 -0600
Subject: [PATCH] Timings v2
@ -6,7 +6,7 @@ Subject: [PATCH] Timings v2
diff --git a/src/main/java/co/aikar/timings/FullServerTickHandler.java b/src/main/java/co/aikar/timings/FullServerTickHandler.java
new file mode 100644
index 00000000..98079dc0
index 000000000..98079dc0c
--- /dev/null
+++ b/src/main/java/co/aikar/timings/FullServerTickHandler.java
@@ -0,0 +1,82 @@
@ -94,7 +94,7 @@ index 00000000..98079dc0
+}
diff --git a/src/main/java/co/aikar/timings/NullTimingHandler.java b/src/main/java/co/aikar/timings/NullTimingHandler.java
new file mode 100644
index 00000000..8c43e206
index 000000000..8c43e2061
--- /dev/null
+++ b/src/main/java/co/aikar/timings/NullTimingHandler.java
@@ -0,0 +1,61 @@
@ -161,7 +161,7 @@ index 00000000..8c43e206
+}
diff --git a/src/main/java/co/aikar/timings/TimedEventExecutor.java b/src/main/java/co/aikar/timings/TimedEventExecutor.java
new file mode 100644
index 00000000..feddcdbd
index 000000000..feddcdbd4
--- /dev/null
+++ b/src/main/java/co/aikar/timings/TimedEventExecutor.java
@@ -0,0 +1,81 @@
@ -248,7 +248,7 @@ index 00000000..feddcdbd
+}
diff --git a/src/main/java/co/aikar/timings/Timing.java b/src/main/java/co/aikar/timings/Timing.java
new file mode 100644
index 00000000..b2260104
index 000000000..b2260104c
--- /dev/null
+++ b/src/main/java/co/aikar/timings/Timing.java
@@ -0,0 +1,76 @@
@ -330,7 +330,7 @@ index 00000000..b2260104
+}
diff --git a/src/main/java/co/aikar/timings/TimingData.java b/src/main/java/co/aikar/timings/TimingData.java
new file mode 100644
index 00000000..f222d6b7
index 000000000..f222d6b7d
--- /dev/null
+++ b/src/main/java/co/aikar/timings/TimingData.java
@@ -0,0 +1,120 @@
@ -456,7 +456,7 @@ index 00000000..f222d6b7
+}
diff --git a/src/main/java/co/aikar/timings/TimingHandler.java b/src/main/java/co/aikar/timings/TimingHandler.java
new file mode 100644
index 00000000..521c985e
index 000000000..521c985e6
--- /dev/null
+++ b/src/main/java/co/aikar/timings/TimingHandler.java
@@ -0,0 +1,200 @@
@ -662,7 +662,7 @@ index 00000000..521c985e
+}
diff --git a/src/main/java/co/aikar/timings/TimingHistory.java b/src/main/java/co/aikar/timings/TimingHistory.java
new file mode 100644
index 00000000..c2c2fb83
index 000000000..c2c2fb838
--- /dev/null
+++ b/src/main/java/co/aikar/timings/TimingHistory.java
@@ -0,0 +1,352 @@
@ -1020,7 +1020,7 @@ index 00000000..c2c2fb83
+}
diff --git a/src/main/java/co/aikar/timings/TimingHistoryEntry.java b/src/main/java/co/aikar/timings/TimingHistoryEntry.java
new file mode 100644
index 00000000..0e114eb3
index 000000000..0e114eb32
--- /dev/null
+++ b/src/main/java/co/aikar/timings/TimingHistoryEntry.java
@@ -0,0 +1,55 @@
@ -1081,7 +1081,7 @@ index 00000000..0e114eb3
+}
diff --git a/src/main/java/co/aikar/timings/TimingIdentifier.java b/src/main/java/co/aikar/timings/TimingIdentifier.java
new file mode 100644
index 00000000..63b4f318
index 000000000..63b4f318a
--- /dev/null
+++ b/src/main/java/co/aikar/timings/TimingIdentifier.java
@@ -0,0 +1,108 @@
@ -1195,7 +1195,7 @@ index 00000000..63b4f318
+}
diff --git a/src/main/java/co/aikar/timings/Timings.java b/src/main/java/co/aikar/timings/Timings.java
new file mode 100644
index 00000000..f907649b
index 000000000..f907649ba
--- /dev/null
+++ b/src/main/java/co/aikar/timings/Timings.java
@@ -0,0 +1,284 @@
@ -1485,7 +1485,7 @@ index 00000000..f907649b
+}
diff --git a/src/main/java/co/aikar/timings/TimingsCommand.java b/src/main/java/co/aikar/timings/TimingsCommand.java
new file mode 100644
index 00000000..56b10e89
index 000000000..56b10e898
--- /dev/null
+++ b/src/main/java/co/aikar/timings/TimingsCommand.java
@@ -0,0 +1,119 @@
@ -1610,7 +1610,7 @@ index 00000000..56b10e89
+}
diff --git a/src/main/java/co/aikar/timings/TimingsExport.java b/src/main/java/co/aikar/timings/TimingsExport.java
new file mode 100644
index 00000000..df7f4259
index 000000000..df7f42595
--- /dev/null
+++ b/src/main/java/co/aikar/timings/TimingsExport.java
@@ -0,0 +1,342 @@
@ -1958,10 +1958,10 @@ index 00000000..df7f4259
+}
diff --git a/src/main/java/co/aikar/timings/TimingsManager.java b/src/main/java/co/aikar/timings/TimingsManager.java
new file mode 100644
index 00000000..e0f3e07f
index 000000000..3443f9b71
--- /dev/null
+++ b/src/main/java/co/aikar/timings/TimingsManager.java
@@ -0,0 +1,187 @@
@@ -0,0 +1,188 @@
+/*
+ * This file is licensed under the MIT License (MIT).
+ *
@ -1995,6 +1995,7 @@ index 00000000..e0f3e07f
+import org.bukkit.plugin.java.PluginClassLoader;
+import co.aikar.util.LoadingMap;
+
+import javax.annotation.Nullable;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Collection;
@ -2137,7 +2138,7 @@ index 00000000..e0f3e07f
+ * @param clazz Class to check
+ * @return Plugin if created by a plugin
+ */
+ public static Plugin getPluginByClassloader(Class<?> clazz) {
+ public static Plugin getPluginByClassloader(@Nullable Class<?> clazz) {
+ if (clazz == null) {
+ return null;
+ }
@ -2151,7 +2152,7 @@ index 00000000..e0f3e07f
+}
diff --git a/src/main/java/co/aikar/timings/TimingsReportListener.java b/src/main/java/co/aikar/timings/TimingsReportListener.java
new file mode 100644
index 00000000..e7c389c0
index 000000000..e7c389c05
--- /dev/null
+++ b/src/main/java/co/aikar/timings/TimingsReportListener.java
@@ -0,0 +1,72 @@
@ -2229,7 +2230,7 @@ index 00000000..e7c389c0
+}
diff --git a/src/main/java/co/aikar/timings/UnsafeTimingHandler.java b/src/main/java/co/aikar/timings/UnsafeTimingHandler.java
new file mode 100644
index 00000000..5edaba12
index 000000000..5edaba128
--- /dev/null
+++ b/src/main/java/co/aikar/timings/UnsafeTimingHandler.java
@@ -0,0 +1,51 @@
@ -2286,7 +2287,7 @@ index 00000000..5edaba12
+}
diff --git a/src/main/java/co/aikar/util/Counter.java b/src/main/java/co/aikar/util/Counter.java
new file mode 100644
index 00000000..23ac07f2
index 000000000..23ac07f2c
--- /dev/null
+++ b/src/main/java/co/aikar/util/Counter.java
@@ -0,0 +1,35 @@
@ -2327,7 +2328,7 @@ index 00000000..23ac07f2
+}
diff --git a/src/main/java/co/aikar/util/JSONUtil.java b/src/main/java/co/aikar/util/JSONUtil.java
new file mode 100644
index 00000000..96274975
index 000000000..962749750
--- /dev/null
+++ b/src/main/java/co/aikar/util/JSONUtil.java
@@ -0,0 +1,129 @@
@ -2462,7 +2463,7 @@ index 00000000..96274975
+}
diff --git a/src/main/java/co/aikar/util/LoadingIntMap.java b/src/main/java/co/aikar/util/LoadingIntMap.java
new file mode 100644
index 00000000..24eae4be
index 000000000..24eae4bea
--- /dev/null
+++ b/src/main/java/co/aikar/util/LoadingIntMap.java
@@ -0,0 +1,71 @@
@ -2539,7 +2540,7 @@ index 00000000..24eae4be
+}
diff --git a/src/main/java/co/aikar/util/LoadingMap.java b/src/main/java/co/aikar/util/LoadingMap.java
new file mode 100644
index 00000000..1474384e
index 000000000..1474384e8
--- /dev/null
+++ b/src/main/java/co/aikar/util/LoadingMap.java
@@ -0,0 +1,340 @@
@ -2885,7 +2886,7 @@ index 00000000..1474384e
+}
diff --git a/src/main/java/co/aikar/util/MRUMapCache.java b/src/main/java/co/aikar/util/MRUMapCache.java
new file mode 100644
index 00000000..df592d85
index 000000000..df592d855
--- /dev/null
+++ b/src/main/java/co/aikar/util/MRUMapCache.java
@@ -0,0 +1,102 @@
@ -2992,7 +2993,7 @@ index 00000000..df592d85
+ }
+}
diff --git a/src/main/java/org/bukkit/Bukkit.java b/src/main/java/org/bukkit/Bukkit.java
index d6cdbdd8..242c6fba 100644
index d6cdbdd80..242c6fba7 100644
--- a/src/main/java/org/bukkit/Bukkit.java
+++ b/src/main/java/org/bukkit/Bukkit.java
@@ -567,7 +567,6 @@ public final class Bukkit {
@ -3004,7 +3005,7 @@ index d6cdbdd8..242c6fba 100644
/**
diff --git a/src/main/java/org/bukkit/Server.java b/src/main/java/org/bukkit/Server.java
index da4ced2d..33934411 100644
index da4ced2d6..33934411d 100644
--- a/src/main/java/org/bukkit/Server.java
+++ b/src/main/java/org/bukkit/Server.java
@@ -1083,12 +1083,27 @@ public interface Server extends PluginMessageRecipient {
@ -3038,7 +3039,7 @@ index da4ced2d..33934411 100644
*
diff --git a/src/main/java/org/bukkit/command/BufferedCommandSender.java b/src/main/java/org/bukkit/command/BufferedCommandSender.java
new file mode 100644
index 00000000..fd452bce
index 000000000..fd452bce4
--- /dev/null
+++ b/src/main/java/org/bukkit/command/BufferedCommandSender.java
@@ -0,0 +1,18 @@
@ -3061,7 +3062,7 @@ index 00000000..fd452bce
+ }
+}
diff --git a/src/main/java/org/bukkit/command/Command.java b/src/main/java/org/bukkit/command/Command.java
index 7ca5be84..86c78098 100644
index 7ca5be840..86c780982 100644
--- a/src/main/java/org/bukkit/command/Command.java
+++ b/src/main/java/org/bukkit/command/Command.java
@@ -32,7 +32,8 @@ public abstract class Command {
@ -3091,7 +3092,7 @@ index 7ca5be84..86c78098 100644
return true;
}
diff --git a/src/main/java/org/bukkit/command/FormattedCommandAlias.java b/src/main/java/org/bukkit/command/FormattedCommandAlias.java
index 50257883..9c80f464 100644
index 502578837..9c80f4640 100644
--- a/src/main/java/org/bukkit/command/FormattedCommandAlias.java
+++ b/src/main/java/org/bukkit/command/FormattedCommandAlias.java
@@ -9,6 +9,7 @@ public class FormattedCommandAlias extends Command {
@ -3114,7 +3115,7 @@ index 50257883..9c80f464 100644
}
diff --git a/src/main/java/org/bukkit/command/MessageCommandSender.java b/src/main/java/org/bukkit/command/MessageCommandSender.java
new file mode 100644
index 00000000..5527e7c8
index 000000000..5527e7c86
--- /dev/null
+++ b/src/main/java/org/bukkit/command/MessageCommandSender.java
@@ -0,0 +1,105 @@
@ -3224,7 +3225,7 @@ index 00000000..5527e7c8
+
+}
diff --git a/src/main/java/org/bukkit/command/SimpleCommandMap.java b/src/main/java/org/bukkit/command/SimpleCommandMap.java
index 1b5b37bf..bdc0de8c 100644
index 1b5b37bfe..bdc0de8ce 100644
--- a/src/main/java/org/bukkit/command/SimpleCommandMap.java
+++ b/src/main/java/org/bukkit/command/SimpleCommandMap.java
@@ -31,7 +31,7 @@ public class SimpleCommandMap implements CommandMap {
@ -3259,7 +3260,7 @@ index 1b5b37bf..bdc0de8c 100644
// Note: we don't return the result of target.execute as thats success / failure, we return handled (true) or not handled (false)
diff --git a/src/main/java/org/bukkit/command/defaults/TimingsCommand.java b/src/main/java/org/bukkit/command/defaults/TimingsCommand.java
deleted file mode 100644
index bba914d7..00000000
index bba914d7f..000000000
--- a/src/main/java/org/bukkit/command/defaults/TimingsCommand.java
+++ /dev/null
@@ -1,253 +0,0 @@
@ -3517,7 +3518,7 @@ index bba914d7..00000000
- // Spigot end
-}
diff --git a/src/main/java/org/bukkit/entity/Player.java b/src/main/java/org/bukkit/entity/Player.java
index 9252d4af..067e217e 100644
index 9252d4afa..067e217ea 100644
--- a/src/main/java/org/bukkit/entity/Player.java
+++ b/src/main/java/org/bukkit/entity/Player.java
@@ -1580,6 +1580,11 @@ public interface Player extends HumanEntity, Conversable, CommandSender, Offline
@ -3533,7 +3534,7 @@ index 9252d4af..067e217e 100644
@Override
diff --git a/src/main/java/org/bukkit/plugin/SimplePluginManager.java b/src/main/java/org/bukkit/plugin/SimplePluginManager.java
index 8b130abb..80c152ba 100644
index 8b130abb7..80c152ba9 100644
--- a/src/main/java/org/bukkit/plugin/SimplePluginManager.java
+++ b/src/main/java/org/bukkit/plugin/SimplePluginManager.java
@@ -293,7 +293,6 @@ public final class SimplePluginManager implements PluginManager {
@ -3590,7 +3591,7 @@ index 8b130abb..80c152ba 100644
}
}
diff --git a/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java b/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java
index 113b899c..81b42822 100644
index 113b899cc..81b428222 100644
--- a/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java
+++ b/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java
@@ -40,7 +40,6 @@ import org.bukkit.plugin.PluginLoader;
@ -3639,7 +3640,7 @@ index 113b899c..81b42822 100644
eventSet.add(new TimedRegisteredListener(listener, executor, eh.priority(), plugin, eh.ignoreCancelled()));
} else {
diff --git a/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java b/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java
index af8af8ef..44093af2 100644
index af8af8ef0..44093af29 100644
--- a/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java
+++ b/src/main/java/org/bukkit/plugin/java/PluginClassLoader.java
@@ -23,7 +23,8 @@ import org.bukkit.plugin.PluginDescriptionFile;
@ -3653,7 +3654,7 @@ index af8af8ef..44093af2 100644
private final Map<String, Class<?>> classes = new java.util.concurrent.ConcurrentHashMap<String, Class<?>>(); // Spigot
private final PluginDescriptionFile description;
diff --git a/src/main/java/org/bukkit/util/CachedServerIcon.java b/src/main/java/org/bukkit/util/CachedServerIcon.java
index 5ca863b3..04804706 100644
index 5ca863b36..048047067 100644
--- a/src/main/java/org/bukkit/util/CachedServerIcon.java
+++ b/src/main/java/org/bukkit/util/CachedServerIcon.java
@@ -12,4 +12,6 @@ import org.bukkit.event.server.ServerListPingEvent;
@ -3665,10 +3666,10 @@ index 5ca863b3..04804706 100644
+ public String getData(); // Spigot
+}
diff --git a/src/main/java/org/spigotmc/CustomTimingsHandler.java b/src/main/java/org/spigotmc/CustomTimingsHandler.java
index 8d982974..7e89b97b 100644
index 8d982974e..e9f76006e 100644
--- a/src/main/java/org/spigotmc/CustomTimingsHandler.java
+++ b/src/main/java/org/spigotmc/CustomTimingsHandler.java
@@ -1,165 +1,76 @@
@@ -1,165 +1,105 @@
+/*
+ * This file is licensed under the MIT License (MIT).
+ *
@ -3707,14 +3708,13 @@ index 8d982974..7e89b97b 100644
-import java.util.List;
-import java.util.Queue;
-import java.util.concurrent.ConcurrentLinkedQueue;
+import co.aikar.timings.NullTimingHandler;
+import co.aikar.timings.Timing;
+import co.aikar.timings.Timings;
+import co.aikar.timings.TimingsManager;
+import sun.reflect.Reflection;
-import org.bukkit.Bukkit;
-import org.bukkit.World;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.logging.Level;
@ -3732,6 +3732,8 @@ index 8d982974..7e89b97b 100644
+@Deprecated
+public final class CustomTimingsHandler {
+ private final Timing handler;
+ private static Boolean sunReflectAvailable;
+ private static Method getCallerClass;
- private static Queue<CustomTimingsHandler> HANDLERS = new ConcurrentLinkedQueue<CustomTimingsHandler>();
- /*========================================================================*/
@ -3744,24 +3746,25 @@ index 8d982974..7e89b97b 100644
- private long curTickTotal = 0;
- private long violations = 0;
+ public CustomTimingsHandler(String name) {
+ Timing timing;
+ if (sunReflectAvailable == null) {
+ String javaVer = System.getProperty("java.version");
+ String[] elements = javaVer.split("\\.");
- public CustomTimingsHandler(String name)
- {
- this( name, null );
- }
+ Plugin plugin = null;
+ try {
+ plugin = TimingsManager.getPluginByClassloader(Reflection.getCallerClass(2));
+ } catch (Exception ignored) {}
-
- public CustomTimingsHandler(String name, CustomTimingsHandler parent)
- {
- this.name = name;
- this.parent = parent;
- HANDLERS.add( this );
- }
-
+ int major = Integer.parseInt(elements.length >= 2 ? elements[1] : javaVer);
+ if (major <= 8) {
+ sunReflectAvailable = true;
- /**
- * Prints the timings and extra data to the given stream.
- *
@ -3777,21 +3780,18 @@ index 8d982974..7e89b97b 100644
- if ( count == 0 )
- {
- continue;
+ new AuthorNagException("Deprecated use of CustomTimingsHandler. Please Switch to Timings.of ASAP").printStackTrace();
+ if (plugin != null) {
+ timing = Timings.of(plugin, "(Deprecated API) " + name);
+ } else {
+ try {
+ final Method ofSafe = TimingsManager.class.getMethod("getHandler", String.class, String.class, Timing.class, boolean.class);
+ timing = (Timing) ofSafe.invoke("Minecraft", "(Deprecated API) " + name, null, true);
+ } catch (Exception e) {
+ Bukkit.getLogger().log(Level.SEVERE, "This handler could not be registered");
+ timing = Timings.NULL_HANDLER;
+ Class<?> reflection = Class.forName("sun.reflect.Reflection");
+ getCallerClass = reflection.getMethod("getCallerClass", int.class);
+ } catch (ClassNotFoundException | NoSuchMethodException ignored) {
+ }
+ } else {
+ sunReflectAvailable = false;
}
- long avg = time / count;
-
- printStream.println( " " + timings.name + " Time: " + time + " Count: " + count + " Avg: " + avg + " Violations: " + timings.violations );
}
- }
- printStream.println( "# Version " + Bukkit.getVersion() );
- int entities = 0;
- int livingEntities = 0;
@ -3799,11 +3799,10 @@ index 8d982974..7e89b97b 100644
- {
- entities += world.getEntities().size();
- livingEntities += world.getLivingEntities().size();
- }
}
- printStream.println( "# Entities " + entities );
- printStream.println( "# LivingEntities " + livingEntities );
+ handler = timing;
}
- }
- /**
- * Resets all timings.
@ -3815,12 +3814,15 @@ index 8d982974..7e89b97b 100644
- for ( CustomTimingsHandler timings : HANDLERS )
- {
- timings.reset();
- }
- }
+ Class calling = null;
+ if (sunReflectAvailable) {
+ try {
+ calling = (Class) getCallerClass.invoke(null, 2);
+ } catch (IllegalAccessException | InvocationTargetException ignored) {
}
}
- TimingsCommand.timingStart = System.nanoTime();
- }
+ public void startTiming() { handler.startTiming(); }
+ public void stopTiming() { handler.stopTiming(); }
- /**
- * Ticked every tick by CraftBukkit to count the number of times a timer
@ -3841,7 +3843,8 @@ index 8d982974..7e89b97b 100644
- }
- }
- }
-
+ Timing timing;
- /**
- * Starts timing to track a section of code.
- */
@ -3857,7 +3860,11 @@ index 8d982974..7e89b97b 100644
- }
- }
- }
-
+ Plugin plugin = null;
+ try {
+ plugin = TimingsManager.getPluginByClassloader(calling);
+ } catch (Exception ignored) {}
- /**
- * Stops timing a section of code.
- */
@ -3877,10 +3884,23 @@ index 8d982974..7e89b97b 100644
- if ( parent != null )
- {
- parent.stopTiming();
- }
- }
- }
-
+ new AuthorNagException("Deprecated use of CustomTimingsHandler. Please Switch to Timings.of ASAP").printStackTrace();
+ if (plugin != null) {
+ timing = Timings.of(plugin, "(Deprecated API) " + name);
+ } else {
+ try {
+ final Method ofSafe = TimingsManager.class.getDeclaredMethod("getHandler", String.class, String.class, Timing.class);
+ ofSafe.setAccessible(true);
+ timing = (Timing) ofSafe.invoke(null,"Minecraft", "(Deprecated API) " + name, null);
+ } catch (Exception e) {
+ e.printStackTrace();
+ Bukkit.getLogger().log(Level.SEVERE, "This handler could not be registered");
+ timing = Timings.NULL_HANDLER;
}
}
+ handler = timing;
}
- /**
- * Reset this timer, setting all values to zero.
- */
@ -3893,6 +3913,9 @@ index 8d982974..7e89b97b 100644
- start = 0;
- timingDepth = 0;
- }
+ public void startTiming() { handler.startTiming(); }
+ public void stopTiming() { handler.stopTiming(); }
+
}
--
2.19.0