• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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 
17 package com.android.apicheck;
18 
19 import java.lang.Comparable;
20 import java.util.TreeSet;
21 
22 public class Errors
23 {
24     public static boolean hadError = false;
25     private static boolean warningsAreErrors = false;
26     private static TreeSet<Message> allErrors = new TreeSet<Message>();
27 
28     private static class Message implements Comparable {
29         SourcePositionInfo pos;
30         String msg;
31 
Message(SourcePositionInfo p, String m)32         Message(SourcePositionInfo p, String m) {
33             pos = p;
34             msg = m;
35         }
36 
compareTo(Object o)37         public int compareTo(Object o) {
38             Message that = (Message)o;
39             int r = this.pos.compareTo(that.pos);
40             if (r != 0) return r;
41             return this.msg.compareTo(that.msg);
42         }
43 
44         @Override
toString()45         public String toString() {
46             return this.pos.toString() + this.msg;
47         }
48     }
49 
error(Error error, SourcePositionInfo where, String text)50     public static void error(Error error, SourcePositionInfo where, String text) {
51         if (error.level == HIDDEN) {
52             return;
53         }
54 
55         String which = (!warningsAreErrors && error.level == WARNING) ? " warning " : " error ";
56         String message = which + error.code + ": " + text;
57 
58         if (where == null) {
59             where = new SourcePositionInfo("unknown", 0, 0);
60         }
61 
62         allErrors.add(new Message(where, message));
63 
64         if (error.level == ERROR || (warningsAreErrors && error.level == WARNING)) {
65             hadError = true;
66         }
67     }
68 
printErrors()69     public static void printErrors() {
70         for (Message m: allErrors) {
71             System.err.println(m.toString());
72         }
73     }
74 
75     public static int HIDDEN = 0;
76     public static int WARNING = 1;
77     public static int ERROR = 2;
78 
setWarningsAreErrors(boolean val)79     public static void setWarningsAreErrors(boolean val) {
80         warningsAreErrors = val;
81     }
82 
83     public static class Error {
84         public int code;
85         public int level;
86 
Error(int code, int level)87         public Error(int code, int level)
88         {
89             this.code = code;
90             this.level = level;
91         }
92     }
93 
94     public static Error PARSE_ERROR = new Error(1, ERROR);
95     public static Error ADDED_PACKAGE = new Error(2, WARNING);
96     public static Error ADDED_CLASS = new Error(3, WARNING);
97     public static Error ADDED_METHOD = new Error(4, WARNING);
98     public static Error ADDED_FIELD = new Error(5, WARNING);
99     public static Error ADDED_INTERFACE = new Error(6, WARNING);
100     public static Error REMOVED_PACKAGE = new Error(7, WARNING);
101     public static Error REMOVED_CLASS = new Error(8, WARNING);
102     public static Error REMOVED_METHOD = new Error(9, WARNING);
103     public static Error REMOVED_FIELD = new Error(10, WARNING);
104     public static Error REMOVED_INTERFACE = new Error(11, WARNING);
105     public static Error CHANGED_STATIC = new Error(12, WARNING);
106     public static Error CHANGED_FINAL = new Error(13, WARNING);
107     public static Error CHANGED_TRANSIENT = new Error(14, WARNING);
108     public static Error CHANGED_VOLATILE = new Error(15, WARNING);
109     public static Error CHANGED_TYPE = new Error(16, WARNING);
110     public static Error CHANGED_VALUE = new Error(17, WARNING);
111     public static Error CHANGED_SUPERCLASS = new Error(18, WARNING);
112     public static Error CHANGED_SCOPE = new Error(19, WARNING);
113     public static Error CHANGED_ABSTRACT = new Error(20, WARNING);
114     public static Error CHANGED_THROWS = new Error(21, WARNING);
115     public static Error CHANGED_NATIVE = new Error(22, HIDDEN);
116     public static Error CHANGED_CLASS = new Error(23, WARNING);
117     public static Error CHANGED_DEPRECATED = new Error(24, WARNING);
118     public static Error CHANGED_SYNCHRONIZED = new Error(25, ERROR);
119 
120     public static Error[] ERRORS = {
121         PARSE_ERROR,
122         ADDED_PACKAGE,
123         ADDED_CLASS,
124         ADDED_METHOD,
125         ADDED_FIELD,
126         ADDED_INTERFACE,
127         REMOVED_PACKAGE,
128         REMOVED_CLASS,
129         REMOVED_METHOD,
130         REMOVED_FIELD,
131         REMOVED_INTERFACE,
132         CHANGED_STATIC,
133         CHANGED_FINAL,
134         CHANGED_TRANSIENT,
135         CHANGED_VOLATILE,
136         CHANGED_TYPE,
137         CHANGED_VALUE,
138         CHANGED_SUPERCLASS,
139         CHANGED_SCOPE,
140         CHANGED_ABSTRACT,
141         CHANGED_THROWS,
142         CHANGED_NATIVE,
143         CHANGED_CLASS,
144         CHANGED_DEPRECATED,
145         CHANGED_SYNCHRONIZED,
146         };
147 
setErrorLevel(int code, int level)148     public static boolean setErrorLevel(int code, int level) {
149         for (Error e: ERRORS) {
150             if (e.code == code) {
151                 e.level = level;
152                 return true;
153             }
154         }
155         return false;
156     }
157 }
158