Home | History | Annotate | Download | only in java
      1 /*
      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_CLASS_GENERATOR_H
     18 #define AAPT_JAVA_CLASS_GENERATOR_H
     19 
     20 #include "ResourceTable.h"
     21 #include "ResourceValues.h"
     22 #include "process/IResourceTableConsumer.h"
     23 #include "util/StringPiece.h"
     24 
     25 #include <ostream>
     26 #include <string>
     27 
     28 namespace aapt {
     29 
     30 class AnnotationProcessor;
     31 class ClassDefinition;
     32 
     33 struct JavaClassGeneratorOptions {
     34     /*
     35      * Specifies whether to use the 'final' modifier
     36      * on resource entries. Default is true.
     37      */
     38     bool useFinal = true;
     39 
     40     enum class SymbolTypes {
     41         kAll,
     42         kPublicPrivate,
     43         kPublic,
     44     };
     45 
     46     SymbolTypes types = SymbolTypes::kAll;
     47 
     48     /**
     49      * A list of JavaDoc annotations to add to the comments of all generated classes.
     50      */
     51     std::vector<std::string> javadocAnnotations;
     52 };
     53 
     54 /*
     55  * Generates the R.java file for a resource table.
     56  */
     57 class JavaClassGenerator {
     58 public:
     59     JavaClassGenerator(IAaptContext* context, ResourceTable* table,
     60                        const JavaClassGeneratorOptions& options);
     61 
     62     /*
     63      * Writes the R.java file to `out`. Only symbols belonging to `package` are written.
     64      * All symbols technically belong to a single package, but linked libraries will
     65      * have their names mangled, denoting that they came from a different package.
     66      * We need to generate these symbols in a separate file.
     67      * Returns true on success.
     68      */
     69     bool generate(const StringPiece16& packageNameToGenerate, std::ostream* out);
     70 
     71     bool generate(const StringPiece16& packageNameToGenerate,
     72                   const StringPiece16& outputPackageName,
     73                   std::ostream* out);
     74 
     75     const std::string& getError() const;
     76 
     77 private:
     78     bool addMembersToTypeClass(const StringPiece16& packageNameToGenerate,
     79                                const ResourceTablePackage* package,
     80                                const ResourceTableType* type,
     81                                ClassDefinition* outTypeClassDef);
     82 
     83     void addMembersToStyleableClass(const StringPiece16& packageNameToGenerate,
     84                                     const std::u16string& entryName,
     85                                     const Styleable* styleable,
     86                                     ClassDefinition* outStyleableClassDef);
     87 
     88     bool skipSymbol(SymbolState state);
     89 
     90     IAaptContext* mContext;
     91     ResourceTable* mTable;
     92     JavaClassGeneratorOptions mOptions;
     93     std::string mError;
     94 };
     95 
     96 inline const std::string& JavaClassGenerator::getError() const {
     97     return mError;
     98 }
     99 
    100 } // namespace aapt
    101 
    102 #endif // AAPT_JAVA_CLASS_GENERATOR_H
    103