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.rop.cst;
17 
18 import com.android.dx.rop.type.Prototype;
19 import com.android.dx.rop.type.Type;
20 
21 /**
22  * Prototype reference.
23  */
24 public final class CstProtoRef extends TypedConstant {
25 
26     /** {@code non-null;} the prototype */
27     private final Prototype prototype;
28 
CstProtoRef(Prototype prototype)29     public CstProtoRef(Prototype prototype) {
30         this.prototype = prototype;
31     }
32 
33     /**
34      * Makes an instance for the given value. This may (but does not
35      * necessarily) return an already-allocated instance.
36      *
37      * @param descriptor the method descriptor
38      * @return {@code non-null;} the appropriate instance
39      */
make(CstString descriptor)40     public static CstProtoRef make(CstString descriptor) {
41         Prototype prototype = Prototype.fromDescriptor(descriptor.getString());
42         return new CstProtoRef(prototype);
43     }
44 
45     /** {@inheritDoc} */
46     @Override
equals(Object other)47     public boolean equals(Object other) {
48         if (!(other instanceof CstProtoRef)) {
49             return false;
50         }
51         CstProtoRef otherCstProtoRef = (CstProtoRef) other;
52         return getPrototype().equals(otherCstProtoRef.getPrototype());
53     }
54 
55     @Override
hashCode()56     public int hashCode() {
57         return prototype.hashCode();
58     }
59 
60     /** {@inheritDoc} */
61     @Override
isCategory2()62     public boolean isCategory2() {
63         return false;
64     }
65 
66     /** {@inheritDoc} */
67     @Override
typeName()68     public String typeName() {
69         return "proto";
70     }
71 
72     /** {@inheritDoc} */
73     @Override
compareTo0(Constant other)74     protected int compareTo0(Constant other) {
75         CstProtoRef otherCstProtoRef = (CstProtoRef) other;
76         return prototype.compareTo(otherCstProtoRef.getPrototype());
77     }
78 
79     /** {@inheritDoc} */
80     @Override
toHuman()81     public String toHuman() {
82         return prototype.getDescriptor();
83     }
84 
85     /** {@inheritDoc} */
86     @Override
toString()87     public final String toString() {
88         return typeName() + "{" + toHuman() + '}';
89     }
90 
getPrototype()91     public Prototype getPrototype() {
92         return prototype;
93     }
94 
95     @Override
getType()96     public Type getType() {
97         return Type.METHOD_TYPE;
98     }
99 }
100