aboutsummaryrefslogtreecommitdiff
path: root/tools/product_config/src/com/android/build/config/ErrorReporter.java
blob: 0a0c9f4a842b63409a264c81dff26061697ccdca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.build.config;

import java.lang.reflect.Field;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Base class for reporting errors.
 */
public class ErrorReporter {
    /**
     * List of Entries that have occurred.
     */
    // Also used as the lock for this object.
    private final ArrayList<Entry> mEntries = new ArrayList();

    /**
     * The categories that are for this Errors object.
     */
    private Map<Integer, Category> mCategories;

    /**
     * Whether there has been a warning or an error yet.
     */
    private boolean mHadWarningOrError;

    /**
     * Whether there has been an error yet.
     */
    private boolean mHadError;

    public static class FatalException extends RuntimeException {
        FatalException(String message) {
            super(message);
        }

        FatalException(String message, Throwable chain) {
            super(message, chain);
        }
    }

    /**
     * Whether errors are errors, warnings or hidden.
     */
    public static enum Level {
        HIDDEN("hidden"),
        WARNING("warning"),
        ERROR("error");

        private final String mLabel;

        Level(String label) {
            mLabel = label;
        }

        String getLabel() {
            return mLabel;
        }
    }

    /**
     * The available error codes.
     */
    public class Category {
        private final int mCode;
        private boolean mIsLevelSettable;
        private Level mLevel;
        private String mHelp;

        /**
         * Construct a Category object.
         */
        public Category(int code, boolean isLevelSettable, Level level, String help) {
            if (!isLevelSettable && level != Level.ERROR) {
                throw new RuntimeException("Don't have WARNING or HIDDEN without isLevelSettable");
            }
            mCode = code;
            mIsLevelSettable = isLevelSettable;
            mLevel = level;
            mHelp = help;
        }

        /**
         * Get the numeric code for the Category, which can be used to set the level.
         */
        public int getCode() {
            return mCode;
        }

        /**
         * Get whether the level of this Category can be changed.
         */
        public boolean isLevelSettable() {
            return mIsLevelSettable;
        }

        /**
         * Set the level of this category.
         */
        public void setLevel(Level level) {
            if (!mIsLevelSettable) {
                throw new RuntimeException("Can't set level for error " + mCode);
            }
            mLevel = level;
        }

        /**
         * Return the level, including any overrides.
         */
        public Level getLevel() {
            return mLevel;
        }

        /**
         * Return the category's help text.
         */
        public String getHelp() {
            return mHelp;
        }

        /**
         * Add an error with no source position.
         */
        public void add(String message) {
            ErrorReporter.this.add(this, false, new Position(), message);
        }

        /**
         * Add an error.
         */
        public void add(Position pos, String message) {
            ErrorReporter.this.add(this, false, pos, message);
        }

        /**
         * Add an error with no source position, and throw a FatalException, stopping processing
         * immediately.
         */
        public void fatal(String message) {
            ErrorReporter.this.add(this, true, new Position(), message);
        }

        /**
         * Add an error, and throw a FatalException, stopping processing immediately.
         */
        public void fatal(Position pos, String message) {
            ErrorReporter.this.add(this, true, pos, message);
        }
    }

    /**
     * An instance of an error happening.
     */
    public static class Entry {
        private final Category mCategory;
        private final Position mPosition;
        private final String mMessage;

        Entry(Category category, Position position, String message) {
            mCategory = category;
            mPosition = position;
            mMessage = message;
        }

        public Category getCategory() {
            return mCategory;
        }

        public Position getPosition() {
            return mPosition;
        }

        public String getMessage() {
            return mMessage;
        }

        @Override
        public String toString() {
            return mPosition
                    + "[" + mCategory.getLevel().getLabel() + " " + mCategory.getCode() + "] "
                    + mMessage;
        }
    }

    private void initLocked() {
        if (mCategories == null) {
            HashMap<Integer, Category> categories = new HashMap();
            for (Field field: getClass().getFields()) {
                if (Category.class.isAssignableFrom(field.getType())) {
                    Category category = null;
                    try {
                        category = (Category)field.get(this);
                    } catch (IllegalAccessException ex) {
                        // Wrap and rethrow, this is always on this class, so it's
                        // our programming error if this happens.
                        throw new RuntimeException("Categories on Errors should be public.", ex);
                    }
                    Category prev = categories.put(category.getCode(), category);
                    if (prev != null) {
                        throw new RuntimeException("Duplicate categories with code "
                                + category.getCode());
                    }
                }
            }
            mCategories = Collections.unmodifiableMap(categories);
        }
    }

    /**
     * Returns a map of the category codes to the categories.
     */
    public Map<Integer, Category> getCategories() {
        synchronized (mEntries) {
            initLocked();
            return mCategories;
        }
    }

    /**
     * Add an error.
     */
    private void add(Category category, boolean fatal, Position pos, String message) {
        synchronized (mEntries) {
            initLocked();
            if (mCategories.get(category.getCode()) != category) {
                throw new RuntimeException("Errors.Category used from the wrong Errors object.");
            }
            final Entry entry = new Entry(category, pos, message);
            mEntries.add(entry);
            final Level level = category.getLevel();
            if (level == Level.WARNING || level == Level.ERROR) {
                mHadWarningOrError = true;
            }
            if (level == Level.ERROR) {
                mHadError = true;
            }
            if (fatal) {
                throw new FatalException(entry.toString());
            }
        }
    }

    /**
     * Returns whether there has been a warning or an error yet.
     */
    public boolean hadWarningOrError() {
        synchronized (mEntries) {
            return mHadWarningOrError;
        }
    }

    /**
     * Returns whether there has been an error yet.
     */
    public boolean hadError() {
        synchronized (mEntries) {
            return mHadError;
        }
    }

    /**
     * Returns a list of all entries that were added.
     */
    public List<Entry> getEntries() {
        synchronized (mEntries) {
            return new ArrayList<Entry>(mEntries);
        }
    }

    /**
     * Prints the errors.
     */
    public void printErrors(PrintStream out) {
        synchronized (mEntries) {
            for (Entry entry: mEntries) {
                if (entry.getCategory().getLevel() == Level.HIDDEN) {
                    continue;
                }
                out.println(entry.toString());
            }
        }
    }
}