Wow, this works... (A bean property annotation for Java)
Ok, I've added a few more features (the ability to call an external method prior to actually setting the value). I've also found out that there will not be any IDE support available yet. Netbeans will not have JSR269 support until 6.0, and Eclipse will not have the support until 3.3. Bummer.
Right now this works with javac from JDK 6, need to try it in various IDEs to see if it will work as well. (Yes, I understand this is against the specification for JSR269 [no preprocessing annotations]).
[pfnguyen@ares panno]$ ls
A.java B.java
[pfnguyen@ares panno]$ cat A.java
import com.hanhuy.panno.Property;
import java.sql.SQLException;
public class A {
private @Property String fool = "I pity the foo!";
private @Property boolean alwaysRight = false;
private @Property(useGet = true) boolean booleanWithGet = false;
// preCallMethod can also point to another object, e.g. "someObject.method"
@Property(preCallMethod = "genericValidator",
preCallThrows = "SQLException")
private int constrainedProperty = 42;
public A() {
System.out.println("A<init>: " + getFool());
}
private void genericValidator(
A source, String property, int oldValue, int newValue)
throws SQLException {
System.out.println(
"Attempting to modify '" + property + "' to " + newValue);
if (newValue != 54)
throw new SQLException(
"This is some arbitrary exception: " + newValue);
}
}
[pfnguyen@ares panno]$ cat B.java
import java.sql.SQLException;
public class B {
public static void main(String args[]) {
A a = new A();
System.out.println(a.getFool());
a.setFool("I am not a fool");
System.out.println(a.getFool());
System.out.println(a.isAlwaysRight());
a.setAlwaysRight(true);
System.out.println(a.isAlwaysRight());
System.out.println(a.getBooleanWithGet());
System.out.println(a.getConstrainedProperty());
try {
a.setConstrainedProperty(54);
}
catch (SQLException e) {
System.out.println("This should not happen yet!");
}
try {
a.setConstrainedProperty(42);
}
catch (SQLException e) {
System.out.println("Got expected exception: " + e.getMessage());
e.printStackTrace();
}
}
}
[pfnguyen@ares panno]$ javac -cp ~/development/panno/build/hanhuy-panno.jar *.java
[pfnguyen@ares panno]$ java -cp . B
A<init>: I pity the foo!
I pity the foo!
I am not a fool
false
true
false
42
Attempting to modify 'constrainedProperty' to 54
Attempting to modify 'constrainedProperty' to 42
Got expected exception: This is some arbitrary exception: 42
java.sql.SQLException: This is some arbitrary exception: 42
at A.genericValidator(A.java:28)
at A.setConstrainedProperty(A.java:13)
at B.main(B.java:21)
[pfnguyen@ares panno]$ javap A
Compiled from "A.java"
public class A extends java.lang.Object{
public A();
public void setFool(java.lang.String);
public java.lang.String getFool();
public void setAlwaysRight(boolean);
public boolean isAlwaysRight();
public void setBooleanWithGet(boolean);
public boolean getBooleanWithGet();
public void setConstrainedProperty(int) throws java.sql.SQLException;
public int getConstrainedProperty();
}
6