Monday, 17 March 2008

5 Things i donot understand in JavaGenericsFaq Tutorial

Firstly,
I am pleased to inform that i have completed 1 time revision of the Angelika Langer's JavaGenericsFaq and i can say it is gr8 collection of java generics tutorial

Please note that i am using jrockit90_150_04 compiler that gets shipped with weblogic9.1 server.I am also aware that few problems noted below are fine with other compilers?

Problem/Bug 1:

Doubt in Question "Why doesn't method overriding work" in JavaGenericsFaq

In the sample program it states that ambiguous suitation may occur.

However,I tried the same and it looks fine?. After opening source code of .class file I realized that the superclass methods generic types are replaced by "Object" and subclass types are replaced by "CharSequence".


class Box<T> {
private T theThing;
public Box(T t) { theThing=t; }
public void reset(T t) { theThing=t; }
public T get() { return theThing; }
}

class WordBox<S extends CharSequence> extends Box<String> {

public WordBox(S t) { super(t.toString().toLowerCase()); }

public void reset(S t) { super.reset(t.toString().toLowerCase()); }

}

public class OverridingExample {
public static void main(String[] args) {
WordBox<String> city=new WordBox<String>("Stogland");
city.reset("Vijayawada");
System.out.println(city.get());
}
}

----------------------------------
Problem/Bug 2:

Another small bug in method rev in JavaGenericsFaq page no: 211
found a method declaration as below:


private static <T> void rev(List<T> list) {
List<T> tmp = new ArrayList<T>(list);
for (int i=0;i<list.size();i++){
tmp.set(i,list.get(list.size()-i-1));
}
list = tmp;
}


Where as it should have been declared as:-

private static <T> List<T> revShouldHaveBeenLikeThis(List<T> list) {
List<T> tmp = new ArrayList<T>(list);
for (int i=0;i<list.size();i++){
tmp.set(i,list.get(list.size()-i-1));
}
list = tmp;
return list;
}



Infact a question has also been raised in java sun forum
--------------------------------

Problem/Bug 3:

Another problem in Page No: 336

In JavaGenericsFaq page no: 336 following program supposed to fail due to compilation problems but works fine here?


class Factory {

public static <T> ArrayList<T> make() {
return new ArrayList<T>();
}

public static <T> ArrayList<T> make(int size) {
return new ArrayList<T>(size);
}

public static <T,S extends T> ArrayList<T> make(List<S> list) {
return new ArrayList<T>(list);
}
}

