R
rwfields
Why does the following code dump a ClassCastException?
import java.util.*;
public class ClassLoader extends java.lang.ClassLoader
{
protected Set<Package> packages = new TreeSet<Package>();
public ClassLoader()
{
super();
loadPackageSet();
}
public Set getPackageSet()
{
return packages;
}
protected void loadPackageSet()
{
Package pkg[] = super.getPackages();
for ( int i = 0; i < pkg.length; i++ )
{
// Could catch the ClassCastException here, in which case the
// only package that actually makes it into the Set is
// java.lang. The other 127 packages dump the exception.
packages.add(pkg);
}
}
}
Thanks,
Randall
import java.util.*;
public class ClassLoader extends java.lang.ClassLoader
{
protected Set<Package> packages = new TreeSet<Package>();
public ClassLoader()
{
super();
loadPackageSet();
}
public Set getPackageSet()
{
return packages;
}
protected void loadPackageSet()
{
Package pkg[] = super.getPackages();
for ( int i = 0; i < pkg.length; i++ )
{
// Could catch the ClassCastException here, in which case the
// only package that actually makes it into the Set is
// java.lang. The other 127 packages dump the exception.
packages.add(pkg);
}
}
}
Thanks,
Randall