The question tells it all.
For the experts, is there a reason the SUN java 5 compiler accepts recursive annotations (contrary to the langspec), while the later compilers do not? I mean, what could be an argument against recursive annotations.
Edit: a recursive annotation is something like:
@Panel(layout=BorderLayout.class,
nested={
@Panel(region=NORTH, layout=FlowLayout.class, ...)
@Panel(region=SOUTH, layout=FlowLayout.class, ...)
}
)
Annotations help to associate metadata (information) to the program elements i.e. instance variables, constructors, methods, classes, etc. Annotations are not pure comments as they can change the way a program is treated by the compiler. See below code for example.
The benefits of type annotations and example use cases For instance, they can produce informational messages for the developer at compile time, detecting errors or suppressing warnings. In addition, annotations can be processed to generate Java source files or resources that can be used to modify annotated code.
Annotation methods can't have parameters. Annotation methods return types are limited to primitives, String, Enums, Annotation or array of these. Java Annotation methods can have default values. Annotations can have meta annotations attached to them.
First -- I'm not sure what you mean by recursive annotations. Do you mean annotations that can contain references to other annotations of the same type? Something like
@Panel(layout=BorderLayout.class,
nested={
@Panel(region=NORTH, layout=FlowLayout.class, ...)
@Panel(region=SOUTH, layout=FlowLayout.class, ...)
}
)
(which would be an example of where I'd like to use it if it were possible...)
As for my use of custom annotations (and processors): code generation.
See http://code.google.com/p/javadude/wiki/Annotations
For example, JavaBean properties:
@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...
}
or a mix-in example
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
}
See The drawbacks of annotation processing in Java? and my answer to it for some potential issues with their usage.
I have been using annotations recently, as it is used heavily by Oracle Weblogic Server to modify the behavior of Java Web Services. There's a full listing of the annotations they define here. In particular, I end up using their Policy annotation the most, since that's what their security model is based off of; you can see their detailed examples on their documentation page.
I've never heard of recursive annotations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With