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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
package org.zwobble.couscous.ast;
import com.google.common.collect.ImmutableList;
import org.zwobble.couscous.ast.visitors.NodeMapper;
import org.zwobble.couscous.ast.visitors.NodeTransformer;
import org.zwobble.couscous.values.UnitValue;
import java.util.List;
import java.util.function.Function;
import static org.zwobble.couscous.util.ExtraLists.eagerMap;
public class MethodNode implements CallableNode {
public static Builder staticMethod(String name) {
return builder(name).isStatic(true);
}
public static Builder builder(String name) {
return new Builder(name);
}
public static class Builder {
private final ImmutableList.Builder<AnnotationNode> annotations =
ImmutableList.builder();
private boolean isStatic = false;
private final String name;
private final ImmutableList.Builder<FormalArgumentNode> arguments =
ImmutableList.builder();
private TypeName returnType = UnitValue.REF;
private final ImmutableList.Builder<StatementNode> body =
ImmutableList.builder();
public Builder(String name) {
this.name = name;
}
public Builder annotations(List<AnnotationNode> annotations) {
this.annotations.addAll(annotations);
return this;
}
public Builder isStatic(boolean isStatic) {
this.isStatic = isStatic;
return this;
}
public Builder argument(FormalArgumentNode argument) {
arguments.add(argument);
return this;
}
public Builder arguments(List<FormalArgumentNode> arguments) {
this.arguments.addAll(arguments);
return this;
}
public Builder returns(TypeName returnType) {
this.returnType = returnType;
return this;
}
public Builder statement(StatementNode statement) {
this.body.add(statement);
return this;
}
public Builder body(List<StatementNode> statements) {
this.body.addAll(statements);
return this;
}
public MethodNode build() {
return new MethodNode(
annotations.build(),
isStatic,
name,
arguments.build(),
returnType,
body.build());
}
}
public static MethodNode method(
List<AnnotationNode> annotations,
boolean isStatic,
String name,
List<FormalArgumentNode> arguments,
TypeName returnType,
List<StatementNode> body)
{
return new MethodNode(annotations, isStatic, name, arguments, returnType, body);
}
private final List<AnnotationNode> annotations;
private final boolean isStatic;
private final String name;
private final List<FormalArgumentNode> arguments;
private final TypeName returnType;
private final List<StatementNode> body;
private MethodNode(
List<AnnotationNode> annotations,
boolean isStatic,
String name,
List<FormalArgumentNode> arguments,
TypeName returnType,
List<StatementNode> body)
{
this.annotations = annotations;
this.isStatic = isStatic;
this.name = name;
this.arguments = arguments;
this.returnType = returnType;
this.body = body;
}
public List<AnnotationNode> getAnnotations() {
return annotations;
}
public boolean isStatic() {
return isStatic;
}
public String getName() {
return name;
}
public List<FormalArgumentNode> getArguments() {
return arguments;
}
public TypeName getReturnType() {
return returnType;
}
public List<StatementNode> getBody() {
return body;
}
public MethodSignature signature() {
return new MethodSignature(
name,
eagerMap(arguments, argument -> argument.getType()));
}
public MethodNode mapBody(Function<List<StatementNode>, List<StatementNode>> function) {
return new MethodNode(annotations, isStatic, name, arguments, returnType, function.apply(body));
}
@Override
public <T> T accept(NodeMapper<T> visitor) {
return visitor.visit(this);
}
public MethodNode transform(NodeTransformer transformer) {
return new MethodNode(
transformer.transformAnnotations(annotations),
isStatic,
transformer.transformMethodName(signature()),
transformer.transformFormalArguments(arguments),
transformer.transform(returnType),
transformer.transformStatements(body));
}
@Override
public String toString() {
return "MethodNode(" +
"annotations=" + annotations +
", isStatic=" + isStatic +
", name=" + name +
", arguments=" + arguments +
", returnType=" + returnType +
", body=" + body +
')';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MethodNode that = (MethodNode) o;
if (isStatic != that.isStatic) return false;
if (!annotations.equals(that.annotations)) return false;
if (!name.equals(that.name)) return false;
if (!arguments.equals(that.arguments)) return false;
if (!returnType.equals(that.returnType)) return false;
return body.equals(that.body);
}
@Override
public int hashCode() {
int result = annotations.hashCode();
result = 31 * result + (isStatic ? 1 : 0);
result = 31 * result + name.hashCode();
result = 31 * result + arguments.hashCode();
result = 31 * result + returnType.hashCode();
result = 31 * result + body.hashCode();
return result;
}
}
|