JAVA 5 FEATURES Byju Veedu Metadata Annotations The
JAVA 5 FEATURES Byju Veedu
Metadata (Annotations) • The metadata feature in J 2 SE 5. 0 provides the ability to associate additional data alongside Java classes, interfaces, methods, and fields. This additional data, or annotation, can be read by the javac compiler or other tools, and depending on configuration can also be stored in the class file and can be discovered at runtime using the Java reflection API. • Annotation processing tool (Apt) includes a set of new reflective APIs and supporting infrastructure to process program annotations. • The apt reflective APIs provide a build-time, source-based, read-only view of program structure which cleanly models the Java programming language's type system. • First, apt runs annotation processors that can produce new source code and other files. Next, apt can cause compilation of both original and generated source files, easing development.
Declaring annotation import java. lang. annotation. *; /** * Indicates that the annotated method is a test method. * This annotation should be used only on parameter less static methods. */ @Retention(Retention. Policy. RUNTIME) @Target(Element. Type. METHOD) @interface Test { }
Using Annotation class Foo { @Test public static void m 1() { } public static void m 2() { } @Test public static void m 3() { throw new Runtime. Exception("Boom"); } public static void m 4() { } @Test public static void m 5() { } public static void m 6() { } @Test public static void m 7() { throw new Runtime. Exception("Crash"); } public static void m 8() { } }
Processing annotation public class Run. Tests { public static void main(String[] args) throws Exception { int passed = 0, failed = 0; for (Method m : Class. for. Name(args[0]). get. Methods()) { if (m. is. Annotation. Present(Test. class)) { try { m. invoke(null); passed++; } catch (Throwable ex) { System. out. printf("Test %s failed: %s %n", m, ex. get. Cause()); failed++; } } } System. out. printf("Passed: %d, Failed %d%n", passed, failed); } }
Generic Types • Generic types enable an API designer to provide common functionality that can be used with multiple data types and which also can be checked for type safety at compile time. • The Collections API provides common functionality like Linked. Lists, Array. Lists and Hash. Maps that can be used by more than one Java type • The user of a generified API has to simply declare the type used at compile type using the <> notation
Generics Example • Without Generics Array. List list = new Array. List(); list. add(0, new Integer(42)); int total = ((Integer)list. get(0)). int. Value(); • Using Generics Array. List<Integer> list = new Array. List<Integer>(); list. add(0, new Integer(42)); int total = list. get(0). int. Value();
Autoboxing and Auto-unboxing • Automatic converting between primitive types, like int, boolean, and their equivalent Object-based counterparts like Integer and Boolean etc. • Without Boxing Array. List<Integer> list = new Array. List<Integer>(); list. add(0, new Integer(42)); int total = (list. get(0)). int. Value(); • With Boxing Array. List<Integer> list = new Array. List<Integer>(); list. add(0, 42); int total = list. get(0);
Enhanced for loop The new enhanced for loop can replace the iterator when simply traversing through a Collection as follows • Before Array. List<Integer> list = new Array. List<Integer>(); for (Iterator i = list. iterator(); i. has. Next(); ) { Integer value=(Integer)i. next(); } • After Array. List<Integer> list = new Array. List<Integer>(); for (Integer i : list) {. . . }
Enumerated Types • This type provides enumerated type when compared to using static final constants. public enum Stop. Light { red, amber, green };
Static Import • Static Import enables you to refer to static constants from a class without needing to inherit from it. Instead of Border. Layout. CENTER each time we add a component, we can simply refer to CENTER. • Eg: import static java. awt. Border. Layout. *; get. Content. Pane(). add(new JPanel(), CENTER);
Formatted Output • Using printf-type functionality to generate formatted output. This will help migrate legacy C applications, as the same text layout can be preserved with little or no change. • Eg: System. out. printf("name count%n"); System. out. printf("%s %5 d%n", user, total);
Formatted Input • The scanner API provides basic input functionality for reading data from the system console or any data stream. The following example reads a String from standard input and expects a following int value. • Eg: Scanner s= new Scanner(System. in); String param= s. next(); int value=s. next. Int(); s. close();
Varargs • The varargs functionality allows multiple arguments to be passed as parameters to methods. It requires the simple. . . notation for the method that accepts the argument list and is used to implement the flexible number of arguments required for printf. • Eg; void argtest(Object. . . args) { for (int i=0; i <args. length; i++) { } } argtest("test", "data");
Concurrency Utilities • Java. cocurrent. util package provides powerful, high-level thread constructs, including executors, which are a thread task framework, thread safe queues, Timers, locks (including atomic ones), and other synchronization primitives.
Hands-on • Using annotations • Enhanced for loop • Static import • Generics using collections • Autoboxing • Enums
- Slides: 16