aboutsummaryrefslogtreecommitdiff
path: root/aidl_to_java.h
blob: aa1ee98f05d438fed744efb75d2a6166f63c84f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * Copyright (C) 2018, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#pragma once

#include "aidl_language.h"
#include "aidl_typenames.h"

#include <map>
#include <memory>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

namespace android {
namespace aidl {
namespace java {

using std::map;
using std::pair;
using std::set;
using std::string;
using std::unique_ptr;
using std::vector;

// This header provides functions that translate AIDL things to Java things.

std::string ConstantValueDecorator(
    const AidlTypeSpecifier& type,
    const std::variant<std::string, std::vector<std::string>>& raw_value);

// Returns the Java type signature of the AIDL type spec
// This includes generic type parameters with array modifiers.
string JavaSignatureOf(const AidlTypeSpecifier& aidl);

// Returns the Java boxing type of the AIDL type spec.
// aidl type should be a primitive type.
string JavaBoxingTypeOf(const AidlTypeSpecifier& aidl);

// Returns the instantiable Jva type signature of the AIDL type spec
// This includes generic type parameters, but excludes array modifiers.
string InstantiableJavaSignatureOf(const AidlTypeSpecifier& aidl);

// Returns the default Java value of the AIDL type spec
string DefaultJavaValueOf(const AidlTypeSpecifier& aidl);

// This carries information that is required to generate code for
// marshalling and unmarshalling a method argument or a parcelable field
struct CodeGeneratorContext {
  CodeWriter& writer;  // CodeWriter::Write() is mutable
  const AidlTypenames& typenames;
  const AidlTypeSpecifier& type;
  const string parcel;
  const string var;
  const uint32_t min_sdk_version;
  // Set PARCELABLE_WRITE_RETURN_VALUE when the marshalled data will be returned to the client.
  // This is given as a hint to the Parcelable that is being marshalled
  // so that the Parcelable can release its resource after the marshalling
  // is done.
  const string write_to_parcel_flag;

  // Most of the variables created by AIDL compiler are typed, i.e., the code
  // knows exactly what type of object is in the parcel -- because the parcel
  // itself is created by the code generated by AIDL compiler.
  //
  // However, for some collection types (List and Map for now), we write the
  // elements in them untyped (object is flattened along with its type name)
  // as the AIDL compiler does not know the type of the contained elements.
  // So, when unmarshalling such collection, we need to provide a classloader
  // from where the parcel can reflectively find a class object for
  // the contained element.
  //
  // This field is a pointer to a boolean state variable that indicates whether
  // the code for declaring and getting the classloader has been emitted or not.
  // We emit the code at most once per an AIDL method, otherwise we are wasting
  // time doing the same thing multiple time.
  bool* const is_classloader_created;
};

// Writes code fragment that writes a variable to the parcel.
void WriteToParcelFor(const CodeGeneratorContext& c);

// Writes code fragment that reads data from the parcel into a variable. When
// the variable type is array or List, the array or List is created.
bool CreateFromParcelFor(const CodeGeneratorContext& c);

// Writes code fragment that reads data from the parcel into an existing
// array or a List.
bool ReadFromParcelFor(const CodeGeneratorContext& c);

// Writes an expression that returns the string representation of a field
// in a parcelable
void ToStringFor(const CodeGeneratorContext& c);

// Generates create/read/write helper functions which are missing in Parcel.
void GenerateParcelHelpers(CodeWriter& out, const AidlDefinedType& defined_type,
                           const AidlTypenames& typenames, const Options& options);

}  // namespace java
}  // namespace aidl
}  // namespace android