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:com.quatico.base.aem.test.api.builders.AbstractBuilder.java

protected <R> R getValue(String property, Class<R> type) throws IllegalStateException {
    try {/*from   w  w w  . j a va2  s . co  m*/
        Object result = getChild(property, AbstractBuilder.class).getResult();
        if (result == null) {
            throw new IllegalStateException();
        }
        if (wrapPrimitive(type).isAssignableFrom(result.getClass())) {
            //noinspection unchecked
            return (R) result;
        }
        throw new ClassCastException();
    } catch (ClassCastException | IllegalPropertyException ex) {
        throw new IllegalStateException(ex);
    }
}

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

/**
 * Retrieve the JsoMap object for specified key.
 *
 * @param key key name./* ww w .ja va2  s .  c  om*/
 * @return JsoMap object.
 * @throws ClassCastException, IllegalArgumentException.
 */
@SuppressWarnings("unchecked")
// JSONObject.keys() has no type information.
JsoMap getObj(String key) throws ClassCastException, IllegalArgumentException {
    try {
        Object o = super.get(key);
        if (o instanceof JSONObject) {
            JSONObject value = (JSONObject) o;
            ArrayList<String> keys = new ArrayList<String>(value.length());
            for (Iterator<String> it = value.keys(); it.hasNext();) {
                keys.add(it.next());
            }
            String[] names = new String[keys.size()];
            return new JsoMap(value, keys.toArray(names));
        } else if (o instanceof Integer) {
            throw new IllegalArgumentException();
        } else {
            throw new ClassCastException();
        }
    } catch (JSONException e) {
        return null;
    }
}

From source file:com.link_intersystems.lang.ContextAwareTest.java

@Test
public void proxyThrowsException() throws Exception {
    final TestContextAware contextAware = new TestContextAware();
    TargetInterface targetInterface = createStrictMock(TargetInterface.class);

    expect(targetInterface.concat("HELLO ", "WORLD")).andAnswer(new IAnswer<String>() {

        public String answer() throws Throwable {
            assertTrue(contextAware.isActivated());
            Method2 method = contextAware.getInvocationMethod();
            Assert.assertNotNull(method);
            Method declaredMethod = TargetInterface.class.getDeclaredMethod("concat", String.class,
                    String.class);
            Assert.assertEquals(declaredMethod, method.getMember());
            throw new ClassCastException();
        }//from   www .j  av  a2 s.  c  o m
    });

    replay(targetInterface);
    TargetInterface targetInterfaceContextAware = contextAware.createContextProxy(targetInterface);
    Method2 method = contextAware.getInvocationMethod();
    Assert.assertNull(method);
    try {
        targetInterfaceContextAware.concat("HELLO ", "WORLD");
    } catch (ClassCastException illegalAccessException) {
        method = contextAware.getInvocationMethod();
        Assert.assertNull(method);
    }
    assertFalse(contextAware.isActivated());
}

From source file:org.jtransfo.internal.ConverterHelperTest.java

@Test
public void testGetDeclaredTypeConverterCce() throws Exception {
    MappedBy mappedBy = mock(MappedBy.class);
    when(mappedBy.typeConverterClass()).thenReturn(MappedBy.DefaultTypeConverter.class);
    when(mappedBy.typeConverter()).thenReturn(NoConversionTypeConverter.class.getName());
    when(mappedBy.field()).thenReturn(MappedBy.DEFAULT_FIELD);
    when(reflectionHelper.newInstance(NoConversionTypeConverter.class.getName()))
            .thenThrow(new ClassCastException());

    exception.expect(JTransfoException.class);
    exception.expectMessage("Declared TypeConverter class org.jtransfo.NoConversionTypeConverter "
            + "cannot be cast to a TypeConverter.");
    converterHelper.getDeclaredTypeConverter(mappedBy);
}

From source file:com.anrisoftware.globalpom.measurement.AbstractValue.java

@Override
public int compareTo(Object o) {
    if (o instanceof Value) {
        return compare((Value) o);
    }/*w w  w.j  a  va2s.  c  om*/
    if (o instanceof Number) {
        return compare((Number) o);
    }
    throw new ClassCastException();
}

From source file:net.mlw.vlh.ValueListInfo.java

