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.immutable.reference; 32 33 import com.android.tools.smali.dexlib2.ReferenceType; 34 import com.android.tools.smali.dexlib2.iface.reference.CallSiteReference; 35 import com.android.tools.smali.dexlib2.iface.reference.FieldReference; 36 import com.android.tools.smali.dexlib2.iface.reference.MethodHandleReference; 37 import com.android.tools.smali.dexlib2.iface.reference.MethodProtoReference; 38 import com.android.tools.smali.dexlib2.iface.reference.MethodReference; 39 import com.android.tools.smali.dexlib2.iface.reference.Reference; 40 import com.android.tools.smali.dexlib2.iface.reference.StringReference; 41 import com.android.tools.smali.dexlib2.iface.reference.TypeReference; 42 import com.android.tools.smali.util.ExceptionWithContext; 43 44 import javax.annotation.Nonnull; 45 46 public class ImmutableReferenceFactory { 47 @Nonnull of(Reference reference)48 public static ImmutableReference of(Reference reference) { 49 if (reference instanceof StringReference) { 50 return ImmutableStringReference.of((StringReference)reference); 51 } 52 if (reference instanceof TypeReference) { 53 return ImmutableTypeReference.of((TypeReference)reference); 54 } 55 if (reference instanceof FieldReference) { 56 return ImmutableFieldReference.of((FieldReference)reference); 57 } 58 if (reference instanceof MethodReference) { 59 return ImmutableMethodReference.of((MethodReference)reference); 60 } 61 if (reference instanceof MethodProtoReference) { 62 return ImmutableMethodProtoReference.of((MethodProtoReference) reference); 63 } 64 if (reference instanceof CallSiteReference) { 65 return ImmutableCallSiteReference.of((CallSiteReference) reference); 66 } 67 if (reference instanceof MethodHandleReference) { 68 return ImmutableMethodHandleReference.of((MethodHandleReference) reference); 69 } 70 throw new ExceptionWithContext("Invalid reference type"); 71 } 72 73 @Nonnull of(int referenceType, Reference reference)74 public static ImmutableReference of(int referenceType, Reference reference) { 75 switch (referenceType) { 76 case ReferenceType.STRING: 77 return ImmutableStringReference.of((StringReference)reference); 78 case ReferenceType.TYPE: 79 return ImmutableTypeReference.of((TypeReference)reference); 80 case ReferenceType.FIELD: 81 return ImmutableFieldReference.of((FieldReference)reference); 82 case ReferenceType.METHOD: 83 return ImmutableMethodReference.of((MethodReference)reference); 84 case ReferenceType.METHOD_PROTO: 85 return ImmutableMethodProtoReference.of((MethodProtoReference)reference); 86 case ReferenceType.CALL_SITE: 87 return ImmutableCallSiteReference.of((CallSiteReference) reference); 88 case ReferenceType.METHOD_HANDLE: 89 return ImmutableMethodHandleReference.of((MethodHandleReference) reference); 90 } 91 throw new ExceptionWithContext("Invalid reference type: %d", referenceType); 92 } 93 } 94