Thursday, May 1, 2008

Java Annotations

Annotations in General
Basically it is the Metadata a syntactitcal way of adding metadata to source doe
Differences in computer languages have given rise to a variety of words for programmer-added metadata, including annotation (Java, Python), attribute (C#), pragma (C), and metadata (HTML).

Java Annotations
Annotations provide data about a program that is not part of the program itself. They have no direct effect on the operation of the code they annotate.Annotations are modifiers you can add to your code and apply to package declarations, type declarations, constructors, methods, fields, parameters, and variables.

JSR 175, A Metadata Facility for the Java Programming Language, gives more details on it.

The Interface java.lang.annotation.Annotation is extended by all annotation types. Note that an interface that manually extends this one does not define an annotation type. Also note that this interface does not itself define an annotation type.

J2SE 5 comes with following prebuilt annotations...

(1) java.lang.Overrides - If a method is decorated with this annotation type but does not override a superclass method, then the compiler will generate an error message... may be if one gives incorrect argument type then function will be overloaded so its better use this.
Should be used only on methods (not on classes, package declarations, or other constructs)

@Override
interface Closable {
void close();
}

class File implements Closable {
@Override
public void close() {
//... close this file...
}
}

The compiler generates an error complaining that File.close doesn't override any method from its superclass. This is because it is not overriding Closable.close, it is implementing it!

While it's not required to use this annotation when overriding a method, it helps to prevent errors. If a method marked with @Override fails to correctly override a method in one of its superclasses, the compiler generates an error.


(2) java.lang.annotation.Documented
This annotation indicates that the annotation to which this is applied is to be documented by javadoc and similar tools. Note that this annotation is just a hint, and a tool can ignore the annotation if it desires to do so.

(3) java.lang.Deprecated
This annotation provides a hint to the Java compiler to warn users if they use the class, method, or field annotated with this annotation.
When an element is deprecated, it should also be documented using the Javadoc @deprecated tag, as shown in the following example. The use of the "@" symbol in both Javadoc comments and in annotations is not coincidental—they are related conceptually. Also, note that the Javadoc tag starts with a lowercase "d" and the annotation starts with an uppercase "D".

// Javadoc comment follows
/**
* @deprecated
* explanation of why it was deprecated
*/
@Deprecated
static void deprecatedMethod() { }
}


(4) java.lang.annotation.Inherited
Indicates that an annotation type is automatically inherited. If an Inherited meta-annotation is present on an annotation type declaration, and the user queries the annotation type on a class declaration, and the class declaration has no annotation for this type, then the class's superclass will automatically be queried for the annotation type. This process will be repeated until an annotation for this type is found, or the top of the class hierarchy (Object) is reached. If no superclass has an annotation for this type, then the query will indicate that the class in question has no such annotation.


(5) java.lang.annotation.Retention
The Retention annotation takes a single parameter that determines the decorated annotation's availability. The values of this parameter are:

  1. RetentionPolicy.SOURCE: The decorated annotation is available at the source code level only
  2. RetentionPolicy.CLASS: The decorated annotation is available in the source code and compiled class file, but is not loaded into the JVM at runtime
  3. RetentionPolicy.RUNTIME: The decorated annotation is available in the source code, the compiled class file, and is also loaded into the JVM at runtime

By default, all annotations are available at the source level only.

(6) java.lang.annotation.Target
This annotation is used to indicate the type of program element (such as a class, method, or field) to which the declared annotation is applicable. If the Target annotation is not present on an annotation type declaration, then the declared type may be used on any program element. If the Target annotation is present, then the compiler will enforce the specified usage restriction. Legal values for the Target annotation are contained in the java.lang.annotation.ElementType enumeration.

(7) SuppressWarnings

@SuppressWarnings("deprecation")
void useDeprecatedMethod() {
objectOne.deprecatedMethod(); //deprecation warning - suppressed
}

Every compiler warning belongs to a category. The Java Language Specification lists two categories: "deprecation" and "unchecked." The "unchecked" warning can occur when interfacing with legacy code written before the advent of generics (discussed in the lesson titled "Generics"). To suppress more than one category of warnings, use the following syntax:

@SuppressWarnings({"unchecked", "deprecation"})

consider the following code:

int a;
Object object = new Object();

I declared two variables and did nto use them in my code. When I compiled the code, I got 2 warnings:

The local variable a is never read
The local variable object is never read

To remove the warnings, I will use the @SuppressWarnings annotation.

@SuppressWarnings("unused")
Object object = new Object();
@SuppressWarnings("unused")
int a;

// Suppress warnings about missing serialVersionUID
@SuppressWarnings( "serial" ) public void myMethod1()

Note that @SuppressWarnings(value={"unchecked"}) is same as @SuppressWarnings("unchecked")

