MethodId.java revision 23abc2fe89ec3713645d64bdb74415a9090084f4
19258b6bc66e09368ada54001f619d53b4fc976d5ager@chromium.org/*
29a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com * Copyright (C) 2011 The Android Open Source Project
39a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com *
49a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com * Licensed under the Apache License, Version 2.0 (the "License");
59a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com * you may not use this file except in compliance with the License.
69a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com * You may obtain a copy of the License at
79a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com *
89a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com *      http://www.apache.org/licenses/LICENSE-2.0
99a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com *
109a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com * Unless required by applicable law or agreed to in writing, software
119a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com * distributed under the License is distributed on an "AS IS" BASIS,
129a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com * See the License for the specific language governing permissions and
149a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com * limitations under the License.
159a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com */
169a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
179a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.compackage com.google.dexmaker;
189a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
199a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comimport com.android.dx.rop.cst.CstMethodRef;
209a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comimport com.android.dx.rop.cst.CstNat;
219a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comimport com.android.dx.rop.cst.CstString;
229a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comimport com.android.dx.rop.type.Prototype;
239a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.comimport java.util.List;
249a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
259a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com/**
269a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com * Identifies a method or constructor.
279a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com */
285ad5acef6bd4ebc785f946d8bcc2a88b1e031827ricow@chromium.orgpublic final class MethodId<D, R> {
295ad5acef6bd4ebc785f946d8bcc2a88b1e031827ricow@chromium.org    final TypeId<D> declaringType;
309a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    final TypeId<R> returnType;
319a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    final String name;
329a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    final TypeList parameters;
339a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
349a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    /** cached converted state */
35b3284ad36ee358a35b81379ad1c449e4f8021362kasperl@chromium.org    final CstNat nat;
369a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    final CstMethodRef constant;
379a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
389a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    MethodId(TypeId<D> declaringType, TypeId<R> returnType, String name, TypeList parameters) {
399a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com        if (declaringType == null || returnType == null || name == null || parameters == null) {
409a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com            throw new NullPointerException();
41b912362e2b2e704d09faac4290e027fd744bf587kasperl@chromium.org        }
42b912362e2b2e704d09faac4290e027fd744bf587kasperl@chromium.org        this.declaringType = declaringType;
439a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com        this.returnType = returnType;
449a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com        this.name = name;
459a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com        this.parameters = parameters;
469a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com        this.nat = new CstNat(new CstString(name), new CstString(descriptor(false)));
475a8ca6c70c6fc9716f18f6223c98d1fef5752cf6kasperl@chromium.org        this.constant = new CstMethodRef(declaringType.constant, nat);
489a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    }
499a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
509a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    public TypeId<D> getDeclaringType() {
513a37e9b96c768f6b5b6b09542e1cb1a1ece7a022ager@chromium.org        return declaringType;
523a37e9b96c768f6b5b6b09542e1cb1a1ece7a022ager@chromium.org    }
539a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
549a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    public TypeId<R> getReturnType() {
559a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com        return returnType;
569a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    }
5765dad4b091d2925543c6326db635d0f7cf9e1edcager@chromium.org
58c4c927273ae2b690c4a015b4640a2a469c9a1a69ager@chromium.org    /**
599a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com     * Returns the method's name. This is "<init>" if this is a constructor.
609a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com     */
619a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    public String getName() {
629a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com        return name;
639a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    }
649a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
659a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    public List<TypeId<?>> getParameters() {
669a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com        return parameters.asList();
679a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    }
689a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
699a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    /**
709a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com     * Returns a descriptor like "(Ljava/lang/Class;[I)Ljava/lang/Object;".
719a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com     */
729a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    String descriptor(boolean includeThis) {
739a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com        StringBuilder result = new StringBuilder();
749a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com        result.append("(");
759a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com        if (includeThis) {
769a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com            result.append(declaringType.name);
779a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com        }
789a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com        for (TypeId t : parameters.types) {
799a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com            result.append(t.name);
809a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com        }
819a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com        result.append(")");
829a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com        result.append(returnType.name);
839a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com        return result.toString();
849a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    }
859a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
869a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    Prototype prototype(boolean includeThis) {
879a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com        return Prototype.intern(descriptor(includeThis));
889a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    }
899a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
909a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    @Override public boolean equals(Object o) {
919a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com        return o instanceof MethodId
929a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com                && ((MethodId<?, ?>) o).declaringType.equals(declaringType)
939a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com                && ((MethodId<?, ?>) o).name.equals(name)
949a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com                && ((MethodId<?, ?>) o).parameters.equals(parameters)
959a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com                && ((MethodId<?, ?>) o).returnType.equals(returnType);
969a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    }
979a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
989a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    @Override public int hashCode() {
999a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com        int result = 17;
1009a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com        result = 31 * result + declaringType.hashCode();
1019a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com        result = 31 * result + name.hashCode();
1029a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com        result = 31 * result + parameters.hashCode();
1039a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com        result = 31 * result + returnType.hashCode();
1049a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com        return result;
1059a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    }
1069a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com
1079a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    @Override public String toString() {
1089a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com        return declaringType + "." + name + "(" + parameters + ")";
1099a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com    }
1109a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com}
1119a4089a092cad9ff23b6416b92cd5d818dc101d1mads.s.ager@gmail.com