/**
    * Extracts a value with the given key from filters and converts it to a String.
    * If it isn't a String a warning is logged and null is returned.
    * //  ww w. j av  a  2s.  c o  m
    * @param key
    * @return The value stored under the key in filters or null if not present or not a String.
    */
protected String getFilterParameterAsString(String key) {
    Object parameter = filters.get(key);

    if (parameter != null) {
        if (parameter instanceof String) {
            return (String) parameter;
        } else {
            if (LOGGER.isWarnEnabled()) {
                LOGGER.warn("Parameters contain the parameter '" + key
                        + "' but it isn't String, as expected. It is a " + parameter.getClass()
                        + " with the value " + parameter, new ClassCastException());
            }
        }
    }
    return null;
}

From source file:android.app.FragmentState.java

/**
 * Create a new instance of a Fragment with the given class name.  This is
 * the same as calling its empty constructor.
 *
 * @param context The calling context being used to instantiate the fragment.
 * This is currently just used to get its ClassLoader.
 * @param fname The class name of the fragment to instantiate.
 * @param args Bundle of arguments to supply to the fragment, which it
 * can retrieve with {@link #getArguments()}.  May be null.
 * @return Returns a new fragment instance.
 * @throws InstantiationException If there is a failure in instantiating
 * the given fragment class.  This is a runtime exception; it is not
 * normally expected to happen.//from   ww w . j  ava 2 s.  c  o  m
 */
public static Fragment instantiate(Context context, String fname, @Nullable Bundle args) {
    try {
        Class<?> clazz = sClassMap.get(fname);
        if (clazz == null) {
            // Class not found in the cache, see if it's real, and try to add it
            clazz = context.getClassLoader().loadClass(fname);
            if (!Fragment.class.isAssignableFrom(clazz)) {
                throw new InstantiationException(
                        "Trying to instantiate a class " + fname + " that is not a Fragment",
                        new ClassCastException());
            }
            sClassMap.put(fname, clazz);
        }
        Fragment f = (Fragment) clazz.newInstance();
        if (args != null) {
            args.setClassLoader(f.getClass().getClassLoader());
            f.mArguments = args;
        }
        return f;
    } catch (ClassNotFoundException e) {
        throw new InstantiationException("Unable to instantiate fragment " + fname
                + ": make sure class name exists, is public, and has an" + " empty constructor that is public",
                e);
    } catch (java.lang.InstantiationException e) {
        throw new InstantiationException("Unable to instantiate fragment " + fname
                + ": make sure class name exists, is public, and has an" + " empty constructor that is public",
                e);
    } catch (IllegalAccessException e) {
        throw new InstantiationException("Unable to instantiate fragment " + fname
                + ": make sure class name exists, is public, and has an" + " empty constructor that is public",
                e);
    }
}

From source file:jp.terasoluna.fw.web.struts.form.DynaValidatorActionFormExTest.java

/**
 * testReset02() <br>//  w w w .ja  v  a2  s  . c  o m
 * <br>
 *
 * (??n)
 * <br>
 * _?FC
 * <br><br>
 * l?F(?) mapping:not null<br>
 *         (?) request:ftHgW?[i[NGXgIuWFNg<br>
 *         (?) resetter:ResetterOIuWFNg<br>
 *
 * <br>
 * l?F(l) void:?|<br>
 *         (?) DynaValidatorActionFormEx_ResetterStub01#reset():?s<br>
 *         (?) ?O:??O?<br>
 *                    G?[?O?F""<br>
 *                    ?O?<br>
 *                    ClassCastException<br>
 *
 * <br>
 * ftHgW?[i[NGXgIuWFNg??AResetterOIuWFNg???A?O?o?A?ImF?B
 * <br>
 *
 * @throws Exception
 *             ?\bh?O
 */
public void testReset02() throws Exception {
    // NGXg??
    MockHttpServletRequest request = new MockHttpServletRequest();
    // ZbV??
    MockHttpSession session = new MockHttpSession();
    // ReLXg??
    MockServletContext context = new MockServletContext();
    // ANVT?[ubg??
    DynaValidatorActionFormEx_ActionServletStub01 servlet = new DynaValidatorActionFormEx_ActionServletStub01();
    // vtBbNX??
    String prefix = "";
    // W?[????
    ModuleConfig moduleConfig = new ModuleConfigImpl(prefix);
    // }bsO???
    ActionMapping mapping = new ActionMapping();

    // ?
    request.setSession(session);
    request.setAttribute(Globals.MODULE_KEY, moduleConfig);
    session.setServletContext(context);
    context.setAttribute(Resetter.RESETTER_KEY + prefix, new Object());
    context.setAttribute(Globals.MODULE_KEY, moduleConfig);
    this.formEx.setServlet(servlet);
    servlet.setContext(context);

    // eXg?s
    this.formEx.reset(mapping, request);

    // eXgmF
    assertTrue(LogUTUtil.checkError("", new ClassCastException()));
}

From source file:jp.terasoluna.fw.web.struts.form.DynaValidatorActionFormExTest.java

/**
 * testReset05() <br>/*from w  w  w. j  a v  a  2  s .  c  o  m*/
 * <br>
 *
 * (??n)
 * <br>
 * _?FC
 * <br><br>
 * l?F(?) mapping:not null<br>
 *         (?) request:TuW?[i[NGXgIuWFNg<br>
 *         (?) resetter:ResetterOIuWFNg<br>
 *
 * <br>
 * l?F(l) void:?|<br>
 *         (?) DynaValidatorActionFormEx_ResetterStub01#reset():?s<br>
 *         (?) ?O:??O?<br>
 *                    G?[?O?F""<br>
 *                    ?O?<br>
 *                    ClassCastException<br>
 *
 * <br>
 * TuW?[i[NGXgIuWFNg??AResetterOIuWFNg???A?O?o?A?ImF?B
 * <br>
 *
 * @throws Exception
 *             ?\bh?O
 */
public void testReset05() throws Exception {
    // NGXg??
    MockHttpServletRequest request = new MockHttpServletRequest();
    // ZbV??
    MockHttpSession session = new MockHttpSession();
    // ReLXg??
    MockServletContext context = new MockServletContext();
    // ANVT?[ubg??
    DynaValidatorActionFormEx_ActionServletStub01 servlet = new DynaValidatorActionFormEx_ActionServletStub01();
    // vtBbNX??
    String prefix = "sub1";
    // W?[????
    ModuleConfig moduleConfig = new ModuleConfigImpl(prefix);
    // }bsO???
    ActionMapping mapping = new ActionMapping();

    // ?
    request.setSession(session);
    request.setAttribute(Globals.MODULE_KEY, moduleConfig);
    session.setServletContext(context);
    context.setAttribute(Resetter.RESETTER_KEY + prefix, new Object());
    context.setAttribute(Globals.MODULE_KEY, moduleConfig);
    this.formEx.setServlet(servlet);
    servlet.setContext(context);

    // eXg?s
    this.formEx.reset(mapping, request);

    // eXgmF
    assertTrue(LogUTUtil.checkError("", new ClassCastException()));
}

From source file:WeakIdentityMap.java

static int compare(Object a, Object b) {
    if (a == b) {
        return 0;
    }//from   w w  w  .  ja  v a  2 s .c om
    if (a == null) {
        return 1;
    }
    if (b == null) {
        return -1;
    }
    Class ac = a.getClass();
    if (!(ac.isArray())) {
        return ((Comparable) a).compareTo(b);
    }
    if (ac != b.getClass()) {
        throw new ClassCastException();
    }
    if (a instanceof Object[]) {
        return compare((Object[]) a, (Object[]) b);
    } else if (a instanceof int[]) {
        return compare((int[]) a, (int[]) b);
    } else if (a instanceof float[]) {
        return compare((float[]) a, (float[]) b);
    } else if (a instanceof long[]) {
        return compare((long[]) a, (long[]) b);
    } else if (a instanceof double[]) {
        return compare((double[]) a, (double[]) b);
    } else if (a instanceof byte[]) {
        return compare((byte[]) a, (byte[]) b);
    } else if (a instanceof char[]) {
        return compare((char[]) a, (char[]) b);
    } else if (a instanceof boolean[]) {
        return compare((boolean[]) a, (boolean[]) b);
    } else if (a instanceof short[]) {
        return compare((short[]) a, (short[]) b);
    } else {
        throw new ClassCastException();
    }
}