Assertion Exception

AnnotationTypeMismatchException
Its a runtime exception. Thrown to indicate that a program has attempted to access an element of an annotation whose type has changed after the annotation was compiled (or serialized).

IncompleteAnnotationException
Its a RuntimeException
Thrown to indicate that a program has attempted to access an element of an annotation type that was added to the annotation type definition after the annotation was compiled (or serialized). This exception will not be thrown if the new element has a default value.

Custom Annotation
From Structure point of view Annotations fall into three basic categories:

* Marker annotations have no variables. The annotation simply appears, identified by name, with no additional data supplied. For example, @MarkerAnnotation is a marker annotation. It includes no data, just the annotation name.
An annotation type with no elements is termed a marker annotation type, for example:

/**
* Indicates that the specification of the annotated API element
* is preliminary and subject to change.
*/
public @interface Preliminary { }

It is permissible to omit the parentheses in marker annotations, as shown below:

@Preliminary public class TimeTravel { ... }


* Single-value annotations are similar to markers, but provide a single piece of data. Because only a single bit of data is supplied, you can use a shortcut syntax (assuming the annotation type is defined to accept this syntax): @SingleValueAnnotation("my data"). This should look a lot like a normal Java method call, aside from the @ sign.

In annotations with a single element, the element should be named value, as shown below:

/**
* Associates a copyright notice with the annotated API element.
*/
public @interface Copyright {
String value();
}

It is permissible to omit the element name and equals sign (=) in a single-element annotation whose element name is value, as shown below:

@Copyright("2002 Yoyodyne Propulsion Systems")
public class OscillationOverthruster { ... }


* Full annotations have multiple data members. As a result, you must use a fuller syntax (and the annotation doesn't look quite so much like a normal Java method anymore):
 @FullAnnotation(var1="data value 1", var2="data value 2", var3="data value 3"). <


@interface ClassPreamble {
String author();
String date();
int currentRevision() default 1;
String lastModified() default "N/A";
String lastModifiedBy() default "N/A";
String[] reviewers(); // Note use of array
}


The annotation type definition looks somewhat like an interface definition where the keyword interface is preceded by the @ character (@ = "AT" as in Annotation Type). The body of the annotation definition above contains annotation type element declarations, which look a lot like methods. Note that they may define optional default values.

// this code lives in a file called Copyright.java
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
* embedded copyright
*/
@Documented
@Retention(RetentionPolicy.RUNTIME) @interface Copyright
{
// ------------------------------ FIELDS ------------------------------

/**
* copyright to embed via annotations so it is embedded in all classes that reference it
*/
String defaultCopyright =
"copyright (c) 1998-2008 Roedy Green, "
+ "Canadian Mind Products, http://mindprod.com";
// -------------------------- PUBLIC INSTANCE METHODS --------------------------
/**
* get value of this annotation
*
* @return the copyright string
*/
String value() default defaultCopyright;
}


Annotation Uses
In general, annotations benefits falls into three categories: documentation, compiler checking, and code analysis.
  • Documentation : is probably the least relevant reason for adding metadata to the Java language. Javadoc is very powerful.
  • Code Analysis :
  • Compiler Checking : like Override
Annotations allow programmers to decorate Java code with their own attributes. These attributes can be used for code documentation, code generation, and, even during runtime, for providing special services such as enhanced business-level security or special business logic.

Metadata provides a helpful way to indicate if methods are dependent on other methods, if they are incomplete, if a certain class must reference another class, and so on. This is indeed useful, but documentation is probably the least relevant reason for adding metadata to the Java language. Javadoc already provides a fairly easy-to-understand and robust way to document code. Besides, who wants to write a documentation tool when one already exists and works fine for the most part?

Many APIs require a fair amount of boilerplate code. For example, in order to write a JAX-RPC web service, you must provide a paired interface and implementation. This boilerplate could be generated automatically by a tool if the program were “decorated” with annotations indicating which methods were remotely accessible.

Other APIs require “side files” to be maintained in parallel with programs. For example JavaBeans requires a BeanInfo class to be maintained in parallel with a bean, and Enterprise JavaBeans (EJB) requires a deployment descriptor. It would be more convenient and less error-prone if the information in these side files were maintained as annotations in the program itself.

used to create documentation
used to track down dependencies in code
used to perform rudimentary compile-time checking.

The more advanced uses of annotations include writing an annotation processor that can read a Java program and take actions based on its annotations.






and should be run by the testing tool:

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 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);
}
}


References *
http://mindprod.com/jgloss/annotations.html
http://java.sun.com/docs/books/tutorial/java/javaOO/annotations.html
http://www.javaworld.com/javaworld/jw-07-2004/jw-0719-tiger3.html
http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html
http://www.ibm.com/developerworks/java/library/j-annotate1/index.html

