Revert "Reverts to track dalvik and libcore"

This reverts commit 53d6ff445706c390c541d10ef11f1b2f19ab14e8.

Change-Id: I7cfff5b532b0dd6ffef010732cd248f58236421b
diff --git a/src/class_linker_test.cc b/src/class_linker_test.cc
index ba67402..c23087d 100644
--- a/src/class_linker_test.cc
+++ b/src/class_linker_test.cc
@@ -641,7 +641,6 @@
     class_descriptor = "Ljava/lang/reflect/Method;";
 
     // alphabetical references
-    offsets.push_back(CheckOffset(OFFSETOF_MEMBER(MethodClass, NO_ANNOTATIONS_),     "NO_ANNOTATIONS"));
     offsets.push_back(CheckOffset(OFFSETOF_MEMBER(MethodClass, ORDER_BY_SIGNATURE_), "ORDER_BY_SIGNATURE"));
   };
 };
diff --git a/src/dex_file.cc b/src/dex_file.cc
index 519cdc2..c532134 100644
--- a/src/dex_file.cc
+++ b/src/dex_file.cc
@@ -384,7 +384,44 @@
   }
 }
 
-DexFile::~DexFile() {}
+DexFile::~DexFile() {
+  if (dex_object_ != NULL) {
+    UNIMPLEMENTED(WARNING) << "leaked a global reference to an com.android.dex.Dex instance";
+  }
+}
+
+jobject DexFile::GetDexObject(JNIEnv* env) const {
+  MutexLock mu(dex_object_lock_);
+  if (dex_object_ != NULL) {
+    return dex_object_;
+  }
+
+  void* address = const_cast<void*>(reinterpret_cast<const void*>(base_));
+  jobject byte_buffer = env->NewDirectByteBuffer(address, length_);
+  if (byte_buffer == NULL) {
+    return NULL;
+  }
+
+  jclass c = env->FindClass("com/android/dex/Dex");
+  if (c == NULL) {
+    return NULL;
+  }
+
+  jmethodID mid = env->GetStaticMethodID(c, "create", "(Ljava/nio/ByteBuffer;)Lcom/android/dex/Dex;");
+  if (mid == NULL) {
+    return NULL;
+  }
+
+  jvalue args[1];
+  args[0].l = byte_buffer;
+  jobject local = env->CallStaticObjectMethodA(c, mid, args);
+  if (local == NULL) {
+    return NULL;
+  }
+
+  dex_object_ = env->NewGlobalRef(local);
+  return dex_object_;
+}
 
 bool DexFile::Init() {
   InitMembers();
diff --git a/src/dex_file.h b/src/dex_file.h
index 359825a..4ad701f 100644
--- a/src/dex_file.h
+++ b/src/dex_file.h
@@ -9,8 +9,10 @@
 
 #include "UniquePtr.h"
 #include "globals.h"
+#include "jni.h"
 #include "leb128.h"
 #include "logging.h"
+#include "mutex.h"
 #include "stringpiece.h"
 #include "strutil.h"
 #include "utils.h"
@@ -348,6 +350,10 @@
     return location_;
   }
 
+  // Returns a com.android.dex.Dex object corresponding to the mapped-in dex file.
+  // Used by managed code to implement annotations.
+  jobject GetDexObject(JNIEnv* env) const;
+
   const Header& GetHeader() const {
     CHECK(header_ != NULL);
     return *header_;
@@ -874,6 +880,8 @@
         length_(length),
         location_(location),
         closer_(closer),
+        dex_object_lock_("a dex_object_lock_"),
+        dex_object_(NULL),
         header_(0),
         string_ids_(0),
         type_ids_(0),
@@ -920,6 +928,10 @@
   // Helper object to free the underlying allocation.
   UniquePtr<Closer> closer_;
 
+  // A cached com.android.dex.Dex instance, possibly NULL. Use GetDexObject.
+  mutable Mutex dex_object_lock_;
+  mutable jobject dex_object_;
+
   // Points to the header section.
   const Header* header_;
 
diff --git a/src/java_lang_Class.cc b/src/java_lang_Class.cc
index 68b83f2..de5315a 100644
--- a/src/java_lang_Class.cc
+++ b/src/java_lang_Class.cc
@@ -168,6 +168,17 @@
     return JNI_FALSE;
 }
 
