ImmutableReferenceFactory.java revision f81150ad43efac4d590f6b7ea1425896b7ffb011
19d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek/*
29d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek * Copyright 2012, Google Inc.
39d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek * All rights reserved.
49d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek *
59d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek * Redistribution and use in source and binary forms, with or without
69d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek * modification, are permitted provided that the following conditions are
79d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek * met:
89d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek *
99d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek *     * Redistributions of source code must retain the above copyright
109d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek * notice, this list of conditions and the following disclaimer.
119d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek *     * Redistributions in binary form must reproduce the above
129d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek * copyright notice, this list of conditions and the following disclaimer
139d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek * in the documentation and/or other materials provided with the
149d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek * distribution.
152d88708cbe4e4ec5e04e4acb6bd7f5be68557379John McCall *     * Neither the name of Google Inc. nor the names of its
1655fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth * contributors may be used to endorse or promote products derived from
177cd088e519d7e6caa4c4c12db52e0e4ae35d25c2John McCall * this software without specific prior written permission.
1857e264e9f4ff0a72f3585a960cdf63437b76fa93Fariborz Jahanian *
1955fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20edcc27f6171fd8cc736d057d87b128252fcb7344Fariborz Jahanian * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2155fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2255fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2355fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2450df6ae41f232612e5e88b19e0db9900d08d2f6cJohn McCall * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
258fe83e1df954d72c0f4ffc15d20a5222ec151c21Benjamin Kramer * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
269d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
279d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
289d64c15223497f31899cc47fbe1531873dc06f2eTed Kremenek * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2928685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3028685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek */
3128685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenek
3228685ab2ddeea146841bf2e277e25c7b32dfc9f6Ted Kremenekpackage org.jf.dexlib2.immutable.reference;
33265941bc308d65cc270d5c4de5806f37ce405606John McCall
34265941bc308d65cc270d5c4de5806f37ce405606John McCallimport org.jf.dexlib2.ReferenceType;
35265941bc308d65cc270d5c4de5806f37ce405606John McCallimport org.jf.dexlib2.iface.reference.*;
36265941bc308d65cc270d5c4de5806f37ce405606John McCallimport org.jf.util.ExceptionWithContext;
37265941bc308d65cc270d5c4de5806f37ce405606John McCall
38265941bc308d65cc270d5c4de5806f37ce405606John McCallimport javax.annotation.Nonnull;
39265941bc308d65cc270d5c4de5806f37ce405606John McCall
40265941bc308d65cc270d5c4de5806f37ce405606John McCallpublic class ImmutableReferenceFactory {
41265941bc308d65cc270d5c4de5806f37ce405606John McCall    @Nonnull
42265941bc308d65cc270d5c4de5806f37ce405606John McCall    public static ImmutableReference of(Reference reference) {
43265941bc308d65cc270d5c4de5806f37ce405606John McCall        if (reference instanceof StringReference) {
44265941bc308d65cc270d5c4de5806f37ce405606John McCall            return ImmutableStringReference.of((StringReference)reference);
45265941bc308d65cc270d5c4de5806f37ce405606John McCall        }
46265941bc308d65cc270d5c4de5806f37ce405606John McCall        if (reference instanceof TypeReference) {
47d64c2eb83d7ec86faa4f2554935a977a19573f59John McCall            return ImmutableTypeReference.of((TypeReference)reference);
48265941bc308d65cc270d5c4de5806f37ce405606John McCall        }
49265941bc308d65cc270d5c4de5806f37ce405606John McCall        if (reference instanceof FieldReference) {
50265941bc308d65cc270d5c4de5806f37ce405606John McCall            return ImmutableFieldReference.of((FieldReference)reference);
51265941bc308d65cc270d5c4de5806f37ce405606John McCall        }
52265941bc308d65cc270d5c4de5806f37ce405606John McCall        if (reference instanceof MethodReference) {
53265941bc308d65cc270d5c4de5806f37ce405606John McCall            return ImmutableMethodReference.of((MethodReference)reference);
54265941bc308d65cc270d5c4de5806f37ce405606John McCall        }
55265941bc308d65cc270d5c4de5806f37ce405606John McCall        throw new ExceptionWithContext("Invalid reference type");
56265941bc308d65cc270d5c4de5806f37ce405606John McCall    }
57265941bc308d65cc270d5c4de5806f37ce405606John McCall
58265941bc308d65cc270d5c4de5806f37ce405606John McCall    @Nonnull
59265941bc308d65cc270d5c4de5806f37ce405606John McCall    public static ImmutableReference of(int referenceType, Reference reference) {
60265941bc308d65cc270d5c4de5806f37ce405606John McCall        switch (referenceType) {
61265941bc308d65cc270d5c4de5806f37ce405606John McCall            case ReferenceType.STRING:
62265941bc308d65cc270d5c4de5806f37ce405606John McCall                return ImmutableStringReference.of((StringReference)reference);
63265941bc308d65cc270d5c4de5806f37ce405606John McCall            case ReferenceType.TYPE:
64f85e193739c953358c865005855253af4f68a497John McCall                return ImmutableTypeReference.of((TypeReference)reference);
65f85e193739c953358c865005855253af4f68a497John McCall            case ReferenceType.FIELD:
66f85e193739c953358c865005855253af4f68a497John McCall                return ImmutableFieldReference.of((FieldReference)reference);
67f85e193739c953358c865005855253af4f68a497John McCall            case ReferenceType.METHOD:
68f85e193739c953358c865005855253af4f68a497John McCall                return ImmutableMethodReference.of((MethodReference)reference);
69f85e193739c953358c865005855253af4f68a497John McCall        }
70f85e193739c953358c865005855253af4f68a497John McCall        throw new ExceptionWithContext("Invalid reference type: %d", referenceType);
71f85e193739c953358c865005855253af4f68a497John McCall    }
72f85e193739c953358c865005855253af4f68a497John McCall}
73f85e193739c953358c865005855253af4f68a497John McCall