Module: Rjb

Defined in:
lib/rjb.rb,
lib/rjb/list.rb,
lib/rjb/version.rb,
ext/rjb.c

Defined Under Namespace

Modules: Iterable, Iterator, JMethod, MODIFIER Classes: Rjb_JavaBridge, Rjb_JavaClass, Rjb_JavaProxy

Constant Summary collapse

JIterable =
import('java.lang.Iterable')
JIterator =
import('java.util.Iterator')
VERSION =
rb_str_new2(RJB_VERSION)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_classpath(jarname) ⇒ Object

Rjb::add_classpath(jarname)



2628
2629
2630
2631
2632
2633
2634
# File 'ext/rjb.c', line 2628

static VALUE rjb_s_add_classpath(VALUE self, VALUE jarname)
{
    VALUE cpath = rb_cvar_get(self, cvar_classpath);
    SafeStringValue(jarname);
    rb_ary_push(cpath, jarname);
    return cpath;
}

.add_jar(jarname) ⇒ Object

Rjb::add_jar(jarname)



2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
# File 'ext/rjb.c', line 2639

static VALUE rjb_s_add_jar(VALUE self, VALUE jarname)
{
    size_t i;
    JNIEnv* jenv;
    size_t count;
    jvalue args[2];

    if (rb_type(jarname) != T_ARRAY)
    {
        SafeStringValue(jarname);
        count = 0;
    }
    else
    {
        count = RARRAY_LEN(jarname);
    }
    jenv = rjb_prelude();
    if (!j_url_loader)
    {
        j_url_loader = (*jenv)->NewGlobalRef(jenv,
                                             (*jenv)->FindClass(jenv, "java/net/URLClassLoader"));
        RJB_LOAD_METHOD(rjb_load_class, j_url_loader, "loadClass",
                        "(Ljava/lang/String;)Ljava/lang/Class;");
        RJB_LOAD_METHOD(url_loader_new, j_url_loader, "<init>",
                        "([Ljava/net/URL;Ljava/lang/ClassLoader;)V");
        RJB_LOAD_METHOD(url_geturls, j_url_loader, "getURLs",
                        "()[Ljava/net/URL;");
        RJB_LOAD_METHOD(url_add_url, j_url_loader, "addURL",
                        "(Ljava/net/URL;)V");
    }
    if (!url_loader)
    {
        args[0].l = (*jenv)->NewObjectArray(jenv, (jsize)((count == 0) ? 1 : count), j_url, NULL);
        rjb_check_exception(jenv, 0);
        if (!count)
        {
            (*jenv)->SetObjectArrayElement(jenv, args[0].l, 0,
                                       conv_jarname_to_url(jenv, jarname));
        }
        else
        {
            for (i = 0; i < count; i++) {
                (*jenv)->SetObjectArrayElement(jenv, args[0].l, (jint)i,
                                       conv_jarname_to_url(jenv, rb_ary_entry(jarname, i)));
            }
        }
        rjb_check_exception(jenv, 0);
        args[1].l = get_class_loader(jenv);
        url_loader = (*jenv)->NewObjectA(jenv, j_url_loader, url_loader_new, args);
        rjb_check_exception(jenv, 0);
        (*jenv)->NewGlobalRef(jenv, url_loader);
        (*jenv)->DeleteLocalRef(jenv, args[0].l);
    }
    else
    {
        jvalue v;
        if (count)
        {
            for (i = 0; i < count; i++)
            {
                v.l = conv_jarname_to_url(jenv, rb_ary_entry(jarname, i));
                (*jenv)->CallObjectMethod(jenv, url_loader, url_add_url, v);
                rjb_check_exception(jenv, 0);
                (*jenv)->DeleteLocalRef(jenv, v.l);
            }
        }
        else
        {
            v.l = conv_jarname_to_url(jenv, jarname);
            (*jenv)->CallObjectMethod(jenv, url_loader, url_add_url, v);
            rjb_check_exception(jenv, 0);
            (*jenv)->DeleteLocalRef(jenv, v.l);
        }
    }
    return Qtrue;
}

