57

There are situations, where it is practical to have a type-cast return a null value instead of throwing a ClassCastException. C# has the as operator to do this. Is there something equivalent available in Java so you don't have to explicitly check for the ClassCastException?

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
VoidPointer
  • 17,651
  • 15
  • 54
  • 58

6 Answers6

62

Here's an implementation of as, as suggested by @Omar Kooheji:

public static <T> T as(Class<T> clazz, Object o){
    if(clazz.isInstance(o)){
        return clazz.cast(o);
    }
    return null;
}

as(A.class, new Object()) --> null
as(B.class, new B())  --> B
Aaron Maenpaa
  • 119,832
  • 11
  • 95
  • 108
  • 1
    Aaron, what if I want to pass a generic type as the clazz parameter to your "as" function? – mfalto Aug 26 '12 at 21:23
  • 2
    self answer... that cant be done in Java because of type erasure – mfalto Aug 26 '12 at 21:34
  • 2
    I like the design of the above function `as`, but it bothers me the fact that the `isInstance` method will be invoked twice, due to the use of the `cast` method of the class `Class`. Internally, this method uses the `IsIsntance` method to check for compatibility between types. Nevertheless, we can expect that the JVM may inline the `cast` code inside the given `as` function and after that the Jitter may remove redundant calls to the `IsInstance` method, but we cannot be sure about that. – Miguel Gamboa Jan 09 '13 at 19:24
29

I'd think you'd have to roll your own:

return (x instanceof Foo) ? (Foo) x : null;

EDIT: If you don't want your client code to deal with nulls, then you can introduce a Null Object

interface Foo {
    public void doBar();
}
class NullFoo implements Foo {
    public void doBar() {} // do nothing
}
class FooUtils {
    public static Foo asFoo(Object o) {
        return (o instanceof Foo) ? (Foo) o : new NullFoo();
    }
}
class Client {
    public void process() {
        Object o = ...;
        Foo foo = FooUtils.asFoo(o);
        foo.doBar(); // don't need to check for null in client
    }
}
toolkit
  • 49,809
  • 17
  • 109
  • 135
15

You can use the instanceof keyword in place of C#'s is, but there is nothing like as.

Example:

if(myThing instanceof Foo) {
   Foo myFoo = (Foo)myThing; //Never throws ClassCastException
   ...
}
Chris Marasti-Georg
  • 34,091
  • 15
  • 92
  • 137
12

You could write a static utility method like this. I don't think it's terribly readable, but it's the best approximation of what you're trying to do. And if you use static imports it wouldn't be too bad in terms of readability.

package com.stackoverflow.examples;
public class Utils {
    @SuppressWarnings("unchecked")
    public static <T> T safeCast(Object obj, Class<T> type) {
        if (type.isInstance(obj)) {
            return (T) obj;
        }
        return null;
    }
}

Here's a test case that demonstrates how it works (and that it does work.)

package com.stackoverflow.examples;
import static com.stackoverflow.examples.Utils.safeCast;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;

import org.junit.Test;

public class UtilsTest {

    @Test
    public void happyPath() {
        Object x = "abc";
        String y = safeCast(x, String.class);
        assertNotNull(y);
    }

    @Test
    public void castToSubclassShouldFail() {
        Object x = new Object();
        String y = safeCast(x, String.class);
        assertNull(y);
    }

    @Test
    public void castToUnrelatedTypeShouldFail() {
        Object x = "abc";
        Integer y = safeCast(x, Integer.class);
        assertNull(y);
    }
}
Mike Deck
  • 18,045
  • 16
  • 68
  • 92
  • This doesn't actually work. isAssignableFrom tests to see whether obj is an instance of a superclass of T. Thus, safeCast(new Object(), A.class).doA() will return a class cast exception, rather than an NPE. – Aaron Maenpaa Sep 29 '08 at 14:43
  • you're right. I had the two class objects backwards. I've edited the answer and it should be correct now. – Mike Deck Sep 29 '08 at 15:12
  • 1
    Is there any particular reason for using type.isAssignableFrom(obj.getClass() instead of type.isInstance(obj)? – VoidPointer Sep 30 '08 at 10:39
  • 2
    nope, isInstance works and is more concise. I originally thought isInstance wouldn't work for instances of subclasses, but it clearly does. The answer has been updated to use isInstance, Thanks. – Mike Deck Oct 02 '08 at 20:54
4

In java 8 you can also use stream syntax with Optional:

Object o = new Integer(1);

Optional.ofNullable(o)
        .filter(Number.class::isInstance)
        .map(Number.class::cast)
        .ifPresent(n -> System.out.print("o is a number"));
Dmitry Klochkov
  • 2,484
  • 24
  • 31
-4

I'm speculating you could propably creas an as operator

something like

as<T,Type> (left, right)  
which evaluates to 
if (typeof(left) == right)
   return (right)left
else
    return null

I'm not sure how you'd do it, I'm a c# at the moment and my Java hat has gotten a bit dusty since I left university.

LiraNuna
  • 64,916
  • 15
  • 117
  • 140
Omar Kooheji
  • 54,530
  • 68
  • 182
  • 238