23

Can this method return true somehow?

public static <T> boolean isVoid(T t)
{
    return t instanceof Void;
}
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417

4 Answers4

51

Yes, but I'm sure that isn't really useful:

public static void main(final String[] args) throws Exception {
    final Constructor c = Void.class.getDeclaredConstructors()[0];
    c.setAccessible(true);
    System.out.println(c.newInstance(null) instanceof Void);
}

A Void class can't be instantiated so normally your code wouldn't require to deal with Void instances. The above code snippet is just an example of what havoc you can unleash when using reflection... ;-)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sanjay T. Sharma
  • 22,857
  • 4
  • 59
  • 71
  • 2
    +1 very nice! btw, you just use `c.newInstance()` (don't need the `null` param) – Bohemian Jul 09 '11 at 19:18
  • 6
    +1 The only Class prevented from creating via Reflection is Class. – Peter Lawrey Jul 09 '11 at 19:34
  • 6
    This is why if you want to make sure no one ever instantiates a class (e.g. library) you not only need to make the constructor private, but also make that constructor throw an exception. I'd recommend UnsupportedOperationException. – Sled Jul 25 '11 at 16:33
  • 1
    @Sanjary T. Sharma Ah, "QFT".equals( "Quoted for Truth" )? – Sled Jul 25 '11 at 18:40
  • The equivalent code works without requiring anything complex in Scala: `().isInstanceOf[Unit] == true` – soc Jul 25 '11 at 19:38
6

I fail to see why you would check if a value is an instance of void (or Void) since, like said nth times, cannot be instanciated, or even extended without hacking with reflexion. However, for a more useful situation, if you want to know if a given Class is of a void type, you would not use instanceof and your method parameter would be of type Class<?> instead. A test case would be :

public class VoidCheckTest {

    public static void main(String...args) throws SecurityException, NoSuchMethodException {
        Class<VoidCheckTest> c = VoidCheckTest.class;

        Method m = c.getMethod("main", String[].class);

        System.out.println(m.getReturnType().getName() + " = " + isVoid(m.getReturnType()));        
    }

    private static boolean isVoid(Class<?> t) {
        return Void.class.isAssignableFrom(t) || void.class.equals(t);
    }
}

which would output

void = true

There might other use cases for this method, but I don't see any other right now.

Yanick Rochon
  • 51,409
  • 25
  • 133
  • 214
3

No:

Constructor<Void> cv = Void.class.getDeclaredConstructor(); 
cv.setAccessible(true); 
Void v = cv.newInstance(); 
System.out.println(v instanceof Void); //-> true
emboss
  • 38,880
  • 7
  • 101
  • 108
2

No. To make it return true you have to call it and pass an argument of type Void. But the constructor of class Void is private, so you cannot call it. Moreover, this class is final, so you cannot even extend it. Thus you cannot create an instance of class Void. But it is needed to make your method to return true.

BTW: If you really want to do it, call the constructor of Void by reflection. But I can think about this as a system abuse exercise. Good luck.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AlexR
  • 114,158
  • 16
  • 130
  • 208