Platinum Solutions Corporate Website


The answer you entered to the math problem is incorrect.

New features of JDK 5.0

The JDK 5.0 release has some new features and much improved enhancements to the JDK that developers should be happy and eager to take advantage of. I will attempt to showcase these improvements over the course of the next few weeks.

This week's new feature is Metadata (Annotations).
quote from Sun:

This language feature lets you avoid writing boilerplate code under many circumstances by enabling tools to generate it from annotations in the source code. This leads to a "declarative" programming style where the programmer says what should be done and tools emit the code to do it. Also it eliminates the need for maintaining "side files" that must be kept up to date with changes in source files. Instead the information can be maintained in the source file. Refer to JSR 175.

If any of you have worked with the vast number of frameworks out there, you probably have experienced hours of frustration that go along with editing and debugging the xml files required by these frameworks to function. For those of you who use Hibernate, you are well aware of the duplication of Java code and the xml mapping file that goes along with each class/table. This is where Annotations can help. Instead of using an xml file for defining the metadata about the Java class, you can use Annotations to define the metadata inside of the class itself. This feature has been added to Hibernate 3.x and if you are still using Hibernate 2.1 then you'll just have to use XDoclet.
Below is an example of life before Annotations and life after:

package com.platinumsolutions.model;

public class Employee
{

private int id;
private String firstName;
private String lastName;
// ... (getters and setters below)

}

with an xml file that would look like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>

<class name="com.platinumsolutions.model.Employee" table="EMPLOYEE">
<id column="ID" name="id" type="long">
<generator class="sequence"/>
</id>
<property column="FIRST_NAME" length="10" name="firstName" not-null="true" type="string"/>
<property column="LAST_NAME" length="10" name="lastName" not-null="true" type="string"/>
</class>

</hibernate-mapping>

As you can see, the xml file essentially duplicates the Java class file definition along with some database descriptions. Here is what the class looks like with Annotations (no xml file is needed):

package com.platinumsolutions.model;

import javax.persistence.Entity;
import javax.persistence.GeneratorType;
import javax.persistence.Id;

@Entity(table="EMPLOYEE")
public class Employee
{

@Id(generate=GeneratorType.AUTO)
private int id;

private String firstName;

private String lastName

// ... (getters and setters below)

}

This is much more concise -- we have eliminated several lines of xml code which leads to a leaner application that is less error prone and easier to maintain. Annotations can also help in other areas besides the reduction of xml files. Below is a simple example from Sun that shows how to create a framework for unit testing by marking certain methods as "testable":

import java.lang.annotation.*;

/**
* Indicates that the annotated method is a test method.
* This annotation should be used only on parameterless static methods.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Test { }

Note that the annotation type declaration is itself annotated. Such annotations are called meta-annotations. The first (@Retention(RetentionPolicy.RUNTIME)) indicates that annotations with this type are to be retained by the VM so they can be read reflectively at run-time. The second (@Target(ElementType.METHOD)) indicates that this annotation type can be used to annotate only method declarations.

Here is a sample program, some of whose methods are annotated with the above interface:

public class Foo {

@Test public static void m1() { }

public static void m2() { }
@Test public static void m3() {

throw new RuntimeException("Boom");

}
public static void m4() { }
@Test public static void m5() { }
public static void m6() { }
@Test public static void m7() {

throw new RuntimeException("Crash");

}
public static void m8() { }

}

Here is the testing tool:

import java.lang.reflect.*;

public class RunTests {

public static void main(String[] args) throws Exception {

int passed = 0, failed = 0;
for (Method m : Class.forName(args[0]).getMethods()) {

if (m.isAnnotationPresent(Test.class)) {
try {

m.invoke(null);
passed++;

} catch (Throwable ex) {

System.out.printf("Test %s failed: %s %n", m, ex.getCause());
failed++;

}

}

}
System.out.printf("Passed: %d, Failed %d%n", passed, failed);

}

These examples are just the tip of the iceberg. Look for Annotations to become a key player in future frameworks and tools for the Java Platform.

Reply

Please solve the math problem above and type in the result. e.g. for 1+1, type 2.
The content of this field is kept private and will not be shown publicly.
  • Lines and paragraphs break automatically.

More information about formatting options