Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do you use (or define) non standard annotations, and for what reason. How about recursive annotations?

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, ...)
    }
)
like image 379
Ingo Avatar asked Apr 08 '09 14:04

Ingo


People also ask

What is annotations method?

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.

What are the advantages of annotations in Java?

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.

Can Java annotations have methods?

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.


2 Answers

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.

like image 145
Scott Stanchfield Avatar answered Oct 04 '22 06:10

Scott Stanchfield


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.

like image 43
Dan Lew Avatar answered Oct 04 '22 06:10

Dan Lew