Unformatted text preview:

Java @Annotations Matt Beldyk!CSCI 5448 - Object Oriented Design and Analysis!Spring 2011About!Me!!• Ma*hew!Beldyk!• Computer!Science!Masters!Student!• Bachelors!at!Ohio!University!• Systems!Engineer!at!UNAVCO!Inc.!– Many!projects!at!most!levels!of!the!stack!– From!backporGng!processors!into!older!GCC!– To!creaGng!distributed!webservice!systems!– To!chasing!race!condiGons!– To!wriGng!generally!various!code!wherever!needed!The Plan of Attack • Describe Annotations!• Describe the built in default annotations!• Show how to make and use your own annotations!• Show some real world annotation examples!• ???!• Profit!What are Annotations? They allow you to annotate classes and methods in Java!Holy cyclic definition, Batman! • Introduced in Java 5!• JSR #175: http://www.jcp.org/en/jsr/detail?id=175 !• Allow developers to introduce metadata about functions and classes in code!• Better than just a comment !• 3 introduced by default by the JDK!o @Deprecated!o @SuppressWarnings!o @Override!• You can write your own too@Deprecated: The warning of disappearing functionality // Someone is going to use this without reading the comment !// Dear Developer, don't use this anymore !void orderNCubedSort(){}! // Use this function and the compiler will warn you at compile time!@Deprecated!void bubbleBobbleSort(){} @Deprecated is used to tell the developer that a function shouldn't be used anymore. It generally means that in a later version of the code, that function will go away. When a developer does use this function, the compiler will warn the developer about this.@SuppressWarnings: No really, I know what I'm doing! Object a = new Foo();! !//The compiler will warn!//you!Foo b = (Foo)a;! //Yay, no messages!//about this from javac!@SuppressWarnings!Foo c = (Foo)c; @SuppressWarnings tells the compiler that even though it might be inclined to warn the developer about what they are doing in the next line, it really is ok and the developer knows about the issue Use @SuppressWarnings sparingly! Often javac really does know what is best and is warning you for a reason!@Override: The class extender's friend public class Foo extends Bar{! ! //BUG: Compiler doesn't know ! public String tooString(){! // do stuff! }! //BUG: Won't compile!! @Override! public String tuString(){! //do other stuff! }!} @Override tells the compiler that you intend to have a function override the parent's version of it. If there is a typo, the compiler knows there's a problem and will throw an error.! @Override also tells the developer that this code is overriding a parent's version. Easier to read than a non-existent comment to the same effectBut these just appear to be new keywords? Nay, dear student. In fact, annotations are much more powerful and you can create your own!There!are!two!kinds!of!annotaGons!• Regular!AnnotaGons!Are!applied!to!regular!java!classes,!methods,!statements.!• Meta!AnnotaGons!Are!applied!to!annotaGon!definiGons!to!describe!the!behavior!of!the!annotaGon!being!declared!and!how!it!can!be!used.!Let's Create our own Annotation public @interface myAnnotation{}! Thats all there is to it! Now you can annotate your code with myAnnotation:! @myAnnotation public void doCoolAnnotatedThings(){! //TODO put code here!}Whoohoo, but that's not very compelling. An annotation with additional metadata:! public @interface SwordMan{! String nickname();! String sword();!}! and to use it:! @SwordMan(nickname="Elf Stone", sword="Arundil")!Traveler strider = new Ranger();A!word!on!Meta!AnnotaGons!@Documented!@interface!appearsInJavadoc();!This!allows!the!annotaGon!to!be!noted!in !the!javadoc!that’s!eventually!created.!!!!@Inherited!@interface!appearsInChildren();!When!a!class!or!method!is!annotated!with!@Inherited,!children!of!that!class!will!also!have!this!annotaGon!(otherwise!they!won’t)!!!Meta!AnnotaGons:!RetenGon!Policies!§ RetenGonPolicy.SOURCE!AnnotaGons!only!appear!in!source!files!(for!when!their!only!use!is!in!compiled!code)!§ RetenGonPolicy.CLASS!The!annotaGon!is!compiled!into!the!.class!file,!but!code!can’t!use!reflecGon!to!use!it.!§ RetenGonPolicy.RUNTIME!The!annotaGon!can!be!read!at!runGme!via!reflecGon!§ The!default!is!RetenGonPolicy.CLASS!§ Example:!!@Retention(RetentionPolicy.RUNTIME)!public @interface iCanBeReadWithReflection{}!Meta!AnnotaGons:!@Target!• This!marks!an!annotaGon!with!limitaGons!on!how!it!can!be!used.!• @Target!takes!a!list!of!ElementType!enums!to!say!how!it!can!be!used!• ElementType.ANNOTATION_TYPE,!CONSTRUCTOR,!FIELD,!LOCAL_VARIABLE,!METHOD,!PACKAGE,!PARAMETER,!and!TYPE.!Well, that's cool, but I already know how to use comments... Annotations can be used in three ways:!• Assisting visual inspection of code!• Custom annotation parsing source tools (such as javadoc)!• Via reflectionI do like mirrors! Tell me more about reflection. Here's an example from JavaBeat: http://www.javabeat.net/articles/30-annotations-in-java-50-1.html! First we need to set up our annotations:! !@Retention(RetentionPolicy.RUNTIME)!public @interface Author{! String name() default "unknown"; !}!@Retention(RetentionPolicy.RUNTIME)!public @interface Version{! double number(); !}Now we need to apply those annotations to something. @Author(name = "Johny") @Version(number = 1.0) public class AnnotatedClass { @Author(name = "Author1") @Version(number = 2.0f) public void annotatedMethod1() { } @Author(name = "Author2") @Version(number = 4.0) public void annotatedMethod2() { } }! http://www.javabeat.net/articles/30-annotations-in-java-50-1.htmlTime to setup that class. package reflections; import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Method; public class AnnotationReader { public static void main(String args[]) throws Exception { Class<AnnotatedClass> classObject = AnnotatedClass.class; readAnnotation(classObject); Method method1 = classObject.getMethod("annotatedMethod1",new Class[]{}); readAnnotation(method1); Method method2 = classObject.getMethod("annotatedMethod2", new Class[]{}); readAnnotation(method2); } http://www.javabeat.net/articles/30-annotations-in-java-50-1.htmlAnd use those annotations: static void readAnnotation(AnnotatedElement element) {! System.out.println("\nFinding annotations on " +! element.getClass().getName());


View Full Document

CU-Boulder CSCI 5448 - Java @Annotations

Documents in this Course
Django

Django

42 pages

ENRS

ENRS

30 pages

PhoneGap

PhoneGap

22 pages

Load more
Download Java @Annotations
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Java @Annotations and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Java @Annotations 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?