+jobject Class_getDex(JNIEnv* env, jobject javaClass) {
+  Class* c = Decode<Class*>(env, javaClass);
+
+  DexCache* dex_cache = c->GetDexCache();
+  if (dex_cache == NULL) {
+    return NULL;
+  }
+
+  return Runtime::Current()->GetClassLinker()->FindDexFile(dex_cache).GetDexObject(env);
+}
+
 jobject Class_getClassLoader(JNIEnv* env, jclass, jobject javaClass) {
   Class* c = Decode<Class*>(env, javaClass);
   Object* result = reinterpret_cast<Object*>(const_cast<ClassLoader*>(c->GetClassLoader()));
@@ -248,16 +259,6 @@
   return NULL;
 }
 
-jobject Class_getEnclosingConstructor(JNIEnv* env, jobject javaThis) {
-  UNIMPLEMENTED(WARNING) << "needs annotations";
-  return NULL;
-}
-
-jobject Class_getEnclosingMethod(JNIEnv* env, jobject javaThis) {
-  UNIMPLEMENTED(WARNING) << "needs annotations";
-  return NULL;
-}
-
 /*
  * private native String getNameNative()
  *
@@ -441,9 +442,7 @@
   NATIVE_METHOD(Class, getDeclaredFields, "(Ljava/lang/Class;Z)[Ljava/lang/reflect/Field;"),
   NATIVE_METHOD(Class, getDeclaredMethods, "(Ljava/lang/Class;Z)[Ljava/lang/reflect/Method;"),
   NATIVE_METHOD(Class, getDeclaringClass, "()Ljava/lang/Class;"),
-  //NATIVE_METHOD(Class, getEnclosingClass, "()Ljava/lang/Class;"),
-  NATIVE_METHOD(Class, getEnclosingConstructor, "()Ljava/lang/reflect/Constructor;"),
-  NATIVE_METHOD(Class, getEnclosingMethod, "()Ljava/lang/reflect/Method;"),
+  NATIVE_METHOD(Class, getDex, "()Lcom/android/dex/Dex;"),
   //NATIVE_METHOD(Class, getInnerClassName, "()Ljava/lang/String;"),
   //NATIVE_METHOD(Class, getModifiers, "(Ljava/lang/Class;Z)I"),
   NATIVE_METHOD(Class, getNameNative, "()Ljava/lang/String;"),
diff --git a/src/object.h b/src/object.h
index 9b1d3c7..1eee2b8 100644
--- a/src/object.h
+++ b/src/object.h
@@ -569,7 +569,7 @@
   DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
 };
 
-// C++ mirror of java.lang.reflect.Method
+// C++ mirror of java.lang.reflect.Method and java.lang.reflect.Constructor
 class MANAGED Method : public AccessibleObject {
  public:
   // An function that invokes a method with an array of its arguments.
@@ -2032,7 +2032,8 @@
   // access flags; low 16 bits are defined by VM spec
   uint32_t access_flags_;
 
-  // Total class size; used when allocating storage on gc heap.
+  // Total size of the Class instance; used when allocating storage on gc heap.
+  // See also object_size_.
   size_t class_size_;
 
   // tid used to check for recursive <clinit> invocation
@@ -2046,6 +2047,7 @@
 
   // Total object size; used when allocating storage on gc heap.
   // (For interfaces and abstract classes this will be zero.)
+  // See also class_size_.
   size_t object_size_;
 
   // primitive type index, or kPrimNot (0); set for generated prim classes
@@ -2317,7 +2319,6 @@
 
 class MANAGED MethodClass : public Class {
  private:
-  ObjectArray<Object>* NO_ANNOTATIONS_;
   Object* ORDER_BY_SIGNATURE_;
   friend struct MethodClassOffsets;  // for verifying offset information
   DISALLOW_IMPLICIT_CONSTRUCTORS(MethodClass);