1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * https://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.bcel.verifier;
20
21 /**
22 * A VerificationResult is what a PassVerifier returns after verifying.
23 */
24 public class VerificationResult {
25
26 /**
27 * Constant to indicate verification has not been tried yet. This happens if some earlier verification pass did not
28 * return VERIFIED_OK.
29 */
30 public static final int VERIFIED_NOTYET = 0;
31
32 /** Constant to indicate verification was passed. */
33 public static final int VERIFIED_OK = 1;
34
35 /** Constant to indicate verfication failed. */
36 public static final int VERIFIED_REJECTED = 2;
37
38 /**
39 * This string is the canonical message for verifications that have not been tried yet. This happens if some earlier
40 * verification pass did not return {@link #VERIFIED_OK}.
41 */
42 private static final String VERIFIED_NOTYET_MSG = "Not yet verified.";
43
44 /** This string is the canonical message for passed verification passes. */
45 private static final String VERIFIED_OK_MSG = "Passed verification.";
46
47 /**
48 * Canonical VerificationResult for not-yet-tried verifications. This happens if some earlier verification pass did not
49 * return {@link #VERIFIED_OK}.
50 */
51 public static final VerificationResult VR_NOTYET = new VerificationResult(VERIFIED_NOTYET, VERIFIED_NOTYET_MSG);
52
53 /** Canonical VerificationResult for passed verifications. */
54 public static final VerificationResult VR_OK = new VerificationResult(VERIFIED_OK, VERIFIED_OK_MSG);
55
56 /** The numeric status. */
57 private final int numeric;
58
59 /** The detailed message. */
60 private final String detailMessage;
61
62 /**
63 * The usual constructor.
64 *
65 * @param status The verification status.
66 * @param message The detail message.
67 */
68 public VerificationResult(final int status, final String message) {
69 numeric = status;
70 detailMessage = message;
71 }
72
73 /**
74 * Returns if two VerificationResult instances are equal.
75 *
76 * @param o The object to compare.
77 * @return true if equal.
78 */
79 @Override
80 public boolean equals(final Object o) {
81 if (!(o instanceof VerificationResult)) {
82 return false;
83 }
84 final VerificationResult other = (VerificationResult) o;
85 return other.numeric == this.numeric && other.detailMessage.equals(this.detailMessage);
86 }
87
88 /**
89 * Returns a detailed message.
90 *
91 * @return The detail message.
92 */
93 public String getMessage() {
94 return detailMessage;
95 }
96
97 /**
98 * Returns one of the {@link #VERIFIED_OK}, {@link #VERIFIED_NOTYET}, {@link #VERIFIED_REJECTED} constants.
99 *
100 * @return The verification status.
101 */
102 public int getStatus() {
103 return numeric;
104 }
105
106 /**
107 * @return A hash code value for the object.
108 */
109 @Override
110 public int hashCode() {
111 return numeric ^ detailMessage.hashCode();
112 }
113
114 /**
115 * Returns a String representation of the VerificationResult.
116 */
117 @Override
118 public String toString() {
119 String ret = "";
120 if (numeric == VERIFIED_NOTYET) {
121 ret = "VERIFIED_NOTYET";
122 }
123 if (numeric == VERIFIED_OK) {
124 ret = "VERIFIED_OK";
125 }
126 if (numeric == VERIFIED_REJECTED) {
127 ret = "VERIFIED_REJECTED";
128 }
129 ret += "\n" + detailMessage + "\n";
130 return ret;
131 }
132 }