| // Copyright (c) 2019, the R8 project authors. Please see the AUTHORS file |
| // for details. All rights reserved. Use of this source code is governed by a |
| // BSD-style license that can be found in the LICENSE file. |
| |
| package com.android.tools.r8.naming.mappinginformation; |
| |
| import com.android.tools.r8.DiagnosticsHandler; |
| import com.android.tools.r8.naming.MapVersion; |
| import com.android.tools.r8.naming.mappinginformation.ScopedMappingInformation.ScopeReference; |
| import com.google.gson.JsonElement; |
| import com.google.gson.JsonObject; |
| |
| public abstract class MappingInformation { |
| |
| static final int NO_LINE_NUMBER = -1; |
| |
| public static final String MAPPING_ID_KEY = "id"; |
| |
| private final int lineNumber; |
| |
| MappingInformation(int lineNumber) { |
| this.lineNumber = lineNumber; |
| } |
| |
| public int getLineNumber() { |
| return lineNumber; |
| } |
| |
| public abstract String serialize(); |
| |
| public boolean isScopedMappingInformation() { |
| return false; |
| } |
| |
| public ScopedMappingInformation asScopedMappingInformation() { |
| return null; |
| } |
| |
| public boolean isMetaInfMappingInformation() { |
| return false; |
| } |
| |
| public MetaInfMappingInformation asMetaInfMappingInformation() { |
| return null; |
| } |
| |
| public boolean isFileNameInformation() { |
| return false; |
| } |
| |
| public FileNameInformation asFileNameInformation() { |
| return null; |
| } |
| |
| public boolean isCompilerSynthesizedMappingInformation() { |
| return false; |
| } |
| |
| public CompilerSynthesizedMappingInformation asCompilerSynthesizedMappingInformation() { |
| return null; |
| } |
| |
| public abstract boolean allowOther(MappingInformation information); |
| |
| public static MappingInformation fromJsonObject( |
| MapVersion version, |
| JsonObject object, |
| DiagnosticsHandler diagnosticsHandler, |
| int lineNumber, |
| ScopeReference implicitSingletonScope) { |
| if (object == null) { |
| diagnosticsHandler.info(MappingInformationDiagnostics.notValidJson(lineNumber)); |
| return null; |
| } |
| JsonElement id = object.get(MAPPING_ID_KEY); |
| if (id == null) { |
| diagnosticsHandler.info( |
| MappingInformationDiagnostics.noKeyInJson(lineNumber, MAPPING_ID_KEY)); |
| return null; |
| } |
| String idString = id.getAsString(); |
| if (idString == null) { |
| diagnosticsHandler.info( |
| MappingInformationDiagnostics.notValidString(lineNumber, MAPPING_ID_KEY)); |
| return null; |
| } |
| return deserialize( |
| idString, version, object, diagnosticsHandler, lineNumber, implicitSingletonScope); |
| } |
| |
| private static MappingInformation deserialize( |
| String id, |
| MapVersion version, |
| JsonObject object, |
| DiagnosticsHandler diagnosticsHandler, |
| int lineNumber, |
| ScopeReference implicitSingletonScope) { |
| switch (id) { |
| case MetaInfMappingInformation.ID: |
| return MetaInfMappingInformation.deserialize( |
| version, object, diagnosticsHandler, lineNumber); |
| case FileNameInformation.ID: |
| return FileNameInformation.build(version, object, diagnosticsHandler, lineNumber); |
| case CompilerSynthesizedMappingInformation.ID: |
| return CompilerSynthesizedMappingInformation.deserialize( |
| version, object, diagnosticsHandler, lineNumber, implicitSingletonScope); |
| default: |
| diagnosticsHandler.info(MappingInformationDiagnostics.noHandlerFor(lineNumber, id)); |
| return null; |
| } |
| } |
| |
| static JsonElement getJsonElementFromObject( |
| JsonObject object, |
| DiagnosticsHandler diagnosticsHandler, |
| int lineNumber, |
| String key, |
| String id) { |
| JsonElement element = object.get(key); |
| if (element == null) { |
| diagnosticsHandler.info( |
| MappingInformationDiagnostics.noKeyForObjectWithId(lineNumber, key, MAPPING_ID_KEY, id)); |
| } |
| return element; |
| } |
| } |