wildcatsの日記

赤羽在住でIT関係の会社の社長やってます。

Janino

Janino-2.4.3.jarを用いてAbstractFactoryの汎用版を作ろうかと思った。
とりあえずPersonってインターフェイスを作ってManImplとWomanImplってクラスを作って試してみた。
ソースを貼ってみると


public static Person create(String name, int age, int gendor) {
Person person = null;
try {
ExpressionEvaluator ee = new ExpressionEvaluator(
"import jp.mydns.wildcats.example.janino.*; gendor == 0 ? new ManImpl(name,age) : new WomanImpl(name,age)",
Person.class, new String[] { "gendor", "name", "age", },
new Class[] { int.class, String.class, int.class });

person = (Person) ee.evaluate(new Object[] { new Integer(gendor),
name, new Integer(age) });
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (CompileException e) {
throw new RuntimeException(e);
} catch (ParseException e) {
throw new RuntimeException(e);
} catch (ScanException e) {
throw new RuntimeException(e);
}
return person;
}

こんな感じで実行してみると

java.lang.RuntimeException: org.codehaus.janino.CompileException: Line 1, Column 56:
Reference types "jp.mydns.wildcats.example.janino.ManImpl" and "jp.mydns.wildcats.example.janino.WomanImpl" don't match
ってスタックトレースを吐いて終了になる。
JaninoのUnitCompiler.javaのソースを見てみると

if (!mhsType.isPrimitive() && !rhsType.isPrimitive()) {
if (mhsType.isAssignableFrom(rhsType)) {
expressionType = mhsType;
} else
if (rhsType.isAssignableFrom(mhsType)) {
expressionType = rhsType;
} else {
this.compileError("Reference types \"" + mhsType + "\" and \"" + rhsType + "\" don't match", ce.getLocation());
return this.iClassLoader.OBJECT;
}
} else
{
this.compileError("Incompatible expression types \"" + mhsType + "\" and \"" + rhsType + "\"", ce.getLocation());
return this.iClassLoader.OBJECT;
}
ってな感じてmhsType(ManImpl)とrhsType(WomanImpl)がisAssignableFromで評価されてて偽になってるみたい。
うーん。微妙だな。