Item 35: Prefer annotations to naming patterns

1.  Prior to release 1.5, it was common to use naming patterns to indicate that some program elements demanded special treatment by a tool or framework. Naming patterns has several big disadvantages. First, typographical errors may result in silent failures. A second disadvantage of naming patterns is that there is no way to ensure that they are used only on appropriate program elements. A third disadvantage of naming patterns is that they provide no good way to associate parameter values with program elements.

 

2.  An annotation is called a marker annotation, if it has no parameters but simply “marks” the annotated element.

 

3.  Annotations never change the semantics of the annotated code, but enable it for special treatment by other tools.

 

4.  If a reflective method call throws an exception, the reflection facility wraps it in an InvocationTargetException, which can be extracted from the InvocationTargetException.getCause method.

 

5.  The syntax for array parameters in annotations is flexible. It is optimized for single-element arrays. Single element argument is still valid with the array-parameter version and result in single-element arrays. To specify a multiple-element array, surround the elements with curly braces and separate them with commas.

 

你可能感兴趣的:(annotation,naming pattern)