Friday, June 15, 2007

Lean and Metaphors

A criticism of Lean Software Development is hard to find. While I think it offers a number of useful ideas and tools, I am somewhat sceptic towards the widespread use of metaphors from manufacturing found in Lean Software Development: An Agile Toolkit.. and elsewhere.

Metaphors are powerful in the way that they allow us to transfer insights from one area of experience to another. However, for these insights to be valid it is required that the metaphor has the same characteristics and dynamics as the system it is supposed to model. Or to put it more bluntly: Lean will solve all your problems to the extent that your software development organisation is like a manufacturing plant...

It is not all bad, though. The assembly line model may fail to capture all aspects of software development - but it does bring issues such as queues and waste into focus and provide a framework for discussing and adressing them. So even if Lean is not the final answer to everything it remains a useful tool for certain applications.

However, if software development is a field in its own right why do we then always have to use metaphors and analogues to describe it? It is clear that there is much to be learned from viewing software development as .. well, software development.

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);
typeBuilder.SetParent(typeof(MyParentClass));


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.Ldarg_0);
wrappedObjSetILG.Emit(OpCodes.Ldarg_1);
wrappedObjSetILG.Emit(OpCodes.Stfld, wrappedObjectField);
wrappedObjSetILG.Emit(OpCodes.Ret);
wrappedObjPropertyBuilder.SetSetMethod(wrappedObjSetBuilder);
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))
{
continue;
}
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.Ldarg_0);
getILG.Emit(OpCodes.Ldfld, wrappedObjectField);
getILG.Emit(OpCodes.Call, propertyInfo.GetGetMethod());
getILG.Emit(OpCodes.Ret);

propertyBuilder.SetGetMethod(getBuilder);
}

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 net.java.javafx.FXShell 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 openjfx.dev.java.net