Example usage for java.lang ClassCastException printStackTrace

List of usage examples for java.lang ClassCastException printStackTrace

Introduction

In this page you can find the example usage for java.lang ClassCastException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:org.opendatakit.common.android.utilities.ODKCursorUtils.java

/**
 * Return the data stored in the cursor at the given index and given position
 * (ie the given row which the cursor is currently on) as null OR whatever
 * data type it is.//from  www  .java 2s.c  o m
 * <p>
 * This does not actually convert data types from one type to the other.
 * Instead, it safely preserves null values and returns boxed data values. If
 * you specify ArrayList or HashMap, it JSON deserializes the value into one
 * of those.
 *
 * @param c
 * @param clazz
 * @param i
 * @return
 */
@SuppressWarnings("unchecked")
public static final <T> T getIndexAsType(Cursor c, Class<T> clazz, int i) {
    // If you add additional return types here be sure to modify the javadoc.
    try {
        if (i == -1)
            return null;
        if (c.isNull(i)) {
            return null;
        }
        if (clazz == Long.class) {
            Long l = c.getLong(i);
            return (T) l;
        } else if (clazz == Integer.class) {
            Integer l = c.getInt(i);
            return (T) l;
        } else if (clazz == Double.class) {
            Double d = c.getDouble(i);
            return (T) d;
        } else if (clazz == String.class) {
            String str = c.getString(i);
            return (T) str;
        } else if (clazz == Boolean.class) {
            // stored as integers
            Integer l = c.getInt(i);
            return (T) Boolean.valueOf(l != 0);
        } else if (clazz == ArrayList.class) {
            // json deserialization of an array
            String str = c.getString(i);
            return (T) ODKFileUtils.mapper.readValue(str, ArrayList.class);
        } else if (clazz == HashMap.class) {
            // json deserialization of an object
            String str = c.getString(i);
            return (T) ODKFileUtils.mapper.readValue(str, HashMap.class);
        } else {
            throw new IllegalStateException("Unexpected data type in SQLite table");
        }
    } catch (ClassCastException e) {
        e.printStackTrace();
        throw new IllegalStateException(
                "Unexpected data type conversion failure " + e.toString() + " in SQLite table ");
    } catch (JsonParseException e) {
        e.printStackTrace();
        throw new IllegalStateException(
                "Unexpected data type conversion failure " + e.toString() + " on SQLite table");
    } catch (JsonMappingException e) {
        e.printStackTrace();
        throw new IllegalStateException(
                "Unexpected data type conversion failure " + e.toString() + " on SQLite table");
    } catch (IOException e) {
        e.printStackTrace();
        throw new IllegalStateException(
                "Unexpected data type conversion failure " + e.toString() + " on SQLite table");
    }
}

From source file:de.openali.odysseus.chart.framework.model.data.DataRange.java

@SafeVarargs
@Deprecated/*w  ww.  j a v a2  s.c  om*/
public static <T> DataRange<T> createFromComparable(final T... items) {
    final ComparableComparator comp = new ComparableComparator();

    T min = null;
    T max = null;

    boolean hasMinAndMax = false;
    for (final T item : items) {
        if (item != null) {
            // erster von Null verschiedener Wert wird als max und min genutzt
            if (hasMinAndMax) {
                try {
                    if (comp.compare(item, min) < 0) {
                        min = item;
                    }
                    if (comp.compare(item, max) > 0) {
                        max = item;
                    }
                } catch (final ClassCastException e) {
                    e.printStackTrace();
                }
            } else {
                min = item;
                max = item;
                hasMinAndMax = true;
            }
        }
    }

    return new DataRange<>(min, max);
}

From source file:com.chiorichan.util.WebFunc.java

