1 /*
2 * Copyright (C) 2016, 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 "generate_java.h"
18
19 #include <memory>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include <android-base/stringprintf.h>
25
26 #include "code_writer.h"
27 #include "type_java.h"
28
29 using std::unique_ptr;
30 using ::android::aidl::java::Variable;
31 using std::string;
32 using android::base::StringPrintf;
33
34 namespace android {
35 namespace aidl {
36
37 // =================================================
VariableFactory(const string & base)38 VariableFactory::VariableFactory(const string& base)
39 : base_(base),
40 index_(0) {
41 }
42
Get(const Type * type)43 Variable* VariableFactory::Get(const Type* type) {
44 Variable* v = new Variable(
45 type, StringPrintf("%s%d", base_.c_str(), index_));
46 vars_.push_back(v);
47 index_++;
48 return v;
49 }
50
Get(int index)51 Variable* VariableFactory::Get(int index) {
52 return vars_[index];
53 }
54
55 namespace java {
56
generate_java(const string & filename,const string & originalSrc,AidlInterface * iface,JavaTypeNamespace * types,const IoDelegate & io_delegate)57 int generate_java(const string& filename, const string& originalSrc,
58 AidlInterface* iface, JavaTypeNamespace* types,
59 const IoDelegate& io_delegate) {
60 Class* cl = generate_binder_interface_class(iface, types);
61
62 Document* document = new Document(
63 "" /* no comment */,
64 (!iface->GetPackage().empty()) ? iface->GetPackage() : "",
65 originalSrc,
66 unique_ptr<Class>(cl));
67
68 CodeWriterPtr code_writer = io_delegate.GetCodeWriter(filename);
69 document->Write(code_writer.get());
70
71 return 0;
72 }
73
74 } // namespace java
75 } // namespace android
76 } // namespace aidl
77