patternjavaMinor
Typesafe Java properties with events and runtime reflection
Viewed 0 times
reflectioneventspropertieswithruntimetypesafejavaand
Problem
Goal: typesafe PropertyChanged events, minimum effort implementation for PropertyChanged events, runtime reflection of properties.
Am I reinventing the wheel or have I created something worthwile? Would you use it in your next project?
```
public interface StaticProperty {
public boolean canGet(); /** assert canGet()] */
public boolean canSet();
public boolean isvalid(); /** return rules().areAllMet() */
public boolean supportsPersistence(); /** assert instanceof Serializable && new T() */
public boolean canReturnNull(); /** assert canReturnNull && this.property != null */
public boolean canSetToNull(); /** assert canSetToNull() && value != null */
public boolean firesEvent();
public abstract class AbstractProperty
implements
StaticProperty {
protected boolean canGet = false; /** All false by default: explicitly set true in base */
protected boolean canGetNull = false;
protected boolean canSet = false;
protected boolean canSetNull = false;
protected boolean isValid = false;
protected boolean firesEvent = false;
protected boolean supportsPersistence;
protected AbstractProperty() {}
@Override public boolean canSet() {
return canSet;
}
@Override public boolean canSetToNull() {
return canSetNull;
}
@Override public boolean canReturnNull() {
return canGetNull;
}
@Override public boolean firesEvent() {
return firesEvent;
}
@Override public boolean isvalid() {
return isValid;
}
@Override public boolean canGet() {
return canGet;
}
@Override public boolean supportsPersistence() {
return supportsPersistence;
}
}
}
public class Props {
/**
* Any class having either getters, setters, or both
* With this, you can do:
*
* for (Property property : someObject.properties()) {
* query.select(property.name())
*
Am I reinventing the wheel or have I created something worthwile? Would you use it in your next project?
```
public interface StaticProperty {
public boolean canGet(); /** assert canGet()] */
public boolean canSet();
public boolean isvalid(); /** return rules().areAllMet() */
public boolean supportsPersistence(); /** assert instanceof Serializable && new T() */
public boolean canReturnNull(); /** assert canReturnNull && this.property != null */
public boolean canSetToNull(); /** assert canSetToNull() && value != null */
public boolean firesEvent();
public abstract class AbstractProperty
implements
StaticProperty {
protected boolean canGet = false; /** All false by default: explicitly set true in base */
protected boolean canGetNull = false;
protected boolean canSet = false;
protected boolean canSetNull = false;
protected boolean isValid = false;
protected boolean firesEvent = false;
protected boolean supportsPersistence;
protected AbstractProperty() {}
@Override public boolean canSet() {
return canSet;
}
@Override public boolean canSetToNull() {
return canSetNull;
}
@Override public boolean canReturnNull() {
return canGetNull;
}
@Override public boolean firesEvent() {
return firesEvent;
}
@Override public boolean isvalid() {
return isValid;
}
@Override public boolean canGet() {
return canGet;
}
@Override public boolean supportsPersistence() {
return supportsPersistence;
}
}
}
public class Props {
/**
* Any class having either getters, setters, or both
* With this, you can do:
*
* for (Property property : someObject.properties()) {
* query.select(property.name())
*
Solution
I honestly think that your code suffers from over-engineering.
As you can see in your
Regarding this code:
If you want to create a SQL query to search for properties like you are doing here, I recommend using Hibernate (which simplifies this a lot -- once you actually get the whole system up and running correctly...)
If I want to execute an event whenever a property has changed, I override the corresponding
There are way too many interfaces that has to be implemented/extended for me to want to use your code. I don't think it is worth all the trouble of your code just to make it a bit more typesafe, I would rather use the built-in Java Reflection API.
As you can see in your
Woei interface and WoeiImpl class, there is a lot of code that is required to use what you are building here. So to be blunt, I have to answer Would you use it in your next project? with NO.Regarding this code:
for (Property property : someObject.properties()) {
query.select(property.name())
.from(someObject.name());
}If you want to create a SQL query to search for properties like you are doing here, I recommend using Hibernate (which simplifies this a lot -- once you actually get the whole system up and running correctly...)
If I want to execute an event whenever a property has changed, I override the corresponding
set-method in my code to do what I want to be done whenever the property is changing.There are way too many interfaces that has to be implemented/extended for me to want to use your code. I don't think it is worth all the trouble of your code just to make it a bit more typesafe, I would rather use the built-in Java Reflection API.
Code Snippets
for (Property property : someObject.properties()) {
query.select(property.name())
.from(someObject.name());
}Context
StackExchange Code Review Q#5475, answer score: 5
Revisions (0)
No revisions yet.