Skip to content

Commit

Permalink
[build] Allow JDK8 build by making traces utils compatible (#1483)
Browse files Browse the repository at this point in the history
(including the StackWalker usage, as a stub)
  • Loading branch information
bsideup authored and simonbasle committed Jan 8, 2019
1 parent ec9dcdb commit 41b50e4
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 103 deletions.
9 changes: 9 additions & 0 deletions reactor-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,13 @@ if (JavaVersion.current().java9Compatible) {
dependencies {
compileOnly sourceSets.java8stubs.output
}
}
else {
sourceSets {
java9stubs.java.srcDirs = ['src/main/java9stubs']
}

dependencies {
compileOnly sourceSets.java9stubs.output
}
}
88 changes: 86 additions & 2 deletions reactor-core/src/main/java/reactor/core/publisher/Traces.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

package reactor.core.publisher;

import java.util.Iterator;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -65,7 +66,7 @@ final class Traces {

static {
String[] strategyClasses = {
Traces.class.getPackage().getName() + ".StackWalkerCallSiteSupplierFactory",
Traces.class.getName() + "$StackWalkerCallSiteSupplierFactory",
Traces.class.getName() + "$SharedSecretsCallSiteSupplierFactory",
Traces.class.getName() + "$ExceptionCallSiteSupplierFactory",
};
Expand All @@ -87,6 +88,90 @@ final class Traces {
.orElseThrow(() -> new IllegalStateException("Valid strategy not found"));
}

/**
* Utility class for the call-site extracting on Java 9+.
*
*/
@SuppressWarnings("unused")
static final class StackWalkerCallSiteSupplierFactory implements Supplier<Supplier<String>> {

static {
// Trigger eager StackWalker class loading.
StackWalker.getInstance();
}

/**
* Transform the current stack trace into a {@link String} representation,
* each element being prepended with a tabulation and appended with a
* newline.
*
* @return the string version of the stacktrace.
*/
@Override
public Supplier<String> get() {
StackWalker.StackFrame[] stack = StackWalker.getInstance().walk(s -> {
StackWalker.StackFrame[] result = new StackWalker.StackFrame[10];
Iterator<StackWalker.StackFrame> iterator = s.iterator();
iterator.next(); // .get

int i = 0;
while (iterator.hasNext()) {
StackWalker.StackFrame frame = iterator.next();

if (i >= result.length) {
return new StackWalker.StackFrame[0];
}

result[i++] = frame;

if (isUserCode(frame.getClassName())) {
break;
}
}
StackWalker.StackFrame[] copy = new StackWalker.StackFrame[i];
System.arraycopy(result, 0, copy, 0, i);
return copy;
});

if (stack.length == 0) {
return () -> "";
}

if (stack.length == 1) {
return () -> "\t" + stack[0].toString() + "\n";
}

return () -> {
StringBuilder sb = new StringBuilder();

for (int j = stack.length - 2; j > 0; j--) {
StackWalker.StackFrame previous = stack[j];

if (!full) {
if (previous.isNativeMethod()) {
continue;
}

String previousRow = previous.getClassName() + "." + previous.getMethodName();
if (shouldSanitize(previousRow)) {
continue;
}
}
sb.append("\t")
.append(previous.toString())
.append("\n");
break;
}

sb.append("\t")
.append(stack[stack.length - 1].toString())
.append("\n");

return sb.toString();
};
}
}

@SuppressWarnings("unused")
static class SharedSecretsCallSiteSupplierFactory implements Supplier<Supplier<String>> {

Expand Down Expand Up @@ -303,5 +388,4 @@ else if (i == traces.size()) {

return apiLine + " ⇢ " + userCodeLine;
}

}

This file was deleted.

30 changes: 30 additions & 0 deletions reactor-core/src/main/java9stubs/java/lang/StackWalker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package java.lang;

import java.util.function.Function;
import java.util.stream.Stream;

import sun.reflect.CallerSensitive;

/**
* Stub for the Java 9 compatibility when compiled with JDK 8.
*/
public class StackWalker {

public static StackWalker getInstance() {
return null;
}

@CallerSensitive
public <T> T walk(Function<? super Stream<StackFrame>, ? extends T> function) {
return null;
}

public interface StackFrame {
String getClassName();

String getMethodName();

boolean isNativeMethod();
}

}

0 comments on commit 41b50e4

Please sign in to comment.