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:com.opensymphony.xwork2.util.finder.ResourceFinder.java

/**
 * Assumes the class specified points to a file in the classpath that contains
 * the name of a class that implements or is a subclass of the specfied class.
 * <p/>/*from  w  w w.  ja  va  2  s  . c o  m*/
 * Any class that cannot be loaded will be cause an exception to be thrown.
 * <p/>
 * Example classpath:
 * <p/>
 * META-INF/java.io.InputStream    # contains the classname org.acme.AcmeInputStream
 * META-INF/java.io.OutputStream
 * <p/>
 * ResourceFinder finder = new ResourceFinder("META-INF/");
 * Class clazz = finder.findImplementation(java.io.InputStream.class);
 * clazz.getName();  // returns "org.acme.AcmeInputStream"
 *
 * @param interfase a superclass or interface
 * @return
 * @throws IOException            if the URL cannot be read
 * @throws ClassNotFoundException if the class found is not loadable
 * @throws ClassCastException     if the class found is not assignable to the specified superclass or interface
 */
public Class findImplementation(Class interfase) throws IOException, ClassNotFoundException {
    String className = findString(interfase.getName());
    Class impl = classLoaderInterface.loadClass(className);
    if (!interfase.isAssignableFrom(impl)) {
        throw new ClassCastException("Class not of type: " + interfase.getName());
    }
    return impl;
}

From source file:com.RSMSA.policeApp.Dialogues.PaymentConfirmationDialogue.java

public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {//  w  w  w. j  a  va2 s .c  om
        this.mListener = (OnCompleteListener) activity;
    } catch (final ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement OnCompleteListener");
    }
}

From source file:edu.berkeley.path.bots.core.Coordinate.java

public double distanceHaversineInMeters(Coordinate otherCoord) {
    // SRID's must match and not be null.
    // This can't be null, duh, and we don't check if otherCoord is, which
    // means the NullPointerException will be thrown.
    if (null == this.srid() || null == otherCoord.srid()) {
        throw new ClassCastException("This distance function uses the spheroid distance, but you "
                + "are using Coordinates with null SRID~s (from a PostgreSQL "
                + "point?).  This doesn't really make any sense and you "
                + "probably want to use .distanceCarteasianInSRUnits( other )" + "instead.");
    } else if (!this.srid().equals(otherCoord.srid())) {
        throw new ClassCastException("The SRID of otherCoord does't match this one.");
    }/*from w ww.ja  v a2 s.com*/
    final double piOver180 = Math.PI / 180.0;
    final double earthRadiusInMeters = 6367000;
    final double lon1 = piOver180 * this.lon();
    final double lat1 = piOver180 * this.lat();
    final double lon2 = piOver180 * otherCoord.lon();
    final double lat2 = piOver180 * otherCoord.lat();
    // Haversine formula:
    final double dlon = lon2 - lon1;
    final double dlat = lat2 - lat1;
    final double a = FastMath.sin(dlat / 2) * FastMath.sin(dlat / 2)
            + FastMath.cos(lat1) * FastMath.cos(lat2) * FastMath.sin(dlon / 2) * FastMath.sin(dlon / 2);
    final double c = 2 * FastMath.atan2(FastMath.sqrt(a), FastMath.sqrt(1 - a));
    return earthRadiusInMeters * c;
}

From source file:com.irccloud.android.fragment.UsersListFragment.java

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {/*from  w w w. ja  v  a  2s  .c  o m*/
        mListener = (OnUserSelectedListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement OnUserSelectedListener");
    }
    if (cid == -1) {
        cid = activity.getIntent().getIntExtra("cid", 0);
        channel = activity.getIntent().getStringExtra("name");
    }
}

From source file:com.RSMSA.policeApp.Dialogues.PaymentConfirmationDialogue.java

public void onAttach(Fragment activity) {
    super.onAttach(activity.getActivity());
    try {/*from  w w  w .  j a v a  2s. c  o  m*/
        this.mListener = (OnCompleteListener) activity;
    } catch (final ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement OnCompleteListener");
    }
}

From source file:ch.ralscha.extdirectspring.util.ParametersResolver.java

private Object resolveAuthenticationPrincipal(ParameterInfo parameterInfo) {
    Object principal = this.getPrincipalExpression.getValue();

    if (principal != null && !parameterInfo.getType().isAssignableFrom(principal.getClass())) {
        if (parameterInfo.authenticationPrincipalAnnotationErrorOnInvalidType()) {
            throw new ClassCastException(principal + " is not assignable to " + parameterInfo.getType());
        }/*www. j  a  v a  2s  .  co m*/
        return null;
    }
    return principal;
}

