Reflection Example for HBox and VBox

「HBox と VBox をリフレクションで作れるか」という コメント を頂いたので、サンプルを作ってみました。
This is my answer to the question. Please refer to the following code.


実行結果 (result)

package fooami.reflection;

import javafx.reflect.*;

/**
 * Reflection Example.
 * @author hide1080
 */

var color1 = javafx.scene.paint.Color.RED;
var color2 = javafx.scene.paint.Color.BLUE;
var color3 = javafx.scene.paint.Color.YELLOW;
var color4 = javafx.scene.paint.Color.GREEN;

function run(args: String[]) {
    var context = FXLocal.getContext();

    // リフレクションで4個の Rectangle を作成する。
    // Create four mirror objects by reflection api for rectangle.
    var classRect = context.findClass("javafx.scene.shape.Rectangle");
    var objValRect1 = classRect.allocate();
    objValRect1.initVar("translateX", context.mirrorOf(5.0));
    objValRect1.initVar("translateY", context.mirrorOf(5.0));
    objValRect1.initVar("width", context.mirrorOf(100.0));
    objValRect1.initVar("height", context.mirrorOf(100.0));
    objValRect1.initVar("fill", context.mirrorOf(color1));
    objValRect1 = objValRect1.initialize();

    var objValRect2 = classRect.allocate();
    objValRect2.initVar("translateX", context.mirrorOf(5.0));
    objValRect2.initVar("translateY", context.mirrorOf(5.0));
    objValRect2.initVar("width", context.mirrorOf(100.0));
    objValRect2.initVar("height", context.mirrorOf(100.0));
    objValRect2.initVar("fill", context.mirrorOf(color2));
    objValRect2 = objValRect2.initialize();

    var objValRect3 = classRect.allocate();
    objValRect3.initVar("translateX", context.mirrorOf(5.0));
    objValRect3.initVar("translateY", context.mirrorOf(5.0));
    objValRect3.initVar("width", context.mirrorOf(100.0));
    objValRect3.initVar("height", context.mirrorOf(100.0));
    objValRect3.initVar("fill", context.mirrorOf(color3));
    objValRect3 = objValRect3.initialize();

    var objValRect4 = classRect.allocate();
    objValRect4.initVar("translateX", context.mirrorOf(5.0));
    objValRect4.initVar("translateY", context.mirrorOf(5.0));
    objValRect4.initVar("width", context.mirrorOf(100.0));
    objValRect4.initVar("height", context.mirrorOf(100.0));
    objValRect4.initVar("fill", context.mirrorOf(color4));
    objValRect4 = objValRect4.initialize();

    // リフレクションで2個の HBox を作成する。
    // Create two HBox mirror objects, then,
    // they have a couple of rectangle's mirror in each.
    var classHBox = context.findClass("javafx.scene.layout.HBox");
    var objValHBox1 = classHBox.allocate();
    objValHBox1.initVar("spacing", context.mirrorOf(5.0));
    objValHBox1 = objValHBox1.initialize();
    // initVar でシーケンスタイプのフィールドを初期化しようとすると
    // 内部で FXLocal.SequenceValue から FXLocal.ObjectValue への
    // ClassCastException となってしまうので FXVarMember を介して
    // シーケンスを設定する。
    // Note: because ClassCastException (from FXLocal.SequenceValue
    // to FXLocal.ObjectValue) happened internally when it tried to
    // initialize the field of the sequence type with initVar(),
    // I'll set via FXVarMember.
    var seqValue = context.makeSequence(objValRect1.getType(),
    objValRect1, objValRect2);
    var varMember = classHBox.getVariable("content");
    varMember.setValue(objValHBox1, seqValue);

    var objValHBox2 = classHBox.allocate();
    objValHBox2.initVar("spacing", context.mirrorOf(5.0));
    objValHBox2 = objValHBox2.initialize();
    seqValue = context.makeSequence(objValRect1.getType(),
    objValRect3, objValRect4);
    varMember.setValue(objValHBox2, seqValue);

    // VBox を作成する。
    // Create a VBox.
    var classVBox = context.findClass("javafx.scene.layout.VBox");
    var objValVBox = classVBox.allocate();
    objValVBox.initVar("spacing", context.mirrorOf(5.0));
    objValVBox = objValVBox.initialize();
    seqValue = context.makeSequence(objValHBox1.getType(),
        objValHBox1, objValHBox2);
    varMember = classVBox.getVariable("content");
    varMember.setValue(objValVBox, seqValue);

    // Scene を作成する。
    // Create a Scene.
    var classScene = context.findClass("javafx.scene.Scene");
    var objValScene = classScene.allocate();
    objValScene.initVar("width", context.mirrorOf(215.0));
    objValScene.initVar("height", context.mirrorOf(215.0));
    objValScene = objValScene.initialize();
    seqValue = context.makeSequence(objValVBox.getType(), objValVBox);
    varMember = classScene.getVariable("content");
    varMember.setValue(objValScene, seqValue);

    // Stage を作成する。
    // Create a Stage.
    var classStage = context.findClass("javafx.stage.Stage");
    var objValStage = classStage.allocate();
    objValStage.initVar("title", context.mirrorOf("Reflection Example"));
    objValStage.initVar("scene", objValScene);
    objValStage = objValStage.initialize();
    var stage = (objValStage as FXLocal.Value).asObject();

    return stage;
}