Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
adonovan | ec1cdc9 | 2020-08-07 08:15:51 -0700 | [diff] [blame] | 14 | package com.google.devtools.build.lib.analysis; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 15 | |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 16 | import com.google.common.base.Preconditions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 17 | import com.google.common.collect.ImmutableList; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 18 | import java.util.ArrayList; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 19 | import java.util.Collection; |
Ulf Adams | 07dba94 | 2015-03-05 14:47:37 +0000 | [diff] [blame] | 20 | import java.util.Collections; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 21 | import java.util.List; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 22 | import javax.annotation.Nullable; |
| 23 | |
| 24 | /** |
| 25 | * Checked exception for analysis-time errors, which can store the errors for later reporting. |
| 26 | * |
| 27 | * <p>It's more robust for a method to throw this exception than expecting a |
| 28 | * {@link RuleErrorConsumer} object (which may be null). |
| 29 | */ |
| 30 | public final class AnalysisIssues extends Exception { |
| 31 | |
| 32 | /** |
| 33 | * An error entry. |
| 34 | * |
| 35 | * <p>{@link AnalysisIssues} can accumulate multiple of these, and report all of them at once. |
| 36 | */ |
| 37 | public static final class Entry { |
| 38 | private final String attribute; |
| 39 | private final String messageTemplate; |
| 40 | private final Object[] arguments; |
| 41 | |
| 42 | private Entry(@Nullable String attribute, String messageTemplate, Object... arguments) { |
| 43 | this.attribute = attribute; |
| 44 | this.messageTemplate = messageTemplate; |
| 45 | this.arguments = arguments; |
| 46 | } |
| 47 | |
| 48 | private void reportTo(RuleErrorConsumer errors) { |
| 49 | String msg = String.format(messageTemplate, arguments); |
| 50 | if (attribute == null) { |
| 51 | errors.ruleError(msg); |
| 52 | } else { |
| 53 | errors.attributeError(attribute, msg); |
| 54 | } |
| 55 | } |
| 56 | |
Laszlo Csomor | fbdfa61 | 2016-01-22 10:03:19 +0000 | [diff] [blame] | 57 | private void reportTo(StringBuilder sb) { |
| 58 | String msg = String.format(messageTemplate, arguments); |
| 59 | if (attribute == null) { |
| 60 | sb.append("ERROR: ").append(msg); |
| 61 | } else { |
| 62 | sb.append("ERROR: in attribute \"").append(attribute).append("\": ").append(msg); |
| 63 | } |
| 64 | } |
| 65 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 66 | @Override |
| 67 | public String toString() { |
| 68 | if (attribute == null) { |
| 69 | return String.format("ERROR: " + messageTemplate, arguments); |
| 70 | } else { |
| 71 | List<Object> args = new ArrayList<>(); |
| 72 | args.add(attribute); |
Ulf Adams | 07dba94 | 2015-03-05 14:47:37 +0000 | [diff] [blame] | 73 | Collections.addAll(args, arguments); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 74 | return String.format("ERROR in '%s': " + messageTemplate, args.toArray()); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | private final ImmutableList<Entry> entries; |
| 80 | |
| 81 | public AnalysisIssues(Entry entry) { |
| 82 | this.entries = ImmutableList.of(Preconditions.checkNotNull(entry)); |
| 83 | } |
| 84 | |
| 85 | public AnalysisIssues(Collection<Entry> entries) { |
| 86 | this.entries = ImmutableList.copyOf(Preconditions.checkNotNull(entries)); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Creates a attribute error entry that will be added to a {@link AnalysisIssues} later. |
| 91 | */ |
| 92 | public static Entry attributeError(String attribute, String messageTemplate, |
| 93 | Object... arguments) { |
| 94 | return new Entry(attribute, messageTemplate, arguments); |
| 95 | } |
| 96 | |
| 97 | public static Entry ruleError(String messageTemplate, Object... arguments) { |
| 98 | return new Entry(null, messageTemplate, arguments); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Report all accumulated errors and warnings to the given consumer object. |
| 103 | */ |
| 104 | public void reportTo(RuleErrorConsumer errors) { |
| 105 | Preconditions.checkNotNull(errors); |
| 106 | for (Entry e : entries) { |
| 107 | e.reportTo(errors); |
| 108 | } |
| 109 | } |
| 110 | |
Laszlo Csomor | fbdfa61 | 2016-01-22 10:03:19 +0000 | [diff] [blame] | 111 | @Nullable |
| 112 | private String asString() { |
| 113 | if (entries == null) { |
| 114 | return null; |
| 115 | } |
| 116 | |
| 117 | StringBuilder sb = new StringBuilder(); |
| 118 | for (Entry e : entries) { |
| 119 | e.reportTo(sb); |
| 120 | } |
| 121 | return sb.toString(); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public String getMessage() { |
| 126 | return asString(); |
| 127 | } |
| 128 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 129 | @Override |
| 130 | public String toString() { |
Laszlo Csomor | fbdfa61 | 2016-01-22 10:03:19 +0000 | [diff] [blame] | 131 | String s = asString(); |
| 132 | return s == null ? "" : s; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 133 | } |
| 134 | } |