java generics(under the hood of the compiler) by harmeet singh

24
Java Generics (Under The Hood Of The Compiler) By: Harmeet Singh (Taara) (Java EE Developer) [email protected] http://harmeetsingh13.blogspot.in skype: harmeetsingh0013 Contact: Via mail or Skype

Upload: harmeet-singh

Post on 24-Dec-2014

149 views

Category:

Technology


2 download

DESCRIPTION

Generics are the powerful feature in Java programming language. In JDK 1.5 the Generics are introduce and it create new evolution in java. Generics are major topic and vast topic but in this we only care about some basics for generics.

TRANSCRIPT

Page 1: Java generics(Under The Hood Of The Compiler) by Harmeet singh

Java Generics(Under The Hood Of The Compiler)

By: Harmeet Singh (Taara)(Java EE Developer)

[email protected]://harmeetsingh13.blogspot.in

skype: harmeetsingh0013Contact: Via mail or Skype

Page 2: Java generics(Under The Hood Of The Compiler) by Harmeet singh

Contents

1. Generic Type.2. WildCards in Generics.3. Unchecked Warnings.4. Heap Pollution.5. Type Eraser.6. Bridge method.7. Type Erasure of A Parameterized Type.8. Type Erasure of A Type Parameter.9. Type Erasure of A Generic Method.10.Reficiation.11. Reifilable Type.

Page 3: Java generics(Under The Hood Of The Compiler) by Harmeet singh

Acknowledgement

● Special Thanks to “Angelika Langer” (Trainer & Author and Mentor). Everything about generics in this presentation is get from “Angelika Langer” Generics FAQ.

[email protected]

Page 4: Java generics(Under The Hood Of The Compiler) by Harmeet singh

Generic Type● Type Parameter: Is a placeholder that will later be replace but a type

argument.● A parameterized or generic type is an instantiation of a generic type with

actual argument. ● Define Generics:

class Pair<S,T>{private S first;private T second;-----------------

}

Page 5: Java generics(Under The Hood Of The Compiler) by Harmeet singh

Generic Type● Types that cannot have type parameter :

o Anonymous Inner Classeso Exception Typeso Enum Types.

● Generic Type Instantiated: o Pair<String, Long> pair = new Pair<String, Long>();

● Generics Declarations: o Collection<String> : Concrete Declaration.o Collection<?> : Wild Card Declaration.

Page 6: Java generics(Under The Hood Of The Compiler) by Harmeet singh

WildCard in Generics● We are not create directly object whose types is wildcard parameterized

objectArrayList<?> list = new ArrayList<?>(); //error

● WildCard parameterized type is not declare in a supertypeclass WildCardParam<?>{ //error---------------------

}

Page 7: Java generics(Under The Hood Of The Compiler) by Harmeet singh

WildCards In Generics● WildCard Types Family:

o “ ? “ : Unbounded WildCard o “ ? extends Type ” : WildCard With an upper boundo “ ? super Type “ : WildCard With an lower bound

Page 8: Java generics(Under The Hood Of The Compiler) by Harmeet singh

WildCards in Generics● WildCards Bounds: A Reference type that is used to further describe the

family of types denoted by a wildcard. ● Difference between WildCard Bound and Type Parameter Bound:

o A WildCard can have only one bound, while a type parameter can have several bounds.

o A WildCard can have lower or an upper bound, while there is no such things as a lower bound for a types parameter.

Page 9: Java generics(Under The Hood Of The Compiler) by Harmeet singh

WildCards In Generics● Does “extends” always means Inheritance ? : extends is an overloaded

keyword in java. it has different meanings, depending on the context it appears. The extends keyword appear in four different locations : o In the Definition of Class.o In the Definition of Interface.o In the Definition of Type Parameter bounds.o In the Definition of WildCards Bounds.

Page 10: Java generics(Under The Hood Of The Compiler) by Harmeet singh

Unchecked Warnings● Compiler and the runtime system do not have enough type information to

perform all type checks that would be necessary to ensure type safety. example: TreeSet set = new TreeSet();

set.add(“abc”); //unchecked warnings. ● Xlint:unchecked enable “unchecked” warnings.● Xlint:-unchecked and @SuppressWarnings(“unchecked”) disable

“unchecked” warnings.

