Example usage for java.lang ClassCastException ClassCastException

List of usage examples for java.lang ClassCastException ClassCastException

Introduction

In this page you can find the example usage for java.lang ClassCastException ClassCastException.

Prototype

public ClassCastException() 

Source Link

Document

Constructs a ClassCastException with no detail message.

Usage

From source file:edu.usc.goffish.gofs.formats.gml.KeyValuePair.java

@SuppressWarnings("unchecked")
public static KeyValuePair createKVP(String key, Object value) {
    if (value.getClass() == String.class) {
        return new StringKeyValuePair(key, (String) value);
    } else if (value.getClass() == Long.class) {
        return new LongKeyValuePair(key, (Long) value);
    } else if (value.getClass() == Integer.class) {
        return new IntKeyValuePair(key, (Integer) value);
    } else if (value.getClass() == Double.class) {
        return new DoubleKeyValuePair(key, ((Number) value).doubleValue());
    } else if (value.getClass() == List.class) {
        return new ListKeyValuePair(key, (List<KeyValuePair>) value);
    } else {//from   ww  w  .j a  v a 2 s.  c o  m
        // unknown value type
        throw new ClassCastException();
    }
}

From source file:com.sonicle.webtop.core.app.util.ClassHelper.java

public static Class loadClass(String className, Class requiredParentClass, String targetDescription) {
    String tdesc = StringUtils.defaultIfBlank(targetDescription, "Target");

    try {/*  w w w  .  ja va  2s .  com*/
        Class clazz = Class.forName(className);
        if (!requiredParentClass.isAssignableFrom(clazz))
            throw new ClassCastException();
        return clazz;

    } catch (ClassNotFoundException ex) {
        LOGGER.debug("{} class not found [{}]", tdesc, className);
    } catch (ClassCastException ex) {
        LOGGER.warn("A valid {} class must extends '{}' class", tdesc, requiredParentClass.toString());
    } catch (Throwable t) {
        LOGGER.error("Unable to load class [{}]", className, t);
    }
    return null;
}

From source file:com.github.pjungermann.config.specification.types.TypeConversionFailedErrorTest.java

@Test
public void getMessage_always_returnMessageResolvableWithCorrectCodes() {
    TypeConversionFailedError error = new TypeConversionFailedError("fake-key", "fake-value", Long.class,
            new ClassCastException());

    MessageSourceResolvable resolvable = error.getMessage();

    assertArrayEquals(new String[] { TypeConversionFailedError.MESSAGE_CODE }, resolvable.getCodes());
    assertArrayEquals(new Object[] { error.key, error.value, error.type, error.cause },
            resolvable.getArguments());//ww w. j  a  v  a  2  s .c  om
    assertEquals(TypeConversionFailedError.MESSAGE_CODE, resolvable.getDefaultMessage());
}

From source file:jef.tools.Assert.java

/**
 * classc/*from w w  w .  j a  va 2 s.  com*/
 * @param obj
 * @param c
 */
public static void isType(Object obj, Class<?> c) {
    if (!(c.isAssignableFrom(obj.getClass())))
        throw new ClassCastException();
}

From source file:org.intermine.app.net.DefaultRetryPolicy.java

/**
 * Checks whether Internal Server Error with an empty body received.
 *//*from  w w  w  .  jav  a2  s  . c  om*/
protected boolean exceptionalModeRequired(SpiceException ex) {
    HttpNetworkException netEx;

    try {
        netEx = (HttpNetworkException) ex.getCause();

        if (null == netEx) {
            throw new ClassCastException();
        }
    } catch (ClassCastException e) {
        return false;
    }

    String httpMsg = netEx.getErrorMessage();
    return isExceptionalStatusCode(netEx.getStatusCode()) && Strs.isNullOrEmpty(httpMsg);
}

From source file:CharPrefixTree.java

@Override
public boolean remove(final Object o) {
    if (o instanceof String) {
        final String s = (String) o;
        Node node = this.root;

        for (int i = 0; node != null && i < s.length() - 1; ++i)
            node = node.children.get(s.charAt(i));

        return node != null;
    } else//from  w  w  w.  j  av a2s.  c o  m
        throw new ClassCastException();
}

From source file:com.google.i18n.addressinput.common.JsoMap.java

/**
 * Retrieve the string value for specified key.
 *
 * @param key key name./*  w w  w .j a  v a2  s .  co m*/
 * @return string value.
 * @throws ClassCastException, IllegalArgumentException.
 */
@Override
public String get(String key) {
    try {
        Object o = super.get(key);
        if (o instanceof String) {
            return (String) o;
        } else if (o instanceof Integer) {
            throw new IllegalArgumentException();
        } else {
            throw new ClassCastException();
        }
    } catch (JSONException e) {
        return null;
    }
}

From source file:com.android.i18n.addressinput.JsoMap.java

/**
 * Retrieve the string value for specified key.
 *
 * @param key key name./*  w w w  . ja  v  a 2 s.c  o  m*/
 * @return string value.
 * @throws ClassCastException, IllegalArgumentException.
 */
@Override
public String get(String key) { // throws ClassCastException, IllegalArgumentException
    try {
        Object o = super.get(key);
        if (o instanceof String) {
            return (String) o;
        } else if (o instanceof Integer) {
            throw new IllegalArgumentException();
        } else {
            throw new ClassCastException();
        }
    } catch (JSONException e) {
        return null;
    }
}

From source file:click.kobaken.rxirohaandroid_sample.view.fragment.AccountRegisterFragment.java

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (!(getActivity() instanceof AccountRegisterListener)) {
        throw new ClassCastException();
    }/*  w  w w. ja v a 2  s . co m*/
    accountRegisterListener = (AccountRegisterListener) getActivity();
}

From source file:CharPrefixTree.java

@Override
public boolean contains(final Object o) {
    if (o instanceof String) {
        final String s = (String) o;
        if (s.length() == 0)
            return !isEmpty();

        Node node = this.root;
        for (int i = 0; node != null && i < s.length(); ++i)
            node = node.children.get(s.charAt(i));
        return node != null;
    } else/* w  w  w  . j ava2s. c  om*/
        throw new ClassCastException();
}