-
Notifications
You must be signed in to change notification settings - Fork 33
/
jpl.pl
5422 lines (4417 loc) · 187 KB
/
jpl.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Part of JPL -- SWI-Prolog/Java interface
Author: Paul Singleton, Fred Dushin and Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (c) 2004-2020, Paul Singleton
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
:- module(jpl,
[ jpl_get_default_jvm_opts/1,
jpl_set_default_jvm_opts/1,
jpl_get_actual_jvm_opts/1,
jpl_pl_lib_version/1,
jpl_c_lib_version/1,
jpl_pl_syntax/1,
jpl_new/3,
jpl_call/4,
jpl_get/3,
jpl_set/3,
jpl_servlet_byref/3,
jpl_servlet_byval/3,
jpl_class_to_classname/2,
jpl_class_to_type/2,
jpl_classname_to_class/2,
jpl_classname_to_type/2, % name does not reflect that it deals with entity names
jpl_datum_to_type/2,
jpl_entityname_to_type/2, % new alias for jpl_classname_to_type/2
jpl_false/1,
jpl_is_class/1,
jpl_is_false/1,
jpl_is_null/1,
jpl_is_object/1,
jpl_is_object_type/1,
jpl_is_ref/1,
jpl_is_true/1,
jpl_is_type/1,
jpl_is_void/1,
jpl_null/1,
jpl_object_to_class/2,
jpl_object_to_type/2,
jpl_primitive_type/1,
jpl_ref_to_type/2,
jpl_true/1,
jpl_type_to_class/2,
jpl_type_to_classname/2, % name does not reflect that it deals with entity names
jpl_type_to_entityname/2, % new alias for jpl_type_to_classname/2
jpl_void/1,
jpl_array_to_length/2,
jpl_array_to_list/2,
jpl_datums_to_array/2,
jpl_enumeration_element/2,
jpl_enumeration_to_list/2,
jpl_hashtable_pair/2,
jpl_iterator_element/2,
jpl_list_to_array/2,
jpl_terms_to_array/2,
jpl_array_to_terms/2,
jpl_map_element/2,
jpl_set_element/2
]).
:- autoload(library(apply),[maplist/2]).
:- use_module(library(debug),[assertion/1, debugging/1,debug/3]).
:- use_module(library(yall)).
:- autoload(library(lists),
[member/2,nth0/3,nth1/3,append/3,flatten/2,select/3]).
:- autoload(library(shlib),[load_foreign_library/1]).
/** <module> A Java interface for SWI Prolog 7.x
The library(jpl) provides a bidirectional interface to a Java Virtual Machine.
@see http://jpl7.org/
*/
% suppress debugging this library
:- set_prolog_flag(generate_debug_info, false).
%! jpl_new(+X, +Params, -V) is det.
%
% X can be:
% * an atomic classname, e.g. =|'java.lang.String'|=
% * or an atomic descriptor, e.g. =|'[I'|= or =|'Ljava.lang.String;'|=
% * or a suitable type, i.e. any class(_,_) or array(_), e.g. class([java,util],['Date'])
%
% If X is an object (non-array) type or descriptor and Params is a
% list of values or references, then V is the result of an invocation
% of that type's most specifically-typed constructor to whose
% respective formal parameters the actual Params are assignable (and
% assigned).
%
% If X is an array type or descriptor and Params is a list of values
% or references, each of which is (independently) assignable to the
% array element type, then V is a new array of as many elements as
% Params has members, initialised with the respective members of
% Params.
%
% If X is an array type or descriptor and Params is a non-negative
% integer N, then V is a new array of that type, with N elements, each
% initialised to Java's appropriate default value for the type.
%
% If V is literally =|{Term}|= then we attempt to convert a
% =|new org.jpl7.Term|= instance to
% a corresponding term; this is of little obvious use here, but is
% consistent with jpl_call/4 and jpl_get/3.
jpl_new(X, Params, V) :-
( var(X)
-> throwme(jpl_new,x_is_var)
; jpl_is_type(X) % NB Should check for "instantiable type"? Also accepts "double" for example.
-> Type = X
; atom(X) % an atom not captured by jpl_is_type/1 e.g. 'java.lang.String', '[L', even "void"
-> ( jpl_entityname_to_type(X, Type)
-> true
; throwme(jpl_new,x_not_classname(X))
)
; throwme(jpl_new,x_not_instantiable(X))
),
jpl_new_1(Type, Params, Vx),
( nonvar(V),
V = {Term} % yucky way of requesting Term->term conversion
-> ( jni_jref_to_term(Vx, TermX) % fails if Vx is not a JRef to a org.jpl7.Term
-> Term = TermX
; throwme(jpl_new,not_a_jpl_term(Vx))
)
; V = Vx
).
%! jpl_new_1(+Tx, +Params, -Vx)
%
% (serves only jpl_new/3)
%
% Tx can be a class(_,_) or array(_) type.
%
% Params must be a proper list of constructor parameters.
%
% At exit, Vx is bound to a JPL reference to a new, initialised instance of Tx
jpl_new_1(class(Ps,Cs), Params, Vx) :-
!, % green (see below)
Tx = class(Ps,Cs),
( var(Params)
-> throwme(jpl_new_class,params_is_var)
; \+ is_list(Params)
-> throwme(jpl_new_class,params_is_not_list(Params))
; true
),
length(Params, A), % the "arity" of the required constructor
jpl_type_to_class(Tx, Cx), % throws Java exception if class is not found
N = '<init>', % JNI's constructor naming convention for GetMethodID()
Tr = void, % all constructors have this return "type"
findall(
z3(I,MID,Tfps),
jpl_method_spec(Tx, I, N, A, _Mods, MID, Tr, Tfps), % cached
Z3s
),
( Z3s == [] % no constructors which require the given qty of parameters?
-> ( jpl_call(Cx, isInterface, [], @(true))
-> throwme(jpl_new_class,class_is_interface(Tx))
; throwme(jpl_new_class,class_without_constructor(Tx,A))
)
; ( catch(
jpl_datums_to_types(Params, Taps), % infer actual parameter types
% 2020-07-21: make catcher's 1st context arg an "anonvar" instead of a overspecified predicate indicator
error(type_error(acyclic,Te),context(_,Msg)),
throwme(jpl_new_class,acyclic(Te,Msg)) % rethrow
)
-> true
; throwme(jpl_new_class,bad_jpl_datum(Params))
),
findall(
z3(I,MID,Tfps), % select constructors to which actual parameters are assignable
( member(z3(I,MID,Tfps), Z3s),
jpl_types_fit_types(Taps, Tfps) % assignability test: actual parameter types "fit" formal parameter types?
),
Z3sA
),
( Z3sA == [] % no type-assignable constructors?
-> ( Z3s = [_]
-> throwme(jpl_new_class,single_constructor_mismatch(Tx/A))
; throwme(jpl_new_class,any_constructor_mismatch(Params))
)
; Z3sA = [z3(I,MID,Tfps)]
-> true
; jpl_z3s_to_most_specific_z3(Z3sA, z3(I,MID,Tfps))
-> true
; throwme(jpl_new_class,constructor_multimatch(Params))
)
),
catch(
jNewObject(Cx, MID, Tfps, Params, Vx),
error(java_exception(_), 'java.lang.InstantiationException'),
throwme(jpl_new_class,class_is_abstract(Tx)) % Rethrow
),
jpl_cache_type_of_ref(Tx, Vx). % since we know it
jpl_new_1(array(T), Params, Vx) :-
!,
( var(Params)
-> throwme(jpl_new_array,params_is_var)
; integer(Params) % integer I -> array[0..I-1] of default values
-> ( Params >= 0
-> Len is Params
; throwme(jpl_new_array,params_is_negative(Params))
)
; is_list(Params) % [V1,..VN] -> array[0..N-1] of respective values
-> length(Params, Len)
),
jpl_new_array(T, Len, Vx), % NB may throw out-of-memory exception
( nth0(I, Params, Param), % nmember fails silently when Params is integer
jpl_set(Vx, I, Param),
fail
; true
),
jpl_cache_type_of_ref(array(T), Vx). % since we know it
jpl_new_1(T, _Params, _Vx) :- % doomed attempt to create new primitive type instance (formerly a dubious completist feature :-)
jpl_primitive_type(T),
!,
throwme(jpl_new_primitive,primitive_type_requested(T)).
% ( var(Params)
% -> throwme(jpl_new_primitive,params_is_var)
% ; Params == []
% -> jpl_primitive_type_default_value(T, Vx)
% ; Params = [Param]
% -> jpl_primitive_type_term_to_value(T, Param, Vx)
% ; throwme(jpl_new_primitive,params_is_bad(Params))
% ).
jpl_new_1(T, _, _) :- throwme(jpl_new_catchall,catchall(T)).
%! jpl_new_array(+ElementType, +Length, -NewArray) is det
%
% binds NewArray to a jref to a newly created Java array of ElementType and Length
jpl_new_array(boolean, Len, A) :-
jNewBooleanArray(Len, A).
jpl_new_array(byte, Len, A) :-
jNewByteArray(Len, A).
jpl_new_array(char, Len, A) :-
jNewCharArray(Len, A).
jpl_new_array(short, Len, A) :-
jNewShortArray(Len, A).
jpl_new_array(int, Len, A) :-
jNewIntArray(Len, A).
jpl_new_array(long, Len, A) :-
jNewLongArray(Len, A).
jpl_new_array(float, Len, A) :-
jNewFloatArray(Len, A).
jpl_new_array(double, Len, A) :-
jNewDoubleArray(Len, A).
jpl_new_array(array(T), Len, A) :-
jpl_type_to_class(array(T), C),
jNewObjectArray(Len, C, @(null), A). % initialise each element to null
jpl_new_array(class(Ps,Cs), Len, A) :-
jpl_type_to_class(class(Ps,Cs), C),
jNewObjectArray(Len, C, @(null), A).
%! jpl_call(+X, +MethodName:atom, +Params:list(datum), -Result:datum) is det
%
% X should be either
% * an object reference, e.g. =|<jref>(1552320)|= (for static or instance methods)
% * or a classname, e.g. =|'java.util.Date'|= (for static methods only)
% * or a descriptor, e.g. =|'Ljava.util.Date;'|= (for static methods only)
% * or type, e.g. =|class([java,util],['Date'])|= (for static methods only)
%
% MethodName should be a method name (as an atom) (may involve dynamic overload resolution based on inferred types of params)
%
% Params should be a proper list (perhaps empty) of suitable actual parameters for the named method.
%
% The class or object may have several methods with the given name;
% JPL will resolve (per call) to the most appropriate method based on the quantity and inferred types of Params.
% This resolution mimics the corresponding static resolution performed by Java compilers.
%
% Finally, an attempt will be made to unify Result with the method's returned value,
% or with =|@(void)|= (the compound term with name =|@|= and argument =|void|=) if it has none.
jpl_call(X, Mspec, Params, R) :-
( jpl_object_to_type(X, Type) % the usual case (goal fails safely if X is var or rubbish)
-> Obj = X,
Kind = instance
; var(X)
-> throwme(jpl_call,arg1_is_var)
; atom(X)
-> ( jpl_entityname_to_type(X, Type) % does this attempt to load the class?
-> ( jpl_type_to_class(Type, ClassObj)
-> Kind = static
; throwme(jpl_call,no_such_class(X))
)
; throwme(jpl_call,arg1_is_bad(X))
)
; X = class(_,_)
-> Type = X,
jpl_type_to_class(Type, ClassObj),
Kind = static
; X = array(_)
-> throwme(jpl_call,arg1_is_array(X))
; throwme(jpl_call,arg1_is_bad(X))
),
( atom(Mspec) % the usual case, i.e. a method name
-> true
; var(Mspec)
-> throwme(jpl_call,mspec_is_var)
; throwme(jpl_call,mspec_is_bad(Mspec))
),
( is_list(Params)
-> ( catch(
jpl_datums_to_types(Params, Taps),
% 2020-07-21: make catcher's 1st context arg an "anonvar" instead of a overspecified predicate indicator
error(type_error(acyclic,Te),context(_,Msg)),
throwme(jpl_call,acyclic(Te,Msg)) % rethrow
)
-> true
; throwme(jpl_call,nonconvertible_params(Params))
),
length(Params, A)
; var(Params)
-> throwme(jpl_call,arg3_is_var)
; throwme(jpl_call,arg3_is_bad(Params))
),
( Kind == instance
-> jpl_call_instance(Type, Obj, Mspec, Params, Taps, A, Rx)
; jpl_call_static(Type, ClassObj, Mspec, Params, Taps, A, Rx)
),
( nonvar(R),
R = {Term} % yucky way of requesting Term->term conversion
-> ( jni_jref_to_term(Rx, TermX) % fails if Rx isn't a JRef to a org.jpl7.Term
-> Term = TermX
; throwme(jpl_call,not_a_jpl_term(Rx))
)
; R = Rx
).
%! jpl_call_instance(+ObjectType, +Object, +MethodName, +Params, +ActualParamTypes, +Arity, -Result)
%
% calls the MethodName-d method (instance or static) of Object (which is of ObjectType),
% which most specifically applies to Params,
% which we have found to be (respectively) of ActualParamTypes,
% and of which there are Arity, yielding Result.
jpl_call_instance(Type, Obj, Mname, Params, Taps, A, Rx) :-
findall( % get remaining details of all accessible methods of Obj's class (as denoted by Type)
z5(I,Mods,MID,Tr,Tfps),
jpl_method_spec(Type, I, Mname, A, Mods, MID, Tr, Tfps),
Z5s
),
( Z5s = []
-> throwme(jpl_call_instance,no_such_method(Mname/A))
; findall(
z5(I,Mods,MID,Tr,Tfps), % those to which Params is assignable
( member(z5(I,Mods,MID,Tr,Tfps), Z5s),
jpl_types_fit_types(Taps, Tfps) % assignability test: actual param types "fit" formal param types
),
Z5sA % Params-assignable methods
),
( Z5sA == []
-> throwme(jpl_call_instance,param_not_assignable(Params))
; Z5sA = [z5(I,Mods,MID,Tr,Tfps)]
-> true % exactly one applicable method
; jpl_z5s_to_most_specific_z5(Z5sA, z5(I,Mods,MID,Tr,Tfps))
-> true % exactly one most-specific applicable method
; throwme(jpl_call_instance,multiple_most_specific(Mname/Params))
)
),
( member(static, Mods) % if the chosen method is static
-> jpl_object_to_class(Obj, ClassObj), % get a java.lang.Class instance which personifies Obj's class
jpl_call_static_method(Tr, ClassObj, MID, Tfps, Params, Rx) % call static method w.r.t. associated Class object
; jpl_call_instance_method(Tr, Obj, MID, Tfps, Params, Rx) % else call (non-static) method w.r.t. object itself
).
%! jpl_call_static(+ClassType, +ClassObject, +MethodName, +Params, +ActualParamTypes, +Arity, -Result)
%
% calls the MethodName-d static method of the class (which is of ClassType,
% and which is represented by the java.lang.Class instance ClassObject)
% which most specifically applies to Params,
% which we have found to be (respectively) of ActualParamTypes,
% and of which there are Arity, yielding Result.
jpl_call_static(Type, ClassObj, Mname, Params, Taps, A, Rx) :-
findall( % get all accessible static methods of the class denoted by Type and ClassObj
z5(I,Mods,MID,Tr,Tfps),
( jpl_method_spec(Type, I, Mname, A, Mods, MID, Tr, Tfps),
member(static, Mods)
),
Z5s
),
( Z5s = []
-> throwme(jpl_call_static,no_such_method(Mname))
; findall(
z5(I,Mods,MID,Tr,Tfps),
( member(z5(I,Mods,MID,Tr,Tfps), Z5s),
jpl_types_fit_types(Taps, Tfps) % assignability test: actual param types "fit" formal param types
),
Z5sA % Params-assignable methods
),
( Z5sA == []
-> throwme(jpl_call_static,param_not_assignable(Params))
; Z5sA = [z5(I,Mods,MID,Tr,Tfps)]
-> true % exactly one applicable method
; jpl_z5s_to_most_specific_z5(Z5sA, z5(I,Mods,MID,Tr,Tfps))
-> true % exactly one most-specific applicable method
; throwme(jpl_call_instance,multiple_most_specific(Mname/Params))
)
),
jpl_call_static_method(Tr, ClassObj, MID, Tfps, Params, Rx).
%! jpl_call_instance_method(+Type, +ClassObject, +MethodID, +FormalParamTypes, +Params, -Result)
jpl_call_instance_method(void, Class, MID, Tfps, Ps, R) :-
jCallVoidMethod(Class, MID, Tfps, Ps),
jpl_void(R).
jpl_call_instance_method(boolean, Class, MID, Tfps, Ps, R) :-
jCallBooleanMethod(Class, MID, Tfps, Ps, R).
jpl_call_instance_method(byte, Class, MID, Tfps, Ps, R) :-
jCallByteMethod(Class, MID, Tfps, Ps, R).
jpl_call_instance_method(char, Class, MID, Tfps, Ps, R) :-
jCallCharMethod(Class, MID, Tfps, Ps, R).
jpl_call_instance_method(short, Class, MID, Tfps, Ps, R) :-
jCallShortMethod(Class, MID, Tfps, Ps, R).
jpl_call_instance_method(int, Class, MID, Tfps, Ps, R) :-
jCallIntMethod(Class, MID, Tfps, Ps, R).
jpl_call_instance_method(long, Class, MID, Tfps, Ps, R) :-
jCallLongMethod(Class, MID, Tfps, Ps, R).
jpl_call_instance_method(float, Class, MID, Tfps, Ps, R) :-
jCallFloatMethod(Class, MID, Tfps, Ps, R).
jpl_call_instance_method(double, Class, MID, Tfps, Ps, R) :-
jCallDoubleMethod(Class, MID, Tfps, Ps, R).
jpl_call_instance_method(array(_), Class, MID, Tfps, Ps, R) :-
jCallObjectMethod(Class, MID, Tfps, Ps, R).
jpl_call_instance_method(class(_,_), Class, MID, Tfps, Ps, R) :-
jCallObjectMethod(Class, MID, Tfps, Ps, R).
%! jpl_call_static_method(+Type, +ClassObject, +MethodID, +FormalParamTypes, +Params, -Result)
jpl_call_static_method(void, Class, MID, Tfps, Ps, R) :-
jCallStaticVoidMethod(Class, MID, Tfps, Ps),
jpl_void(R).
jpl_call_static_method(boolean, Class, MID, Tfps, Ps, R) :-
jCallStaticBooleanMethod(Class, MID, Tfps, Ps, R).
jpl_call_static_method(byte, Class, MID, Tfps, Ps, R) :-
jCallStaticByteMethod(Class, MID, Tfps, Ps, R).
jpl_call_static_method(char, Class, MID, Tfps, Ps, R) :-
jCallStaticCharMethod(Class, MID, Tfps, Ps, R).
jpl_call_static_method(short, Class, MID, Tfps, Ps, R) :-
jCallStaticShortMethod(Class, MID, Tfps, Ps, R).
jpl_call_static_method(int, Class, MID, Tfps, Ps, R) :-
jCallStaticIntMethod(Class, MID, Tfps, Ps, R).
jpl_call_static_method(long, Class, MID, Tfps, Ps, R) :-
jCallStaticLongMethod(Class, MID, Tfps, Ps, R).
jpl_call_static_method(float, Class, MID, Tfps, Ps, R) :-
jCallStaticFloatMethod(Class, MID, Tfps, Ps, R).
jpl_call_static_method(double, Class, MID, Tfps, Ps, R) :-
jCallStaticDoubleMethod(Class, MID, Tfps, Ps, R).
jpl_call_static_method(array(_), Class, MID, Tfps, Ps, R) :-
jCallStaticObjectMethod(Class, MID, Tfps, Ps, R).
jpl_call_static_method(class(_,_), Class, MID, Tfps, Ps, R) :-
jCallStaticObjectMethod(Class, MID, Tfps, Ps, R).
%! jpl_get(+X, +Fspec, -V:datum) is det
%
% X can be
%
% * a classname
% * or a descriptor
% * or an (object or array) type (for static fields)
% * or a non-array object (for static and non-static fields)
% * or an array (for 'length' pseudo field, or indexed element retrieval)
%
% Fspec can be
%
% * an atomic field name
% * or an integral array index (to get an element from an array)
% * or a pair I-J of integers (to get a subrange of an array).
%
% Finally, an attempt will be made to unify V with the retrieved value or object reference.
%
% Examples
%
% ==
% jpl_get('java.awt.Cursor', 'NE_RESIZE_CURSOR', Q).
% Q = 7.
%
% jpl_new(array(class([java,lang],['String'])), [for,while,do,if,then,else,try,catch,finally], A),
% jpl_get(A, 3-5, B).
% B = [if, then, else].
% ==
jpl_get(X, Fspec, V) :-
( jpl_object_to_type(X, Type)
-> Obj = X,
jpl_get_instance(Type, Type, Obj, Fspec, Vx) % pass Type twice for FAI
; var(X)
-> throwme(jpl_get,arg1_is_var)
; jpl_is_type(X) % e.g. class([java,lang],['String']), array(int)
-> Type = X,
( jpl_type_to_class(Type, ClassObj)
-> jpl_get_static(Type, ClassObj, Fspec, Vx)
; throwme(jpl_get,named_class_not_found(Type))
)
; atom(X)
-> ( jpl_entityname_to_type(X, Type) % does this attempt to load the class? (NO!)
-> ( jpl_type_to_class(Type, ClassObj)
-> jpl_get_static(Type, ClassObj, Fspec, Vx)
; throwme(jpl_get,named_class_not_found(Type))
)
; throwme(jpl_get,arg1_is_bad(X))
)
; throwme(jpl_get,arg1_is_bad_2(X))
),
( nonvar(V),
V = {Term} % yucky way of requesting Term->term conversion
-> ( jni_jref_to_term(Vx, TermX) % fails if Rx is not a JRef to a org.jpl7.Term
-> Term = TermX
; throwme(jpl_get,not_a_jpl_term(X))
)
; V = Vx
).
%! jpl_get_static(+Type:type, +ClassObject:jref, +FieldName:atom, -Value:datum) is det
%
% ClassObject is an instance of java.lang.Class which represents
% the same class as Type; Value (Vx below) is guaranteed unbound
% on entry, and will, before exit, be unified with the retrieved
% value
jpl_get_static(Type, ClassObj, Fname, Vx) :-
( atom(Fname) % assume it's a field name
-> true
; var(Fname)
-> throwme(jpl_get_static,arg2_is_var)
; throwme(jpl_get_static,arg2_is_bad(Fname))
),
% get static fields of the denoted class
findall(
z4(I,Mods,FID,Tf),
( jpl_field_spec(Type, I, Fname, Mods, FID, Tf),
member(static, Mods)
),
Z4s
),
( Z4s = []
-> throwme(jpl_get_static,no_such_field(Fname))
; Z4s = [z4(I,_Mods,FID,Tf)]
-> jpl_get_static_field(Tf, ClassObj, FID, Vx)
; throwme(jpl_get_static,multiple_fields(Fname))
).
%! jpl_get_instance(+Type, +Type, +Object, +FieldSpecifier, -Value) is det
jpl_get_instance(class(_,_), Type, Obj, Fname, Vx) :-
( atom(Fname) % the usual case
-> true
; var(Fname)
-> throwme(jpl_get_instance,arg2_is_var)
; throwme(jpl_get_instance,arg2_is_bad(Fname))
),
findall(
z4(I,Mods,FID,Tf),
jpl_field_spec(Type, I, Fname, Mods, FID, Tf),
Z4s
),
( Z4s = []
-> throwme(jpl_get_instance,no_such_field(Fname))
; Z4s = [z4(I,Mods,FID,Tf)]
-> ( member(static, Mods)
-> jpl_object_to_class(Obj, ClassObj),
jpl_get_static_field(Tf, ClassObj, FID, Vx)
; jpl_get_instance_field(Tf, Obj, FID, Vx)
)
; throwme(jpl_get_instance,multiple_fields(Fname))
).
jpl_get_instance(array(ElementType), _, Array, Fspec, Vx) :-
( var(Fspec)
-> throwme(jpl_get_instance_array,arg2_is_var)
; integer(Fspec)
-> ( Fspec < 0 % lo bound check
-> throwme(jpl_get_instance_array,arg2_is_bad(Fspec))
; jGetArrayLength(Array, Len),
Fspec >= Len % hi bound check
-> throwme(jpl_get_instance_array,arg2_is_too_large(Fspec))
; jpl_get_array_element(ElementType, Array, Fspec, Vx)
)
; Fspec = N-M % NB should we support e.g. 3-2 -> [] ?
-> ( integer(N),
integer(M)
-> ( N >= 0,
M >= N
-> jGetArrayLength(Array, Len),
( N >= Len
-> throwme(jpl_get_instance_array,bad_range_low(N-M))
; M >= Len
-> throwme(jpl_get_instance_array,bad_range_high(N-M))
; jpl_get_array_elements(ElementType, Array, N, M, Vx)
)
; throwme(jpl_get_instance_array,bad_range_pair_values(N-M))
)
; throwme(jpl_get_instance_array,bad_range_pair_types(N-M))
)
; atom(Fspec)
-> ( Fspec == length % special-case for this solitary array "method"
-> jGetArrayLength(Array, Vx)
; throwme(jpl_get_instance_array,no_such_field(Fspec))
)
; throwme(jpl_get_instance_array,wrong_spec(Fspec))
).
%! jpl_get_array_element(+ElementType:type, +Array:jref, +Index, -Vc) is det
%
% Array is a JPL reference to a Java array of ElementType; Vc is
% (unified with a JPL repn of) its Index-th (numbered from 0)
% element Java values are now converted to Prolog terms within
% foreign code
%
% @tbd more of this could be done within foreign code
jpl_get_array_element(Type, Array, Index, Vc) :-
( ( Type = class(_,_)
; Type = array(_)
)
-> jGetObjectArrayElement(Array, Index, Vr)
; jpl_primitive_type(Type)
-> jni_type_to_xput_code(Type, Xc),
jni_alloc_buffer(Xc, 1, Bp), % one-element buf for a Type
jpl_get_primitive_array_region(Type, Array, Index, 1, Bp),
jni_fetch_buffer_value(Bp, 0, Vr, Xc), % zero-th element
jni_free_buffer(Bp)
),
Vr = Vc. % redundant since Vc is always (?) unbound at call
%! jpl_get_array_elements(+ElementType, +Array, +N, +M, -Vs)
%
% serves only jpl_get_instance/5
%
% Vs will always be unbound on entry
jpl_get_array_elements(ElementType, Array, N, M, Vs) :-
( ( ElementType = class(_,_)
; ElementType = array(_)
)
-> jpl_get_object_array_elements(Array, N, M, Vs)
; jpl_get_primitive_array_elements(ElementType, Array, N, M, Vs)
).
jpl_get_instance_field(boolean, Obj, FieldID, V) :-
jGetBooleanField(Obj, FieldID, V).
jpl_get_instance_field(byte, Obj, FieldID, V) :-
jGetByteField(Obj, FieldID, V).
jpl_get_instance_field(char, Obj, FieldID, V) :-
jGetCharField(Obj, FieldID, V).
jpl_get_instance_field(short, Obj, FieldID, V) :-
jGetShortField(Obj, FieldID, V).
jpl_get_instance_field(int, Obj, FieldID, V) :-
jGetIntField(Obj, FieldID, V).
jpl_get_instance_field(long, Obj, FieldID, V) :-
jGetLongField(Obj, FieldID, V).
jpl_get_instance_field(float, Obj, FieldID, V) :-
jGetFloatField(Obj, FieldID, V).
jpl_get_instance_field(double, Obj, FieldID, V) :-
jGetDoubleField(Obj, FieldID, V).
jpl_get_instance_field(class(_,_), Obj, FieldID, V) :-
jGetObjectField(Obj, FieldID, V).
jpl_get_instance_field(array(_), Obj, FieldID, V) :-
jGetObjectField(Obj, FieldID, V).
%! jpl_get_object_array_elements(+Array, +LoIndex, +HiIndex, -Vcs) is det
%
% Array should be a (zero-based) array of some object (array or
% non-array) type; LoIndex is an integer, 0 =< LoIndex <
% length(Array); HiIndex is an integer, LoIndex-1 =< HiIndex <
% length(Array); at call, Vcs will be unbound; at exit, Vcs will be a
% list of (references to) the array's elements [LoIndex..HiIndex]
% inclusive
jpl_get_object_array_elements(Array, Lo, Hi, Vcs) :-
( Lo =< Hi
-> Vcs = [Vc|Vcs2],
jGetObjectArrayElement(Array, Lo, Vc),
Next is Lo+1,
jpl_get_object_array_elements(Array, Next, Hi, Vcs2)
; Vcs = []
).
%! jpl_get_primitive_array_elements(+ElementType, +Array, +LoIndex, +HiIndex, -Vcs) is det.
%
% Array should be a (zero-based) Java array of (primitive)
% ElementType; Vcs should be unbound on entry, and on exit will be
% a list of (JPL representations of the values of) the elements
% [LoIndex..HiIndex] inclusive
jpl_get_primitive_array_elements(ElementType, Array, Lo, Hi, Vcs) :-
Size is Hi-Lo+1,
( Size == 0
-> Vcs = []
; jni_type_to_xput_code(ElementType, Xc),
jni_alloc_buffer(Xc, Size, Bp),
jpl_get_primitive_array_region(ElementType, Array, Lo, Size, Bp),
jpl_primitive_buffer_to_array(ElementType, Xc, Bp, 0, Size, Vcs),
jni_free_buffer(Bp)
).
jpl_get_primitive_array_region(boolean, Array, Lo, S, I) :-
jGetBooleanArrayRegion(Array, Lo, S, jbuf(I,boolean)).
jpl_get_primitive_array_region(byte, Array, Lo, S, I) :-
jGetByteArrayRegion(Array, Lo, S, jbuf(I,byte)).
jpl_get_primitive_array_region(char, Array, Lo, S, I) :-
jGetCharArrayRegion(Array, Lo, S, jbuf(I,char)).
jpl_get_primitive_array_region(short, Array, Lo, S, I) :-
jGetShortArrayRegion(Array, Lo, S, jbuf(I,short)).
jpl_get_primitive_array_region(int, Array, Lo, S, I) :-
jGetIntArrayRegion(Array, Lo, S, jbuf(I,int)).
jpl_get_primitive_array_region(long, Array, Lo, S, I) :-
jGetLongArrayRegion(Array, Lo, S, jbuf(I,long)).
jpl_get_primitive_array_region(float, Array, Lo, S, I) :-
jGetFloatArrayRegion(Array, Lo, S, jbuf(I,float)).
jpl_get_primitive_array_region(double, Array, Lo, S, I) :-
jGetDoubleArrayRegion(Array, Lo, S, jbuf(I,double)).
jpl_get_static_field(boolean, Array, FieldID, V) :-
jGetStaticBooleanField(Array, FieldID, V).
jpl_get_static_field(byte, Array, FieldID, V) :-
jGetStaticByteField(Array, FieldID, V).
jpl_get_static_field(char, Array, FieldID, V) :-
jGetStaticCharField(Array, FieldID, V).
jpl_get_static_field(short, Array, FieldID, V) :-
jGetStaticShortField(Array, FieldID, V).
jpl_get_static_field(int, Array, FieldID, V) :-
jGetStaticIntField(Array, FieldID, V).
jpl_get_static_field(long, Array, FieldID, V) :-
jGetStaticLongField(Array, FieldID, V).
jpl_get_static_field(float, Array, FieldID, V) :-
jGetStaticFloatField(Array, FieldID, V).
jpl_get_static_field(double, Array, FieldID, V) :-
jGetStaticDoubleField(Array, FieldID, V).
jpl_get_static_field(class(_,_), Array, FieldID, V) :-
jGetStaticObjectField(Array, FieldID, V).
jpl_get_static_field(array(_), Array, FieldID, V) :-
jGetStaticObjectField(Array, FieldID, V).
%! jpl_set(+X, +Fspec, +V) is det.
%
% sets the Fspec-th field of (class or object) X to value V iff it is assignable
%
% X can be
% * a class instance (for static or non-static fields)
% * or an array (for indexed element or subrange assignment)
% * or a classname, or a class(_,_) or array(_) type (for static fields)
% * but not a String (no fields to retrieve)
%
% Fspec can be
% * an atomic field name (overloading through shadowing has yet to be handled properly)
% * or an array index I (X must be an array object: V is assigned to X[I])
% * or a pair I-J of integers (X must be an array object, V must be a list of values: successive members of V are assigned to X[I..J])
%
% V must be a suitable value or object.
jpl_set(X, Fspec, V) :-
( jpl_object_to_type(X, Type) % the usual case (test is safe if X is var or rubbish)
-> Obj = X,
catch(
jpl_set_instance(Type, Type, Obj, Fspec, V), % first 'Type' is for FAI
% 2020-07-21: make catcher's 1st context arg an "anonvar" instead of a overspecified predicate indicator
error(type_error(acyclic,Te),context(_,Msg)),
throwme(jpl_set,acyclic(Te,Msg)) % rethrow
)
; var(X)
-> throwme(jpl_set,arg1_is_var)
; ( atom(X)
-> ( jpl_entityname_to_type(X, Type) % it's a classname or descriptor...
-> true
; throwme(jpl_set,classname_does_not_resolve(X))
)
; ( X = class(_,_) % it's a class type...
; X = array(_) % ...or an array type
)
-> Type = X
),
( jpl_type_to_class(Type, ClassObj) % ...whose Class object is available
-> true
; throwme(jpl_set,named_class_not_found(Type))
)
-> catch(
jpl_set_static(Type, ClassObj, Fspec, V),
% 2020-07-21: make catcher's 1st context arg an "anonvar" instead of a overspecified predicate indicator
error(type_error(acyclic,Te),context(_,Msg)),
throwme(jpl_set,acyclic(Te,Msg)) % rethrow
)
; throwme(jpl_set,arg1_is_bad(X))
).
%! jpl_set_instance(+Type, +Type, +ObjectReference, +FieldName, +Value) is det.
%
% ObjectReference is a JPL reference to a Java object
% of the class denoted by Type (which is passed twice for first agument indexing);
%
% FieldName should name a public, non-final (static or non-static) field of this object,
% but could be anything, and is validated here;
%
% Value should be assignable to the named field, but could be anything, and is validated here
jpl_set_instance(class(_,_), Type, Obj, Fname, V) :- % a non-array object
( atom(Fname) % the usual case
-> true
; var(Fname)
-> throwme(jpl_set_instance_class,arg2_is_var)
; throwme(jpl_set_instance_class,arg2_is_bad(Fname))
),
findall(
z4(I,Mods,FID,Tf),
jpl_field_spec(Type, I, Fname, Mods, FID, Tf), % public fields of class denoted by Type
Z4s
),
( Z4s = []
-> throwme(jpl_set_instance_class,no_such_field(Fname))
; Z4s = [z4(I,Mods,FID,Tf)]
-> ( member(final, Mods)
-> throwme(jpl_set_instance_class,field_is_final(Fname))
; jpl_datum_to_type(V, Tv)
-> ( jpl_type_fits_type(Tv, Tf)
-> ( member(static, Mods)
-> jpl_object_to_class(Obj, ClassObj),
jpl_set_static_field(Tf, ClassObj, FID, V)
; jpl_set_instance_field(Tf, Obj, FID, V) % oughta be jpl_set_instance_field?
)
; throwme(jpl_set_instance_class,incompatible_value(Tf,V))
)
; throwme(jpl_set_instance_class,arg3_is_bad(V))
)
; throwme(jpl_set_instance_class,multiple_fields(Fname)) % 'existence'? or some other sort of error maybe?
).
jpl_set_instance(array(Type), _, Obj, Fspec, V) :-
( is_list(V) % a list of array element values
-> Vs = V
; var(V)
-> throwme(jpl_set_instance_array,arg3_is_var)
; Vs = [V] % a single array element value
),
length(Vs, Iv),
( var(Fspec)
-> throwme(jpl_set_instance_array,arg2_is_var)
; integer(Fspec) % single-element assignment
-> ( Fspec < 0
-> throwme(jpl_set_instance_array,arg2_is_bad(Fspec))
; Iv is 1
-> N is Fspec
; Iv is 0
-> throwme(jpl_set_instance_array,no_values(Fspec,Vs))
; throwme(jpl_set_instance_array,more_than_one_value(Fspec,Vs))
)
; Fspec = N-M % element-sequence assignment
-> ( integer(N),
integer(M)
-> ( N >= 0,
Size is (M-N)+1,
Size >= 0
-> ( Size == Iv
-> true
; Size < Iv
-> throwme(jpl_set_instance_array,too_few_values(N-M,Vs))
; throwme(jpl_set_instance_array,too_many_values(N-M,Vs))
)
; throwme(jpl_set_instance_array,bad_range_pair_values(N-M))
)
; throwme(jpl_set_instance_array,bad_range_pair_types(N-M))
)
; atom(Fspec)
-> ( Fspec == length
-> throwme(jpl_set_instance_array,cannot_assign_to_final_field)
; throwme(jpl_set_instance_array,no_such_field(Fspec))
)
; throwme(jpl_set_instance_array,arg2_is_bad_2(Fspec))
),
jpl_set_array(Type, Obj, N, Iv, Vs).
%! jpl_set_static(+Type, +ClassObj, +FieldName, +Value) is det.
%
% We can rely on:
% * Type being a class/2 type representing some accessible class
% * ClassObj being an instance of java.lang.Class which represents the same class as Type
%
% but FieldName could be anything, so we validate it here,
% look for a suitable (static) field of the target class,
% then call jpl_set_static_field/4 to attempt to assign Value (which could be anything) to it
%
% NB this does not yet handle shadowed fields correctly.
jpl_set_static(Type, ClassObj, Fname, V) :-
( atom(Fname) % the usual case
-> true
; var(Fname)
-> throwme(jpl_set_static,arg2_is_unbound)
; throwme(jpl_set_static,arg2_is_bad(Fname))
),
findall( % get all static fields of the denoted class
z4(I,Mods,FID,Tf),
( jpl_field_spec(Type, I, Fname, Mods, FID, Tf),
member(static, Mods)
),
Z4s
),
( Z4s = []
-> throwme(jpl_set_static,no_such_public_static_field(field,Fname))
; Z4s = [z4(I,Mods,FID,Tf)] % exactly one synonymous field?
-> ( member(final, Mods)
-> throwme(jpl_set_static,cannot_assign_final_field(Fname))
; jpl_datum_to_type(V, Tv)
-> ( jpl_type_fits_type(Tv, Tf)
-> jpl_set_static_field(Tf, ClassObj, FID, V)
; throwme(jpl_set_static,value_not_assignable(Tf,V))
)
; throwme(jpl_set_static,arg3_is_bad(field_value,V))
)
; throwme(jpl_set_static,multiple_matches(field,Fname))
).
%! jpl_set_array(+ElementType, +Array, +Offset, +DatumQty, +Datums) is det.
%
% Datums, of which there are DatumQty, are stashed in successive
% elements of Array which is an array of ElementType starting at
% the Offset-th (numbered from 0)
% throws error(type_error(acyclic,_),context(jpl_datum_to_type/2,_))
jpl_set_array(T, A, N, I, Ds) :-
( jpl_datums_to_types(Ds, Tds) % most specialised types of given values
-> ( jpl_types_fit_type(Tds, T) % all assignable to element type?
-> true
; throwme(jpl_set_array,not_all_values_assignable(T,Ds))
)
; throwme(jpl_set_array,not_all_values_convertible(T,Ds))
),
( ( T = class(_,_)
; T = array(_) % array elements are objects
)
-> ( nth0(J, Ds, D), % for each datum
Nd is N+J, % compute array index
( D = {Tq} % quoted term?
-> jni_term_to_jref(Tq, D2) % convert to a JPL reference to a corresponding org.jpl7.Term object
; D = D2
),
jSetObjectArrayElement(A, Nd, D2),
fail % iterate
; true
)
; jpl_primitive_type(T) % array elements are primitive values