• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018, 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.MethodHandleType;
34 import com.android.tools.smali.util.ExceptionWithContext;
35 import com.android.tools.smali.dexlib2.base.reference.BaseMethodHandleReference;
36 import com.android.tools.smali.dexlib2.iface.reference.FieldReference;
37 import com.android.tools.smali.dexlib2.iface.reference.MethodHandleReference;
38 import com.android.tools.smali.dexlib2.iface.reference.MethodReference;
39 import com.android.tools.smali.dexlib2.iface.reference.Reference;
40 
41 import javax.annotation.Nonnull;
42 
43 public class ImmutableMethodHandleReference extends BaseMethodHandleReference implements ImmutableReference {
44     protected final int methodHandleType;
45     @Nonnull protected final ImmutableReference memberReference;
46 
ImmutableMethodHandleReference(int methodHandleType, @Nonnull ImmutableReference memberReference)47     public ImmutableMethodHandleReference(int methodHandleType, @Nonnull ImmutableReference memberReference) {
48         this.methodHandleType = methodHandleType;
49         this.memberReference = memberReference;
50     }
51 
ImmutableMethodHandleReference(int methodHandleType, @Nonnull Reference memberReference)52     public ImmutableMethodHandleReference(int methodHandleType, @Nonnull Reference memberReference) {
53         this.methodHandleType = methodHandleType;
54         this.memberReference = ImmutableReferenceFactory.of(memberReference);
55     }
56 
57     @Nonnull
of(@onnull MethodHandleReference methodHandleReference)58     public static ImmutableMethodHandleReference of(@Nonnull MethodHandleReference methodHandleReference) {
59         if (methodHandleReference instanceof ImmutableMethodHandleReference) {
60             return (ImmutableMethodHandleReference) methodHandleReference;
61         }
62         int methodHandleType = methodHandleReference.getMethodHandleType();
63         ImmutableReference memberReference;
64 
65         switch (methodHandleType) {
66             case MethodHandleType.STATIC_PUT:
67             case MethodHandleType.STATIC_GET:
68             case MethodHandleType.INSTANCE_PUT:
69             case MethodHandleType.INSTANCE_GET:
70                 memberReference = ImmutableFieldReference.of(
71                         (FieldReference) methodHandleReference.getMemberReference());
72                 break;
73             case MethodHandleType.INVOKE_STATIC:
74             case MethodHandleType.INVOKE_INSTANCE:
75             case MethodHandleType.INVOKE_CONSTRUCTOR:
76             case MethodHandleType.INVOKE_DIRECT:
77             case MethodHandleType.INVOKE_INTERFACE:
78                 memberReference = ImmutableMethodReference.of(
79                         (MethodReference) methodHandleReference.getMemberReference());
80                 break;
81             default:
82                 throw new ExceptionWithContext("Invalid method handle type: %d", methodHandleType);
83         }
84         return new ImmutableMethodHandleReference(methodHandleType, memberReference);
85     }
86 
getMethodHandleType()87     @Override public int getMethodHandleType() { return methodHandleType; }
getMemberReference()88     @Nonnull @Override public Reference getMemberReference() { return memberReference; }
89 }
90