pub const ERROR_JAVA: &str = r#"package rustanything;
import java.lang.reflect.Constructor;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
/** Generated by rust-anything: a port's error, thrown from Rust without a stack trace (cheap from native code). */
public final class RustAnythingError extends RuntimeException {
private static final long serialVersionUID = 1L;
private static final String[] PREFIXES = {
"", "java.lang.", "java.util.", "java.io.", "java.time.", "java.util.concurrent."
};
private static final ConcurrentHashMap<String, Optional<Constructor<? extends RuntimeException>>> CONSTRUCTORS = new ConcurrentHashMap<>();
public final String type;
public RustAnythingError(String type, String message) {
super(message, null, false, false);
this.type = type;
}
// Plain reflection, not MethodHandle: Android only supports MethodHandle.invokeExact from
// API 26, and these libraries run on older phones too.
private static Optional<Constructor<? extends RuntimeException>> constructor(String type) {
for (String prefix : PREFIXES) {
try {
Class<?> c = Class.forName(prefix + type);
if (RuntimeException.class.isAssignableFrom(c)) {
@SuppressWarnings("unchecked")
Constructor<? extends RuntimeException> ctor =
(Constructor<? extends RuntimeException>) c.getConstructor(String.class);
return Optional.of(ctor);
}
} catch (ReflectiveOperationException | IllegalArgumentException | LinkageError e) {
// try the next package
}
}
return Optional.empty();
}
/** Turns {@code "Type: message"} into that RuntimeException; anything else a RuntimeException. */
public static RuntimeException from(String message) {
if (message != null) {
int colon = message.indexOf(": ");
if (colon > 0 && isTypeName(message, colon)) {
return make(message.substring(0, colon), message.substring(colon + 2));
}
}
return new RuntimeException(message);
}
private static boolean isTypeName(String s, int end) {
char first = s.charAt(0);
if (!((first >= 'A' && first <= 'Z') || (first >= 'a' && first <= 'z'))) {
return false;
}
for (int i = 1; i < end; i++) {
char c = s.charAt(i);
boolean ok = (c >= 'A' && c <= 'Z')
|| (c >= 'a' && c <= 'z')
|| (c >= '0' && c <= '9')
|| c == '.'
|| c == '_'
|| c == '$';
if (!ok) {
return false;
}
}
return true;
}
/** The exception the original throws: `type` with the message, else a RuntimeException. */
public RuntimeException real() {
return make(type, getMessage());
}
private static final ThreadLocal<String[]> SLOTS = ThreadLocal.withInitial(() -> new String[2]);
/** This thread's error slot: the Rust side writes a failed call's type and message there. */
public static String[] slot() {
return SLOTS.get();
}
/** Throws the error a call left in `slot`, if any. */
public static void check(String[] slot) {
if (slot[0] != null) {
String type = slot[0];
String message = slot[1];
slot[0] = null;
slot[1] = null;
throw make(type, message);
}
}
private static RuntimeException make(String type, String message) {
Optional<Constructor<? extends RuntimeException>> ctor = CONSTRUCTORS.computeIfAbsent(type, RustAnythingError::constructor);
if (ctor.isPresent()) {
try {
return ctor.get().newInstance(message);
} catch (ReflectiveOperationException | LinkageError | SecurityException e) {
// fall through
}
}
return new RuntimeException(type + ": " + message);
}
}
"#;Expand description
rustanything/RustAnythingError.java: the error the Rust side throws, turned into the real
exception type in Java.