| 1 |
~~ The MIT License |
| 2 |
~~ |
| 3 |
~~ Copyright (c) 2009 codehaus.org. |
| 4 |
~~ |
| 5 |
~~ Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 |
~~ of this software and associated documentation files (the "Software"), to deal |
| 7 |
~~ in the Software without restriction, including without limitation the rights |
| 8 |
~~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 |
~~ copies of the Software, and to permit persons to whom the Software is |
| 10 |
~~ furnished to do so, subject to the following conditions: |
| 11 |
~~ |
| 12 |
~~ The above copyright notice and this permission notice shall be included in |
| 13 |
~~ all copies or substantial portions of the Software. |
| 14 |
~~ |
| 15 |
~~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
~~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
~~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 |
~~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 |
~~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 |
~~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 |
~~ THE SOFTWARE. |
| 22 |
~~ |
| 23 |
|
| 24 |
----- |
| 25 |
Animal Sniffer Annotations |
| 26 |
----- |
| 27 |
Stephen Connolly |
| 28 |
----- |
| 29 |
2009-11-03 |
| 30 |
----- |
| 31 |
|
| 32 |
Animal Sniffer Annotations |
| 33 |
|
| 34 |
Animal Sniffer Annotations provides Java 1.5+ annotations which allow marking methods which Animal Sniffer should |
| 35 |
ignore signature violations of. |
| 36 |
|
| 37 |
* Introduction |
| 38 |
|
| 39 |
Animal Sniffer is designed to detect when you have used classes/methods/fields which are not part of the JRE |
| 40 |
that you are targetting. There are certain situations when you might be compiling with a newer JDK than the |
| 41 |
JRE you are targetting. For example newer versions of MacOS do not have JDK 1.5 available and so only JDK 1.6 |
| 42 |
can be used. Another case is where you are targetting JRE 1.5, but you need to work around some bugs in the |
| 43 |
JDK 1.5 compiler (there are some well known bugs to do with generics) and so you compile with JDK 1.6. |
| 44 |
|
| 45 |
There are some situations where you may want to make use of some of the classes that are available on the newer |
| 46 |
JRE when your program is executed on the newer JDK. For example, JDK 1.6 introduced <<<java.awt.SplashScreen>>>. |
| 47 |
In these cases you would wrap the use of the newer classes inside a <<<try ... catch (LinkageError e)>>> block, e.g. |
| 48 |
|
| 49 |
--- |
| 50 |
... |
| 51 |
import javax.awt.SplashScreen; |
| 52 |
... |
| 53 |
public final class Someclass { |
| 54 |
|
| 55 |
... |
| 56 |
|
| 57 |
public static void main(String[] args) { |
| 58 |
... |
| 59 |
// long running initialization |
| 60 |
... |
| 61 |
try { |
| 62 |
SplashScreen.getSplashScreen().close(); |
| 63 |
} catch (LinkageError e) { |
| 64 |
// JDK 5 doesn't display a splash screen, no need to hide it |
| 65 |
} |
| 66 |
... |
| 67 |
} |
| 68 |
|
| 69 |
... |
| 70 |
} |
| 71 |
--- |
| 72 |
|
| 73 |
The above code will require JDK 1.6 or newer to compile, but will safely run on JRE 1.5. When you run animal-sniffer |
| 74 |
over the above class with the signatures for JRE 1.5, however, animal sniffer will report a signature failure. |
| 75 |
|
| 76 |
Animal Sniffer Annotations provides an out-of-the-box annotation <<<@IgnoreJRERequirement>>> which you can use |
| 77 |
to annotate methods which are to be ignored by animal sniffer, e.g. |
| 78 |
|
| 79 |
--- |
| 80 |
public final class MapFactory { |
| 81 |
|
| 82 |
... |
| 83 |
|
| 84 |
@IgnoreJRERequirement |
| 85 |
public ConcurrentMap newConcurrentMap() { |
| 86 |
try { |
| 87 |
// Note this map implementation is only available in JRE 1.6+ |
| 88 |
return new ConcurrentSkipListMap(); |
| 89 |
} catch (LinkageError e) { |
| 90 |
// fall back to JRE 1.5's only concurrent map |
| 91 |
return new ConcurrentHashMap(); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
... |
| 96 |
} |
| 97 |
--- |
| 98 |
|
| 99 |
Note: if you have compiled with the <<<org.jvnet:animal-sniffer-annotation:1.0>>> you do not have to change |
| 100 |
anything as animal-sniffer automatically detects this annotation as well (even although it is in a different |
| 101 |
package. |
| 102 |
|
| 103 |
Animal sniffer also allows you to specify your own custom annotation if needed. Check the configuration of |
| 104 |
the relevant task for more information. |
| 105 |
|
| 106 |
* Using animal-sniffer-annotations in Maven projects |
| 107 |
|
| 108 |
To annotate your methods, you need to add either an optional and/or provided dependency on |
| 109 |
<<<animal-sniffer-annotations>>>, (technically optional is the correct way, but to work around some |
| 110 |
incorrectly written maven plugins, you may end up using scope provided) e.g. |
| 111 |
|
| 112 |
--- |
| 113 |
<project> |
| 114 |
... |
| 115 |
<dependencies> |
| 116 |
... |
| 117 |
<dependency> |
| 118 |
<groupId>${project.groupId}</groupId> |
| 119 |
<artifactId>${project.artifactId}</artifactId> |
| 120 |
<version>${project.version}</version> |
| 121 |
<optional>true</optional> |
| 122 |
<!-- if you are using badly written maven plugins then blame them and add |
| 123 |
<scope>provided</scope> |
| 124 |
--> |
| 125 |
</dependency> |
| 126 |
... |
| 127 |
</dependencies> |
| 128 |
... |
| 129 |
<build> |
| 130 |
... |
| 131 |
<plugins> |
| 132 |
... |
| 133 |
<plugin> |
| 134 |
<artifactId>maven-compiler-plugin</artifactId> |
| 135 |
... |
| 136 |
<configuration> |
| 137 |
... |
| 138 |
<source>1.5</source> |
| 139 |
<target>1.5</target> |
| 140 |
... |
| 141 |
</configuration> |
| 142 |
... |
| 143 |
</plugin> |
| 144 |
... |
| 145 |
</plugins> |
| 146 |
... |
| 147 |
</project> |
| 148 |
--- |
| 149 |
|
| 150 |
|