.bind(rbobj, itfname) ⇒ Object

jclass Rjb::bind(rbobj, interface_name)



2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
# File 'ext/rjb.c', line 2465

static VALUE rjb_s_bind(VALUE self, VALUE rbobj, VALUE itfname)
{
    VALUE result = Qnil;
    jclass itf;
    JNIEnv* jenv = rjb_prelude();

    itf = rjb_find_class(jenv, itfname);
    rjb_check_exception(jenv, 1);
    if (itf)
    {
	struct rj_bridge* ptr = ALLOC(struct rj_bridge);
	memset(ptr, 0, sizeof(struct rj_bridge));
	ptr->bridge = (*jenv)->NewGlobalRef(jenv,
                                   (*jenv)->AllocObject(jenv, rjb_rbridge));
	if (!ptr->bridge)
	{
	    free(ptr);
	    rjb_check_exception(jenv, 1);
	    return Qnil;
	}
	ptr->proxy = (*jenv)->CallObjectMethod(jenv, ptr->bridge,
					       rjb_register_bridge, itf);
        ptr->proxy = (*jenv)->NewGlobalRef(jenv, ptr->proxy);
	ptr->wrapped = rbobj;
	result = Data_Wrap_Struct(rjbb, rj_bridge_mark, rj_bridge_free, ptr);
	rb_ary_push(proxies, result);
        rb_ivar_set(result, rb_intern("@wrapped"), rbobj);
    }
    return result;
}

.classesObject

return all classes that were already loaded. this method simply returns the global hash, but it’s safe because the hash was frozen.



1944
1945
1946
1947
# File 'ext/rjb.c', line 1944

static VALUE rjb_s_classes(VALUE self)
{
    return rjb_loaded_classes;
}

.import(cls) ⇒ Object



69
70
71
72
73
74
# File 'lib/rjb.rb', line 69

def self.import(cls)
  s_import(cls)
rescue NoSuchFieldError => e
  puts "#{cls}: #{e.inspect}" if $VERBOSE
  s_import(cls)
end

.load(*args) ⇒ Object

load Java Virtual Machine def load(class_path = ”, vmargs = []) class_path: passes for the class dir and jar name vmargs: strng array of vmarg (such as -Xrs)

change in rjb 0.1.7, omit first argument for JNI version.

because I misunderstood the number means (JVM but JNI).


1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
# File 'ext/rjb.c', line 1744

