Thursday, May 31, 2007

.NET Class Generation - Delegating Object Properties

In a recent assignment I had the need for a dynamically generated class that - among other things - could be generated with a field of arbitrary type and delegate the properties of said field. In other words something like

public class MyWrapperClass : MyParentClass
private ObjectType _WrappedObject;

public ObjectType WrappedObject
set { _WrappedObject = value; }

public string Field1
get { return _WrappedObject.Field1; }

public int Field2
get { return _WrappedObject.Field2; }

// ... other stuff
where ObjectType can be any type and MyParentClass is a base class.

Fortunately, .NET reflection and class generation is pretty straight-forward. Generating the class itself requires an assembly, module etc.:

AppDomain appDomain = Thread.GetDomain();
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = "MyAssembly";
AssemblyBuilder assemblyBuilder
= appDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MyModule");
TypeBuilder typeBuilder
= moduleBuilder.DefineType("MyWrapperClass", TypeAttributes.Public);

The field holding our wrapped object must be declared to be the same type as the object to be wrapped

private ObjectType _ObjectToBeWrapped

This can be done with the following code:

Type objectType = wrappedObject.GetType();
FieldBuilder wrappedObjectField
= typeBuilder.DefineField("_WrappedObject", objectType, FieldAttributes.Private);
We will also need a setter for the object

public ObjectType WrappedObject { set { _WrappedObject = value; } }
And this requires some IL code generation:

// Create property ObjectToBeWrapped
PropertyBuilder wrappedObjPropertyBuilder
= typeBuilder.DefineProperty("WrappedObject", PropertyAttributes.None,
objectType, null);

// Create property setter
MethodBuilder wrappedObjSetBuilder =
typeBuilder.DefineMethod("set_WrappedObject", MethodAttributes.Public,
null, new Type[] { objectType });
ILGenerator wrappedObjSetILG = wrappedObjSetBuilder.GetILGenerator();
wrappedObjSetILG.Emit(OpCodes.Stfld, wrappedObjectField);
Note that the "set { value = .. }" notation actually corresponds to a method "set_MyProperty(object value) {...", much like Java-style getters & setters.

Now for the fun bit: Create a getter for each property in the wrapped object. We iterate through the properties and generating a getter method works much like the setter above.

foreach (PropertyInfo propertyInfo in objectType.GetProperties())
string name = propertyInfo.Name;
if (IgnoreProperty(name))
PropertyBuilder propertyBuilder =
typeBuilder.DefineProperty(name, PropertyAttributes.None,
propertyInfo.PropertyType, null);

MethodBuilder getBuilder =
typeBuilder.DefineMethod("get_" + name, MethodAttributes.Public,
propertyInfo.PropertyType, Type.EmptyTypes);

ILGenerator getILG = getBuilder.GetILGenerator();
getILG.Emit(OpCodes.Ldfld, wrappedObjectField);
getILG.Emit(OpCodes.Call, propertyInfo.GetGetMethod());


Friday, May 25, 2007

JavaFX in IntelliJ IDEA

No, at this point in time there is no JavaFX plugin for IDEA. It is fairly simple to get it up and running, though.

1. First you want to get openjfx (subversion or .zip).

2. Create a Java project in IDEA and put the files from trunk/lib (Filters.jar, javafxrt.jar & swing-layout.jar) in the classpath (e.g. add as a project library).

3. Create a .fx file

4. Create a run configuration

Basically you want to run with your .fx file as an argument.

4. Make sure that your .fx file is in the classpath
..Otherwise it won't work

As for syntax highlighting this can be set up by making a file type definition for *.fx files:

So all in all pretty ugly. A real JavaFX plugin is still on the IDEA wishlist.

And yes, there is already a plugin for Eclipse. There is also a fairly comprehensive tutorial using NetBeans on