Best way to “negate” an instanceof

后端 未结 9 1311
后悔当初
后悔当初 2021-01-30 02:01

I was thinking if there exists a better/nicer way to negate an instanceof in Java. Actually, I\'m doing something like:

if(!(str instanceof String))         


        
相关标签:
9条回答
  • 2021-01-30 02:18

    If you can use static imports, and your moral code allows them

    public class ObjectUtils {
        private final Object obj;
        private ObjectUtils(Object obj) {
            this.obj = obj;
        }
    
        public static ObjectUtils thisObj(Object obj){
            return new ObjectUtils(obj);
        }
    
        public boolean isNotA(Class<?> clazz){
            return !clazz.isInstance(obj);
        }
    }
    

    And then...

    import static notinstanceof.ObjectUtils.*;
    
    public class Main {
    
        public static void main(String[] args) {
            String a = "";
            if (thisObj(a).isNotA(String.class)) {
                System.out.println("It is not a String");
            }
            if (thisObj(a).isNotA(Integer.class)) {
                System.out.println("It is not an Integer");
            }
        }    
    }
    

    This is just a fluent interface exercise, I'd never use that in real life code!
    Go for your classic way, it won't confuse anyone else reading your code!

    0 讨论(0)
  • 2021-01-30 02:23

    If you find it more understandable, you can do something like this with Java 8 :

    public static final Predicate<Object> isInstanceOfTheClass = 
        objectToTest -> objectToTest instanceof TheClass;
    
    public static final Predicate<Object> isNotInstanceOfTheClass = 
        isInstanceOfTheClass.negate(); // or objectToTest -> !(objectToTest instanceof TheClass)
    
    if (isNotInstanceOfTheClass.test(myObject)) {
        // do something
    }
    
    0 讨论(0)
  • 2021-01-30 02:25

    I don't know what you imagine when you say "beautiful", but what about this? I personally think it's worse than the classic form you posted, but somebody might like it...

    if (str instanceof String == false) { /* ... */ }
    
    0 讨论(0)
  • 2021-01-30 02:29

    You could use the Class.isInstance method:

    if(!String.class.isInstance(str)) { /* do Something */ }
    

    ... but it is still negated and pretty ugly.

    0 讨论(0)
  • 2021-01-30 02:35

    Usually you don't want just an if but an else clause as well.

    if(!(str instanceof String)) { /* do Something */ } 
    else { /* do something else */ }
    

    can be written as

    if(str instanceof String) { /* do Something else */ } 
    else { /* do something */ }
    

    Or you can write the code so you don't need to know if its a String or not. e.g.

    if(!(str instanceof String)) { str = str.toString(); } 
    

    can be written as

    str = str.toString();
    
    0 讨论(0)
  • 2021-01-30 02:35

    ok just my two cents, use a is string method:

    public static boolean isString(Object thing) {
        return thing instanceof String;
    }
    
    public void someMethod(Object thing){
        if (!isString(thing)) {
            return null;
        }
        log.debug("my thing is valid");
    }
    
    0 讨论(0)
提交回复
热议问题