My favorites | Sign in
Project Home Wiki Issues Source
Search
for
AnnotationsDecoratorExample  
Updated Jul 9, 2011 by ZombiesA...@gmail.com

JavaDude Tools->Annotations->Annotations examples

Decorator Example

Suppose 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:

  • A property named "basePerson" is created in the generated DoctorGen class
  • Delegation methods are created in the generated DoctorGen class for all IPerson methods, delegating to whatever "basePerson" is set to.
  • The constructor is passed the IPerson to wrap and sets it as the "basePerson"
  • The getName() method is overridden to provide the tweak
  • All other IPerson methods are simply delegated to the wrapped "basePerson"
// 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
Powered by Google Project Hosting