Page 11: Java generics(Under The Hood Of The Compiler) by Harmeet singh

Heap Pollution● A Situation where a variable of parameterized types refer to an Object that

is not of that parameterized type. ● Reason of Heap Pollution Occur :-

1. Mixing Raw and Parameterized Type.2. Unwise Casting.3. Separate Compilation.

Page 12: Java generics(Under The Hood Of The Compiler) by Harmeet singh

Type Erasure● How Compiler Translate Java Generics:

○ Compiler translate generic type or method(in any language, not just java) has in principle two choice. ■ Code Specialization : The compiler generates a new

representation of a generic type of method.■ Code Sharing : The compiler generates code for only one

representation of a generic type or method and maps all the instantiations of the generic type of method to the unique representation , perform type check and type conversion where needed.

Page 13: Java generics(Under The Hood Of The Compiler) by Harmeet singh

Type Erasure● What is Type Erasure ?

o The compiler generates only one byte code representation of a generic type or method and maps all the instantiations of the generic type or method to the unique representation.

● The type Erasure process can be imagined as a translation from generic Java Source code back into regular Java Code.

● The Steps performed during type erasure : o Eliding Type Parameter : When the Compiler finds the definition of a

generic type or method, it removes all occurrences of the type parameters and replaces them by their leftmost, or type Object if no bounds specified. Ex: <T> var; into Object var;

Page 14: Java generics(Under The Hood Of The Compiler) by Harmeet singh

Type Erasure ● The Steps performed during type erasure :

o Eliding Type argument: When the compiler finds a parameterized type, i.e an instantiation of a generic types, then it removes the type arguments. Like : List<String>, Set<Long> and Map<String, ?> are translated to List, Set and Map respectively.

Page 15: Java generics(Under The Hood Of The Compiler) by Harmeet singh

Bridge Method● A Synthetic method that the compiler generates in the course of type

erasure. it is sometimes needed when a type extends or implements a parameterized class or interface.

Example Before type Erasure:

Page 16: Java generics(Under The Hood Of The Compiler) by Harmeet singh

Bridge Method

Example After type Erasure:

● Side Effect of type erasure is that two methods have identical signature before type erasure and different signature after type erasure.

Page 17: Java generics(Under The Hood Of The Compiler) by Harmeet singh

Bridge Method● Under which circumstances is a bridge method generated ?

o Bridge methods are necessary when a class implements a parameterized interface or extends a parametrized superclass and type erasure changes the arguments type of any of the inherited non-static method.

Page 18: Java generics(Under The Hood Of The Compiler) by Harmeet singh

Bridge Method● The compiler must add the bridge methods even if the subclass does not

override the inherited method.

Page 19: Java generics(Under The Hood Of The Compiler) by Harmeet singh

Type Erasure OF Parameterized Type

● The Erasure of a parameterized type is the type without any type argument (i.e the raw type). The definition extends to arrays and nested types.

Page 20: Java generics(Under The Hood Of The Compiler) by Harmeet singh

Type Erasure Of Type Parameter● The type erasure of a type parameter is the erasure of its leftmost bound.

The type erasure of an unbounded type parameter is type Object. Examples :

Page 21: Java generics(Under The Hood Of The Compiler) by Harmeet singh

Type Erasure Of Generic Type● The Erasure of a method signature consisting of the same name and the

erasures of all the formal method parameters types.Examples :

Page 22: Java generics(Under The Hood Of The Compiler) by Harmeet singh

Reficiation● Representing type parameters and arguments of generic types and

method at runtime. Reficiation is the opposite of type Erasure.

Page 23: Java generics(Under The Hood Of The Compiler) by Harmeet singh

Reifiable● A type whose type information is fully available at run time, that is, a type that does not lose

information in the course of type erasure. ● The following types are reifiable:

o primitive typeso non-generic (or non-parameterized) reference typeso unbounded wildcard instantiationso raw typeso arrays of any of the above

● The non-reifiable types, which lose type information as a side effect of type erasure, are:o instantiations of a generic type with at least one concrete type argumento instantiations of a generic type with at least one bounded wildcard as type argument

● Reifiable types are permitted in some places where non-reifiable types are disallowed.o as type in an instanceof expressiono as component type of an array

Page 24: Java generics(Under The Hood Of The Compiler) by Harmeet singh

References

● http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html