1 /* 2 * Copyright 2018, Google Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * * Redistributions in binary form must reproduce the above 12 * copyright notice, this list of conditions and the following disclaimer 13 * in the documentation and/or other materials provided with the 14 * distribution. 15 * * Neither the name of Google Inc. nor the names of its 16 * contributors may be used to endorse or promote products derived from 17 * this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 package org.jf.dexlib2; 33 34 import com.google.common.collect.BiMap; 35 import com.google.common.collect.ImmutableBiMap; 36 import org.jf.util.ExceptionWithContext; 37 38 import javax.annotation.Nonnull; 39 40 public class MethodHandleType { 41 public static final int STATIC_PUT = 0; 42 public static final int STATIC_GET = 1; 43 public static final int INSTANCE_PUT = 2; 44 public static final int INSTANCE_GET = 3; 45 public static final int INVOKE_STATIC = 4; 46 public static final int INVOKE_INSTANCE = 5; 47 public static final int INVOKE_CONSTRUCTOR = 6; 48 public static final int INVOKE_DIRECT = 7; 49 public static final int INVOKE_INTERFACE = 8; 50 51 private static final BiMap<Integer, String> methodHandleTypeNames = new ImmutableBiMap.Builder<Integer, String>() 52 .put(STATIC_PUT, "static-put") 53 .put(STATIC_GET, "static-get") 54 .put(INSTANCE_PUT, "instance-put") 55 .put(INSTANCE_GET, "instance-get") 56 .put(INVOKE_STATIC, "invoke-static") 57 .put(INVOKE_INSTANCE, "invoke-instance") 58 .put(INVOKE_CONSTRUCTOR, "invoke-constructor") 59 .put(INVOKE_DIRECT, "invoke-direct") 60 .put(INVOKE_INTERFACE, "invoke-interface") 61 .build(); 62 toString(int methodHandleType)63 @Nonnull public static String toString(int methodHandleType) { 64 String val = methodHandleTypeNames.get(methodHandleType); 65 if (val == null) { 66 throw new InvalidMethodHandleTypeException(methodHandleType); 67 } 68 return val; 69 } 70 getMethodHandleType(String methodHandleType)71 public static int getMethodHandleType(String methodHandleType) { 72 Integer ret = methodHandleTypeNames.inverse().get(methodHandleType); 73 if (ret == null) { 74 throw new ExceptionWithContext("Invalid method handle type: %s", methodHandleType); 75 } 76 return ret; 77 } 78 79 public static class InvalidMethodHandleTypeException extends ExceptionWithContext { 80 private final int methodHandleType; 81 InvalidMethodHandleTypeException(int methodHandleType)82 public InvalidMethodHandleTypeException(int methodHandleType) { 83 super("Invalid method handle type: %d", methodHandleType); 84 this.methodHandleType = methodHandleType; 85 } 86 InvalidMethodHandleTypeException(int methodHandleType, String message, Object... formatArgs)87 public InvalidMethodHandleTypeException(int methodHandleType, String message, Object... formatArgs) { 88 super(message, formatArgs); 89 this.methodHandleType = methodHandleType; 90 } 91 getMethodHandleType()92 public int getMethodHandleType() { 93 return methodHandleType; 94 } 95 } 96 } 97