Skip to content

Commit

Permalink
Takes care to not load any types that refer to new ones (#941)
Browse files Browse the repository at this point in the history
  • Loading branch information
adriancole authored Jul 5, 2019
1 parent 8604dc4 commit 025cf66
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 40 deletions.
53 changes: 19 additions & 34 deletions brave/src/main/java/brave/internal/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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}.
*
* <p>The upper 4-bytes are epoch seconds and the lower 4-bytes are random. This makes it
* convertible to <a href="http://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-request-tracing.html"></a>Amazon
Expand All @@ -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
Expand All @@ -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();
}
Expand Down
4 changes: 2 additions & 2 deletions brave/src/test/java/brave/internal/PlatformTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 025cf66

Please sign in to comment.