Monday, April 28, 2008

Collection and Set interface

Its easy to note that the collection interface and set interface both have same methods... but the Set interface doesnt allow duplicates and the Collection interface does allow.. why is it so ??

Reason ...
Every Set IS A Collection. The reason to extend Collection is that the contract is different. Collection says nothing about duplicates. Set is not allowed to contain duplicates. Even though there are no additional methods in Set, as a developer, we know that a Set will have this additional characteristic. If there is a parameter declared as a Set, then any developer using it knows that it will not contain duplicates, and any developer assigning to it knows that he can't use a collection that allows duplicates.

Tuesday, December 25, 2007

why a servlet is preferred for JDBC

Servlet is preferred for JDBC code and not a static initializer block in a Class because the latter will be able to provide connections only when the static initializer block is run not before that ...
on the other hand ... servlet can be loaded on startup

Google : Open Social

Really interesting stuff ...
one can even make web sites that uses "Open Social API"
benefits of open social for
  • developer .. they have more clients
  • consumers ... they have more apps
Makes sense !! heres how ....

http://code.google.com/apis/opensocial/

enables one to make application that can be used accross social networks !!

Wednesday, December 12, 2007

All about ADF tables


How do i make a ADF TreeTable ??
Saying it in very simple terms ... (i) first make POJOs ... using parent POJO and child POJO make a tree like hierarchy (ii) then using the ADF model classes convert them into ADF treeTable model !


Step one sounds easy .. can i have more details on step two ?
In ADF we have the following hierarchy...

java.lang.Object
   javax.faces.model.DataModel
      oracle.adf.view.faces.model.CollectionModel
         oracle.adf.view.faces.model.TreeModel
            oracle.adf.view.faces.model.ChildPropertyTreeModel

javax.faces.model.DataModel
has default constructor
inherits directly from java.lang.Object
The data collection underlying a DataModel instance is modeled as a collection of row objects that can be accessed by a zero-relative cursor (row index). The APIs provide mechanisms to position to a specified zero-relative row index, and to retrieve an object that represents the data that corresponds to the current row index.
DataModel is an abstraction around arbitrary data binding technologies that can be used to adapt a variety of data sources for use by JavaServer Faces components that support per-row processing for their child components
has a property rowIndex that starts from 0 ... up to max_no_of_child
has other methods like getRowData() getRowCount() .. setWrappedData() and getWrappedData() .. they set the object representing the data collection wrapped by this DataModel


oracle.adf.view.faces.model.CollectionModel
implements no RowKeyIndex
has default ctor
The data model that is used by ADF Table components. This extends the faces DataModel class and adds on support for rowKeys and sorting.


*** oracle.adf.view.faces.model.TreeModel ***
It just has a default constructor ...

rows in a treeModel may contain other rows...
to figure out if current row is a container use method isContainer()
If a row is a container, use the enterContainer() method to access its child rows. Once the enterContainer() method is called all the CollectionModel API's methods (like DataModel.getRowCount()) operate on the child collection. To return back to the parent row, use the exitContainer() method.

|-Root1 (rowKey="r1", rowIndex=0)
| |-Folder1 (rowKey="r1f1", rowIndex=0)
| | |-Node1 (rowKey="r1f1n1", rowIndex=0)
| | |-Node2 (rowKey="r1f1n2", rowIndex=1)
| | |-Node3 (rowKey="r1f1n3", rowIndex=2)
| |
| |-Folder2 (rowKey="r1f2", rowIndex=1)
| |-Node4 (rowKey="r1f2n1", rowIndex=0)
|
|-Root2 (rowKey="r2", rowIndex=1)
|-Root3 (rowKey="r3", rowIndex=2)
|-Root4 (rowKey="r4", rowIndex=3)

so each row has a setRowKey() and setRowIndex() ... rowKey is inherited from CollectionModel and rowIndex from DataModel... the rowKey is unique accross the whole table ... rowIndex is relative to its parent ...
use setRowKey(null) to reach the root ...

To access Node4 use:

setRowIndex(0); // isContainer()==true
enterContainer(); // enter Root1, getRowCount()==2
setRowIndex(1); // isContainer()==true
enterContainer(); // enter Folder2, getRowCount()==1
setRowIndex(0); // isContainer()==false
getRowData(); // To access a particular row in the list, first make that row current, and then call the getRowData() method on the Table

Or, more simply:
setRowKey("r1f2n1");
getRowData();

*** oracle.adf.view.faces.model.ChildPropertyTreeModel ***
It extends TreeModel

Creates a TreeModel from a List of beans. To use this class you must have a tree of beans (or Maps). The structure of your tree must be a List (or array) of root beans, and each bean must have a getter method (or Map property) that returns the children List. All elements of your tree must be the same type.




