1 /* 2 * Copyright 2012, Google LLC 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google LLC nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 package com.android.tools.smali.dexlib2; 32 33 import com.android.tools.smali.dexlib2.iface.reference.CallSiteReference; 34 import com.android.tools.smali.dexlib2.iface.reference.FieldReference; 35 import com.android.tools.smali.dexlib2.iface.reference.MethodHandleReference; 36 import com.android.tools.smali.dexlib2.iface.reference.MethodProtoReference; 37 import com.android.tools.smali.dexlib2.iface.reference.MethodReference; 38 import com.android.tools.smali.dexlib2.iface.reference.Reference; 39 import com.android.tools.smali.dexlib2.iface.reference.StringReference; 40 import com.android.tools.smali.dexlib2.iface.reference.TypeReference; 41 import com.android.tools.smali.util.ExceptionWithContext; 42 43 public final class ReferenceType { 44 public static final int STRING = 0; 45 public static final int TYPE = 1; 46 public static final int FIELD = 2; 47 public static final int METHOD = 3; 48 public static final int METHOD_PROTO = 4; 49 public static final int CALL_SITE = 5; 50 public static final int METHOD_HANDLE = 6; 51 public static final int NONE = 7; 52 getReferenceType(Reference reference)53 public static int getReferenceType(Reference reference) { 54 if (reference instanceof StringReference) { 55 return STRING; 56 } else if (reference instanceof TypeReference) { 57 return TYPE; 58 } else if (reference instanceof FieldReference) { 59 return FIELD; 60 } else if (reference instanceof MethodReference) { 61 return METHOD; 62 } else if (reference instanceof MethodProtoReference) { 63 return METHOD_PROTO; 64 } else if (reference instanceof CallSiteReference) { 65 return CALL_SITE; 66 } else if (reference instanceof MethodHandleReference) { 67 return METHOD_HANDLE; 68 } else { 69 throw new IllegalStateException("Invalid reference"); 70 } 71 } 72 73 /** 74 * Validate a specific reference type. Note that the NONE placeholder is specifically not considered valid here. 75 * 76 * @throws InvalidReferenceTypeException 77 */ validateReferenceType(int referenceType)78 public static void validateReferenceType(int referenceType) { 79 if (referenceType < 0 || referenceType > 4) { 80 throw new InvalidReferenceTypeException(referenceType); 81 } 82 } 83 84 public static class InvalidReferenceTypeException extends ExceptionWithContext { 85 private final int referenceType; 86 InvalidReferenceTypeException(int referenceType)87 public InvalidReferenceTypeException(int referenceType) { 88 super("Invalid reference type: %d", referenceType); 89 this.referenceType = referenceType; 90 } 91 InvalidReferenceTypeException(int referenceType, String message, Object... formatArgs)92 public InvalidReferenceTypeException(int referenceType, String message, Object... formatArgs) { 93 super(message, formatArgs); 94 this.referenceType = referenceType; 95 } 96 getReferenceType()97 public int getReferenceType() { 98 return referenceType; 99 } 100 } 101 ReferenceType()102 private ReferenceType() {} 103 }