mengdiwang / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Preconditions.checkInstanceOf #538

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Would it be useful to add method:

public static <T> T checkInstanceOf(Object reference, Class<T> clazz) ?

a draft implementation:

public static <T> T checkInstanceOf(Object reference, Class<T> clazz){
 if (!clazz.isInstance(reference)){
   throw new ClassCastException();
 }
 return (T) reference;
}

Original issue reported on code.google.com by kua...@gmail.com on 1 Feb 2011 at 9:07

GoogleCodeExporter commented 9 years ago
This is an interesting idea, but I think I never encountered the need for this. 
I'm interested to see an example use-case.

If a method only accepts "Foo" instances, I'd rather specify "Foo" in the 
signature, than accept a superclass and then check it. For example, I'd do:

void doSomething(List<String> names) {
    ...
}

instead of

void doSomething(Collection<String> names) {
   checkInstanceOf(names, List.class);
   ...
}

Moreover, what about simply casting your object to T?

T referenceAsT = (T) reference;

It should automatically throw a ClassCastException if reference is not a T.

Maybe this could be useful when using reflection, though, when you are playing 
with "Methods", "Fields", and "Objects" directly, and want to check the type of 
such an object?

Original comment by nev...@gmail.com on 1 Feb 2011 at 9:35

GoogleCodeExporter commented 9 years ago
a) Casting is not equivalent. Casting will succeed when reference is null, 
Class.isInstance(reference) won't

b) It would be nicer to have a way to create formatted messages (as in the 
other Preconditions.x methods) than just throw a plain ClassCastException

c) because of type erasure, you sometimes don't know what you are dealing with 
when iterating over a data structure that's beyond your reach. I'd say this 
method would be very useful

Original comment by SeanPFl...@googlemail.com on 1 Feb 2011 at 9:44

GoogleCodeExporter commented 9 years ago
For comment 1:
Yes, it is a kind of
 T referenceAsT = (T) reference;
but without annoying compiler warnings.

For comment 2:
as for a) b) and c) I completely agree.

Any way it is just an idea. Any enhancements form guava developers are welcome.

Original comment by kua...@gmail.com on 1 Feb 2011 at 9:54

GoogleCodeExporter commented 9 years ago
Good points. About b), it indeed would be nice to have nicely formatted 
messages.

By the way, my previous comment wasn't meant as a critic of the idea, I merely 
wanted to poke at it to understand it better. I'm happy I did, because you both 
gave insightful arguments / explanations :)

Original comment by nev...@gmail.com on 1 Feb 2011 at 10:27

GoogleCodeExporter commented 9 years ago
Class.cast() does the same thing (except it accepts null instances):
http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Class.html#cast(java.
lang.Object)

Original comment by daniel.y...@gmail.com on 1 Feb 2011 at 10:46

GoogleCodeExporter commented 9 years ago
As the annoying compiler warnings point out, casting to T is really only 
casting to the erasure of T.

Class.cast() mostly does the job.  But, it would be nice to have options to 
format an error message for the ClassCasException

Original comment by bill.cla...@gmail.com on 1 Feb 2011 at 5:28

GoogleCodeExporter commented 9 years ago
I don't like this idea.  An type-based precondition is already relative 
uncommon, then by the time you peel off the ones where the signature can be 
changed, or where a plain cast or Class.cast() is sufficient. there's not 
enough left.  This is an example of a method that would be used poorly far more 
often than used well.

Original comment by kevinb@google.com on 1 Feb 2011 at 8:02

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:15

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:09