public class FactoryExample {
public static void main(String[] args) {
ArrayList<? super String> seq1=Factory.make();

ArrayList<? super String> seq2=Factory.<String>make();

ArrayList<? super String> seq3=Factory.make(10); // Supposed to be error here...But works fine for me???


bytecode version of this class opened by DjCompiler

// Decompiled by DJ v3.6.6.79 Copyright 2004 Atanas Neshkov Date: 17/03/2008 09:20:53
// Home Page : http://members.fortunecity.com/neshkov/dj.html - Check often for new version!
// Decompiler options: packimports(3)
// Source File Name: FactoryExample.java

package generic;

import java.util.ArrayList;
import java.util.List;

class Factory
{

Factory()
{
}

public static ArrayList make()
{
return new ArrayList();
}

public static ArrayList make(int size)
{
return new ArrayList(size);
}

public static ArrayList make(List list)
{
return new ArrayList(list);
}
}

// Decompiled by DJ v3.6.6.79 Copyright 2004 Atanas Neshkov Date: 17/03/2008 09:21:44
// Home Page : http://members.fortunecity.com/neshkov/dj.html - Check often for new version!
// Decompiler options: packimports(3)
// Source File Name: FactoryExample.java

package generic;

import java.io.PrintStream;
import java.util.ArrayList;

// Referenced classes of package generic:
// Factory

public class FactoryExample
{

public FactoryExample()
{
}

public static void main(String args[])
{
ArrayList seq1 = Factory.make();
ArrayList seq2 = Factory.make();
ArrayList seq3 = Factory.make(10);
ArrayList seq4 = Factory.make(10);
ArrayList stringList = new ArrayList();
ArrayList seq5 = Factory.make(stringList);
ArrayList seq6 = Factory.make(stringList);
seq1 = seq2 = seq3 = seq4 = seq5 = seq6;
seq6.add("String");
System.out.println(seq2.get(0));
}
}



------------------------------

Problem/Bug 4:

In JavaGenericsFaq page no: 337 below progam supposed to compile but does not compile for me until i replace with the replaced version of the program.



Original version:

public class WorkAroundProblemExample {

public static class Dummy<T> {}
public static final Dummy<? super String> dummy=null;
public static final Dummy<? extends CharSequence> dummy2=null; *// Found problem with the type declaration in this line here*

public static <T> ArrayList<T> make(Dummy<T> d) {
return new ArrayList<T>();
}

public static <T> ArrayList<T> make(Dummy<T> d,int size) {
return new ArrayList<T>(size);
}

public static <T,S extends T> ArrayList<T> make(Dummy<T> d,List<S> list) {
return new ArrayList<T>(list);
}

public static void main(String[] args) {
WorkAroundProblemExample example=new WorkAroundProblemExample();
ArrayList<String> listString=new ArrayList<String>();

ArrayList<? super String> seq1=WorkAroundProblemExample.
make(WorkAroundProblemExample.dummy,listString);

ArrayList<? super String> seq2=WorkAroundProblemExample.
make(WorkAroundProblemExample.dummy2,listString);
*//Gives compilation problem in above line.*

seq1=seq2;seq2.add("String");
System.out.println(seq2.get(0));


}
}



Now when replaced with the below version starts working fine.Only change i did was replacing the line highighted in bold.


public class WorkAroundProblemExample {

public static class Dummy<T> {}
public static final Dummy<? super String> dummy=null;
public static final Dummy<CharSequence> dummy2=null;

public static <T> ArrayList<T> make(Dummy<T> d) {
return new ArrayList<T>();
}

public static <T> ArrayList<T> make(Dummy<T> d,int size) {
return new ArrayList<T>(size);
}

public static <T,S extends T> ArrayList<T> make(Dummy<T> d,List<S> list) {
return new ArrayList<T>(list);
}

public static void main(String[] args) {
WorkAroundProblemExample example=new WorkAroundProblemExample();
ArrayList<String> listString=new ArrayList<String>();

ArrayList<? super String> seq1=WorkAroundProblemExample.
make(WorkAroundProblemExample.dummy,listString);

ArrayList<? super String> seq2=WorkAroundProblemExample.
make(WorkAroundProblemExample.dummy2,listString);

seq1=seq2;seq2.add("String");
System.out.println(seq2.get(0));


}
}


------------------------------

Problem/Bug 5:

In Page: 101 of Java Generics FAQs Book Below method should be


public final class Wrapper<T> {
private final T theObject;
public Wrapper(T t) { theObject = t; }
public T getWrapper() { return theObject; }

private final class WrapperComparator<W extends Wrapper<? extends
Comparable<T>>>
implements Comparator<W> {
public int compare(W lhs, W rhs) {
return lhs.theObject.compareTo((T)(rhs.theObject));
}
}
public <V extends Wrapper<? extends Comparable<T>>> Comparator<V>
comparator() {
return this.new WrapperComparator<V>();
}



INSTEAD OF


public final class Wrapper<T> {
private final T theObject;
public Wrapper(T t) { theObject = t; }
public T getWrapper() { return theObject; }

private final class WrapperComparator<W extends Wrapper<? extends
Comparable<T>>>
implements Comparator<W> {
public int compare(W lhs, W rhs) {
return lhs.theObject.compareTo((T)(rhs.theObject));
}
public <V extends Wrapper<? extends Comparable<T>>> Comparator<V>
comparator() {
return this.new WrapperComparator<V>();
}
}




But in the current code in the book it is ending after comparator() method which will end up in compilation error.


Any comments welcome

No comments: