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(String s) 

Source Link

Document

Constructs a ClassCastException with the specified detail message.

Usage

From source file:Main.java

public static BigInteger getBigInteger(Object value) {
    BigInteger ret = null;/* w  w w .  jav  a2 s.  co m*/
    if (value != null) {
        if (value instanceof BigInteger) {
            ret = (BigInteger) value;
        } else if (value instanceof String) {
            ret = new BigInteger((String) value);
        } else if (value instanceof BigDecimal) {
            ret = ((BigDecimal) value).toBigInteger();
        } else if (value instanceof Number) {
            ret = BigInteger.valueOf(((Number) value).longValue());
        } else {
            throw new ClassCastException("Not possible to coerce [" + value + "] from class " + value.getClass()
                    + " into a BigInteger.");
        }
    }
    return ret;
}

From source file:Main.java

/** Helper methods - gets the <I>Attr</I> <I>Node</I> object from a
<I>NamedNodeMap</I> of attributes.
   @param pNodeMap The <I>NamedNodeMap</I>.
   @param strName Name of the attribute.
   @return An <I>Attr</I> object.
 *///ww  w  .jav  a 2 s  .c  o  m
public static Attr getAttribute(NamedNodeMap pNodeMap, String strName) throws ClassCastException {
    Node pReturn = pNodeMap.getNamedItem(strName);

    if ((null == pReturn) || (pReturn instanceof Attr))
        return (Attr) pReturn;

    throw new ClassCastException("The node retrieved from the NamedNodeMap is not an Attr object.");
}

From source file:org.jasig.cas.ticket.registry.AbstractTicketRegistry.java

/**
 * @throws IllegalArgumentException if class is null.
 * @throws ClassCastException if class does not match requested ticket
 * class./*from w w w . ja  va 2 s.co  m*/
 */
public final Ticket getTicket(final String ticketId, final Class<? extends Ticket> clazz) {
    Assert.notNull(clazz, "clazz cannot be null");

    final Ticket ticket = this.getTicket(ticketId);

    if (ticket == null) {
        return null;
    }

    if (!clazz.isAssignableFrom(ticket.getClass())) {
        throw new ClassCastException("Ticket [" + ticket.getId() + " is of type " + ticket.getClass()
                + " when we were expecting " + clazz);
    }

    return ticket;
}

From source file:Main.java

/**
 * Same as above, but implemented via a new array list instead of a cast.
 * @param T         the target type//from   w  ww .ja v a 2 s  . co m
 * @param l         the input collection
 * @param tClass    T's class
 * @return a new array list with all items of l cast into T
 * 
 * @deprecated In the most common use case this is unnecessary. In the second
 *       common use case this is a sign that you have not understood generics.
 */
public static <T> List<T> typecheck(Collection<?> l, Class<T> tClass) {
    if (l == null)
        return null;

    ArrayList<T> res = new ArrayList<T>();
    for (Object o : l)
        if (o != null && !tClass.isInstance(o))
            throw new ClassCastException("ClassCast from " + o.getClass() + " to " + tClass);
        else
            res.add((T) o);
    return res;
}

From source file:com.akhbulatov.wordkeeper.ui.dialog.CategoryDeleteDialog.java

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {/*from w  w  w . ja v a2s. co  m*/
        mListener = (CategoryDeleteListener) getTargetFragment();
    } catch (ClassCastException e) {
        throw new ClassCastException(
                getTargetFragment().toString() + " must implement " + CategoryDeleteListener.class.getName());
    }
}

From source file:com.nextgis.maplib.datasource.GeoMultiPoint.java

@Override
public void add(GeoGeometry geometry) throws ClassCastException {
    if (!(geometry instanceof GeoPoint)) {
        throw new ClassCastException("GeoMultiPoint: geometry is not GeoPoint type.");
    }/*from w  ww .  j a v  a 2  s .co m*/

    super.add(geometry);
}

From source file:com.nextgis.maplib.datasource.GeoMultiPolygon.java

@Override
public void add(GeoGeometry geometry) throws ClassCastException {
    if (!(geometry instanceof GeoPolygon)) {
        throw new ClassCastException("GeoMultiPolygon: geometry is not GeoPolygon type.");
    }//from   w ww .j av  a2 s .  co  m

    super.add(geometry);
}

From source file:com.nextgis.maplib.datasource.GeoMultiLineString.java

@Override
public void add(GeoGeometry geometry) throws ClassCastException {
    if (!(geometry instanceof GeoLineString)) {
        throw new ClassCastException("GeoMultiLineString: geometry is not GeoLineString type.");
    }//from   w  w w.java  2  s.co m

    super.add(geometry);
}

From source file:org.apache.solr.kelvin.SimpleClassRegistry.java

public void addMappings(Map<String, String> mappings) throws ClassNotFoundException {
    for (Map.Entry<String, String> entry : mappings.entrySet()) {
        Class<?> target = Class.forName(entry.getValue());
        Class<?> required = this.getClass().getTypeParameters()[0].getClass();
        if (!required.isAssignableFrom(target)) {
            throw new ClassCastException(
                    String.format("%s does not implements %s", target.getName(), required.getName()));
        }//from w  ww  .jav a 2s  .c  o m

        map.put(entry.getKey(), target);
    }
}

From source file:edu.umich.flowfence.client.QuarentineModule.java

private static <T> Class<? extends T> checkClass(String className, Class<T> clazz, ClassLoader loader)
        throws ClassNotFoundException {
    Class<?> resultClazz;//www.java  2s  .  com
    if ("void".equals(className)) {
        if ("void".equals(clazz.getName())) {
            return clazz;
        } else if ("java.lang.Void".equals(clazz.getName())) {
            return clazz;
        } else {
            throw new ClassCastException("Void type in non-void context");
        }
    }
    resultClazz = ClassUtils.getClass(loader, className, true);

    // Special handling for primitives.
    // If we can be handled by one of the primitive conversions, allow it.
    if (resultClazz.isPrimitive()) {
        if (ClassUtils.isAssignable(resultClazz, clazz, true)) {
            return clazz;
        } else {
            throw new ClassCastException("Cannot convert " + className + " to " + clazz.getName());
        }
    }

    return resultClazz.asSubclass(clazz);
}