CSS - Dave Landers on Annotations
Cool talk. Dave is a great presenter and has a great demeanor with the crowd.
my notes;
What are annotations?
1) new java modifier – not really a modifier but a lot like them (public, private etc)
2) Gives us a way to extend the list of modifiers
3) Don?t affect semantics of the class they are on but can extend the way the code is used
Annotations defined –
@interface FixMe {String value(); }
- Defines a ?Fix Me? annotation
- Has One String attribute (value)
- Its kind of like a java class
Code annotated
@FixMe(?Missing the method body?)
public void theMethod() {}
to be used at build or run time
Any program element can be annotated, class, interface, package, annotation, methods, constructors etc.
Why do annotations?
Probably the j2ee space, things like web services and ejb are trying to simplify the annotations allow you to do the whole thing in one place (like using xdoclet). Web services etc.
You can write your own annotations.
XDoclet is cool but is not part of the code. Annotations are modifiers not docs, strongly typed, can persist in the class file, can persist at runtime.
Only three in J2SE 5.0 - @Deprecated, @SuppressWarnings not part of javac though,
@Override useful and is a signal to the compiler that its overriding a method, gives the compiler a warning that it should make sure the method is found in a super class, will cause a compiler warning if you are not over-ridding.
Kinds of annotations
Marker annotation – either their or not, kind of a Boolean marker that means something by being present (@Deprecated, @Override)
Single Value annotations - @SuppressWarnings({?unchecked?, ?deprecation?}) etc
Multi-valued annotations
@Review(reviewer=?Landers?, date=?4/1/2004?, comment=?Close stream in finally block?) a more complex annotation that has more information.
Defined with @interface and can have attributes, automatically extends from java.lang.annotation.Annotation (don?t put that into your class)
@interface Foo {
String reviewer() default ?[unknown]?;
String date() default ?0/0/00?;
String comment();
}
date and review have default values that get put into the annotation if they are not specified. If something is marked having a default its optional, other wise its required to have a value in the use.
@Foo(comment=?gak?) would be valid but leaving off comment would be invalid.
Also discussed some of the stuff around defaults and being able to use the ?value? named attribute.
Some examples on all the stuff you can annotate with the FixMe annotations.
Meta-annotations, four come with J2SE 5 - @Docmented, @Inherited, @Target, @Retention
@Documented says that the annotation shows up in java doc
@Inherited – missed this bit
You can specify where your annotation can show up with the @Target thing
@Retention tells the compiler where to put the annotation, SOURCE says only in the source code, CLASS says put it in the class but not at runtime, RUNTIME says the annotation is carried all the way through.
Accessing annotations – lots of places to use them, development etc. For example if you have an @Singleton annotation a checker could look for this annotation and warn you if your class does not look like a singleton.
EJB deployment could build the deployment descriptor at deployment time
BeanInfo generator from an annotated java bean
TestNG – an annotation to mark a test method
Annotation needs @Retention(RUNTIME) to do reflection, Class, Consgtructor, Field, Method, Package have new methods
boolean isAnnotationPresent(Class<? Extends Annotation> a)
<T extends Annotation> T getAnnotation(Class<T> a)
for example
FixMe fm = class.getAnnotatioin(FixMe.class);
System.err.println(?fix me: ? + fm.getValue());
Now doing a cool example using annotations – reporting the FixMe annotations. Also wrote a class loader that refused to load classes that need fixing (i.e. have FixMe annotations).
Here is the cool part – apt the Annotation Processing Tool
JDK tool for processing source annotatioins
Cleanear model fo source and types that doclet
Supports recursive processing of generated files
Multiple processors (vs. single doclet)
Docs @ http://java.sun.com/j2se/1.5.0/docs/guide/apt/index.html
APT can be recursive, the process can build code that is annotated, and then run its self on the generated code.
To use APT - write an AnnotationProcessFactory that creates AnnotationProcessors then run the apt tool.
Factory
Collection<String> supportAnnotationTypes() return annotations supported by this factory
Collection<String> supportedOptions();
AnnotationProcessor getProcessorFor(Set<AnnotationTypeDecloaration> atds, AnnotationProcessEnvironement env); - return the annotation process for the types and env describe by the args.
The processor has one method public void process();
Usually iterates through the classes and does some stuff using the Visitor patern
You specify which processing factories in the
META-INF/services/com.sun.mirror.apt.AnnotationProcessorFactory file (just like the XML stuff)
Going into more examples. I have to bail for my next talk thoug. Good stuff.


I would like to know whether its possible to declare multiple times a same annotation:
for e.g.
If I have a annotation say:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyAnnotation
{
String Name();
}
and use this annotation as following:
@MyAnnotation(Name = "First")
@MyAnnotation(Name = "Second")
public class TestAnnotation
{
}
Is the above usage possible? How?
In C#.NET the above can be done as follows:
[AttributeUsage(AttributeTargets.Class,AllowMultiple=true)]
public class MyAnnotation: Attribute
{
}
and used as:
@MyAnnotation(Name = "First")
@MyAnnotation(Name = "Second")
public class TestAnnotation
{
public TestAnnotation(string name)
{
}
}
Anyone knowing how to achieve the above...
Thanks in advance...
vinAnnotation
Posted by vinAnnotation on February 15, 2005 at 06:17 AM MST #
Mister vinAnnotation
What you ask is possible by using an array of nested annotation:
@interface MyAnnotationS {
MyAnnotation[] value();
}
and then
@MyAnnotationS({@MyAnnotation(name="A"), @MyAnnotation(name="B")})
public class Foo {}
Posted by Alex on March 10, 2005 at 04:39 AM MST #