Next: , Previous: Using pragma foreign_export for Java, Up: Interfacing with Java   [Contents]


15.10.3.6 Using pragma foreign_decl for Java

pragma foreign_decl’ declarations for Java can be used to provide any top-level Java declarations (e.g. ‘import’ declarations or auxiliary class definitions) which are needed by Java code in ‘pragma foreign_proc’ declarations in that module.

For example:

:- pragma foreign_decl("Java", "
import javax.swing.*;
import java.awt.*;

class MyApplet extends JApplet {
    public void init() {
        JLabel label = new JLabel(""Hello, world"");
        label.setHorizontalAlignment(JLabel.CENTER);
        getContentPane().add(label);
    }
}
").
:- pred hello(io.state::di, io.state::uo) is det.
:- pragma foreign_proc("Java",
    hello(_IO0::di, _IO::uo),
    [will_not_call_mercury],
"
    MyApplet app = new MyApplet();
    // …
").