summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2012-06-19 16:54:19 -0700
committerElliott Hughes <enh@google.com>2012-06-19 16:54:19 -0700
commit6fcce3092040f829afd68862091ce00c7661d8ed (patch)
tree438147684436ab04308a6ff01942a1e78304e778
parent72e401c59bce47fdf0274a0d47f0340b05e4f27f (diff)
downloadart-6fcce3092040f829afd68862091ce00c7661d8ed.tar.gz
art-6fcce3092040f829afd68862091ce00c7661d8ed.tar.bz2
art-6fcce3092040f829afd68862091ce00c7661d8ed.zip
More tests for detail messages, plus a new one.
The array-length instruction is likely to encounter nulls. Change-Id: I628f5f00dfaff9414740e2f7015b9fb3d34a1bc9
-rw-r--r--src/compiler.cc4
-rw-r--r--src/runtime_support.cc12
-rw-r--r--test/201-built-in-exception-detail-messages/src/Main.java103
3 files changed, 110 insertions, 9 deletions
diff --git a/src/compiler.cc b/src/compiler.cc
index 4d98d61092..aa465c3efa 100644
--- a/src/compiler.cc
+++ b/src/compiler.cc
@@ -245,9 +245,7 @@ static std::string MakeCompilerSoName(InstructionSet instruction_set) {
}
// Capitalize the instruction set, because that's what we do in the build system.
- std::ostringstream instruction_set_name_os;
- instruction_set_name_os << instruction_set;
- std::string instruction_set_name(instruction_set_name_os.str());
+ std::string instruction_set_name(ToStr<InstructionSet>(instruction_set).str());
for (size_t i = 0; i < instruction_set_name.size(); ++i) {
instruction_set_name[i] = toupper(instruction_set_name[i]);
}
diff --git a/src/runtime_support.cc b/src/runtime_support.cc
index 903bbf91db..06908c996d 100644
--- a/src/runtime_support.cc
+++ b/src/runtime_support.cc
@@ -104,14 +104,12 @@ void ThrowNewIllegalAccessErrorClassForMethodDispatch(Thread* self,
const Method* caller,
const Method* called,
InvokeType type) {
- std::ostringstream type_stream;
- type_stream << type;
self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;",
"illegal class access ('%s' -> '%s')"
"in attempt to invoke %s method '%s' from '%s'",
PrettyDescriptor(referrer).c_str(),
PrettyDescriptor(accessed).c_str(),
- type_stream.str().c_str(),
+ ToStr<InvokeType>(type).c_str(),
PrettyMethod(called).c_str(),
PrettyMethod(caller).c_str());
}
@@ -169,11 +167,9 @@ void ThrowNullPointerExceptionForMethodAccess(Thread* self,
InvokeType type) {
const DexFile& dex_file =
Runtime::Current()->GetClassLinker()->FindDexFile(caller->GetDeclaringClass()->GetDexCache());
- std::ostringstream type_stream;
- type_stream << type;
self->ThrowNewExceptionF("Ljava/lang/NullPointerException;",
"Attempt to invoke %s method '%s' on a null object reference",
- type_stream.str().c_str(),
+ ToStr<InvokeType>(type).c_str(),
PrettyMethod(method_idx, dex_file, true).c_str());
}
@@ -235,6 +231,10 @@ void ThrowNullPointerExceptionFromDexPC(Thread* self, Method* throw_method, uint
self->ThrowNewException("Ljava/lang/NullPointerException;",
"Attempt to write to null array");
break;
+ case Instruction::ARRAY_LENGTH:
+ self->ThrowNewException("Ljava/lang/NullPointerException;",
+ "Attempt to get length of null array");
+ break;
default: {
const DexFile& dex_file = Runtime::Current()->GetClassLinker()
->FindDexFile(throw_method->GetDeclaringClass()->GetDexCache());
diff --git a/test/201-built-in-exception-detail-messages/src/Main.java b/test/201-built-in-exception-detail-messages/src/Main.java
index 638fb47079..9b67db60fb 100644
--- a/test/201-built-in-exception-detail-messages/src/Main.java
+++ b/test/201-built-in-exception-detail-messages/src/Main.java
@@ -24,6 +24,8 @@ public class Main {
arrayStore();
classCast();
classNotFound();
+ negativeArraySize();
+ nullPointers();
reflection();
stringIndex();
}
@@ -275,6 +277,71 @@ public class Main {
}
}
+ private static void negativeArraySize() throws Exception {
+ try {
+ int[] is = new int[-123];
+ fail();
+ } catch (NegativeArraySizeException ex) {
+ assertEquals("-123", ex.getMessage());
+ }
+ }
+
+ private static void nullPointers() throws Exception {
+ // Invoke method.
+ try {
+ Object o = null;
+ o.hashCode();
+ fail();
+ } catch (NullPointerException ex) {
+ assertEquals("Attempt to invoke virtual method 'int java.lang.Object.hashCode()' on a null object reference", ex.getMessage());
+ }
+
+ // Read field.
+ try {
+ A a = null;
+ int i = a.i;
+ fail();
+ } catch (NullPointerException ex) {
+ assertEquals("Attempt to read from field 'int A.i' on a null object reference", ex.getMessage());
+ }
+
+ // Write field.
+ try {
+ A a = null;
+ a.i = 1;
+ fail();
+ } catch (NullPointerException ex) {
+ assertEquals("Attempt to write to field 'int A.i' on a null object reference", ex.getMessage());
+ }
+
+ // Read array.
+ try {
+ int[] is = null;
+ int i = is[0];
+ fail();
+ } catch (NullPointerException ex) {
+ assertEquals("Attempt to read from null array", ex.getMessage());
+ }
+
+ // Write array.
+ try {
+ int[] is = null;
+ is[0] = 1;
+ fail();
+ } catch (NullPointerException ex) {
+ assertEquals("Attempt to write to null array", ex.getMessage());
+ }
+
+ // Invoke method.
+ try {
+ int[] is = null;
+ int i = is.length;
+ fail();
+ } catch (NullPointerException ex) {
+ assertEquals("Attempt to get length of null array", ex.getMessage());
+ }
+ }
+
private static void reflection() throws Exception {
// Can't assign Integer to a String field.
try {
@@ -320,6 +387,42 @@ public class Main {
} catch (IllegalArgumentException expected) {
assertEquals("method A.m argument 1 has type int, got null", expected.getMessage());
}
+
+ try {
+ Method m = String.class.getMethod("charAt", int.class);
+ m.invoke("hello"); // Wrong number of arguments.
+ fail();
+ } catch (IllegalArgumentException iae) {
+ assertEquals("wrong number of arguments; expected 1, got 0", iae.getMessage());
+ }
+ try {
+ Method m = String.class.getMethod("charAt", int.class);
+ m.invoke("hello", "world"); // Wrong type.
+ fail();
+ } catch (IllegalArgumentException iae) {
+ assertEquals("method java.lang.String.charAt argument 1 has type int, got java.lang.String", iae.getMessage());
+ }
+ try {
+ Method m = String.class.getMethod("charAt", int.class);
+ m.invoke("hello", (Object) null); // Null for a primitive argument.
+ fail();
+ } catch (IllegalArgumentException iae) {
+ assertEquals("method java.lang.String.charAt argument 1 has type int, got null", iae.getMessage());
+ }
+ try {
+ Method m = String.class.getMethod("charAt", int.class);
+ m.invoke(new Integer(5)); // Wrong type for 'this'.
+ fail();
+ } catch (IllegalArgumentException iae) {
+ assertEquals("expected receiver of type java.lang.String, but got java.lang.Integer", iae.getMessage());
+ }
+ try {
+ Method m = String.class.getMethod("charAt", int.class);
+ m.invoke(null); // Null for 'this'.
+ fail();
+ } catch (NullPointerException npe) {
+ assertEquals("expected receiver of type java.lang.String, but got null", npe.getMessage());
+ }
}
private static void stringIndex() throws Exception {