Synthetic variable or method in Java

While working I found one keyword Synthetic variable or method in java. Never heard of this before.I found some information on net and thought of sharing knowledge with everyone. If you want add something in this ,it would be really great.

“Synthetic” attribute means a variable introduced by the compiler that doesn’t appear in the actual code. It is a compiler-created field that links a local inner class to a block’s local variable or reference type parameter. Synthetic members are only meant to be accessed by trusted code generated by the compiler. Compiler synthesizes certain hidden fields and methods in order to implement the scoping of names. Any constructs introduced by the compiler that do not have a corresponding construct in the source code must be marked as synthetic, except for default constructors and the class initialization method.[1]
it means when there is a field or method introduced into a class file by the compiler and we can not find the primary declaration in the source file, it will marked as synthetic
A synthetic field pointing to the outermost enclosing instance is named this$0. The next-outermost enclosing instance is this$1, and so forth. All these synthetic fields are initialized by constructor parameters, which have the same names as the fields they initialize. If one of the parameters is the innermost enclosing instance, it is the first. All such constructor parameters are deemed to be synthetic. If the compiler determines that the synthetic field’s value is used only in the code of the constructor, it may omit the field itself, and use only the parameter to implement variable references. A non-private final synthetic method which grants access to a private member or constructor has a name of the form access$N, where N is a decimal numeral.
when an inner class has access to an instance of an outer class, this is implemented by creating a synthetic field in the inner class, generally named this$0, which holds a reference to the outer class instance.
According to Java 6 spec – Any constructs introduced by the compiler that do not have a corresponding construct in the source code must be marked as synthetic, except for default constructors and the class initialization method.

Nested classes are sometimes implemented using synthetic fields and synthetic contructors, e.g. an inner class may use a synthetic field to save a reference to its outer class instance, and it may generate a synthetic constructor to set that field correctly.
Synthetic members are how the compiler deals with the fact that nested classes are actually compiled into top-level classes
if a static member type uses a private member of its containing type (or vice versa), the compiler generates synthetic non-private access methods and converts the expressions that access the private methods into expressions that invoke these specially generated methods. These methods are given the default package access which is sufficient, as the member class and its containing class are guaranteed to be in the same package.

Synthetic class is derived from Attribute and declares this class as `synthetic’, i.e., it needs special handling. It is instantiated from the Attribute.readAttribute() method.
According to the JVM Spec: “A class member that does not appear in the source code must be marked using a Synthetic attribute.” Also, “The Synthetic attribute was introduced in JDK release 1.1 to support nested classes and interfaces.
We can check using isSynthetic() method whether variable is synthetic variable or not.
Examples include a static variable to hold a Class object if you’ve used the “Integer.class” notation, and the member variable in a non-static inner class that holds a reference to the enclosing class. Besides the fact that they are inserted by the compiler

See the java doc on – http://bcel.sourceforge.net/docs/de/fub/bytecode/classfile/Synthetic.html

pimp it

Using Collections.copy for list

Using java Collections to copy array list

I have seen lot’s of people using Collections.copy and most of times you see this error on console

Exception in thread "main" java.lang.IndexOutOfBoundsException:
Source does not fit in dest.

Normally we use following code to copy an array-list

ArrayList<String> items = new ArrayList<String>();

items.add("item 1");
items.add("item 2");
items.add("item 6");

ArrayList<String> items2= new ArrayList<String>(items.size());
Collections.copy(items2,items);

Only problem in the above code is that for list items2 we are assuming that it has same capacity as list items. If you see the code carefully, it is only setting up initial capacity not size and Collections api check for size instead of capacity of list and it result to exception.

IMO there should be an improvement in collection api to copy items, based on capacity, instead of size. See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6350752 .

Solution

You can use any of following option to copy a list

ArrayList<String> items2= new ArrayList<String>(items);
ArrayList<String> items2 = (ArrayList<String>)items.clone();

References

http://java.sun.com/javase/6/docs/api/java/util/Collections.html#copy(java.util.List,%20java.util.List)
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6350752

Introduction of Castor in Java

Castor is an open source data binding framework for moving data from XML to Java programming language objects and from Java to databases. It’s the shortest path between Java objects, XML documents and relational tables. Castor provides Java-to-XML binding, Java-to-SQL persistence, and more.

Castor is made up of (independent) modules as follows:– Castor XML
– Castor XML – Code generator
– Castor JDO – Persistence framework
– Castor JDO – DDL generator
– Additional tools
– Integration with other frameworks
Castor XML
XML data binding framework to bind XML artefacts to Java objects and vice versa.
Castor XML code generator
Code generator that generates Java source code from XML Schema information.
Castor JDO
Java persistence framework to bind Java objects to RDBMS tables. Castor JDO (Java Data Objects) is an open source, 100 percent Java data binding framework. Initially released in December 1999, Castor JDO was one of the first open source data binding frameworks available. Since that time, the technology has come a long way.

Castor JDO DDL generator
Generates DDL statements from JDO mapping files.
Castor is currently integrated with the following frameworks or has support for being integrated:
Spring ORM support for Castor
– Spring OXM for Castor
– Spring XML artifacts
– Web Service toolkits
– Apache Cocoon (Castor transformer)
– extendedXML module for Mule, offering enhanced XML-transformation support for Mule, using Castor

You can find Castor Api doc on – http://www.jdocs.com/castor/1.0.1/overview-summary.html
More information you can find on – http://www.castor.org/

pimp it