You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.5 KiB

/*
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package codetoanalyze.java.infer;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
class SuperClass {}
class SubClassA extends SuperClass {}
class SubClassB extends SuperClass {}
interface MyInterface {
public int getInt();
}
class ImplementationOfInterface implements MyInterface {
public int getInt() {
return 0;
}
}
class AnotherImplementationOfInterface implements MyInterface {
public int getInt() {
return 1;
}
}
public class ClassCastExceptions {
public void classCastException() {
SuperClass a = new SubClassA();
SubClassB b = (SubClassB) a;
}
public int classCastExceptionImplementsInterfaceCallee(MyInterface i) {
ImplementationOfInterface impl = (ImplementationOfInterface) i;
return impl.getInt();
}
public int classCastExceptionImplementsInterface() {
return classCastExceptionImplementsInterfaceCallee(new AnotherImplementationOfInterface());
}
public String getURL() {
return "http://bla.com";
}
public void openHttpURLConnection() throws IOException {
URL url = new URL(getURL());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.disconnect();
}
public void castingArrayOfPrimitiveTypeOK(int[] a) {
int[] b = (int[]) a;
}
}