首先——我不确定你所说的递归注释是什么意思。您是指可以包含对同一类型的其他注释的引用的注释吗?就像是
@Panel(layout=BorderLayout.class,
nested={
@Panel(region=NORTH, layout=FlowLayout.class, ...)
@Panel(region=SOUTH, layout=FlowLayout.class, ...)
}
)
(如果可能的话,这将是我想使用它的一个例子......)
至于我对自定义注释(和处理器)的使用:代码生成。
请参阅http://code.google.com/p/javadude/wiki/Annotations
例如,JavaBean 属性:
@Bean(
properties={
@Property(name="name"),
@Property(name="phone", bound=true),
@Property(name="friend", type=Person.class, kind=PropertyKind.LIST)
}
)
public class Person extends PersonGen {
// generated superclass PersonGen will contain getters/setters
// field definitions, property change support...
}
或混合示例
package sample;
import java.util.List;
public interface IFlightAgent {
List<IFlight> getFlight();
void reserve(IFlight flight);
}
public interface ICarAgent {
List<ICar> getCars();
void reserve(ICar car);
}
public interface IHotelAgent {
List<IHotel> getHotels();
void reserve(IHotel hotel);
}
package sample;
import com.javadude.annotation.Bean;
import com.javadude.annotation.Delegate;
@Bean(delegates = {
@Delegate(type = IHotelAgent.class,
property = "hotelAgent",
instantiateAs = HotelAgentImpl.class),
@Delegate(type = ICarAgent.class,
property = "carAgent",
instantiateAs = CarAgentImpl.class),
@Delegate(type = IFlightAgent.class,
property = "flightAgent",
instantiateAs = FlightAgentImpl.class)
}
)
public class TravelAgent extends TravelAgentGen
implements IHotelAgent, ICarAgent, IFlightAgent
{
// generated superclass TravelAgentGen will create instances
// of the "instantiateAs" classes and delegate the interface
// methods to them
}
请参阅Java 中注释处理的缺点?以及我对它们使用的一些潜在问题的回答。