TclBlend: Blending Tcl and Java
by Scott Stanton


Listing One
public class Simple {
    public Simple(String s) {
    contents = s;
    }
    public String toString() {
    return contents;
    }
    public String contents = "default value";
}

Listing Two
public class Simple2 {
    public Simple2(int arg1, String arg2, boolean arg3) {
    ival = arg1;
    sval = arg2;
    bval = arg3;
    }
    public int getProp1() { return ival; }
    public int getProp2() { return sval; }
    public int getProp3() { return bval; }

    private int ival;
    private String sval;
    private boolean bval;
}

Listing Three
public class Simple3 {
    public void fails() {
    throw new IllegalArgumentException("Bad argument");
    }
}

Listing Four
public class Interp {
    public Interp();
    public void eval(String script) throws TclException;
    public void setVar(String name, TclObject value) throws TclException;
    public TclObject getVar(String name) throws TclException;
    public TclObject getResult();
}

Listing Five
import java.awt.event.*;
public class TestActionListener implements ActionListener {
    public TestActionListener(tcl.lang.Interp i, String s) {
        interp = i;
        script = s;
    }
    public void actionPerformed(ActionEvent e) {
        try {
            interp.setVar("event",
            tcl.lang.ReflectObject.newInstance(interp, e), 0);
            interp.eval(script);
        } catch (tcl.lang.TclException te) {
        System.out.println("error in ActionListener script: "
            + interp.getResult());
        }
    }
    private tcl.lang.Interp interp;
    private String script;
}

Listing Six
set b [java::new java.awt.Button "Quit"]
set f [java::new java.awt.Frame "Demo"]
$f {add java.awt.Component} $b
$f pack
$f show

set al [java::new TestActionListener [java::getInterp] {
    if {[string compare [$event getActionCommand] "Quit"] == 0} {
    puts "test succeeded"
    } else {
    puts "testfailed"
    }
}]
$b addActionListener $al


Example 1

(a)
set obj1 [java::new Simple "a value"]

(b)
set obj1 [java::new Simple "another value"]
puts "The contents of the Simple object are: [$obj1 toString]"


Example 2

(a) 
public class OverloadedSample {
    OverloadedSample(String s);
    OverloadedSample(int i);
}
OverloadedSample x = new OverloadedSample(42);
OverloadedSample y = new OverloadedSample("42");


(b)
set x [java::new { OverloadedSample int } 42]
set y [java::new { OverloadedSample String } 42]


Example 3
set obj [java::new Simple2 42 hello false]
set testdata {
    {Prop1 42}
    {Prop2 hello}
    {Prop3 false}
}
foreach {name value} $testdata {
    if {[string compare [$obj get$name] $value] != 0} {
    puts "got wrong value ($value) from property $name"
    }
}

Example 4
set obj [java::new Simple3]
if {[catch {$obj fails} msg]} {
    puts "Got error: '$msg'"
    set exception [lindex $::errorCode 1]
    $exception printStackTrace
}
=>
Got error: 'java.lang.IllegalArgumentException: Bad argument'
java.lang.IllegalArgumentException: Bad argument
        at Simple3.fails(Simple3.java:4)
        at tcl.lang.Invoke.call(Invoke.java:451)
        at tcl.lang.Invoke.callMethod(Invoke.java:134)
3