static VALUE rjb_s_load(int argc, VALUE* argv, VALUE self)
{
    JNIEnv* jenv;
    JavaVMInitArgs vm_args;
    jint res;
    VALUE classpath;
    VALUE user_path;
    VALUE vm_argv;
    char* userpath;
    ID stradd = rb_intern("<<");
    ID pathsep = rb_intern("PATH_SEPARATOR");
    int i;
    jclass jmethod;
    jclass jfield;
    jclass jconstructor;

    if (rjb_jvm)
    {
	return Qnil;
    }

    memset(&vm_args, 0, sizeof(vm_args));
    vm_args.version = JNI_VERSION_1_4;
    rb_scan_args(argc, argv, "02", &user_path, &vm_argv);
    if (!NIL_P(user_path))
    {
        Check_Type(user_path, T_STRING);
    }
    else
    {
	user_path = rb_str_new2(".");
    }
    classpath = rb_cvar_get(rjb, cvar_classpath);
    for (i = 0; i < RARRAY_LEN(classpath); i++)
    {
        rb_funcall(user_path, stradd, 1, rb_const_get(rb_cFile, pathsep));
        rb_funcall(user_path, stradd, 1, rb_ary_entry(classpath, 0));
    }
    userpath = StringValueCStr(user_path);

    if (!NIL_P(vm_argv))
    {
        Check_Type(vm_argv, T_ARRAY);
    }
    jenv = NULL;
    res = rjb_create_jvm(&jenv, &vm_args, userpath, vm_argv);
    if (res < 0)
    {
	rjb_jvm = NULL;
	rb_raise(rb_eRuntimeError, "can't create Java VM");
    } else {
        main_jenv = jenv;
    }

    RJB_FIND_CLASS(jconstructor, "java/lang/reflect/Constructor");
    RJB_LOAD_METHOD(ctrGetParameterTypes, jconstructor, "getParameterTypes", "()[Ljava/lang/Class;");
    RJB_FIND_CLASS(jmethod, "java/lang/reflect/Method");
    RJB_LOAD_METHOD(method_getModifiers, jmethod, "getModifiers", "()I");
    RJB_LOAD_METHOD(method_getName, jmethod, "getName", "()Ljava/lang/String;");
    RJB_LOAD_METHOD(getParameterTypes, jmethod, "getParameterTypes", "()[Ljava/lang/Class;");
    RJB_LOAD_METHOD(getReturnType, jmethod, "getReturnType", "()Ljava/lang/Class;");
    rjb_check_exception(jenv, 1);

    RJB_FIND_CLASS(jfield, "java/lang/reflect/Field");
    RJB_LOAD_METHOD(field_getModifiers, jfield, "getModifiers", "()I");
    RJB_LOAD_METHOD(field_getName, jfield, "getName", "()Ljava/lang/String;");
    RJB_LOAD_METHOD(field_getType, jfield, "getType", "()Ljava/lang/Class;");
    rjb_check_exception(jenv, 1);

    RJB_HOLD_CLASS(j_class, "java/lang/Class");
    RJB_LOAD_METHOD(rjb_class_getName, j_class, "getName", "()Ljava/lang/String;");
    rjb_check_exception(jenv, 1);

    RJB_HOLD_CLASS(rjb_j_throwable, "java/lang/Throwable");
    RJB_LOAD_METHOD(rjb_throwable_getMessage, rjb_j_throwable, "getMessage", "()Ljava/lang/String;");
    rjb_check_exception(jenv, 1);

    RJB_HOLD_CLASS(j_string, "java/lang/String");
    RJB_LOAD_METHOD(str_tostring, j_string, "toString", "()Ljava/lang/String;");
    rjb_check_exception(jenv, 1);

    RJB_HOLD_CLASS(j_object, "java/lang/Object");
    rjb_check_exception(jenv, 1);

    RJB_HOLD_CLASS(j_url, "java/net/URL");
    RJB_LOAD_METHOD(url_new, j_url, "<init>", "(Ljava/lang/String;)V");
    rjb_check_exception(jenv, 1);

    for (i = PRM_INT; i < PRM_LAST; i++)
    {
	jclass klass;
        RJB_FIND_CLASS(klass, jpcvt[i].classname);
	if (i == PRM_BOOLEAN)
	{
            RJB_LOAD_STATIC_METHOD(jpcvt[i].ctr_id, klass, "valueOf", jpcvt[i].ctrsig);
	}
	else if (jpcvt[i].ctrsig)
	{
            RJB_LOAD_METHOD(jpcvt[i].ctr_id, klass, "<init>", jpcvt[i].ctrsig);
	}
        RJB_LOAD_METHOD(jpcvt[i].to_prim_id, klass,
				   jpcvt[i].to_prim_method, jpcvt[i].prmsig);

        jpcvt[i].klass = (*jenv)->NewGlobalRef(jenv, klass);
    }

    jklass = import_class(jenv, j_class, rb_str_new2("java.lang.Class"));
    rb_define_method(rb_singleton_class(jklass), "forName", rjb_class_forname, -1);
    rb_define_alias(rb_singleton_class(jklass), "for_name", "forName");
    rb_gc_register_address(&jklass);

    return Qnil;
}