Tree VS TreeModel
The Tree component displays a multi-root hierarchy in a simple UI... the TreeTabel displays a single-root hierarchy

You may find the oracle.adf.view.faces.model.ChildPropertyTreeModel class useful when constructing a TreeModel

*** the ***
The "nodeStamp" facet of the Tree is used to display the data for each element in the tree. The Tree does not create components per element; instead, the "nodeStamp" is repeatedly rendered (stamped) once per element. Because of this stamping behavior, only certain types of components are supported as children inside a Tree. Supported components include all components with no behavior and most components that implement the EditableValueHolder or ActionSource interfaces.
The Tree renders expand/collapse icons that the user can click on to expand or collapse a subtree. When these icons are clicked, the Tree generates a DisclosureEvent

The immediate children of a Table component must all be < af:column > components.
Use the "header" facet on a Column to create the column header.
The child components of each Column display the data for each row in that column.... that means it may be a textbox , a label , a textArea etc ...
Column does not create child components per row; instead, each child is repeatedly rendered (stamped) once per row... that means if there are 10 rows then the column elemnt will not create 10 children insteand 10 times rows will be created and each time one element for the column is created

Because of this stamping behavior, some components many not work inside the table. Anything that is just pure output, with no behavior, will work without problems, as will components that don't "mutate" even as they deliver events

one can have column groups ...

Columns can be rendered as row headers by setting the "rowHeader" attribute on < column > to be true. Row header columns must be the first columns in a table.

Range Navigation
When the list being displayed by a Table is huge, you can enable the Table to break up the list into ranges and display a single range at a time. Range controls are provided on the Table to let the user scroll to the next range, or to go back to the previous range.
thi smay also generate RangeChangeEvent

Displaying Details
You can configure the Table to display or hide additional details of a particular row in response to a user gesture. When the details feature is enabled, a new column containing a toggle (per row) will render in the Table. When a toggle is activated, the details for that row are displayed. When a toggle is deactivated, the details for the row are hidden. The user can also display or hide the details for all rows at the same time (the controls for this feature are enabled by setting the "allDetailsEnabled" property to true.)

To enable the details feature set the "detailStamp" facet on the Table. Place the components that are used to show the details (of a row), inside this facet. In the following example, the Person's age is displayed in the details section:







Usually, the default behavior of the Table (with respect to displaying and hiding details) is sufficient. In some cases, however, it might be desirable to programmatically display or hide the details for a particular row. This can be done via the RowKeySet object obtained from the Table by calling the getDisclosureState() method. First, make the relevant row current by calling setRowIndex(...) or setRowKey(...) on the Table, and then call add() or remove() on the RowKeySet object. Adding the row to the set displays the details of the row. Removing the row hides them.



*** ***
The ADF Table component uses a model to access the data in the underlying list. The specific model class is oracle.adf.view.faces.model.CollectionModel . You may also use other model instances, e.g., java.util.List , array, and javax.faces.model.DataModel . The Table will automatically convert the instance into a CollectionModel ...??? not sure how will that happen as there is only one constructor for the CollectionModel and thats the default constructor ???



*** the ***
single-root hierarchy
TreeTable's children must be ADF Column components
the tree table has a "nodeStamp" and a "pathStamp" facet ... Like the Tree, the TreeTable has a "nodeStamp" facet which renders the "Object Name" Column. The TreeTable has a "pathStamp" facet for rendering the focus path.


*** obvious ***
ifmy af:table is inside af:panelcollection .. first panelcollection is made then the table

Tuesday, December 11, 2007

ANT and versioning

http://www.josesandoval.com/2007/12/java-automatically-increment-build.html

Software Engineering

hhhhhhhhmmmmmmmmmmmm
what THEY say ...
The idea is to create a language around the problem you're trying to solve in order to make it easier to solve the problem.
what I understood ...
yes i kind of agree to that ... as far as my experience with product development goes succesfull product development companies have made there own properiatory frameworks first and then used to it to solve the problem in hand ..
so what do they gain by making this framework ... ? wouldnt one save time by using existing framework .. ??
my take is they visualize a big problem break it down to small ones by making small components / classes / APIs and solve each of them individually
They even sav time .. how ?? if a bug comes in the framework they can solve it in less time rather than delegating it to some one else !

There's always a big difference between setting out to invent new solutions and setting out to invent new problems... confused ?? allow me to explain ... rather than solving a big problem first try to break it into new problems .. smaller and more specific ones .. then soolve each of them ... if new design or constraints come up then they too can be solved with similar approach ...
I wonder if its "Decorator Pattern" ;o)

Dont waste a lot of time and money in solving yesterdays problem ... i hope it makes sense .. and thats what JAVA is upto ...

cheerz !!