1 /* 2 * Copyright (C) 2018 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 package com.google.currysrc.aosp; 17 18 import com.google.currysrc.api.process.Processor; 19 import com.google.currysrc.api.process.ast.BodyDeclarationLocators; 20 import com.google.currysrc.processors.AddAnnotation; 21 import com.google.currysrc.processors.AnnotationInfo.AnnotationClass; 22 import java.io.IOException; 23 import java.nio.file.Path; 24 25 /** 26 * Utility methods for manipulating AOSP annotations. 27 */ 28 public final class Annotations { 29 Annotations()30 private Annotations() { 31 } 32 33 /** 34 * Add dalvik.annotation.compat.UnsupportedAppUsage annotations to the body declarations 35 * specified in the supplied file. 36 * 37 * <p>The supplied file is a JSON file consisting of a top level array containing objects of the 38 * following structure: 39 * <pre>{@code 40 * { 41 * "@location": "<body declaration location>", 42 * "maxTargetSdk": <int>|<placeholder>, 43 * "trackingBug": <long>|<placeholder>, 44 * "expectedSignature": <string>, 45 * } 46 * }</pre> 47 * 48 * <p>Where: 49 * <ul> 50 * <li>{@code <body declaration location>} is in the format expected by 51 * {@link BodyDeclarationLocators#fromStringForm(String)}.</li> 52 * <li>{@code <int>} and {@code <long>} are numbers that are inserted into the source as literal 53 * primitive values.</li> 54 * <li>{@code <string>} is a quoted JSON string that is inserted into the source as a literal 55 * string.</li> 56 * <li>{@code <placeholder>} is a quoted JSON string that is inserted into the source as if it 57 * was a constant expression. It is used to reference constants in annotation values, e.g. 58 * {@code dalvik.annotation.compat.UnsupportedAppUsage.VERSION_CODES.P}.</li> 59 * </ul> 60 * 61 * <p>See external/icu/tools/srcgen/unsupported-app-usage.json for an example. 62 * 63 * @param unsupportedAppUsagePath the location of the file. 64 * @return the {@link Processor}. 65 */ addUnsupportedAppUsage(Path unsupportedAppUsagePath)66 public static AddAnnotation addUnsupportedAppUsage(Path unsupportedAppUsagePath) { 67 AnnotationClass annotationClass = new AnnotationClass( 68 "dalvik.annotation.compat.UnsupportedAppUsage") 69 .addProperty("maxTargetSdk", int.class) 70 .addProperty("trackingBug", long.class) 71 .addProperty("implicitMember", String.class) 72 .addProperty("expectedSignature", String.class); 73 try { 74 return AddAnnotation.fromJsonFile(annotationClass, unsupportedAppUsagePath); 75 } catch (IOException e) { 76 throw new IllegalStateException("Could not read JSON from " + unsupportedAppUsagePath, e); 77 } 78 } 79 } 80