Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "logging.h" |
| 18 | #include "ScopedUtfChars.h" |
| 19 | |
| 20 | #include "JniConstants.h" // Last to avoid problems with LOG redefinition. |
| 21 | |
| 22 | namespace art { |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | // A smart pointer that provides read-only access to a Java string's UTF chars. |
| 27 | // Unlike libcore's NullableScopedUtfChars, this will *not* throw NullPointerException if |
| 28 | // passed a null jstring. The correct idiom is: |
| 29 | // |
| 30 | // NullableScopedUtfChars name(env, javaName); |
| 31 | // if (env->ExceptionOccurred()) { |
| 32 | // return NULL; |
| 33 | // } |
| 34 | // // ... use name.c_str() |
| 35 | // |
| 36 | // TODO: rewrite to get rid of this, or change ScopedUtfChars to offer this option. |
| 37 | class NullableScopedUtfChars { |
| 38 | public: |
| 39 | NullableScopedUtfChars(JNIEnv* env, jstring s) |
| 40 | : mEnv(env), mString(s) |
| 41 | { |
| 42 | mUtfChars = (s != NULL) ? env->GetStringUTFChars(s, NULL) : NULL; |
| 43 | } |
| 44 | |
| 45 | ~NullableScopedUtfChars() { |
| 46 | if (mUtfChars) { |
| 47 | mEnv->ReleaseStringUTFChars(mString, mUtfChars); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | const char* c_str() const { |
| 52 | return mUtfChars; |
| 53 | } |
| 54 | |
| 55 | size_t size() const { |
| 56 | return strlen(mUtfChars); |
| 57 | } |
| 58 | |
| 59 | // Element access. |
| 60 | const char& operator[](size_t n) const { |
| 61 | return mUtfChars[n]; |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | JNIEnv* mEnv; |
| 66 | jstring mString; |
| 67 | const char* mUtfChars; |
| 68 | |
| 69 | // Disallow copy and assignment. |
| 70 | NullableScopedUtfChars(const NullableScopedUtfChars&); |
| 71 | void operator=(const NullableScopedUtfChars&); |
| 72 | }; |
| 73 | |
| 74 | static jint DexFile_openDexFile(JNIEnv* env, jclass, jstring javaSourceName, jstring javaOutputName) { |
| 75 | ScopedUtfChars sourceName(env, javaSourceName); |
| 76 | if (sourceName.c_str() == NULL) { |
| 77 | return 0; |
| 78 | } |
| 79 | NullableScopedUtfChars outputName(env, javaOutputName); |
| 80 | if (env->ExceptionOccurred()) { |
| 81 | return 0; |
| 82 | } |
| 83 | UNIMPLEMENTED(WARNING) << sourceName.c_str(); |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | void DexFile_closeDexFile(JNIEnv* env, jclass, jint cookie) { |
| 88 | UNIMPLEMENTED(WARNING); |
| 89 | } |
| 90 | |
| 91 | jclass DexFile_defineClass(JNIEnv* env, jclass, jstring javaName, jobject javaLoader, jint cookie) { |
| 92 | UNIMPLEMENTED(ERROR); |
| 93 | return NULL; |
| 94 | } |
| 95 | |
| 96 | jobjectArray DexFile_getClassNameList(JNIEnv* env, jclass, jint cookie) { |
| 97 | UNIMPLEMENTED(ERROR); |
| 98 | return NULL; |
| 99 | } |
| 100 | |
| 101 | jboolean DexFile_isDexOptNeeded(JNIEnv* env, jclass, jstring javaFilename) { |
| 102 | // TODO: run dex2oat? |
| 103 | UNIMPLEMENTED(WARNING); |
| 104 | return JNI_FALSE; |
| 105 | } |
| 106 | |
| 107 | static JNINativeMethod gMethods[] = { |
| 108 | NATIVE_METHOD(DexFile, closeDexFile, "(I)V"), |
| 109 | NATIVE_METHOD(DexFile, defineClass, "(Ljava/lang/String;Ljava/lang/ClassLoader;I)Ljava/lang/Class;"), |
| 110 | NATIVE_METHOD(DexFile, getClassNameList, "(I)[Ljava/lang/String;"), |
| 111 | NATIVE_METHOD(DexFile, isDexOptNeeded, "(Ljava/lang/String;)Z"), |
| 112 | NATIVE_METHOD(DexFile, openDexFile, "(Ljava/lang/String;Ljava/lang/String;I)I"), |
| 113 | }; |
| 114 | |
| 115 | } // namespace |
| 116 | |
| 117 | void register_dalvik_system_DexFile(JNIEnv* env) { |
| 118 | jniRegisterNativeMethods(env, "dalvik/system/DexFile", gMethods, NELEM(gMethods)); |
| 119 | } |
| 120 | |
| 121 | } // namespace art |