|
AnnotationsDecoratorExample
JavaDude Tools->Annotations->Annotations examples Decorator ExampleSuppose we have interface IPerson: package sample;
public interface IPerson {
String getName();
void setName(String value);
String getAddress();
void setAddress(String value);
String getPhone();
void setPhone(String value);
}and we would like to create a Decorator for it that tweaks the return value of getName to include "Dr." in front of it. You can use @Delegate to do this: package sample;
import com.javadude.annotation.Bean;
import com.javadude.annotation.Delegate;
import com.javadude.annotation.Property;
@Bean(
properties = {
@Property(name = "basePerson", type = IPerson.class)
},
delegates = {
@Delegate(type = IPerson.class, property = "basePerson")
}
)
public class Doctor extends DoctorGen implements IPerson {
public Doctor(IPerson person) {
setBasePerson(person);
}
@Override public String getName() {
return "Dr. " + super.getName();
}
}This specification does the following:
// CODE GENERATED BY JAVADUDE BEAN ANNOTATION PROCESSOR
// -- DO NOT EDIT - THIS CODE WILL BE REGENERATED! --
package sample;
@javax.annotation.Generated(
value = "com.javadude.annotation.processors.BeanAnnotationProcessor",
date = "Thu Aug 14 23:04:39 EDT 2008",
comments = "CODE GENERATED BY JAVADUDE BEAN ANNOTATION PROCESSOR; DO NOT EDIT! THIS CODE WILL BE REGENERATED!")
public abstract class DoctorGen {
public DoctorGen() {
;
}
private sample.IPerson basePerson_;
public sample.IPerson getBasePerson() { return basePerson_; }
public void setBasePerson(sample.IPerson value) {
basePerson_ = value;
}
public java.lang.String getAddress() {
return basePerson_.getAddress();
}
public java.lang.String getName() {
return basePerson_.getName();
}
public java.lang.String getPhone() {
return basePerson_.getPhone();
}
public void setAddress(java.lang.String value) {
basePerson_.setAddress(value);
}
public void setName(java.lang.String value) {
basePerson_.setName(value);
}
public void setPhone(java.lang.String value) {
basePerson_.setPhone(value);
}
@Override
public java.lang.String toString() {
return getClass().getName() + '[' + paramString() + ']';
}
protected java.lang.String paramString() {
return
"basePerson=" + basePerson_;
}
}JavaDude Tools->Annotations->Annotations examples |
► Sign in to add a comment