Home | History | Annotate | Download | only in lang
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one or more
      3  * contributor license agreements.  See the NOTICE file distributed with
      4  * this work for additional information regarding copyright ownership.
      5  * The ASF licenses this file to You under the Apache License, Version 2.0
      6  * (the "License"); you may not use this file except in compliance with
      7  * the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 /*
     18  * Copyright (C) 2012 The Android Open Source Project
     19  *
     20  * Licensed under the Apache License, Version 2.0 (the "License");
     21  * you may not use this file except in compliance with the License.
     22  * You may obtain a copy of the License at
     23  *
     24  *      http://www.apache.org/licenses/LICENSE-2.0
     25  *
     26  * Unless required by applicable law or agreed to in writing, software
     27  * distributed under the License is distributed on an "AS IS" BASIS,
     28  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     29  * See the License for the specific language governing permissions and
     30  * limitations under the License.
     31  */
     32 
     33 package java.lang;
     34 
     35 import com.android.dex.Dex;
     36 
     37 /**
     38  * A dex cache holds resolved copies of strings, fields, methods, and classes from the dexfile.
     39  */
     40 final class DexCache {
     41     /** Lazily initialized dex file wrapper. Volatile to avoid double-check locking issues. */
     42     private volatile Dex dex;
     43 
     44     /** The location of the associated dex file. */
     45     String location;
     46 
     47     /** Holds C pointer to dexFile. */
     48     private long dexFile;
     49 
     50     /**
     51      * References to fields (C array pointer) as they become resolved following
     52      * interpreter semantics. May refer to fields defined in other dex files.
     53      */
     54     private long resolvedFields;
     55 
     56     /**
     57      * References to methods (C array pointer) as they become resolved following
     58      * interpreter semantics. May refer to methods defined in other dex files.
     59      */
     60     private long resolvedMethods;
     61 
     62     /**
     63      * References to types (C array pointer) as they become resolved following
     64      * interpreter semantics. May refer to types defined in other dex files.
     65      */
     66     private long resolvedTypes;
     67 
     68     /**
     69      * References to strings (C array pointer) as they become resolved following
     70      * interpreter semantics. All strings are interned.
     71      */
     72     private long strings;
     73 
     74     /**
     75      * The number of elements in the native resolvedFields array.
     76      */
     77     private int numResolvedFields;
     78 
     79     /**
     80      * The number of elements in the native resolvedMethods array.
     81      */
     82     private int numResolvedMethods;
     83 
     84     /**
     85      * The number of elements in the native resolvedTypes array.
     86      */
     87     private int numResolvedTypes;
     88 
     89     /**
     90      * The number of elements in the native strings array.
     91      */
     92     private int numStrings;
     93 
     94     // Only created by the VM.
     95     private DexCache() {}
     96 
     97     Dex getDex() {
     98         Dex result = dex;
     99         if (result == null) {
    100             synchronized (this) {
    101                 result = dex;
    102                 if (result == null) {
    103                     dex = result = getDexNative();
    104                 }
    105             }
    106         }
    107         return result;
    108     }
    109 
    110     native Class<?> getResolvedType(int typeIndex);
    111     native String getResolvedString(int stringIndex);
    112     native void setResolvedType(int typeIndex, Class<?> type);
    113     native void setResolvedString(int stringIndex, String string);
    114     private native Dex getDexNative();
    115 }
    116 
    117