Auto Loaded Java Classes

With release 1.0.9 the Prolog interpreter provides auto loading of modules. This happens when a qualified predicate or evaluable function call is specified and the specified module has not yet been loaded. The auto loader will try Prolog modules and Java classes. For a Java class the auto loader will on the fly generate a synthetic module. This synthetic module is only generated once so that further access to the module can profit from the polymorphic cache in each call-site of the qualified predicate or evaluable function call.

The synthetic module consists of a number of automatically generated foreign_XX/3 directives. The choice of a predicate or evaluable function name is replaced by an automatic choice by the auto loader. The end-user can thus not anymore choose a custom predicate or evaluable function name for an auto loaded Java class member. Which is not a great loss since the call is anyway qualified by the module name and there will be no ambiguity in the determination of the Java class.

For methods and fields the auto loader will first try to automatically generate an evaluable function or constant. This is only possible if the parameter types and the return type are all numbers. If the signature of the method or the type of the field does not allow creating an evaluable functor or constant the auto loader will then attempt creating a method predicate respectively a getter predicate.

Table 4: Java Class Functor
Knowledge Base Class Member Functor
Predicate

Constructor Constructor new
Method Method <Method Name>
Getter Field <Field Name>
Setter Field set_<Field Name>
Evaluable Function


Function Method <Method Name>
Constant Field <Field Name>

On the other hand there can be a collision inside a Java class itself since the Java language allows overloading of method and constructor names by different parameter types. The actual implementation of the auto loader will create a predicate that will branch into the different variants of the same method or constructor name depending on the actual type of the arguments at runtime. For evaluable functions additionally a bridge between the evaluable function and the branching predicate will be created.

The branching code will also respect the specificity of the formal parameters of a Java class member. For this purpose we obtain a scoring of the formal parameters and sort the Java class members accordingly. The scoring currently works for both primitive and non-primitive Java data types. If a Java class extends a parent Java class the ancient members that are not overridden are also considered in the branching. The branching is then a call into the ancient Prolog predicate.

Table 5: Java Array Class Functor
Knowledge Base Functor
Predicate
Dimension new
Element at
Update
set_at
Evaluable Function

Length
length
Member
at

With release 1.1.2 the auto loader will also automatically register delegates for Java array classes. For array element access the auto loader will first try to automatically generate an evaluable member access. This is only possible if the component type is a number. If the component type does not allow creating an evaluable member access the auto loader will then attempt creating a predicate element access.

Kommentare