Skip to main content

ERROR_JAVA

Constant ERROR_JAVA 

Source
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 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 : new String[] {"java.lang.", "java.util.", "java.io.", ""}) {
            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 e) {
                // try the next package
            }
        }
        return Optional.empty();
    }

    /** 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.