@SuppressWarnings("unchecked")
public static String createTable(Map<Object, Object> tableData, Collection<String> headerArray, String tableId,
        String altTableClass) {//from w  w  w .ja va 2 s  .c o m
    if (tableId == null)
        tableId = "";

    if (tableData == null)
        return "";

    if (altTableClass == null || altTableClass.isEmpty())
        altTableClass = "altrowstable";

    StringBuilder sb = new StringBuilder();
    int x = 0;
    sb.append("<table id=\"" + tableId + "\" class=\"" + altTableClass + "\">\n");

    if (headerArray != null) {
        sb.append("<tr>\n");
        for (String col : headerArray)
            sb.append("<th>" + col + "</th>\n");
        sb.append("</tr>\n");
    }

    int colLength = headerArray != null ? headerArray.size() : tableData.size();
    for (Object row : tableData.values())
        if (row instanceof Map)
            colLength = Math.max(((Map<String, Object>) row).size(), colLength);

    for (Object row : tableData.values()) {
        String clss = x % 2 == 0 ? "evenrowcolor" : "oddrowcolor";
        x++;

        if (row instanceof Map || row instanceof Collection) {
            Map<Object, Object> map = Maps.newLinkedHashMap();

            if (row instanceof Map)
                map = (Map<Object, Object>) row;
            else {
                int y = 0;
                for (Object o : (Collection<Object>) row) {
                    map.put(Integer.toString(y), o);
                    y++;
                }
            }

            sb.append("<tr");

            for (Entry<Object, Object> e : map.entrySet())
                try {
                    if (ObjectFunc.castToStringWithException(e.getKey()).startsWith(":")) {
                        map.remove(e.getKey());
                        sb.append(" " + ObjectFunc.castToStringWithException(e.getKey()).substring(1) + "=\""
                                + ObjectFunc.castToStringWithException(e.getValue()) + "\"");
                    }
                } catch (ClassCastException ex) {
                    ex.printStackTrace();
                }

            sb.append(" class=\"" + clss + "\">\n");

            if (map.size() == 1)
                sb.append("<td style=\"text-align: center; font-weight: bold;\" class=\"\" colspan=\""
                        + colLength + "\">" + map.get(0) + "</td>\n");
            else {
                int cc = 0;
                for (Object col : map.values())
                    if (col != null) {
                        String subclass = col instanceof String && ((String) col).isEmpty() ? " emptyCol" : "";
                        sb.append("<td id=\"col_" + cc + "\" class=\"" + subclass + "\">" + col + "</td>\n");
                        cc++;
                    }
            }
            sb.append("</tr>\n");
        } else if (row instanceof String)
            sb.append("<tr><td class=\"" + clss + "\" colspan=\"" + colLength + "\"><b><center>" + (String) row
                    + "</b></center></td></tr>\n");
        else
            sb.append("<tr><td class=\"" + clss + "\" colspan=\"" + colLength + "\"><b><center>"
                    + row.toString() + "</b></center></td></tr>\n");
    }
    sb.append("</table>\n");

    return sb.toString();
}

From source file:com.github.braully.graph.DatabaseFacade.java

