• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
21 namespace android {
generateJavaImpl(Formatter & out) const22 void 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->definedName() << ";\n\n";
35 
36     out << "class " << baseName << " extends " << iface->definedName() << ".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 
54             out << "@Override\npublic ";
55             method->emitJavaSignature(out);
56 
57             out << "\n";
58             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
66                 const bool returnsValue = !method->results().empty();
67                 if (returnsValue) {
68                     for (const auto& arg : method->results()) {
69                         arg->type().emitJavaFieldInitializer(out, arg->name());
70                     }
71 
72                     const bool needsCallback = method->results().size() > 1;
73                     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
94