prop4j

SourceForge.net Logo

Project summary on SourceForge

Introduction

Object properties are an important concept in object-oriented programming; however, at the moment Java doesn't support properties at the language level (unlike some of the newer OO languages, like Ruby or C#). Developers are expected to implement properties using a design pattern (the "bean pattern"). This leads to obvious problems: developers have to waste time writing boilerplate code, and since there's no enforcement mechanism for the bean pattern conventions, mistakes can easily be made.

There has been some talk of introducing property support into the next release of Java, but at the moment there's nothing solid, and probably won't be for a long time (which is not a bad thing; adding features to a language is not something that should be done lightly).

The prop4j project is an attempt to fill the gap. Since it's implemented as a code pre-processor, it makes it easy to experiment with new language features without modifying the Java runtime or compiler. Don't think of it as a competitor to the (possibly upcoming) property support mechanism in Java; think of it as a supplement to that mechanism.

How to get prop4j

At the moment, there is no stable release, and the software is only available from the Subversion repository on SourceForge. If you have the Subversion client, you can download prop4j with the following command:

svn co https://prop4j.svn.sourceforge.net/svnroot/prop4j prop4j

How it works

The prop4j library consists of an annotation class (prop4j.Property) to allow users to mark bean properties in their code, and a source-code pre-processor that generates getter and setter methods as needed. In order to use prop4j, you need a JDK version 1.5 or higher, and you need to have prop4j.jar in the classpath at build time. Since the property annotations are not retained after compilation, the library is not required at runtime.

Example code:

    import prop4j.Property;
    		
    public class Point {
    		
        @Property
        private int x;
    		
        @Property
        private int y;
    }

This code is then run through the pre-processor:

java -cp prop4j.jar prop4j.Transformer src/Point.java > src-processed/Point.java

The output file should look like this:

    import prop4j.Property;
    		
    public class Point {
    		
        @Property
        private int x;
    		
        @Property
        private int y;
        
        public int getX() {
            return x;
        }
        
        public void setX(int x) {
            this.x = x;
        }
        
        public int getY() {
            return y;
        }
        
        public void setY(int y) {
            this.y = y;
        }
    }

Automatic support for bound properties is not implemented yet, but it's coming soon. There will probably also be an Ant plugin to simplify the pre-processing.

Licensing

prop4j is free software. It is distributed under the terms of the GPL, version 3.

Acknowledgements

This project incorporates a Java source parser written by Sreenivasa Viswanadha and Julio Vilmar Gesser (the original parser package can be downloaded here.)

 

Copyright 2008 - Mike Baranczak