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

/**
 * Typecheck as above, performing on map values
 * @param <K>    Key type//w  ww.  ja  v a2  s . c  o m
 * @param <V>    Designated value type
 * @param m      the map to check
 * @param vClass the value type's class
 * @return m, accordingly cast, if no classCastException is thrown
 * 
 * @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 <K, V> Map<K, V> typecheck(Map<K, ?> m, Class<V> vClass) {
    if (m == null)
        return null;
    for (Map.Entry<K, ?> e : m.entrySet()) {
        Object o = e.getValue();
        if (o != null && !vClass.isInstance(o))
            throw new ClassCastException("ClassCast from " + o.getClass() + " to " + vClass);
    }
    return (Map<K, V>) m;
}

From source file:com.sawyer.advadapters.app.adapters.nfjsonadapter.NFJSONAdapterFragment.java

@Override
public void setListAdapter(ListAdapter adapter) {
    if (adapter instanceof MovieNFJSONArrayAdapter) {
        super.setListAdapter(adapter);
    } else {/*ww w .ja va  2s.  c  om*/
        throw new ClassCastException(
                "Adapter must be of type " + MovieNFJSONArrayAdapter.class.getSimpleName());
    }
}

From source file:dtu.ds.warnme.app.dialog.RegisterDialog.java

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {/*from   w ww  .  ja  va2s.  c o m*/
        listener = (RegisterDialogListener) activity;
    } catch (ClassCastException ex) {
        throw new ClassCastException(activity.toString() + " must implement RegisterDialogListener");
    }
}

From source file:com.wormsim.animals.AnimalGroup.java

/**
 * Note: The natural ordering of this class foregoes the natural declaration
 * of equals(), or something like that.//from   ww w  .j  a  va  2  s  . com
 *
 * @param p_o
 *
 * @return
 */
@Override
public int compareTo(Object p_o) {
    if (p_o instanceof AnimalGroup) {
        AnimalGroup that = (AnimalGroup) p_o;
        return that.dev_time_rem > this.dev_time_rem ? -1
                : (that.dev_time_rem < this.dev_time_rem ? 1
                        : (that.animal.hashCode() - this.animal.hashCode()));
    } else {
        throw new ClassCastException("Invalid Class: " + p_o.getClass());
    }
}

From source file:ca.etsmtl.applets.etsmobile.fragments.NewsListFragment.java

@Override
public void onAttach(final Activity activity) {
    super.onAttach(activity);
    try {/*from w ww.  j  ava  2  s  . co m*/
        selectedItemListener = (NewsListSelectedItemListener) activity;
    } catch (final ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + "l'activit doit implmenter l'interface NewsListSelectedItemListener");
    }
}

From source file:com.bashlord.loginregister.nearbyR.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.nearby_list);

    FragmentManager fm = getSupportFragmentManager();
    PlacePickerFragment fragment = (PlacePickerFragment) fm.findFragmentByTag(FRAGTAG);

    if (fragment == null) {
        FragmentTransaction transaction = fm.beginTransaction();
        fragment = new PlacePickerFragment();
        transaction.add(fragment, FRAGTAG);
        transaction.commit();/*from  w  w w .  j  a  v a2s . co m*/
    }

    // Use fragment as click listener for cards, but must implement correct interface
    if (!(fragment instanceof OnCardClickListener)) {
        throw new ClassCastException("PlacePickerFragment must " + "implement OnCardClickListener interface.");
    }
    OnCardClickListener clickListener = (OnCardClickListener) fm.findFragmentByTag(FRAGTAG);

    mRetentionFragment = (StreamRetentionFragment) fm.findFragmentByTag(RETENTION_TAG);
    if (mRetentionFragment == null) {
        mRetentionFragment = new StreamRetentionFragment();
        fm.beginTransaction().add(mRetentionFragment, RETENTION_TAG).commit();
    } else {
        // If the retention fragment already existed, we need to pull some state.
        // pull state out
        CardStreamState state = mRetentionFragment.getCardStream();

        // dump it in CardStreamFragment.
        mCardStreamFragment = (CardStreamFragment) fm.findFragmentById(R.id.fragment_cardstream);
        mCardStreamFragment.restoreState(state, clickListener);
    }
}

From source file:br.com.bioscada.apps.biotracks.fragments.DeleteMarkerDialogFragment.java

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {//from  www  .ja va 2s  .c om
        caller = (DeleteMarkerCaller) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(
                activity.toString() + " must implement " + DeleteMarkerCaller.class.getSimpleName());
    }
}

From source file:com.appdynamics.demo.gasp.fragment.PlayServicesDialogFragment.java

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {/*  ww  w .  j av  a2s.  c o m*/
        // Instantiate the NoticeDialogListener so we can send events to the host
        PlayServicesDialogListener mListener = (PlayServicesDialogListener) activity;
    } catch (ClassCastException e) {
        // The activity doesn't implement the interface, throw exception
        throw new ClassCastException(activity.toString() + " must implement PlayServicesDialogListener");
    }
}

From source file:br.com.d4n.ui4entity.mvc.ValueOptionReturnValueHandler.java

@SuppressWarnings("rawtypes")
@Override//  w  w w .jav a  2 s  .c o m
public void handleReturnValue(Object returnValue, MethodParameter returnType,
        ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {

    if (returnType.getMethodAnnotation(ValueOptions.class) != null) {

        Map<String, Object> map = new HashMap<String, Object>();
        if (returnValue instanceof List) {

            map.put("type", "list");
        } else if (returnValue instanceof Map) {

            returnValue = ((Map) returnValue).entrySet();
            map.put("type", "map");
        } else
            throw new ClassCastException("@ValueOptions method must be return Map or List");

        map.put(ValueOptionsProviderResolver.OPTIONS_ATTR_NAME, returnValue);
        returnValue = map;

        returnType = new UI4EntityMethodParameter(returnType, returnValue.getClass());
    }

    delegate.handleReturnValue(returnValue, returnType, mavContainer, webRequest);
}

From source file:com.digi.android.wva.fragments.PreConnectionDialog.java

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    if (!(activity instanceof PreConnectionDialogListener)) {
        throw new ClassCastException(activity.toString() + " must implement AuthenticationDialogListener!");
    }/* ww w.  java  2s  .  co m*/
}