Home | History | Annotate | Download | only in native
      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 /*
     18  * java.lang.VMClassLoader
     19  */
     20 #include "Dalvik.h"
     21 #include "native/InternalNativePriv.h"
     22 
     23 
     24 /*
     25  * static Class defineClass(ClassLoader cl, String name,
     26  *     byte[] data, int offset, int len)
     27  *     throws ClassFormatError
     28  *
     29  * Convert an array of bytes to a Class object.
     30  */
     31 static void Dalvik_java_lang_VMClassLoader_defineClass(const u4* args,
     32     JValue* pResult)
     33 {
     34     Object* loader = (Object*) args[0];
     35     StringObject* nameObj = (StringObject*) args[1];
     36     const u1* data = (const u1*) args[2];
     37     int offset = args[3];
     38     int len = args[4];
     39     char* name = NULL;
     40 
     41     name = dvmCreateCstrFromString(nameObj);
     42     ALOGE("ERROR: defineClass(%p, %s, %p, %d, %d)",
     43         loader, name, data, offset, len);
     44     dvmThrowUnsupportedOperationException(
     45         "can't load this type of class file");
     46 
     47     free(name);
     48     RETURN_VOID();
     49 }
     50 
     51 /*
     52  * static Class defineClass(ClassLoader cl, byte[] data, int offset,
     53  *     int len)
     54  *     throws ClassFormatError
     55  *
     56  * Convert an array of bytes to a Class object. Deprecated version of
     57  * previous method, lacks name parameter.
     58  */
     59 static void Dalvik_java_lang_VMClassLoader_defineClass2(const u4* args,
     60     JValue* pResult)
     61 {
     62     Object* loader = (Object*) args[0];
     63     const u1* data = (const u1*) args[1];
     64     int offset = args[2];
     65     int len = args[3];
     66 
     67     ALOGE("ERROR: defineClass(%p, %p, %d, %d)",
     68         loader, data, offset, len);
     69     dvmThrowUnsupportedOperationException(
     70         "can't load this type of class file");
     71 
     72     RETURN_VOID();
     73 }
     74 
     75 /*
     76  * static Class findLoadedClass(ClassLoader cl, String name)
     77  */
     78 static void Dalvik_java_lang_VMClassLoader_findLoadedClass(const u4* args,
     79     JValue* pResult)
     80 {
     81     Object* loader = (Object*) args[0];
     82     StringObject* nameObj = (StringObject*) args[1];
     83     ClassObject* clazz = NULL;
     84     char* name = NULL;
     85     char* descriptor = NULL;
     86 
     87     if (nameObj == NULL) {
     88         dvmThrowNullPointerException("name == null");
     89         goto bail;
     90     }
     91 
     92     /*
     93      * Get a UTF-8 copy of the string, and convert dots to slashes.
     94      */
     95     name = dvmCreateCstrFromString(nameObj);
     96     if (name == NULL)
     97         goto bail;
     98 
     99     descriptor = dvmDotToDescriptor(name);
    100     if (descriptor == NULL)
    101         goto bail;
    102 
    103     clazz = dvmLookupClass(descriptor, loader, false);
    104     LOGVV("look: %s ldr=%p --> %p", descriptor, loader, clazz);
    105 
    106 bail:
    107     free(name);
    108     free(descriptor);
    109     RETURN_PTR(clazz);
    110 }
    111 
    112 /*
    113  * private static int getBootClassPathSize()
    114  *
    115  * Get the number of entries in the boot class path.
    116  */
    117 static void Dalvik_java_lang_VMClassLoader_getBootClassPathSize(const u4* args,
    118     JValue* pResult)
    119 {
    120     int count = dvmGetBootPathSize();
    121     RETURN_INT(count);
    122 }
    123 
    124 /*
    125  * private static String getBootClassPathResource(String name, int index)
    126  *
    127  * Find a resource with a matching name in a boot class path entry.
    128  *
    129  * This mimics the previous VM interface, since we're sharing class libraries.
    130  */
    131 static void Dalvik_java_lang_VMClassLoader_getBootClassPathResource(
    132     const u4* args, JValue* pResult)
    133 {
    134     StringObject* nameObj = (StringObject*) args[0];
    135     StringObject* result;
    136     int idx = args[1];
    137     char* name;
    138 
    139     name = dvmCreateCstrFromString(nameObj);
    140     if (name == NULL)
    141         RETURN_PTR(NULL);
    142 
    143     result = dvmGetBootPathResource(name, idx);
    144     free(name);
    145     dvmReleaseTrackedAlloc((Object*)result, NULL);
    146     RETURN_PTR(result);
    147 }
    148 
    149 /*
    150  * static final Class getPrimitiveClass(char prim_type)
    151  */
    152 static void Dalvik_java_lang_VMClassLoader_getPrimitiveClass(const u4* args,
    153     JValue* pResult)
    154 {
    155     int primType = args[0];
    156 
    157     pResult->l = (Object*)dvmFindPrimitiveClass(primType);
    158 }
    159 
    160 /*
    161  * static Class loadClass(String name, boolean resolve)
    162  *     throws ClassNotFoundException
    163  *
    164  * Load class using bootstrap class loader.
    165  *
    166  * Return the Class object associated with the class or interface with
    167  * the specified name.
    168  *
    169  * "name" is in "binary name" format, e.g. "dalvik.system.Debug$1".
    170  */
    171 static void Dalvik_java_lang_VMClassLoader_loadClass(const u4* args,
    172     JValue* pResult)
    173 {
    174     StringObject* nameObj = (StringObject*) args[0];
    175     bool resolve = (args[1] != 0);
    176     ClassObject* clazz;
    177 
    178     clazz = dvmFindClassByName(nameObj, NULL, resolve);
    179     assert(clazz == NULL || dvmIsClassLinked(clazz));
    180     RETURN_PTR(clazz);
    181 }
    182 
    183 const DalvikNativeMethod dvm_java_lang_VMClassLoader[] = {
    184     { "defineClass",        "(Ljava/lang/ClassLoader;Ljava/lang/String;[BII)Ljava/lang/Class;",
    185         Dalvik_java_lang_VMClassLoader_defineClass },
    186     { "defineClass",        "(Ljava/lang/ClassLoader;[BII)Ljava/lang/Class;",
    187         Dalvik_java_lang_VMClassLoader_defineClass2 },
    188     { "findLoadedClass",    "(Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;",
    189         Dalvik_java_lang_VMClassLoader_findLoadedClass },
    190     { "getBootClassPathSize", "()I",
    191         Dalvik_java_lang_VMClassLoader_getBootClassPathSize },
    192     { "getBootClassPathResource", "(Ljava/lang/String;I)Ljava/lang/String;",
    193         Dalvik_java_lang_VMClassLoader_getBootClassPathResource },
    194     { "getPrimitiveClass",  "(C)Ljava/lang/Class;",
    195         Dalvik_java_lang_VMClassLoader_getPrimitiveClass },
    196     { "loadClass",          "(Ljava/lang/String;Z)Ljava/lang/Class;",
    197         Dalvik_java_lang_VMClassLoader_loadClass },
    198     { NULL, NULL, NULL },
    199 };
    200