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:edu.utah.further.core.api.message.ValidationUtil.java

/**
 * A useful check for {@link Comparable#compareTo(Object)} methods.
 * //from  w  w w.j a  v  a  2 s.  c  o  m
 * @param object
 * @param type
 */
public static void validateInstanceOf(final Object object, final Class<?> type) {
    try {
        if (!ReflectionUtil.instanceOf(object, type)) {
            throw new ClassCastException(cannotCompareMessage(type, object.getClass()));
        }
    } catch (final IllegalArgumentException e) {
        throw new BusinessRuleException(e.getMessage());
    }
}

From source file:net.sf.nmedit.nomad.core.jpf.JPFServiceInstallerTool.java

@SuppressWarnings("unchecked")
private static Class<Service> lookupServiceImplementationClass(Class<? extends Service> serviceClass,
        String serviceImplementationName, ClassLoader loader) throws ClassNotFoundException {
    Class<?> _class = loader.loadClass(serviceImplementationName);

    if (!serviceClass.isAssignableFrom(_class))
        throw new ClassCastException("Service class is not subclass of " + serviceClass + ": " + _class);

    Class<Service> serviceImplementationClass = (Class<Service>) _class;

    return serviceImplementationClass;
}

From source file:CalendarUtils.java

/**
 * See the other trunc method.  Works with an Object, trying to
 * use it as either a Date or Calendar.// w w w  .java  2  s  .  c o  m
 */
public static Date trunc(Object val, int field) {
    if (val instanceof Date) {
        return trunc((Date) val, field);
    } else if (val instanceof Calendar) {
        return trunc((Calendar) val, field).getTime();
    } else {
        throw new ClassCastException("Could not trunc " + val);
    }
}

From source file:com.ichi2.anki.StudyOptionsFragment.java

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {//from   ww  w  .j a va2 s .c o m
        mListener = (StudyOptionsListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement StudyOptionsListener");
    }
}

From source file:com.microsoft.tfs.core.clients.sharepoint.WSSNode.java

@Override
public int compareTo(final Object compare) {
    if (!(compare instanceof WSSNode)) {
        throw new ClassCastException(
                MessageFormat.format("A WssNode to compare against was expected, class passed was {0}", //$NON-NLS-1$
                        compare.getClass().getName()));
    }/*from   w w w. j  av a  2s . com*/
    final WSSNode anotherNode = (WSSNode) compare;
    if (fullPath.equals(anotherNode.getFullPath())) {
        return 0;
    }
    if (WSSObjectType.FOLDER.equals(getWSSObjectType())
            && WSSObjectType.FILE.equals(anotherNode.getWSSObjectType())) {
        return -1;
    }
    if (WSSObjectType.FILE.equals(getWSSObjectType())
            && WSSObjectType.FOLDER.equals(anotherNode.getWSSObjectType())) {
        return 1;
    }
    return getFullPath().compareTo(anotherNode.getFullPath());
}

From source file:org.xbmc.kore.ui.hosts.AddHostFragmentFinish.java

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {//w  w w  .  j a  va  2s.com
        listener = (AddHostFinishListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement AddHostFinishListener interface.");
    }
}

From source file:com.helpshift.kvstore.SharedPreferencesImpl.java

public int getInt(String key, int defValue) {
    String value = getValueByKey(key);
    if (value == null) {
        return defValue;
    } else {// w  w w  .  j  ava2 s .  c  om
        try {
            return Integer.parseInt(getValueByKey(key));
        } catch (NumberFormatException e) {
            throw new ClassCastException("can not cast to integer");
        }
    }
}

From source file:br.liveo.searchliveo.SearchLiveo.java

/**
 * Start context and the listener Search Live library.
 * Use this method when you are using an Fragment
 *
 * @param getActivity - Context Fragment
 * @param context - Listener//from   w w w. ja  v  a 2  s .c o m
 */
public SearchLiveo with(Activity getActivity, OnSearchListener context) {

    if (this.mContext == null) {
        try {
            this.mContext = getActivity;
            this.mSearchListener = context;
        } catch (ClassCastException e) {
            throw new ClassCastException(mContext.getString(R.string.warning_fragment_listener));
        }
    } else {
        build();
    }

    return this;
}

From source file:br.liveo.searchliveo.SearchCardLiveo.java

/**
 * Start context and the listener Search Live library.
 * Use this method when you are using an Fragment
 *
 * @param getActivity - Context Fragment
 * @param context - Listener//from w  w w  .  j  a va  2 s  . c o m
 */
public SearchCardLiveo with(Activity getActivity, OnSearchListener context) {

    if (this.mContext == null) {
        try {
            this.mContext = getActivity;
            this.mSearchListener = context;
        } catch (ClassCastException e) {
            throw new ClassCastException(mContext.getString(R.string.warning_fragment_listener));
        }
    } else {
        build();
    }

    return this;
}

From source file:br.liveo.ndrawer.ui.fragment.MainFragment31.java

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    // This makes sure that the container activity has implemented
    // the callback interface. If not, it throws an exception
    try {/*from w ww  . j  a  va 2  s .  c om*/
        mCallback = (OnMainFragment31SelectedListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement OnMainFragment32SelectedListener");
    }
}