public synchronized static List<RecordResultGraph> getAllResults(String database) {
    List<RecordResultGraph> results = new ArrayList();
    ObjectMapper mapper = new ObjectMapper();
    try {/*from w w  w  . ja va  2 s . c  o  m*/
        List<Map> tmp = mapper.readValue(new File(database), List.class);
        if (tmp != null) {
            try {
                Iterator<Map> iterator = tmp.iterator();
                while (iterator.hasNext()) {
                    RecordResultGraph t = new RecordResultGraph(iterator.next());
                    results.add(t);
                }
            } catch (ClassCastException e) {
                e.printStackTrace();
            }
        }
        Collections.reverse(results);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return results;
}

From source file:com.chiorichan.util.WebUtils.java

@SuppressWarnings("unchecked")
public static String createTable(Map<Object, Object> tableData, List<String> headerArray, String tableId,
        String altTableClass) {/*from  w  ww .j  a  v  a 2 s .c om*/
    if (tableId == null)
        tableId = "";

    if (tableData == null)
        return "";

    if (altTableClass == null || altTableClass.isEmpty())
        altTableClass = "altrowstable";

    StringBuilder sb = new StringBuilder();
    int x = 0;
    sb.append("<table id=\"" + tableId + "\" class=\"" + altTableClass + "\">\n");

    if (headerArray != null) {
        sb.append("<tr>\n");
        for (String col : headerArray) {
            sb.append("<th>" + col + "</th>\n");
        }
        sb.append("</tr>\n");
    }

    int colLength = (headerArray != null) ? headerArray.size() : tableData.size();
    for (Object row : tableData.values()) {
        if (row instanceof Map) {
            colLength = Math.max(((Map<String, Object>) row).size(), colLength);
        }
    }

    for (Object row : tableData.values()) {
        String clss = (x % 2 == 0) ? "evenrowcolor" : "oddrowcolor";
        x++;

        if (row instanceof Map || row instanceof List) {
            Map<Object, Object> map = Maps.newLinkedHashMap();

            if (row instanceof Map)
                map = (Map<Object, Object>) row;
            else {
                int y = 0;
                for (Object o : (List<Object>) row) {
                    map.put(Integer.toString(y), o);
                    y++;
                }
            }

            sb.append("<tr");

            for (Entry<Object, Object> e : map.entrySet())
                try {
                    if (ObjectUtil.castToStringWithException(e.getKey()).startsWith(":")) {
                        map.remove(e.getKey());
                        sb.append(" " + ObjectUtil.castToStringWithException(e.getKey()).substring(1) + "=\""
                                + ObjectUtil.castToStringWithException(e.getValue()) + "\"");
                    }
                } catch (ClassCastException ex) {
                    ex.printStackTrace();
                }

            sb.append(" class=\"" + clss + "\">\n");

            if (map.size() == 1) {
                sb.append("<td style=\"text-align: center; font-weight: bold;\" class=\"\" colspan=\""
                        + colLength + "\">" + map.get(0) + "</td>\n");
            } else {
                int cc = 0;
                for (Object col : map.values()) {
                    if (col != null) {
                        String subclass = (col instanceof String && ((String) col).isEmpty()) ? " emptyCol"
                                : "";
                        sb.append("<td id=\"col_" + cc + "\" class=\"" + subclass + "\">" + col + "</td>\n");
                        cc++;
                    }
                }
            }
            sb.append("</tr>\n");
        } else if (row instanceof String) {
            sb.append("<tr><td class=\"" + clss + "\" colspan=\"" + colLength + "\"><b><center>"
                    + ((String) row) + "</b></center></td></tr>\n");
        } else {
            sb.append("<tr><td class=\"" + clss + "\" colspan=\"" + colLength + "\"><b><center>"
                    + row.toString() + "</b></center></td></tr>\n");
        }
    }
    sb.append("</table>\n");

    return sb.toString();
}

From source file:org.ymkm.lib.controller.support.ControlledFragment.java

/**
 * Instantiates a new {@linkplain ControllableFragment} of specified
 * {@code Class}, whose handler will run in the specified Looper
 * /*ww  w.j  a  v a2 s .co m*/
 * @param f
 *            the subclass of {@linkplain ControlledFragment} to instantiate
 * @param runsInOwnThread
 *            {@code true} if a new thread should be created for this
 *            fragment, {@code false} to let it run in the current thread
 * @return A new instance of {@linkplain ControllableFragment}
 * @throws ControllableFragmentException
 *             if instantiation failed
 */
public static ControllableFragment createFragment(Class<? extends ControllableFragment> f,
        boolean runsInOwnThread, Bundle args) throws ControllableFragmentException {
    assert (null != args);
    try {
        ControlledFragment fragment = (ControlledFragment) f.newInstance();
        args.putBoolean("__new_thread__", runsInOwnThread);
        fragment.setArguments(args);
        return fragment;
    } catch (ClassCastException e) {
        e.printStackTrace();
        throw new ControllableFragmentException(e.getMessage());
    } catch (java.lang.InstantiationException e) {
        e.printStackTrace();
        throw new ControllableFragmentException(e.getMessage());
    } catch (IllegalAccessException e) {
        e.printStackTrace();
        throw new ControllableFragmentException(e.getMessage());
    }
}

From source file:net.gaast.giggity.ScheduleViewActivity.java

public static void onScroll(Context ctx) {
    ScheduleViewActivity me;/* w w  w  .  j av  a2 s .com*/
    try {
        me = (ScheduleViewActivity) ctx;
    } catch (ClassCastException e) {
        e.printStackTrace();
        return;
    }
    me.onScroll();
}

From source file:com.licenta.android.licenseapp.adapter.SectionsPagerAdapter.java

@Override
public CharSequence getPageTitle(int position) {
    try {/* w w  w.j ava  2s .  c  om*/
        return ((TabFragment) pageFragments.get(position)).getTitle();

    } catch (ClassCastException e) {
        e.printStackTrace();
    }
    return "";
}

From source file:com.dimasdanz.kendalipintu.util.UniversalDialogManager.java

public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {//  w  w w  .j a v  a 2 s .  c o  m
        mListener = (DialogManagerListener) activity;
    } catch (ClassCastException e) {
        e.printStackTrace();
    }
}

From source file:com.github.fountaingeyser.typefacecompat.FactoryTypefaceCompat.java

private FactoryTypefaceCompat(Context context) {
    try {// www. j  av a  2 s  .  c  om
        this.mBaseFactory = (LayoutInflaterFactory) ((AppCompatActivity) context).getDelegate();
    } catch (ClassCastException e) {
        e.printStackTrace();
    }
}