blob: 4f7dcac4e794eb38e47086b006381a8cd07f7a80 [file] [log] [blame]
Neel Mehta4b6f4392019-05-09 16:03:47 -07001/*
2 * Copyright (C) 2019 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#include "AST.h"
18#include "Interface.h"
19#include "Method.h"
20
21namespace android {
22void AST::generateJavaImpl(Formatter& out) const {
23 if (!AST::isInterface()) {
24 // types.hal does not get a stub header.
25 return;
26 }
27
28 const Interface* iface = mRootScope.getInterface();
29 const std::string baseName = iface->getBaseName();
30
31 out << "// FIXME: your file license if you have one\n\n";
32 out << "// FIXME: add package information\n\n";
33
34 out << "import " << mPackage.javaPackage() << "." << iface->localName() << ";\n\n";
35
Neel Mehta4b6f4392019-05-09 16:03:47 -070036 out << "class " << baseName << " extends " << iface->localName() << ".Stub"
37 << " {\n";
38
39 out.indent([&] {
40 const Interface* prevInterface = nullptr;
41 for (const auto& tuple : iface->allMethodsFromRoot()) {
42 const Method* method = tuple.method();
43
44 if (method->isHidlReserved()) {
45 continue;
46 }
47
48 const Interface* superInterface = tuple.interface();
49 if (prevInterface != superInterface) {
50 out << "// Methods from " << superInterface->fullJavaName() << " follow.\n";
51 prevInterface = superInterface;
52 }
53
Neel Mehta4b6f4392019-05-09 16:03:47 -070054 out << "@Override\npublic ";
Neel Mehta5b447c02019-05-23 16:12:24 -070055 method->emitJavaSignature(out);
Neel Mehta4b6f4392019-05-09 16:03:47 -070056
Neel Mehta5b447c02019-05-23 16:12:24 -070057 out << "\n";
Neel Mehta4b6f4392019-05-09 16:03:47 -070058 out.indent([&] {
59 out.indent();
60 out << "throws android.os.RemoteException {\n";
61 out.unindent();
62
63 out << "// TODO: Implement\n";
64
65 // Return the appropriate value
Neel Mehta5b447c02019-05-23 16:12:24 -070066 const bool returnsValue = !method->results().empty();
Neel Mehta4b6f4392019-05-09 16:03:47 -070067 if (returnsValue) {
68 for (const auto& arg : method->results()) {
69 arg->type().emitJavaFieldInitializer(out, arg->name());
70 }
71
Neel Mehta5b447c02019-05-23 16:12:24 -070072 const bool needsCallback = method->results().size() > 1;
Neel Mehta4b6f4392019-05-09 16:03:47 -070073 if (needsCallback) {
74 out << "_hidl_cb.onValues(";
75
76 out.join(method->results().begin(), method->results().end(), ", ",
77 [&](auto& arg) { out << arg->name(); });
78
79 out << ");\n";
80 } else {
81 // only 1 value is returned
82 out << "return " << method->results().at(0)->name() << ";\n";
83 }
84 }
85 });
86
87 out << "}\n\n";
88 }
89 });
90
91 out << "}\n";
92}
93} // namespace android