1 /* 2 * Copyright (C) 2017 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 package com.android.dx.dex.file; 17 18 import com.android.dx.rop.cst.Constant; 19 import com.android.dx.rop.cst.CstMethodHandle; 20 import java.util.Collection; 21 import java.util.TreeMap; 22 23 public final class MethodHandlesSection extends UniformItemSection { 24 25 private final TreeMap<CstMethodHandle, MethodHandleItem> methodHandles = new TreeMap<>(); 26 MethodHandlesSection(DexFile dexFile)27 public MethodHandlesSection(DexFile dexFile) { 28 super("method_handles", dexFile, 8); 29 } 30 31 @Override get(Constant cst)32 public IndexedItem get(Constant cst) { 33 if (cst == null) { 34 throw new NullPointerException("cst == null"); 35 } 36 throwIfNotPrepared(); 37 38 IndexedItem result = methodHandles.get((CstMethodHandle) cst); 39 if (result == null) { 40 throw new IllegalArgumentException("not found"); 41 } 42 return result; 43 } 44 45 @Override orderItems()46 protected void orderItems() { 47 int index = 0; 48 for (MethodHandleItem item : methodHandles.values()) { 49 item.setIndex(index++); 50 } 51 } 52 53 @Override items()54 public Collection<? extends Item> items() { 55 return methodHandles.values(); 56 } 57 intern(CstMethodHandle methodHandle)58 public synchronized void intern(CstMethodHandle methodHandle) { 59 if (methodHandle == null) { 60 throw new NullPointerException("methodHandle == null"); 61 } 62 63 throwIfPrepared(); 64 65 MethodHandleItem result = methodHandles.get(methodHandle); 66 if (result == null) { 67 result = new MethodHandleItem(methodHandle); 68 methodHandles.put(methodHandle, result); 69 } 70 } 71 indexOf(CstMethodHandle cstMethodHandle)72 int indexOf(CstMethodHandle cstMethodHandle) { 73 return methodHandles.get(cstMethodHandle).getIndex(); 74 } 75 } 76