hier 0.1.0

A library supports JVM class hierarchy lookup by extending JNI interface
Documentation

Hier

Hier is a library supports JVM class hierarchy lookup by extending JNI interface.

Installation

To use this Hier, you'll need to have complete installation of JDK on machine (additionally, environment variables should be set, e.g. JAVA_HOME).

Usage

Get common super class of 2 classes

use hier::jni_env;
use hier::class::{ HierExt };

fn main() {
    let mut env = jni_env();
    let integer_class = env.lookup_class("java/lang/Integer").unwrap();
    let float_class = env.lookup_class("java/lang/Float").unwrap();
    let common_superclass = env.common_superclass(&integer_class, &float_class).unwrap();
    let cs_class_name = env.class_name(&common_superclass).unwrap();

    println!("{cs_class_name}");
}

Get derived interface of class

use hier::jni_env;
use hier::class::{ HierExt };

fn main() {
    let mut env = jni_env();
    let integer_class = env.lookup_class("java/lang/Integer").unwrap();
    let interfaces = env.interfaces(&integer_class).unwrap();
    let interface_names = interfaces.iter()
        .map(|interface_class| env.class_name(interface_class))
        .collect::<Result<_, Vec<_>>>()
        .unwrap();

    println!("{cs_class_name}");
}

Performance

Finding class through JNI costs huge amount of a performance. Hier caches all found classes, to use cached classes, use HierExt::lookup_class. To release the cache, use HierExt::free_lookup.

Additionally, HierExt::common_superclass always uses cached class to find.

License