.loaded?Boolean

Returns:

  • (Boolean)


1934
1935
1936
1937
# File 'ext/rjb.c', line 1934

static VALUE rjb_s_loaded(VALUE self)
{
    return (rjb_jvm) ? Qtrue : Qfalse;
}

.primitive_conversionObject

For JRuby conpatible option



1961
1962
1963
1964
# File 'ext/rjb.c', line 1961

static VALUE rjb_s_get_pconversion(VALUE self)
{
    return primitive_conversion;
}

.primitive_conversion=(val) ⇒ Object

For JRuby conpatible option



1952
1953
1954
1955
1956
# File 'ext/rjb.c', line 1952

static VALUE rjb_s_set_pconversion(VALUE self, VALUE val)
{
    primitive_conversion = (RTEST(val)) ? Qtrue : Qfalse;
    return val;
}

.s_import(clsname) ⇒ Object

Jclass Rjb::import(classname)



2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
# File 'ext/rjb.c', line 2534

static VALUE rjb_s_import(VALUE self, VALUE clsname)
{
    JNIEnv* jenv;
    jclass jcls;
    VALUE v = rb_hash_aref(rjb_loaded_classes, clsname);
    if (v != Qnil)
    {
	return v;
    }

    jenv = rjb_prelude();
    jcls = rjb_find_class(jenv, clsname);
    if (!jcls)
    {
	rjb_check_exception(jenv, 0);
	rb_raise(rb_eRuntimeError, "`%s' not found", StringValueCStr(clsname));
    }
    v = import_class(jenv, jcls, clsname);
    return v;
}

.throwObject

.unbind(rbobj) ⇒ Object

jclass Rjb::bind(rbobj, interface_name)



2522
2523
2524
2525
2526
2527
2528
2529
# File 'ext/rjb.c', line 2522

static VALUE rjb_s_unbind(VALUE self, VALUE rbobj)
{
#if defined(RUBINIUS)
    return rb_funcall(proxies, rb_intern("delete"), 1, rbobj);
#else
    return rb_ary_delete(proxies, rbobj);
#endif
}

.unload(*args) ⇒ Object



1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
# File 'ext/rjb.c', line 1910

static VALUE rjb_s_unload(int argc, VALUE* argv, VALUE self)
{
    int result = 0;
#if defined(HAVE_RB_HASH_FOREACH) || defined(RUBINIUS)
	rb_hash_foreach(rjb_loaded_classes, clear_classes, 0);
#else
#if defined(RHASH_TBL)
    st_foreach(RHASH_TBL(rjb_loaded_classes), clear_classes, 0);
#else
    st_foreach(RHASH(rjb_loaded_classes)->tbl, clear_classes, 0);
#endif
#endif

    if (rjb_jvm)
    {
        JNIEnv* jenv = rjb_attach_current_thread();
        (*jenv)->ExceptionClear(jenv);
        result = (*rjb_jvm)->DestroyJavaVM(rjb_jvm);
        rjb_jvm = NULL;
        rjb_unload_vm();
    }
    return INT2NUM(result);
}

.urlsObject



2716
2717
2718
2719
2720
2721
2722
2723
2724
# File 'ext/rjb.c', line 2716

static VALUE rjb_s_urls(VALUE self)
{
    JNIEnv* jenv;
    jvalue ret;
    if (!url_loader) return Qnil;
    jenv = rjb_prelude();
    ret.l = (*jenv)->CallObjectMethod(jenv, url_loader, url_geturls);
    return jarray2rv(jenv, ret);
}

Instance Method Details

#import(cls) ⇒ Object



76
77
78
79
80
81
# File 'lib/rjb.rb', line 76

def import(cls)
  s_import(cls)
rescue NoSuchFieldError => e
  puts "#{cls}: #{e.inspect}" if $VERBOSE
  s_import(cls)
end