diff --git a/brave/src/main/java/brave/internal/Platform.java b/brave/src/main/java/brave/internal/Platform.java index b0c88eee7c..f5ee1a2e12 100644 --- a/brave/src/main/java/brave/internal/Platform.java +++ b/brave/src/main/java/brave/internal/Platform.java @@ -93,13 +93,24 @@ public void log(String msg, Object param1, @Nullable Throwable thrown) { /** Attempt to match the host runtime to a capable Platform implementation. */ static Platform findPlatform() { - Platform jre9 = Jre9.buildIfSupported(); - - if (jre9 != null) return jre9; - - Platform jre7 = Jre7.buildIfSupported(); + // Find JRE 9 new methods + try { + Class zoneId = Class.forName("java.time.ZoneId"); + Class.forName("java.time.Clock").getMethod("tickMillis", zoneId); + return new Jre9(); // intentionally doesn't not access the type prior to the above guard + } catch (ClassNotFoundException e) { + // pre JRE 8 + } catch (NoSuchMethodException e) { + // pre JRE 9 + } - if (jre7 != null) return jre7; + // Find JRE 7 new methods + try { + Class.forName("java.util.concurrent.ThreadLocalRandom"); + return new Jre7(); // intentionally doesn't not access the type prior to the above guard + } catch (ClassNotFoundException e) { + // pre JRE 7 + } // compatible with JRE 6 return new Jre6(); @@ -115,7 +126,8 @@ static Platform findPlatform() { public abstract long randomLong(); /** - * Returns the high 8-bytes for {@link brave.Tracing.Builder#traceId128Bit 128-bit trace IDs}. + * Returns the high 8-bytes for {@link brave.Tracing.Builder#traceId128Bit(boolean) 128-bit trace + * IDs}. * *
The upper 4-bytes are epoch seconds and the lower 4-bytes are random. This makes it * convertible to Amazon @@ -136,21 +148,6 @@ public Clock clock() { } static class Jre9 extends Jre7 { - - static Jre9 buildIfSupported() { - // Find JRE 9 new methods - try { - Class zoneId = Class.forName("java.time.ZoneId"); - Class.forName("java.time.Clock").getMethod("tickMillis", zoneId); - return new Jre9(); - } catch (ClassNotFoundException e) { - // pre JRE 8 - } catch (NoSuchMethodException e) { - // pre JRE 9 - } - return null; - } - @IgnoreJRERequirement @Override public Clock clock() { return new Clock() { // we could use jdk.internal.misc.VM to do this more efficiently, but it is internal @@ -171,18 +168,6 @@ static Jre9 buildIfSupported() { } static class Jre7 extends Platform { - - static Jre7 buildIfSupported() { - // Find JRE 7 new methods - try { - Class.forName("java.util.concurrent.ThreadLocalRandom"); - return new Jre7(); - } catch (ClassNotFoundException e) { - // pre JRE 7 - } - return null; - } - @IgnoreJRERequirement @Override public String getHostString(InetSocketAddress socket) { return socket.getHostString(); } diff --git a/brave/src/test/java/brave/internal/PlatformTest.java b/brave/src/test/java/brave/internal/PlatformTest.java index d489195209..1048a89f77 100644 --- a/brave/src/test/java/brave/internal/PlatformTest.java +++ b/brave/src/test/java/brave/internal/PlatformTest.java @@ -42,7 +42,7 @@ @PowerMockIgnore({"org.apache.logging.*", "javax.script.*"}) @PrepareForTest({Platform.class, NetworkInterface.class}) public class PlatformTest { - Platform platform = Platform.Jre7.buildIfSupported(); + Platform platform = new Platform.Jre7(); @Test public void clock_hasNiceToString_jre7() { assertThat(platform.clock()) @@ -62,7 +62,7 @@ public class PlatformTest { when(System.currentTimeMillis()) .thenReturn(1465510280_000L); // Thursday, June 9, 2016 10:11:20 PM - long traceIdHigh = Platform.Jre7.buildIfSupported().nextTraceIdHigh(); + long traceIdHigh = platform.nextTraceIdHigh(); assertThat(HexCodec.toLowerHex(traceIdHigh)).startsWith("5759e988"); } diff --git a/instrumentation/benchmarks/src/main/java/brave/internal/PlatformBenchmarks.java b/instrumentation/benchmarks/src/main/java/brave/internal/PlatformBenchmarks.java index bc70a3ed9a..45caa31b62 100644 --- a/instrumentation/benchmarks/src/main/java/brave/internal/PlatformBenchmarks.java +++ b/instrumentation/benchmarks/src/main/java/brave/internal/PlatformBenchmarks.java @@ -36,8 +36,8 @@ @OutputTimeUnit(TimeUnit.MICROSECONDS) public class PlatformBenchmarks { static final Platform jre6 = new Platform.Jre6(); - static final Platform jre7 = Platform.Jre7.buildIfSupported(); - static final Platform jre9 = Platform.Jre9.buildIfSupported(); + static final Platform jre7 = new Platform.Jre7(); + static final Platform jre9 = new Platform.Jre9(); static final Clock jre7Clock = jre7.clock(); static final Clock jre9Clock = jre9.clock(); diff --git a/instrumentation/servlet/src/main/java/brave/servlet/ServletRuntime.java b/instrumentation/servlet/src/main/java/brave/servlet/ServletRuntime.java index 0bf8335213..e24079e5fe 100644 --- a/instrumentation/servlet/src/main/java/brave/servlet/ServletRuntime.java +++ b/instrumentation/servlet/src/main/java/brave/servlet/ServletRuntime.java @@ -63,7 +63,7 @@ private static ServletRuntime findServletRuntime() { try { Class.forName("javax.servlet.AsyncEvent"); HttpServletRequest.class.getMethod("isAsyncStarted"); - return new Servlet3(); + return new Servlet3(); // intentionally doesn't not access the type prior to the above guard } catch (NoSuchMethodException e) { // pre Servlet v3 } catch (ClassNotFoundException e) { diff --git a/instrumentation/spring-webmvc/src/main/java/brave/spring/webmvc/WebMvcRuntime.java b/instrumentation/spring-webmvc/src/main/java/brave/spring/webmvc/WebMvcRuntime.java index b98f90940d..be2ac8b506 100644 --- a/instrumentation/spring-webmvc/src/main/java/brave/spring/webmvc/WebMvcRuntime.java +++ b/instrumentation/spring-webmvc/src/main/java/brave/spring/webmvc/WebMvcRuntime.java @@ -42,7 +42,7 @@ static WebMvcRuntime findWebMvcRuntime() { // Find spring-webmvc v3.1 new methods try { Class.forName("org.springframework.web.method.HandlerMethod"); - return new WebMvc31(); + return new WebMvc31(); // intentionally doesn't not access the type prior to the above guard } catch (ClassNotFoundException e) { // pre spring-webmvc v3.1 }