last day (14 days later) » 

8:13 AM
10
A: Class.asSubclass signature

JatinIn case of the return type Class<? extends U>. Lets first try understanding, the getClass signature: AbstractList<String> ls = new ArrayList<>(); Class<? extends AbstractList> x = ls.getClass(); Now had the compiler allowed us to do: Class<AbstractList> x = ls.getClass(); This would have be...

 
@CptWasp Check PS on why asSubClass. I hope the answer has cleared your queries.
 
I still don't understand why it's really typesafe. In practice you can get a ClassCastException exactly as with a manual cast... I understand that what you get IS <? extends U>, but for me it's the same as <U> for the client code, and the resulting code isn't cluttered by the wildcard.
 
Iam sorry but you still didnt get my answer. It's not safe to return <U>. Hence compiler can in no way safely return just <U>. For type safety, read the second part of my answer, it's not completely typesafe for backward compatibility reasons. But enough to remove half of bugs.
It cannot be made typesafe for backward compatibility reason**
 
I undertsand that it mimics the getClass method (and I understand the peculiar reason behind the getClass signature); and I follow your reasoning until the "BIG ISSUE" comment. The getClass method by contract must return the runtime type of an Object, and its behavior is the most accurate given the situation; but what is the real purpose of narrowing a type when assigning it to a fresh variable? I can always safely cast: Class<AbstractList> bClass= (Class<AbstractList>)aClass; ... it's unchecked but it should always work. I'm wrong?
The following code works: List<String> ls = new ArrayList<String>(); Class<? extends List> x = ls.getClass(); @SuppressWarnings("unchecked") Class<AbstractList> y= (Class<AbstractList>)x; System.out.println("OK"); I understand I'm missing your point... help me please :)
 
Hi. the cast of Class<AbstractList> y = (Class<AbstractList>) xs; works.
 
8:16 AM
Hi Jatin, thank you for your effort and aid
 
but as per the documentation, we cannot assume:
Class<ArrayList> c = null;
Class<List> cc = c;
generics are invariant. so obviously it wont compile. But as such in the api, i as such no where where `c` can be safely used as `Class<list>`
But I agree with you from the narrowing point of view. This method I think is mostly useful for wildcards
 
Now I understand better the choice under the getClass method, I think that now is clear.
Ok you say it's used as an input for wildcarded methods, primarly
 
ex: Class<?> c = null;
Class<? extends SomeClass> x = c.asSubClass(SomeClass.class);
In above cases, it is mostly used. The type-cast will also work. but i am not sure it be jvm dependent and the cast might fail as Class<List> and Class<AbstractList> are not same.
 
I have a (maybe wrong) refrain from suppressing unchecked warnings
 
Hmm. yes can be.
 
8:20 AM
It could give problems with things like Lists, Maps...
but no problem at all for things that aren't Collections
right?
 
I think:
Class<AbstractList> y = (Class<AbstractList>)xs;
Class<List> ys = (Class<List>)xs;
the above jsut works because internally there might something hidden. but from the compiler perspective (and so the specification). Its not safe to interchange Class<AbstractList> to Class<List>
who knows it might thrown an runtime exception tomorrow because it is not documented.
am I making it clear. I get a feel iam now.
 
Mmm, yes it could be a risk
 
not*
I mean. am I making it clear. I get a feel iam not*.
 
public static <T> Class<T> className(String name, String param, Class<T> type)
        throws CheckException,
               MissingParameterException{
    if(param == null){
        throw new MissingParameterException(name);
    }
    param = param.trim();
    Class<?> theClass = null;
    try{
        theClass = Class.forName(param);
    }
    catch(Exception e){
        throw new CheckException(getHeader(name)
                                 + "il nome di una classe si compone di stringhe separate da "
This code checks for an input parameter
"param"
and casts it to a given (super)type
do you feel it could be better using asSubclass?
I don't like having to return <? extends T>
 
Hmm interesting.
 
8:25 AM
but I use it ONLY for concrete classes, obviously
Not for Collections
I use it for dynamic modules that I put in a Collection in the client code
 
but <? extends T> is completely Ok. For example:
Class<? extends Animal> claz = a.asSomeClass(Animal.class);
Animal a = claz.newInstance();
 
Ok
Let me see my client code and think a little about why it clutters my client code
maybe I'm propagating the <? extends T> without need
 
this is the documentation for asSubClass
This method is useful when a client needs to "narrow" the type of
* a {@code Class} object to pass it to an API that restricts the
* {@code Class} objects that it is willing to accept. A cast would
* generate a compile-time warning, as the correctness of the cast
* could not be checked at runtime (because generic types are implemented
* by erasure)
 
Thank you again, I mark the problem resolved by you
 
I dont think i have done enough to solve your confusion though. I think its clear on why the signature is like that. but the question i think remains, is what is the purpose of this method when cast would work. probably a bounty might help to attract better answers
Anyways thanks for the question and discussion
I think for cast: My gut feeling says. That cast just works in our case at runtime. But it cannot be gaurranteed that this will always work: Class<AbstractList> c = (Class<AbstractList>) ArrayList.class
Note: ArrayList.class.equals(AbstractList.class) gives false
 
8:41 AM
right
if you want give a look to the check method above, when you have time; if you can find a better/clearer/cleaner way to implement it, let me know please
 
Ok. i would say, this is a better method to use it:
public static <T, U> Class<? extends U> asSubClassBetter(Class<T> t, Class<U> c){
    return t.asSubclass(c);
}
there is vertainly lot of magic happening in getClass method:
ArrayList<String> strings = new ArrayList<>();
Object aClass1 = strings.getClass();
@SuppressWarnings("unchecked") Class<AbstractList> c = (Class<AbstractList>)aClass1;
System.out.println("---> " + c.getClass().equals(ArrayList.class));
System.out.println("---> " + c.getClass().equals(AbstractList.class));
check above. they both return false.
sorry my fault. mistake: its this
    System.out.println("---> " + c.equals(ArrayList.class));
    System.out.println("---> " + c.equals(AbstractList.class));
it comes as true and false.
so even though c is Class<AbstractList>. But c.equals(AbstractList.class)) is false.
 
interesting
what I think, is that with Collections and Maps it's better to avoid strange casts... but normally you need to cast more on classes you PUT into Collections that on Collections themselves
 
hey. hey. hey. the reason castign works is because of generics
they are erased.
 
you're right
 
so: Class<AbstractList> c = (Class<AbstractList>)aClass1 is same as
Class c = (Class) aClass1
 
8:53 AM
thank you again, now I check my code
 
To conclude:
List<String> ls = new ArrayList<String>();
Class<? extends List> x = ls.getClass();
Class<? extends AbstractList> aClass = x.asSubclass(AbstractList.class);

had it been: Class<AbstractList> aClass = (Class<AbstractList> x);
You would mistake aClass to be of AbstractList but it is not AbstractList. Bur rather ArrayList and aClass.equals(AbstractList.class) is false. it is absolutely the same logic as getClass. Precisely the same thing.
gosh finally clear. so much confusion.
 
:)
Let me see
 
Please feel free to let me know in case you think other wise. It helps me understand my concepts better :)
 
Yes, I'm getting it. When you see the problem from the perspective of non-collection classes things are simpler, and casts safer (or totally safe). But methods can be used on Collections, too... and the signature takes this into account
And when the cast is safe, I should not avoid at all costs @SuppressWarnings("unchecked")... maybe it's simpler and better to cast, when appropriate.
 
9:16 AM
If i were you. i wouldn't cast. Its just not safe. like above. where one casts it as AbstractList but instead it is ArrayList.
 

last day (14 days later) »