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"));
};
};
}
}
-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();
#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"
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_;
length_(length),
location_(location),
closer_(closer),
+ dex_object_lock_("a dex_object_lock_"),
+ dex_object_(NULL),
header_(0),
string_ids_(0),
type_ids_(0),
// 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_;
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()));
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()
*
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;"),
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.
// 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
// 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
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);