Annotations
From Informatics
Contents |
Annotations
See http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html
Ad-hoc annotations existed before Java 5 like transient, which was used int the code to indicate the field should be ignored by serialization subsystem. @deprecated was used in javadoc tags to state not to use this class, method, etc. Now we have a full-blown annotation system.
Rule of thumb: If the markup is to produce documentation, make it a javadoc tag, else make it an annotation.
An example annotation you will see in geWorkbench are:
@Publish
The corresponding class is:
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Publish {
}
This means:
- We have 2 annotations in our annotation declaration. Retain the annotation Publish for the entire application runtime, and only allow it to be used on methods.
Useful Annotations
Method Level
- @SuppressWarnings("unchecked") - use if you see warnings like The expression of type CriteriaSearch.ParameterComparator needs unchecked conversion to conform to Comparator<? super T> in your IDE.
Ways to avoid @SuppressWarnings("unchecked")
Use following line as example,
DSMicroarraySet normalizedData = (DSMicroarraySet) results.getResults();
If "results" is an AlgorithmExecutionResults in geWorkbench, If you didn't do a casting check, it will tell you it's not safe.
Then you try to do a casting check as following
if (results.getResults() instanceof DSMicroarraySet){ DSMicroarraySet normalizedData = (DSMicroarraySet) results.getResults(); }
In geWorkbench, DSMicroarraySet is a raw type class, so in eclipse, it will give you warnings that DSMicroarraySet should be parameterized.
Then you try to do a parameterized checking and casting
if (results.getResults() instanceof DSMicroarraySet<DSMicroarray>){ DSMicroarraySet<DSMicroarray> normalizedData = (DSMicroarraySet<DSMicroarray>) results.getResults(); }
It will not work. Eclipse will show you:
cannot perform instanceof check against parameterized type DSMicroarraySet<DSMicroarray>
Most of people will just add a @SuppressWarnings("unchecked") to that whole block (ex:whole method), and keep use following line.
DSMicroarraySet normalizedData = (DSMicroarraySet) results.getResults();
But this will lose the ability to check all these kind of checking eclipse try to warn you for that entire block.
The correct way to do it is:
if (results.getResults() instanceof DSMicroarraySet<?>){ DSMicroarraySet<?> normalizedData = (DSMicroarraySet<?>) results.getResults(); }
There's two examples in NormalizationPanel.java, just search "DSMicroarraySet<?>" in that file, and see how to achieve it without using @SuppressWarnings.