Weaken 2 DCHECK()s in reflection.
Reflection can be used on classes that are being initialized
rather than just classes that are already initialized as we
were previously asserting.
Test: Additional tests in 100-reflect2.
Change-Id: I072c28533e9248856b49fddb4bc46109448ee8a9
diff --git a/runtime/native/java_lang_reflect_Field.cc b/runtime/native/java_lang_reflect_Field.cc
index f21ded9..31734a4 100644
--- a/runtime/native/java_lang_reflect_Field.cc
+++ b/runtime/native/java_lang_reflect_Field.cc
@@ -256,7 +256,7 @@
bool allow_references,
const JValue& new_value)
REQUIRES_SHARED(Locks::mutator_lock_) {
- DCHECK(f->GetDeclaringClass()->IsInitialized());
+ DCHECK(f->GetDeclaringClass()->IsInitializing());
MemberOffset offset(f->GetOffset());
const bool is_volatile = f->IsVolatile();
switch (field_type) {
diff --git a/runtime/reflection.cc b/runtime/reflection.cc
index 3132951..4700e33 100644
--- a/runtime/reflection.cc
+++ b/runtime/reflection.cc
@@ -776,7 +776,7 @@
CHECK(constructor->IsConstructor());
ObjPtr<mirror::Class> declaring_class = constructor->GetDeclaringClass();
- CHECK(declaring_class->IsInitialized());
+ CHECK(declaring_class->IsInitializing());
// Calls to String.<init> should have been repplaced with with equivalent StringFactory calls.
CHECK(!declaring_class->IsStringClass());
diff --git a/test/100-reflect2/src/Main.java b/test/100-reflect2/src/Main.java
index 5f6ffa8..56b4a82 100644
--- a/test/100-reflect2/src/Main.java
+++ b/test/100-reflect2/src/Main.java
@@ -308,11 +308,73 @@
}
}
+ private static void testReflectFieldSetDuringClinit() {
+ try {
+ int value = ReflectFieldSetDuringClinit.intField;
+ int expected = 42;
+ if (value != expected) {
+ System.out.println("Unexpected value: " + value + ", expected: " + expected);
+ }
+ } catch (Exception e) {
+ // Error.
+ e.printStackTrace(System.out);
+ }
+ }
+
+ private static void testReflectNewInstanceDuringClinit() {
+ try {
+ int value = ReflectNewInstanceDuringClinit.instance.intField;
+ int expected = 42;
+ if (value != expected) {
+ System.out.println("Unexpected value: " + value + ", expected: " + expected);
+ }
+ } catch (Exception e) {
+ // Error.
+ e.printStackTrace(System.out);
+ }
+ }
+
public static void main(String[] args) throws Exception {
testFieldReflection();
testMethodReflection();
testConstructorReflection();
testPackagePrivateConstructor();
testPackagePrivateAccessibleConstructor();
+ testReflectFieldSetDuringClinit();
+ testReflectNewInstanceDuringClinit();
+ }
+}
+
+class ReflectFieldSetDuringClinit {
+ public static int intField;
+
+ static {
+ try {
+ Field f = ReflectFieldSetDuringClinit.class.getDeclaredField("intField");
+ f.setInt(null, 42);
+ } catch (Exception e) {
+ // Error.
+ e.printStackTrace(System.out);
+ }
+ }
+}
+
+class ReflectNewInstanceDuringClinit {
+ public int intField;
+
+ public ReflectNewInstanceDuringClinit() {
+ intField = 42;
+ }
+
+ public static ReflectNewInstanceDuringClinit instance;
+
+ static {
+ try {
+ Constructor<?> ctor = ReflectNewInstanceDuringClinit.class.getConstructor();
+ instance = (ReflectNewInstanceDuringClinit) ctor.newInstance();
+ } catch (Exception e) {
+ // Error.
+ e.printStackTrace(System.out);
+ }
}
}