|
| 1 | +/* |
| 2 | + * Copyright 2020 The Closure Compiler Authors. |
| 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.google.javascript.jscomp.instrumentation.reporter; |
| 18 | + |
| 19 | +import static com.google.common.base.Preconditions.checkState; |
| 20 | + |
| 21 | +import com.google.auto.value.AutoValue; |
| 22 | +import com.google.common.annotations.GwtIncompatible; |
| 23 | +import com.google.common.base.Splitter; |
| 24 | +import com.google.common.collect.ImmutableList; |
| 25 | +import com.google.common.collect.ImmutableMap; |
| 26 | +import com.google.debugging.sourcemap.Base64VLQ; |
| 27 | +import com.google.gson.Gson; |
| 28 | +import com.google.gson.GsonBuilder; |
| 29 | +import com.google.gson.reflect.TypeToken; |
| 30 | +import com.google.javascript.jscomp.instrumentation.reporter.ProductionInstrumentationReporter.InstrumentationType; |
| 31 | +import java.io.IOException; |
| 32 | +import java.lang.reflect.Type; |
| 33 | +import java.util.ArrayList; |
| 34 | +import java.util.LinkedHashMap; |
| 35 | +import java.util.List; |
| 36 | +import java.util.Map; |
| 37 | +import java.util.function.Predicate; |
| 38 | + |
| 39 | +/** |
| 40 | + * The class contains the data which is provided by the Production Instrumentation pass in the |
| 41 | + * closure compiler. It will read the data from the file name created with the |
| 42 | + * --instrument_mapping_report and is required to follow the format used by Closure Compiler class |
| 43 | + * VariableMap. |
| 44 | + */ |
| 45 | +@GwtIncompatible |
| 46 | +final class InstrumentationMapping { |
| 47 | + |
| 48 | + final ImmutableList<String> fileNames; |
| 49 | + final ImmutableMap<String, Location> parameterMapping; |
| 50 | + |
| 51 | + private InstrumentationMapping( |
| 52 | + ImmutableList<String> fileNames, ImmutableMap<String, Location> parameterMapping) { |
| 53 | + this.fileNames = fileNames; |
| 54 | + this.parameterMapping = parameterMapping; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Parses the file found at location mappingFilePath and populates the properties of this class |
| 59 | + */ |
| 60 | + public static InstrumentationMapping parse(String mappingFilePath) throws IOException { |
| 61 | + |
| 62 | + Map<String, Location> localParameterMapping = new LinkedHashMap<>(); |
| 63 | + |
| 64 | + String instrumentationMappingFile = ProductionInstrumentationReporter.readFile(mappingFilePath); |
| 65 | + List<String> linesOfFile = |
| 66 | + Splitter.on('\n').omitEmptyStrings().splitToList(instrumentationMappingFile); |
| 67 | + |
| 68 | + // Report should contain at least three lines |
| 69 | + checkState(linesOfFile.size() >= 3, "Malformed report %s", linesOfFile); |
| 70 | + |
| 71 | + String fileNamesLine = linesOfFile.get(0).trim(); |
| 72 | + String functionNamesLine = linesOfFile.get(1).trim(); |
| 73 | + String typesLine = linesOfFile.get(2).trim(); |
| 74 | + |
| 75 | + checkState(fileNamesLine.startsWith("FileNames:")); |
| 76 | + checkState(functionNamesLine.startsWith("FunctionNames:")); |
| 77 | + checkState(typesLine.startsWith("Types:")); |
| 78 | + |
| 79 | + String fileNamesAsJsonArray = fileNamesLine.substring(fileNamesLine.indexOf(":") + 1); |
| 80 | + String functionNamesAsJsonArray = |
| 81 | + functionNamesLine.substring(functionNamesLine.indexOf(":") + 1); |
| 82 | + String typesAsJsonArray = typesLine.substring(typesLine.indexOf(":") + 1); |
| 83 | + |
| 84 | + Type stringListType = new TypeToken<List<String>>() {}.getType(); |
| 85 | + |
| 86 | + Gson gson = new GsonBuilder().disableHtmlEscaping().create(); |
| 87 | + List<String> localFileNames = gson.fromJson(fileNamesAsJsonArray, stringListType); |
| 88 | + List<String> functionNames = gson.fromJson(functionNamesAsJsonArray, stringListType); |
| 89 | + |
| 90 | + List<String> typesAsStringList = gson.fromJson(typesAsJsonArray, stringListType); |
| 91 | + List<InstrumentationType> types = InstrumentationType.convertFromStringList(typesAsStringList); |
| 92 | + |
| 93 | + for (int i = 3; i < linesOfFile.size(); ++i) { |
| 94 | + String lineItem = linesOfFile.get(i); |
| 95 | + String id = lineItem.substring(0, lineItem.indexOf(':')); |
| 96 | + String encodedDetails = lineItem.substring(lineItem.indexOf(':') + 1); |
| 97 | + |
| 98 | + StringCharIterator encodedDetailsAsCharIt = new StringCharIterator(encodedDetails); |
| 99 | + |
| 100 | + Location temp = |
| 101 | + Location.create( |
| 102 | + localFileNames.get(Base64VLQ.decode(encodedDetailsAsCharIt)), |
| 103 | + functionNames.get(Base64VLQ.decode(encodedDetailsAsCharIt)), |
| 104 | + types.get(Base64VLQ.decode(encodedDetailsAsCharIt)), |
| 105 | + Base64VLQ.decode(encodedDetailsAsCharIt), |
| 106 | + Base64VLQ.decode(encodedDetailsAsCharIt)); |
| 107 | + localParameterMapping.putIfAbsent(id, temp); |
| 108 | + } |
| 109 | + |
| 110 | + return new InstrumentationMapping( |
| 111 | + ImmutableList.copyOf(localFileNames), ImmutableMap.copyOf(localParameterMapping)); |
| 112 | + } |
| 113 | + |
| 114 | + public String getFileName(String id) { |
| 115 | + return parameterMapping.get(id).fileName(); |
| 116 | + } |
| 117 | + |
| 118 | + public String getFunctionName(String id) { |
| 119 | + return parameterMapping.get(id).functionName(); |
| 120 | + } |
| 121 | + |
| 122 | + public InstrumentationType getType(String id) { |
| 123 | + return parameterMapping.get(id).type(); |
| 124 | + } |
| 125 | + |
| 126 | + public int getLineNo(String id) { |
| 127 | + return parameterMapping.get(id).lineNo(); |
| 128 | + } |
| 129 | + |
| 130 | + public int getColNo(String id) { |
| 131 | + return parameterMapping.get(id).colNo(); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * This function will return a list of unique parameters which match the criteria specified by the |
| 136 | + * predicate. As an example, it can return a list of all unique parameters which match a specific |
| 137 | + * file name. |
| 138 | + */ |
| 139 | + public List<String> getAllMatchingValues(Predicate<String> comparisonPredicate) { |
| 140 | + List<String> result = new ArrayList<>(); |
| 141 | + for (String key : parameterMapping.keySet()) { |
| 142 | + if (comparisonPredicate.test(key)) { |
| 143 | + result.add(key); |
| 144 | + } |
| 145 | + } |
| 146 | + return result; |
| 147 | + } |
| 148 | + |
| 149 | + @AutoValue |
| 150 | + abstract static class Location { |
| 151 | + |
| 152 | + static Location create(String a, String b, InstrumentationType c, int d, int e) { |
| 153 | + return new AutoValue_InstrumentationMapping_Location(a, b, c, d, e); |
| 154 | + } |
| 155 | + |
| 156 | + abstract String fileName(); |
| 157 | + |
| 158 | + abstract String functionName(); |
| 159 | + |
| 160 | + abstract InstrumentationType type(); |
| 161 | + |
| 162 | + abstract int lineNo(); |
| 163 | + |
| 164 | + abstract int colNo(); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * A implementation of the Base64VLQ CharIterator used for decoding the mappings encoded in the |
| 169 | + * JSON string. |
| 170 | + */ |
| 171 | + private static class StringCharIterator implements Base64VLQ.CharIterator { |
| 172 | + |
| 173 | + final String content; |
| 174 | + final int length; |
| 175 | + int current = 0; |
| 176 | + |
| 177 | + StringCharIterator(String content) { |
| 178 | + this.content = content; |
| 179 | + this.length = content.length(); |
| 180 | + } |
| 181 | + |
| 182 | + @Override |
| 183 | + public char next() { |
| 184 | + return content.charAt(current++); |
| 185 | + } |
| 186 | + |
| 187 | + @Override |
| 188 | + public boolean hasNext() { |
| 189 | + return current < length; |
| 190 | + } |
| 191 | + } |
| 192 | +} |
0 commit comments