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 #pragma once 17 18 #include "aidl_language.h" 19 #include "aidl_typenames.h" 20 21 #include <map> 22 #include <memory> 23 #include <set> 24 #include <sstream> 25 #include <string> 26 #include <utility> 27 #include <vector> 28 29 namespace android { 30 namespace aidl { 31 namespace java { 32 33 using std::map; 34 using std::pair; 35 using std::set; 36 using std::string; 37 using std::unique_ptr; 38 using std::vector; 39 40 // This header provides functions that translate AIDL things to Java things. 41 42 std::string ConstantValueDecorator( 43 const AidlTypeSpecifier& type, 44 const std::variant<std::string, std::vector<std::string>>& raw_value); 45 46 // Returns the Java type signature of the AIDL type spec 47 // This includes generic type parameters with array modifiers. 48 string JavaSignatureOf(const AidlTypeSpecifier& aidl); 49 50 // Returns the Java boxing type of the AIDL type spec. 51 // aidl type should be a primitive type. 52 string JavaBoxingTypeOf(const AidlTypeSpecifier& aidl); 53 54 // Returns the instantiable Jva type signature of the AIDL type spec 55 // This includes generic type parameters, but excludes array modifiers. 56 string InstantiableJavaSignatureOf(const AidlTypeSpecifier& aidl); 57 58 // Returns the default Java value of the AIDL type spec 59 string DefaultJavaValueOf(const AidlTypeSpecifier& aidl); 60 61 // This carries information that is required to generate code for 62 // marshalling and unmarshalling a method argument or a parcelable field 63 struct CodeGeneratorContext { 64 CodeWriter& writer; // CodeWriter::Write() is mutable 65 const AidlTypenames& typenames; 66 const AidlTypeSpecifier& type; 67 const string parcel; 68 const string var; 69 const uint32_t min_sdk_version; 70 // Set PARCELABLE_WRITE_RETURN_VALUE when the marshalled data will be returned to the client. 71 // This is given as a hint to the Parcelable that is being marshalled 72 // so that the Parcelable can release its resource after the marshalling 73 // is done. 74 const string write_to_parcel_flag; 75 76 // Most of the variables created by AIDL compiler are typed, i.e., the code 77 // knows exactly what type of object is in the parcel -- because the parcel 78 // itself is created by the code generated by AIDL compiler. 79 // 80 // However, for some collection types (List and Map for now), we write the 81 // elements in them untyped (object is flattened along with its type name) 82 // as the AIDL compiler does not know the type of the contained elements. 83 // So, when unmarshalling such collection, we need to provide a classloader 84 // from where the parcel can reflectively find a class object for 85 // the contained element. 86 // 87 // This field is a pointer to a boolean state variable that indicates whether 88 // the code for declaring and getting the classloader has been emitted or not. 89 // We emit the code at most once per an AIDL method, otherwise we are wasting 90 // time doing the same thing multiple time. 91 bool* const is_classloader_created; 92 }; 93 94 // Writes code fragment that writes a variable to the parcel. 95 void WriteToParcelFor(const CodeGeneratorContext& c); 96 97 // Writes code fragment that reads data from the parcel into a variable. When 98 // the variable type is array or List, the array or List is created. 99 bool CreateFromParcelFor(const CodeGeneratorContext& c); 100 101 // Writes code fragment that reads data from the parcel into an existing 102 // array or a List. 103 bool ReadFromParcelFor(const CodeGeneratorContext& c); 104 105 // Writes an expression that returns the string representation of a field 106 // in a parcelable 107 void ToStringFor(const CodeGeneratorContext& c); 108 109 // Generates create/read/write helper functions which are missing in Parcel. 110 void GenerateParcelHelpers(CodeWriter& out, const AidlDefinedType& defined_type, 111 const AidlTypenames& typenames, const Options& options); 112 113 } // namespace java 114 } // namespace aidl 115 } // namespace android 116