本文整理汇总了Java中java.lang.ClassCastException类的典型用法代码示例。如果您正苦于以下问题:Java ClassCastException类的具体用法?Java ClassCastException怎么用?Java ClassCastException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ClassCastException类属于java.lang包,在下文中一共展示了ClassCastException类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: compareTo
import java.lang.ClassCastException; //导入依赖的package包/类
public int compareTo(Object obj) {
String objString = null;
String ourString = null;
// if obj is not effectively one of us ...
if (! this.getClass().isInstance(obj)) {
throw new ClassCastException ();
}
// get the strings
objString = ((StrungDocumentInfo)obj).getString();
ourString = string;
// if we're ignoring case
if (ignoreCase) {
// we'll compare in uppercase
objString = objString.toUpperCase();
ourString = ourString.toUpperCase();
}
// compare
return ourString.compareTo(objString);
}
示例2: EdgeImp
import java.lang.ClassCastException; //导入依赖的package包/类
/**
* Creates an instance of EdgeImp from a template edge. This template edge
* must be an instance of <code>Repairable</code> so that the properties
* for this edge, such as the probability of failure and the threshold,
* and the required generations for repair, can be copied. Other
* properties such as capacity, efficiency and cost (properties of
* <code>Usable</code> object) are also copied.<p>
*
* Having all relevant properties copied, a complete edge is created by
* connecting it to the <code>from</code> node and <code>to</code> node,
* and assigning a label <code>l</code> to it.
*
* @param from FromNode of this edge.
* @param to ToNode of this edge.
* @param l The label indicating type of this edge
* @param e The repairable edge template for this edge. That is
* <code>e</code> inherits from <code>Repairable</code>.
* @throws ClassCastException If <code>e</code> is not an instance of
* <code>Repairable</code>.
* @throws PreconditionException If <code>from</code> or <code>to</code>
* is Null.
* @throws PreconditionException If <code>l</code> is Null.
* @see Repairable
*/
public EdgeImp (Node from, Node to, String l, Edge e)
throws PreconditionException, ClassCastException {
/**
* call super class constructor with appropriate parameters obtained
* from the template edge
*/
super( ((Repairable) e).getAvgRepGen(),
((Repairable) e).getFailureProb(),
((Repairable) e).getThreshold(),
((Repairable) e).activated() );
// check preconditions
Assertion.pre( from != null, "FromNode is not \"null\"",
"FromNode must not be null");
Assertion.pre( to != null, "ToNode is not \"null\"",
"ToNode must not be null");
Assertion.pre( l != null, "Label to be set is " + l,
"Label to be set must not be null");
fromNode = from; // store the origin node
toNode = to; // store the end node
label = new String(l); // store the label
capacity = e.getMaxCapacity(); // obtain and store values from "e"
efficiency = e.getEfficiency();
edgeCost = e.getCost();
}
示例3: test
import java.lang.ClassCastException; //导入依赖的package包/类
/**
* Runs the test using the specified harness.
*
* @param harness the test harness (<code>null</code> not permitted).
*/
public void test(TestHarness harness)
{
ClassCastException object1 = new ClassCastException();
harness.check(object1 != null);
harness.check(object1.toString(), "java.lang.ClassCastException");
ClassCastException object2 = new ClassCastException("nothing happens");
harness.check(object2 != null);
harness.check(object2.toString(), "java.lang.ClassCastException: nothing happens");
ClassCastException object3 = new ClassCastException(null);
harness.check(object3 != null);
harness.check(object3.toString(), "java.lang.ClassCastException");
}
示例4: getTagsMap
import java.lang.ClassCastException; //导入依赖的package包/类
/**
* Format the instance tags defined in the YAML configuration file to a `LinkedHashMap`.
* Supported inputs: `List`, `Map`.
*/
private static LinkedHashMap<String, String> getTagsMap(Object tagsMap){
try {
// Input has `Map` format
return (LinkedHashMap<String, String>) tagsMap;
}
catch (ClassCastException e){
// Input has `List` format
LinkedHashMap<String, String> tags = new LinkedHashMap<String, String>();
for (String tag: (List<String>)tagsMap) {
tags.put(tag, null);
}
return tags;
}
}
示例5: compare
import java.lang.ClassCastException; //导入依赖的package包/类
public int compare(Object obj01, Object obj02) {
String obj01String = null;
String obj02String = null;
// if obj01 or obj02 is not effectively one of us ...
if ( (! this.getClass().isInstance(obj01)) || (! this.getClass().isInstance(obj02)) ) {
throw new ClassCastException ();
}
// get the objects' strings
obj01String = ((StrungDocumentInfo)obj01).getString();
obj02String = ((StrungDocumentInfo)obj02).getString();
// if we're ignoring case
if (ignoreCase) {
// we'll compare in uppercase
obj01String = obj01String.toUpperCase();
obj02String = obj02String.toUpperCase();
}
// compare
return obj01String.compareTo(obj02String);
}
示例6: equals
import java.lang.ClassCastException; //导入依赖的package包/类
public boolean equals(Object obj) {
String objString = null;
String ourString = null;
// if obj is not effectively one of us ...
if (! this.getClass().isInstance(obj)) {
throw new ClassCastException ();
}
// get the strings
objString = ((StrungDocumentInfo)obj).getString();
ourString = string;
// if we're ignoring case
if (ignoreCase) {
// we'll compare in uppercase
objString = objString.toUpperCase();
ourString = ourString.toUpperCase();
}
// return test result
return ourString.equals(objString);
}
示例7: onReceive
import java.lang.ClassCastException; //导入依赖的package包/类
@Override
public void onReceive(Context context, Intent intent) {
if (intent == null) return;
if (isDebuggable){
Log.d(TAG, "Broadcast received.");
dumpIntent(intent);
}
String action = intent.getStringExtra("action");
action = (action == null) ? "" : action;
if (action.equals("clear")) {
notificationBar.removeAllViews();
return;
}
String packageName = intent.getStringExtra("packageName");
int iconId = intent.getIntExtra("iconId", -1);
Drawable icon = getNotificationIcon(context, packageName, iconId);
if (icon != null) {
int size = getNotificationIconSize(context);
Log.i(TAG, String.format("new size is %d", size));
try {
icon = resize(context, icon, size);
} catch (ClassCastException e) {
// AnimationDrawable cannot be cast to BitmapDrawable
icon = null;
}
}
if (action.equals("added")) {
IconView myImage = new IconView(context);
int padding = Utility.dpToPx(context, 5);
myImage.setPadding(padding, 0, 0, 0);
myImage.setImageDrawable(icon);
myImage.setColorFilter( color, PorterDuff.Mode.SRC_ATOP );
notificationBar.addView(myImage);
}
}
示例8: testGetNotSupportedYet
import java.lang.ClassCastException; //导入依赖的package包/类
@Test(expectedExceptions = ClassCastException.class)
public void testGetNotSupportedYet() throws Exception {
HttpHeaderValue value = new HttpHeaderValue("a");
Boolean b = value.get();
Integer i = value.get();
Float f = value.get();
Double d = value.get();
Date dt = value.get();
}
示例9: NodeImp
import java.lang.ClassCastException; //导入依赖的package包/类
/**
* Creates an instance of NodeImp from a template node. This template node
* must be an instance of <code>Repairable</code> so that the properties
* for this node, such as the probability of failure and the threshold,
* and the required generations for repair, can be copied. There are some
* properties such as capacity and efficiency (similar to properties of
* <code>Usable</code> object) that are exclusive to NodeImp. Thus it is
* assumed that the parameter node "n" must be NodeImp.<p>
*
* Having all relevant properties copied, a complete node is created by
* assigning a label <code>l</code> to it.<p>
*
* @param l The label indicating type of this node
* @param x The X-coordinate of this node
* @param y The Y-coordinate of this node
* @param n The repairable node template for this node. That is
* <code>n</code> inherits from <code>Repairable</code>.
* @throws ClassCastException If <code>n</code> is not an instance of
* <code>Repairable</code>.
* @throws PreconditionException If <code>l</code> is Null.
* @throws PreconditionException If <code>x</code> or <code>y</code> are
* negative.
* @see Repairable
*/
public NodeImp (String l, int x, int y, Node n)
throws PreconditionException, ClassCastException {
/**
* call super class constructor with appropriate parameters obtained
* from the template node
*/
super( ((Repairable) n).getAvgRepGen(),
((Repairable) n).getFailureProb(),
((Repairable) n).getThreshold(),
((Repairable) n).activated() );
// check precondition
Assertion.pre( l != null, "Label to be set is " + l,
"Label to be set must not be null");
Assertion.pre( (x >= 0) && (y >= 0),
"The new coordinates are ("+ x +","+ y +")",
"The new coordinates must not be negative");
label = new String(l); // store the label
/**
* # Modified by Andy 08/07/05:
* Since Node interface no longer extends the Usable interface,
* it has not inherited the methods getMaxCapacity and
* getEfficiency (please note Efficiency and Utilisation are now
* interchangeable terms). Thus "n' must be casted to NodeImp to
* use the respective methods.
*/
capacity = ((NodeImp)n).getMaxCapacity(); // obtain and store values from "n"
efficiency = ((NodeImp)n).getEfficiency();
coord[0] = x; // store the coordinates
coord[1] = y;
}
示例10: test
import java.lang.ClassCastException; //导入依赖的package包/类
/**
* Runs the test using the specified harness.
*
* @param harness the test harness (<code>null</code> not permitted).
*/
public void test(TestHarness harness)
{
// flag that is set when exception is caught
boolean caught = false;
try {
throw new ClassCastException("ClassCastException");
}
catch (ClassCastException e) {
// correct exception was caught
caught = true;
}
harness.check(caught);
}
示例11: decodeList
import java.lang.ClassCastException; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public final <T> List<T> decodeList(Class<T> elementClass) throws IOException {
final List<?> list = (List) this.decode();
for (Object obj : list) {
if (!elementClass.isAssignableFrom(obj.getClass())) {
throw new ClassCastException("unexpected type found while decoding list: " + obj.getClass().toString());
}
}
return (List<T>) list;
}
示例12: decodeMap
import java.lang.ClassCastException; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public final <K, V> Map<K, V> decodeMap(Class<K> keyClass, Class<V> valueClass) throws IOException {
final Map<?, ?> map = (Map) this.decode();
for (Map.Entry<?, ?> entry : map.entrySet()) {
final Object key = entry.getKey();
final Object value = entry.getValue();
if (!keyClass.isAssignableFrom(key.getClass())) {
throw new ClassCastException("unexpected type found while decoding map key: " + key.getClass().toString());
}
if (!valueClass.isAssignableFrom(value.getClass())) {
throw new ClassCastException("unexpected type found while decoding map value: " + value.getClass().toString());
}
}
return (Map<K, V>) map;
}
示例13: makeParser
import java.lang.ClassCastException; //导入依赖的package包/类
/**
* Create a new SAX parser using the `org.xml.sax.parser' system property.
*
* <p>The named class must exist and must implement the
* {@link org.xml.sax.Parser Parser} interface.</p>
*
* @exception java.lang.NullPointerException There is no value
* for the `org.xml.sax.parser' system property.
* @exception java.lang.ClassNotFoundException The SAX parser
* class was not found (check your CLASSPATH).
* @exception IllegalAccessException The SAX parser class was
* found, but you do not have permission to load
* it.
* @exception InstantiationException The SAX parser class was
* found but could not be instantiated.
* @exception java.lang.ClassCastException The SAX parser class
* was found and instantiated, but does not implement
* org.xml.sax.Parser.
* @see #makeParser(java.lang.String)
* @see org.xml.sax.Parser
*/
public static Parser makeParser ()
throws ClassNotFoundException,
IllegalAccessException,
InstantiationException,
NullPointerException,
ClassCastException
{
String className = System.getProperty("org.xml.sax.parser");
if (className == null) {
throw new NullPointerException("No value for sax.parser property");
} else {
return makeParser(className);
}
}