Debian Codegraph @

chunkio-processor/src/com/android/tools/chunkio/codegen/MethodDef.java

raw
1 /*
2 * Copyright (C) 2015 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 package com.android.tools.chunkio.codegen;
18
19 import java.io.IOException;
20 import java.lang.reflect.Type;
21 import java.util.ArrayList;
22 import java.util.EnumSet;
23 import java.util.List;
24 import java.util.Set;
25
26 import javax.lang.model.element.Modifier;
27
28 public class MethodDef {
29 private final String name;
30 private final TypeDef returnType;
31 private final Set<Modifier> modifiers;
32 private final List<ParameterDef> parameters;
33 private final List<TypeDef> exceptions;
34 private final CodeSnippet body;
35
36 private MethodDef(Builder builder) {
37 name = builder.name;
38 returnType = builder.returnType;
39 modifiers = Utils.immutableCopy(builder.modifiers);
40 parameters = Utils.immutableCopy(builder.parameters);
41 exceptions = Utils.immutableCopy(builder.exceptions);
42 body = builder.body.build();
43 }
44
45 String getName() {
46 return name;
47 }
48
49 public static Builder builder(String name) {
50 return new Builder(name);
51 }
52
53 void emit(CodeGenerator generator) throws IOException {
54 generator.emitModifiers(modifiers);
55 generator.emit("$T $L(", returnType, name);
56
57 boolean firstParam = true;
58 for (ParameterDef parameter : parameters) {
59 if (!firstParam) {
60 generator.emit(", ");
61 }
62 firstParam = false;
63 parameter.emit(generator);
64 }
65
66 generator.emit(")");
67
68 if (!exceptions.isEmpty()) {
69 generator.emit(" throws ");
70 boolean firstException = true;
71 for (TypeDef exception : exceptions) {
72 if (!firstException) {
73 generator.emit(", ");
74 }
75 firstException = false;
76 generator.emit("$T", exception);
77 }
78 }
79
80 generator.emit(" {\n");
81 generator.indent();
82
83 generator.emit(body);
84
85 generator.unindent();
86 generator.emit("}\n");
87 }
88
89 public static final class Builder {
90 private final String name;
91 private TypeDef returnType;
92 private Set<Modifier> modifiers;
93 private final List<ParameterDef> parameters = new ArrayList<>();
94 private final List<TypeDef> exceptions = new ArrayList<>();
95
96 private final CodeSnippet.Builder body = CodeSnippet.builder();
97
98 private Builder(String name) {
99 this.name = name;
100 }
101
102 public Builder modifiers(EnumSet<Modifier> modifiers) {
103 this.modifiers = EnumSet.copyOf(modifiers);
104 return this;
105 }
106
107 public Builder addParameter(Type type, String name) {
108 return addParameter(type, name, EnumSet.noneOf(Modifier.class));
109 }
110
111 public Builder addParameter(Type type, String name, EnumSet<Modifier> modifiers) {
112 parameters.add(ParameterDef.builder(type, name, modifiers).build());
113 return this;
114 }
115
116 public Builder throwsException(Type... exceptions) {
117 for (Type exception : exceptions) {
118 this.exceptions.add(TypeDef.of(exception));
119 }
120 return this;
121 }
122
123 public Builder returns(String packageName, String name) {
124 return returns(TypeDef.fromClass(packageName, name));
125 }
126
127 public Builder returns(Type type) {
128 return returns(TypeDef.of(type));
129 }
130
131 public Builder returns(TypeDef type) {
132 returnType = type;
133 return this;
134 }
135
136 public Builder add(CodeSnippet snippet) {
137 body.add(snippet);
138 return this;
139 }
140
141 public Builder add(String format, Object... args) {
142 body.add(format, args);
143 return this;
144 }
145
146 public Builder addStatement(String format, Object... args) {
147 body.addStatement(format, args);
148 return this;
149 }
150
151 public Builder beginControlStatement(String format, Object... args) {
152 body.beginControlStatement(format, args);
153 return this;
154 }
155
156 public Builder continueControlStatement(String format, Object... args) {
157 body.continueControlStatement(format, args);
158 return this;
159 }
160
161 public Builder endControlStatement() {
162 body.endControlStatement();
163 return this;
164 }
165
166 public Builder beginBlock() {
167 body.beginBlock();
168 return this;
169 }
170
171 public Builder endBlock() {
172 body.endBlock();
173 return this;
174 }
175
176 public MethodDef build() {
177 return new MethodDef(this);
178 }
179 }
180 }