From source file:gov.nih.nci.ncicb.cadsr.bulkloader.util.excel.DBExcelUtility.java

private String getRowXMLData(HSSFSheet hssfsheet, String sheetName, int row, int colcount, String prefix,
        String coltype[]) {//from   w  ww  . j a  v a 2s . com
    StringBuffer sb = new StringBuffer();
    Object cdata = null;
    int column = 0;

    try {

        for (int i = 0; i < colcount; i++) {
            column = (short) (dataStart_column + i);
            cdata = ExcelUtility.getObject(hssfsheet, row, (short) column);

            //sb.append("(AV=" + cdata + "," + cdata.toString().length() + ")");
            if (cdata != null && cdata.toString().length() != 0) {
                if (coltype[i].equalsIgnoreCase(DataType.TYPE_STRING)) {
                    sb.append("<" + prefix + ">" + (String) cdata + "</" + prefix + ">");
                } else if (coltype[i].equalsIgnoreCase(DataType.TYPE_INTEGER)) {
                    Integer idata = ExcelUtility.getInteger(hssfsheet, row, (short) (dataStart_column + i));

                    sb.append("<" + prefix + ">" + idata + "</" + prefix + ">");
                } else if (coltype[i].equalsIgnoreCase(DataType.TYPE_DOUBLE)) {
                    Double ddata = ExcelUtility.getDouble(hssfsheet, row, (short) (dataStart_column + i));

                    sb.append("<" + prefix + ">" + ddata + "</" + prefix + ">");
                } else if (coltype[i].equalsIgnoreCase(DataType.TYPE_BOOLEAN)) {
                    Boolean bdata = ExcelUtility.getBoolean(hssfsheet, row, (short) (dataStart_column + i));

                    sb.append("<" + prefix + ">" + bdata + "</" + prefix + ">");
                } else if (coltype[i].equalsIgnoreCase(DataType.TYPE_DATE)) {
                    Date dtval = ExcelUtility.getDate(hssfsheet, row, (short) (dataStart_column + i));
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    StringBuffer dtString = new StringBuffer();
                    String dt = sdf.format(dtval);

                    //System.out.println("dtval = " + dtval);
                    //System.out.println("dt = " + dtString.toString());
                    sb.append("<" + prefix + ">" + dt + "</" + prefix + ">");
                }
            } else {
                sb.append("<none/>");
            }
        }
        return sb.toString();
    } catch (ClassCastException ce) {
        ce.printStackTrace();
        throw new ClassCastException("There is a type problem in the excel worksheet on Sheet='" + sheetName
                + "', and Cell(" + (row + 1) + "," + (column + 1) + ")" + "\n" + ce.getMessage());
    }
}

From source file:com.opensymphony.xwork2.util.finder.ResourceFinder.java

/**
 * Assumes the class specified points to a file in the classpath that contains
 * the name of a class that implements or is a subclass of the specfied class.
 * <p/>/*from w w w  .  j  ava2  s  .com*/
 * Any class that cannot be loaded or assigned to the specified interface will be cause
 * an exception to be thrown.
 * <p/>
 * Example classpath:
 * <p/>
 * META-INF/java.io.InputStream    # contains the classname org.acme.AcmeInputStream
 * META-INF/java.io.InputStream    # contains the classname org.widget.NeatoInputStream
 * META-INF/java.io.InputStream    # contains the classname com.foo.BarInputStream
 * <p/>
 * ResourceFinder finder = new ResourceFinder("META-INF/");
 * List classes = finder.findAllImplementations(java.io.InputStream.class);
 * classes.contains("org.acme.AcmeInputStream");  // true
 * classes.contains("org.widget.NeatoInputStream");  // true
 * classes.contains("com.foo.BarInputStream");  // true
 *
 * @param interfase a superclass or interface
 * @return
 * @throws IOException            if the URL cannot be read
 * @throws ClassNotFoundException if the class found is not loadable
 * @throws ClassCastException     if the class found is not assignable to the specified superclass or interface
 */
public List<Class> findAllImplementations(Class interfase) throws IOException, ClassNotFoundException {
    List<Class> implementations = new ArrayList<Class>();
    List<String> strings = findAllStrings(interfase.getName());
    for (String className : strings) {
        Class impl = classLoaderInterface.loadClass(className);
        if (!interfase.isAssignableFrom(impl)) {
            throw new ClassCastException("Class not of type: " + interfase.getName());
        }
        implementations.add(impl);
    }
    return implementations;
}

From source file:com.cerema.cloud2.ui.fragment.ShareFileFragment.java

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {/*from w  w  w .java  2 s .c  om*/
        mListener = (ShareFragmentListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(
                activity.toString() + " must implement OnShareFragmentInteractionListener");
    }
}