|
Replies:
11
-
Last Post:
Sep 1, 2005 3:53 AM
by: euxx
|
|
|
|
|
|
|
Annotating 3rd party classes
Posted:
Aug 29, 2005 7:52 AM
|
|
|
Annotation facility introduced in Java 5 is very powerful feature that allows to implement many things in a less coupled way (e.g. dependency injection, annotation-driven AOP and ORM mappings, etc). As any great feature annotations could be misused, but it is a separate story. What concerns me now is that first of all you have to put all the annotations in a Java code, which is obviously not always possible for legacy code or 3rd party libraries.
If you think about this, even EJB 3 specification keep the notion of the deployment descriptor that can potentially override annotations from the bytecode. Basically these deloyment descriptors are just another layer on top of bytecode annotations, but the implementation is very specific for EJB 3 needs. It would be great to have a generic layer that would provide abstraction to transparently retrieve annotations defined in both bytecode and in externalized annotation definition. So, 3rd part code can be annotated without code modification.
Such approach could be a good addition to J2EE deployment descriptors and allow generic annotation definitions to be used by common annotation processing tools. Moreover it would enable to use such facility to annotate pre-Java5 code and still use modern tools for annotation processing.
Also see my blog post that is discussing AOP applications of this feature at http://jroller.com/page/eu?entry=annotating_3rd_party_classes
|
|
|
|
|
|
|
Re: Annotating 3rd party classes
Posted:
Aug 30, 2005 1:03 AM
in response to: euxx
|
|
|
annotations are intrinsically evil and should never be used.
Using them to inject behaviour in classes created by others is even worse.
|
|
|
|
|
|
|
|
Re: Annotating 3rd party classes
Posted:
Aug 30, 2005 4:11 AM
in response to: jwenting
|
|
|
> annotations are intrinsically evil and should never > be used.
That is according to whom? And what exactly you are suggesting to use instead of annotations?
> Using them to inject behaviour in classes created by > others is even worse.
Who said anything about behaviour injection?
If you are suggesting something, then please explain it.
|
|
|
|
|
|
|
|
Re: Annotating 3rd party classes
Posted:
Aug 30, 2005 4:38 AM
in response to: jwenting
|
|
|
I disagree that annotations are evil. Annotations are useful for adding information to methods and other code - especially so that IDEs and other code can have more information about these methods and treat them appropriately.
I do however agree that using annotations to modify the behaviour of code can be a bad thing.
@Deprecated is very useful, this certainly belongs as an annotation and NOT as a javadoc tag as it was before. When a method is deprecated, it should be recorded in the bytecode and not just in the docs - this way Java IDEs can pick up this information and treat it appropriately, e.g. print out a warning.
I really think that many more annotations need to be added to Java so that people get an idea on how they should / shouldn't be used by example in the JDK. Something like a @CallRequired annotation for methods, for example, would signal that when overriding a method the superclass version of the method should be called, like in finalize, etc. IDEs could then autogenerate the super.methodCall() code when writing the method skeleton and if the super.methodCall() did not exist in the subclass's method implementation the compiler could spit out a warning. By using annotations this way, even the compiled versions of classes contain this additional information and IDEs and the compiler could be made smarter and catch more problems earlier.
|
|
|
|
|
|
|
|
Re: Annotating 3rd party classes
Posted:
Aug 30, 2005 4:53 AM
in response to: prunge
|
|
|
> @Deprecated is very useful, this certainly belongs as > an annotation and NOT as a javadoc tag as it was > before. When a method is deprecated, it should be > recorded in the bytecode and not just in the docs - > this way Java IDEs can pick up this information and > treat it appropriately, e.g. print out a warning.
For the record, JavaDoc's @deprecated tag is actually ends up in the bytecode and not just in the docs. However annotatons seems a more logical way of doing such things.
|
|
|
|
|
|
|
|
Re: Annotating 3rd party classes
Posted:
Aug 30, 2005 8:20 AM
in response to: euxx
|
|
|
I would take this further and say that all member modifiers should be annotations. In that: preferred use of annotation over keyword: transient, volatile, strictfp either keyword or annotation: public, private, protected, final should have keyword only: native, abstract
As far as allowing annotating others, I would be careful so that the annotatations "appear" the same wether using your package (that adds annotations) or not. Which for the most part means that you can only annotate other members if the annontation is decalred in your package.
|
|
|
|
|
|
|
|
Re: Annotating 3rd party classes
Posted:
Aug 30, 2005 11:43 PM
in response to: euxx
|
|
|
> > @Deprecated is very useful, this certainly belongs > as > > an annotation and NOT as a javadoc tag as it was > > before. When a method is deprecated, it should be > > recorded in the bytecode and not just in the docs > - > > this way Java IDEs can pick up this information > and > > treat it appropriately, e.g. print out a warning. > > > For the record, JavaDoc's @deprecated tag is actually > ends up in the bytecode and not just in the docs. > However annotatons seems a more logical way of doing > such things.
If such information belongs in the bytecode in the first place... Metainformation should not be included in sourcefiles, and annotations constitute metainformation.
|
|
|
|
|
|
|
|
Re: Annotating 3rd party classes
Posted:
Aug 31, 2005 8:25 AM
in response to: jwenting
|
|
|
> Metainformation should not be included in > sourcefiles, and annotations constitute > metainformation.
Am I missing something or what? My original post were suggesting to actually externalize annotations/metadata.
|
|
|
|
|
|
|
|
Re: Annotating 3rd party classes
Posted:
Aug 31, 2005 11:12 PM
in response to: euxx
|
|
|
> > Metainformation should not be included in > > sourcefiles, and annotations constitute > > metainformation. > > Am I missing something or what? My original post were > suggesting to actually externalize > annotations/metadata.
You're still linking metadata directly into code that way. There would have to be a stadardised hook in each class to link to an optional annotation. That annotation could then just as well have been part of the class.
|
|
|
|
|
|
|
|
Re: Annotating 3rd party classes
Posted:
Sep 1, 2005 3:53 AM
in response to: jwenting
|
|
|
> You're still linking metadata directly into code that > way. There would have to be a stadardised hook in > each class to link to an optional annotation. That > annotation could then just as well have been part of > the class.
Not quite. First of all you not always can change the bytecoe.
Second - externalized annotations can use pattern-based expression language (similar or even the same as in AspectWerkz and AspectJ) to define pointcuts in the classes, so it would be just a one place instead of multiple.
|
|
|
|
|
|
|
|
Re: Annotating 3rd party classes
Posted:
Aug 31, 2005 1:41 AM
in response to: jwenting
|
|
|
> annotations are intrinsically evil and should never > be used. > > Using them to inject behaviour in classes created by > others is even worse.
I dont agree at all.
annotation is not evil and can be a great tool, look at the JAXB2, JAX-RPC, or the EJB3 for take a look on how annotation can help us.
instead, you can write evil code with annotation, but also, you can write evil code without it.
|
|
|
|
|
|
|
|
Re: Annotating 3rd party classes
Posted:
Aug 31, 2005 8:28 AM
in response to: wangzaixiang
|
|
|
Without annotations people will often rely on naming conventions on methods for "annotation" purposes, such as prepending get and set in a method to mean getters and setters. Although this naming convention is not evil by itself, the reflection used by some classes (e.g. classes in the java.beans package) to determine properites is evil. Further development of the java.beans package should adopt and encourage the use of annotations.
|
|
|
|
|