VerificationError.java revision 6c15046f2b744978bb3b03a0697d7865d132fe6e
1/*
2 * Copyright 2013, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 *     * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *     * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 *     * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32package org.jf.dexlib2;
33
34import com.google.common.collect.Maps;
35import org.jf.util.ExceptionWithContext;
36
37import java.util.HashMap;
38
39public class VerificationError {
40    public static final int GENERIC = 1;
41    public static final int NO_SUCH_CLASS = 2;
42    public static final int NO_SUCH_FIELD = 3;
43    public static final int NO_SUCH_METHOD = 4;
44    public static final int ILLEGAL_CLASS_ACCESS = 5;
45    public static final int ILLEGAL_FIELD_ACCESS = 6;
46    public static final int ILLEGAL_METHOD_ACCESS = 7;
47    public static final int CLASS_CHANGE_ERROR = 8;
48    public static final int INSTANTIATION_ERROR = 9;
49
50    private static final HashMap<String, Integer> verificationErrorNames = Maps.newHashMap();
51
52    static {
53        verificationErrorNames.put("generic-error", GENERIC);
54        verificationErrorNames.put("no-such-class", NO_SUCH_CLASS);
55        verificationErrorNames.put("no-such-field", NO_SUCH_FIELD);
56        verificationErrorNames.put("no-such-method", NO_SUCH_METHOD);
57        verificationErrorNames.put("illegal-class-access", ILLEGAL_CLASS_ACCESS);
58        verificationErrorNames.put("illegal-field-access", ILLEGAL_FIELD_ACCESS);
59        verificationErrorNames.put("illegal-method-access", ILLEGAL_METHOD_ACCESS);
60        verificationErrorNames.put("class-change-error", CLASS_CHANGE_ERROR);
61        verificationErrorNames.put("instantiation-error", INSTANTIATION_ERROR);
62    }
63
64    public static String getVerificationErrorName(int verificationError) {
65        switch (verificationError) {
66            case GENERIC:
67                return "generic-error";
68            case NO_SUCH_CLASS:
69                return "no-such-class";
70            case NO_SUCH_FIELD:
71                return "no-such-field";
72            case NO_SUCH_METHOD:
73                return "no-such-method";
74            case ILLEGAL_CLASS_ACCESS:
75                return "illegal-class-access";
76            case ILLEGAL_FIELD_ACCESS:
77                return "illegal-field-access";
78            case ILLEGAL_METHOD_ACCESS:
79                return "illegal-method-access";
80            case CLASS_CHANGE_ERROR:
81                return "class-change-error";
82            case INSTANTIATION_ERROR:
83                return "instantiation-error";
84            default:
85                return null;
86        }
87    }
88
89    public static int getVerificationError(String verificationError) {
90        Integer ret = verificationErrorNames.get(verificationError);
91        if (ret == null) {
92            throw new ExceptionWithContext("Invalid verification error: %s", verificationError);
93        }
94        return ret;
95    }
96
97    public static boolean isValidVerificationError(int verificationError) {
98        return verificationError > 0 && verificationError < 10;
99    }
100}
101