简体   繁体   English

如何在运行时检查'instanceof'以及Java对象的通用类型?

[英]How do I check both 'instanceof' and also the Generic type of a Java object at runtime?

How do I check both instanceof and isAssignableFrom of a Java object that uses generics? 如何检查使用泛型的Java对象的instanceofisAssignableFrom

If I have this method that checks instanceof only, BUT I also want to check that it returns a Boolean type as opposed to another type, how would I do this? 如果我有只检查instanceof方法,但是我还想检查它返回的是Boolean类型,而不是其他类型,我该怎么做?

public void myMethod(ObjectA<Boolean> object)
{
    if ( object instanceof ObjectA ) {
       // then do something
    }
}

In your example, the instanceof is unnecessary (unless you wanted to check for null , in which case you should just check for null with == ). 在您的示例中, instanceof是不必要的(除非您想检查null ,在这种情况下,您应该只使用==检查null )。

Your method declaration 您的方法声明

public void myMethod(ObjectA<Boolean> object)

already enforces that callers can only pass values that are of type ObjectA<Boolean> or assignable to type ObjectA<Boolean> . 已经强制调用者只能传递ObjectA<Boolean>类型或可分配给ObjectA<Boolean>类型的ObjectA<Boolean>

In other words, I cannot write code like 换句话说,我无法编写类似

ObjectA<Integer> notBoolean = ...;
myMethod(notBoolean);

the compiler will reject it. 编译器将拒绝它。

Within your method, since the parameter is of type ObjectA<Boolean> , the compiler guarantee is that nothing will get through at runtime that isn't an ObjectA . 在您的方法中,由于参数的类型为ObjectA<Boolean> ,因此编译器保证不会在运行时获得任何不是ObjectA For example, you won't end up with object holding a reference to a Socket or a NoSuchElementException object. 例如,您不会以持有对SocketNoSuchElementException对象的引用的object结尾。


The exception to all of the above is expressions having a raw type. 上述所有情况的例外是具有原始类型的表达式。 Don't use raw types. 不要使用原始类型。 Here's why: 原因如下:

With raw types, a client could potentially write 使用原始类型,客户端可能会写

ObjectA<Integer> notBoolean = ...
ObjectA raw = notBoolean;
myMethod(raw);

and the compiler would forgo the generic type check and allow the invocation (the compiler erases the generic usage in the context of that invocation) . 并且编译器将放弃通用类型检查并允许调用(编译器会在该调用的上下文中擦除通用用法)。

Your code would presumably then fail at runtime with a ClassCastException when trying something like 尝试执行类似的操作时,您的代码可能会在运行时失败,并出现ClassCastException

public void myMethod(ObjectA<Boolean> object)
{
    Boolean ref = object.get(); // ClassCastException
                                // returns an `Integer` and attempts to assign it to a `Boolean` variable
}

Your object would still hold a reference to a ObjectA instance, but whatever it might wrap will no longer necessarily be a Boolean (I'm assuming ObjectA is some kind of wrapper for a Boolean value here). 您的object仍将保留对ObjectA实例的引用,但是它可以包装的内容不再必须是Boolean (我假设ObjectA在这里是Boolean值的某种包装器)。

This ClassCastException will be more or less equivalent to what you could've thrown anyway. 这个ClassCastException或多或少将等同于您可能抛出的异常。

I wouldn't make a check specifically for this raw usage. 我不会专门检查这种原始用法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM