blob: f217afb16f322789cc2a1d78cf7ef5185ba61aaf [file] [log] [blame]
Adam Lesinskica5638f2015-10-21 14:42:43 -07001/*
2 * Copyright (C) 2015 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
17#ifndef AAPT_JAVA_ANNOTATIONPROCESSOR_H
18#define AAPT_JAVA_ANNOTATIONPROCESSOR_H
19
Adam Lesinskib274e352015-11-06 15:14:35 -080020#include <sstream>
Adam Lesinskica5638f2015-10-21 14:42:43 -070021#include <string>
Narayan Kamath1c1544f2020-02-13 14:33:47 +000022#include <unordered_map>
Adam Lesinskica5638f2015-10-21 14:42:43 -070023
Adam Lesinskid5083f62017-01-16 15:07:21 -080024#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070025
Adam Lesinskia693c4a2017-11-09 11:29:39 -080026#include "text/Printer.h"
27
Adam Lesinskica5638f2015-10-21 14:42:43 -070028namespace aapt {
29
Adam Lesinski09f4d702017-08-08 10:39:55 -070030// Builds a JavaDoc comment from a set of XML comments.
31// This will also look for instances of @SystemApi and convert them to
32// actual Java annotations.
33//
34// Example:
35//
36// Input XML:
37//
38// <!-- This is meant to be hidden because
39// It is system api. Also it is @deprecated
40// @SystemApi
41// -->
42//
43// Output JavaDoc:
44//
45// /**
46// * This is meant to be hidden because
47// * It is system api. Also it is @deprecated
48// */
49//
50// Output Annotations:
51//
52// @Deprecated
53// @android.annotation.SystemApi
Adam Lesinskica5638f2015-10-21 14:42:43 -070054class AnnotationProcessor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 public:
Adam Lesinski09f4d702017-08-08 10:39:55 -070056 // Extracts the first sentence of a comment. The algorithm selects the substring starting from
57 // the beginning of the string, and ending at the first '.' character that is followed by a
58 // whitespace character. If these requirements are not met, the whole string is returned.
Adam Lesinskie967d3f2017-07-24 18:19:36 -070059 static android::StringPiece ExtractFirstSentence(const android::StringPiece& comment);
60
Adam Lesinski09f4d702017-08-08 10:39:55 -070061 // Adds more comments. Resources can have value definitions for various configurations, and
62 // each of the definitions may have comments that need to be processed.
Adam Lesinskid5083f62017-01-16 15:07:21 -080063 void AppendComment(const android::StringPiece& comment);
Adam Lesinskica5638f2015-10-21 14:42:43 -070064
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065 void AppendNewLine();
Adam Lesinski76565542016-03-10 21:55:04 -080066
Adam Lesinskia693c4a2017-11-09 11:29:39 -080067 // Writes the comments and annotations to the Printer.
Makoto Onukide6e6f22020-06-22 10:17:02 -070068 void Print(text::Printer* printer, bool strip_api_annotations = false) const;
Adam Lesinskica5638f2015-10-21 14:42:43 -070069
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071 std::stringstream comment_;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 std::stringstream mAnnotations;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073 bool has_comments_ = false;
Narayan Kamath1c1544f2020-02-13 14:33:47 +000074 std::unordered_map<uint32_t, std::string> annotation_parameter_map_;
Adam Lesinskica5638f2015-10-21 14:42:43 -070075
Adam Lesinski09f4d702017-08-08 10:39:55 -070076 void AppendCommentLine(std::string line);
Adam Lesinskica5638f2015-10-21 14:42:43 -070077};
78
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079} // namespace aapt
Adam Lesinskica5638f2015-10-21 14:42:43 -070080
81#endif /* AAPT_JAVA_ANNOTATIONPROCESSOR_H */