Paper about XForms and XFormsMM

I have currently finished a paper about XForms (and XFormsMM) for an univerity course called Adaptive User Interfaces. The paper gives a general introduction to XForms as well as to XFormsMM. XFormsMM is a (quite theoretical) approach how multi-modal forms can be build on top of XForms. It’s based on a paper of Mikko Honkala and Mikko Pohja and on the idea of extending XForms with modality-dependent style-sheets.

You can download the paper here. My own presentation about XForms can be found here.

I have used the following resources, which are also a good starting point to get some information about XForms:

To get started and to get an idea of what XForms is, I’ve made a small example using the FormFaces library:

The code is quite simple. First of all, you have to declare some namespaces for XForms itself and for the XML events XForms is using:

After that, you have to include the FormFaces JavaScript library to your code:

Now you can declare an XML data-model to be modified with the form, e.g. from an XML file:

The corresponding XML file looks like this:

Now, you can write XForms controls to operate on this data-model. A simple control can look like this:

So, all together:

Best regards, Thomas Uhrig.

Implementing Interfaces and Abstract Classes on the fly

I’m currently working on a small LISP-like language for an university course of mine. Hence, the the language is implemented in Java, I wanted to make the Java library accessible withing my language. Though loading classes, invoking methods and reading fields is just one of two ways – from Java to my language. But what about the other direction? How can I implement Java interfaces and abstract classes within my language to pass them to Java class as listeners, observers or handlers?

Approach 1: java.lang.reflect.Proxy

The class java.lang.reflect.Proxy is part of the Java Reflection API and enables you to create a proxy for every given interface. Its use is very straight forward:

The principle of the Proxy class is relatively simple. The class will create a new object (or also a class if you want) that inherit from the Proxy class itself. This new object will implement all method of the specified interfaces. Each method of this interfaces will be redirected to the given invocation handler. In this handler, you can manage the various method calls individually (according to the method name and the given input parameters).
The problem of this approach is that it’s only working for interfaces, not for abstract classes since all created objects inheriting from the Proxy class itself. This is where Javassist comes into play.

Approach 2: javassist.util.proxy.ProxyFactory

The class javassist.util.proxy.ProxyFactory is part of the Javassist library from JBoss. The Javassist library provides powerful abilities for Java reflection, like creating new classes on the fly, compiling Java code during the program execution and many more – like implementing interfaces and abstract classes.
The solution from Javassist looks quite similar to the example above. It goes like this:

The main difference of this approach is that you can easily set an arbitrary super class:

This could be any class as well as any abstract class. You handler will be called for every method-call on this class:

Javassist is published under either MPL, LGPL, or Apache License. So it’s a real good alternative (or extension) to the standard reflection API of Java.

Best regards, Thomas Uhrig.

Tools to understand Java code

I have recently read a question on Stackoverflow whether there are any tools to help understanding Java code. Somebody asked it, because he started to use a pen and a sheet of paper to get the clue of some code given to him. Not uncommon.

But the problem was, that nobody tried to answer the question, instead giving him tips like “learn Java” or “read tutorials”. I think they have misunderstood the question – because there are definitely tools for that:

  • A good IDE just like Eclipse is one basic tool to understand code. It helps you in various ways:

    • It shows the structure of folders and files of the project. You can easily get an overview of everything. Just import the project and have a look what you got.
    • It gives you the ability of syntax-highlighting (trivial, I know), but also the ability to format code like your are used to in a second. That helps reading it, even (or especially?) when you have some experience in the language.
    • It shows you the JavaDoc comments right away at every position in your code. So you will always have the documentation with you when needed.
    • It provides tools such as a debugger, a call-hierarchy view and many more. All this tools will help you to get an understanding of how the code works.

    And it can to much more for you. So it’s really worth to get familiar with your IDE.

  • UML is a widely used standard for modeling and visualizing code. The purpose of UML is simple and exactly what was asked: it should help you to understand code. The strength of UML is the abstraction. You can easily visualize a big architecture without mention any detail at all. On the other hand, you can add any detail you want if it is important for the understanding of the system.

    The basics of UML are not hard to learn, a quick look at Wikipedia should be enough to draw a class diagram. But UML is also enough standardized, that it could not only be read by humans, but also by machines – so you can generate it (so called round-tripping)!

    Tools like ObjectAid helps you to generate class diagrams as easy as possible. Just install it in your IDE, create a new class diagram and drag all Java classes in it.

  • Tests, especially unit tests (e.g. JUnit tests) could be also very useful to understand the code. A good test is something like a additional documentation or a contract on code-level. It exactly defines how a method can be used (e.g. input parameters) and what it should make (that’s what the test is testing ;)). So, if you don’t understand what a method should do and you have tests, take a look at them. And what if you don’t have any tests? Write your own ones! This is a good approach to explore an API in a structured and repeatable way. Whenever you are not sure what the API does, try to create a test about your assumption.

So, what to do if there is a bunch of new code?

  1. Import the project to your IDE.
  2. Get an overview of folders, files, packages and so on.
  3. Explore the code: What’s in the JavaDoc? What types are used? What about the call hierarchy?
  4. Run the unit tests and try to understand what the methods are doing.
  5. Try to run the debugger for a test or two.
  6. Draw or generate a UMl diagram.

I have uploaded some screenshot in the slideshow above for the open-source RSyntaxTextArea library from Fifesoft. But you will also find some good tutorials in the internet for every point mentioned before. Try to get familiar with at least some of the tools.

Best regards,
Thomas Uhrig