Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it fully buildable on JDK 8 (including the StackWalker usage) #1483

Merged
merged 1 commit into from
Jan 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}

}