/
JavaParsingAtomicArrayQueueGenerator.java
565 lines (496 loc) · 21.6 KB
/
JavaParsingAtomicArrayQueueGenerator.java
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
package org.jctools.queues.atomic;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
import java.util.Optional;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.ImportDeclaration;
import com.github.javaparser.ast.Modifier;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.PackageDeclaration;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.ConstructorDeclaration;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.InitializerDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.Parameter;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.comments.Comment;
import com.github.javaparser.ast.comments.JavadocComment;
import com.github.javaparser.ast.expr.AssignExpr;
import com.github.javaparser.ast.expr.AssignExpr.Operator;
import com.github.javaparser.ast.expr.ClassExpr;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.FieldAccessExpr;
import com.github.javaparser.ast.expr.MarkerAnnotationExpr;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.expr.Name;
import com.github.javaparser.ast.expr.NameExpr;
import com.github.javaparser.ast.expr.SimpleName;
import com.github.javaparser.ast.expr.StringLiteralExpr;
import com.github.javaparser.ast.expr.ThisExpr;
import com.github.javaparser.ast.nodeTypes.NodeWithType;
import com.github.javaparser.ast.stmt.BlockStmt;
import com.github.javaparser.ast.stmt.ExpressionStmt;
import com.github.javaparser.ast.stmt.ReturnStmt;
import com.github.javaparser.ast.type.ArrayType;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.ast.type.PrimitiveType;
import com.github.javaparser.ast.type.Type;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
public final class JavaParsingAtomicArrayQueueGenerator extends VoidVisitorAdapter<Void> {
private static final String GEN_DIRECTIVE_CLASS_CONTAINS_ORDERED_FIELD_ACCESSORS = "$gen:ordered-fields";
private static final String GEN_DIRECTIVE_METHOD_IGNORE = "$gen:ignore";
private static final String INDENT_LEVEL = " ";
private final String sourceFileName;
public JavaParsingAtomicArrayQueueGenerator(String sourceFileName) {
super();
this.sourceFileName = sourceFileName;
}
@Override
public void visit(FieldAccessExpr n, Void arg) {
super.visit(n, arg);
if (n.getScope() instanceof NameExpr) {
NameExpr name = (NameExpr) n.getScope();
name.setName(translateQueueName(name.getNameAsString()));
}
}
@Override
public void visit(ClassOrInterfaceDeclaration node, Void arg) {
super.visit(node, arg);
replaceParentClassesForAtomics(node);
node.setName(translateQueueName(node.getNameAsString()));
if (isCommentPresent(node, GEN_DIRECTIVE_CLASS_CONTAINS_ORDERED_FIELD_ACCESSORS)) {
node.setComment(null);
removeStaticFieldsAndInitialisers(node);
patchAtomicFieldUpdaterAccessorMethods(node);
}
for (MethodDeclaration method : node.getMethods()) {
if (isCommentPresent(method, GEN_DIRECTIVE_METHOD_IGNORE)) {
method.remove();
}
}
if (!node.getMethodsByName("failFastOffer").isEmpty()) {
MethodDeclaration deprecatedMethodRedirect = node.addMethod("weakOffer", Modifier.PUBLIC);
patchMethodAsDepreciatedRedirector(deprecatedMethodRedirect, "failFastOffer", PrimitiveType.intType(),
new Parameter(classType("E"), "e"));
}
node.setJavadocComment(formatMultilineJavadoc(0,
"NOTE: This class was automatically generated by "
+ JavaParsingAtomicArrayQueueGenerator.class.getName(),
"which can found in the jctools-build module. The original source file is " + sourceFileName + ".")
+ node.getJavadocComment().orElse(new JavadocComment("")).getContent());
}
@Override
public void visit(ConstructorDeclaration n, Void arg) {
super.visit(n, arg);
// Update the ctor to match the class name
n.setName(translateQueueName(n.getNameAsString()));
}
@Override
public void visit(PackageDeclaration n, Void arg) {
super.visit(n, arg);
// Change the package of the output
n.setName("org.jctools.queues.atomic");
}
@Override
public void visit(Parameter n, Void arg) {
super.visit(n, arg);
// Process parameters to methods and ctors
processSpecialNodeTypes(n);
}
@Override
public void visit(VariableDeclarator n, Void arg) {
super.visit(n, arg);
// Replace declared variables with altered types
processSpecialNodeTypes(n);
}
private static boolean isCommentPresent(Node node, String wanted) {
Optional<Comment> maybeComment = node.getComment();
if (maybeComment.isPresent()) {
Comment comment = maybeComment.get();
String content = comment.getContent().trim();
if (wanted.equals(content)) {
return true;
}
}
return false;
}
private static void removeStaticFieldsAndInitialisers(ClassOrInterfaceDeclaration node) {
// Remove all the static initialisers
for (InitializerDeclaration child : node.getChildNodesByType(InitializerDeclaration.class)) {
child.remove();
}
// Remove all static fields
for (FieldDeclaration field : node.getFields()) {
if (field.getModifiers().contains(Modifier.STATIC)) {
field.remove();
continue;
}
}
}
/**
* Searches all extended or implemented super classes or interfaces for
* special classes that differ with the atomics version and replaces them
* with the appropriate class.
*
* @param n
*/
private static void replaceParentClassesForAtomics(ClassOrInterfaceDeclaration n) {
for (ClassOrInterfaceType parent : n.getExtendedTypes()) {
if ("ConcurrentCircularArrayQueue".equals(parent.getNameAsString())) {
parent.setName("AtomicReferenceArrayQueue");
} else if ("ConcurrentSequencedCircularArrayQueue".equals(parent.getNameAsString())) {
parent.setName("SequencedAtomicReferenceArrayQueue");
} else {
// Padded super classes are to be renamed and thus so does the
// class we must extend.
parent.setName(translateQueueName(parent.getNameAsString()));
}
}
}
/**
* Given a method declaration node this method will replace it's code and
* signature with code to redirect all calls to it to the
* <code>newMethodName</code>. Method signatures of both methods must match
* exactly.
*
* @param methodToPatch
* @param toMethodName
* @param returnType
* @param parameters
*/
private static void patchMethodAsDepreciatedRedirector(MethodDeclaration methodToPatch, String toMethodName,
Type returnType, Parameter... parameters) {
methodToPatch.setType(returnType);
for (Parameter parameter : parameters) {
methodToPatch.addParameter(parameter);
}
methodToPatch.addAnnotation(new MarkerAnnotationExpr("Deprecated"));
methodToPatch.setJavadocComment(
formatMultilineJavadoc(1, "@deprecated This was renamed to " + toMethodName + " please migrate"));
MethodCallExpr methodCall = methodCallExpr("this", toMethodName);
for (Parameter parameter : parameters) {
methodCall.addArgument(new NameExpr(parameter.getName()));
}
BlockStmt body = new BlockStmt();
body.addStatement(new ReturnStmt(methodCall));
methodToPatch.setBody(body);
}
/**
* For each method accessor to a field, add in the calls necessary to
* AtomicFieldUpdaters. Only methods start with so/cas/sv/lv/lp followed by
* the field name are processed. Clearly <code>lv<code>, <code>lp<code> and
* <code>sv<code> are simple field accesses with only <code>so and <code>cas
* <code> using the AtomicFieldUpdaters.
*
* @param n
* the AST node for the containing class
*/
private static void patchAtomicFieldUpdaterAccessorMethods(ClassOrInterfaceDeclaration n) {
String className = n.getNameAsString();
for (FieldDeclaration field : n.getFields()) {
if (field.getModifiers().contains(Modifier.STATIC)) {
// Ignore statics
continue;
}
boolean usesFieldUpdater = false;
for (VariableDeclarator variable : field.getVariables()) {
String variableName = variable.getNameAsString();
String methodNameSuffix = capitalise(variableName);
for (MethodDeclaration method : n.getMethods()) {
String methodName = method.getNameAsString();
if (!methodName.endsWith(methodNameSuffix)) {
// Leave it untouched
continue;
}
String newValueName = "newValue";
if (methodName.startsWith("so")) {
usesFieldUpdater = true;
String fieldUpdaterFieldName = fieldUpdaterFieldName(variableName);
method.setBody(fieldUpdaterLazySet(fieldUpdaterFieldName, newValueName));
} else if (methodName.startsWith("cas")) {
usesFieldUpdater = true;
String fieldUpdaterFieldName = fieldUpdaterFieldName(variableName);
String expectedValueName = "expect";
method.setBody(
fieldUpdaterCompareAndSet(fieldUpdaterFieldName, expectedValueName, newValueName));
} else if (methodName.startsWith("sv")) {
method.setBody(fieldAssignment(variableName, newValueName));
} else if (methodName.startsWith("lv") || methodName.startsWith("lp")) {
method.setBody(returnField(variableName));
} else {
throw new IllegalStateException("Unhandled method: " + methodName);
}
}
if (usesFieldUpdater) {
n.getMembers().add(0, declareLongFieldUpdater(className, variableName));
}
}
if (usesFieldUpdater) {
field.addModifier(Modifier.VOLATILE);
}
}
}
private static String capitalise(String s) {
return s.substring(0, 1).toUpperCase() + s.substring(1);
}
private static String formatMultilineJavadoc(int indent, String... lines) {
String indentation = "";
for (int i = 0; i < indent; i++) {
indentation += INDENT_LEVEL;
}
String out = "\n";
for (String line : lines) {
out += indentation + " * " + line + "\n";
}
out += indentation + " ";
return out;
}
/**
* Generates something like
* <code>P_INDEX_UPDATER.lazySet(this, newValue)</code>
*
* @param fieldUpdaterFieldName
* @param newValueName
* @return
*/
private static BlockStmt fieldUpdaterLazySet(String fieldUpdaterFieldName, String newValueName) {
BlockStmt body = new BlockStmt();
body.addStatement(new ExpressionStmt(
methodCallExpr(fieldUpdaterFieldName, "lazySet", new ThisExpr(), new NameExpr(newValueName))));
return body;
}
/**
* Generates something like
* <code>return P_INDEX_UPDATER.compareAndSet(this, expectedValue, newValue)</code>
*
* @param fieldUpdaterFieldName
* @param expectedValueName
* @param newValueName
* @return
*/
private static BlockStmt fieldUpdaterCompareAndSet(String fieldUpdaterFieldName, String expectedValueName,
String newValueName) {
BlockStmt body = new BlockStmt();
body.addStatement(new ReturnStmt(methodCallExpr(fieldUpdaterFieldName, "compareAndSet", new ThisExpr(),
new NameExpr(expectedValueName), new NameExpr(newValueName))));
return body;
}
/**
* Generates something like <code>field = newValue</code>
*
* @param fieldName
* @param valueName
* @return
*/
private static BlockStmt fieldAssignment(String fieldName, String valueName) {
BlockStmt body = new BlockStmt();
body.addStatement(
new ExpressionStmt(new AssignExpr(new NameExpr(fieldName), new NameExpr(valueName), Operator.ASSIGN)));
return body;
}
/**
* Generates something like
* <code>private static final AtomicLongFieldUpdater<MpmcAtomicArrayQueueProducerIndexField> P_INDEX_UPDATER = AtomicLongFieldUpdater.newUpdater(MpmcAtomicArrayQueueProducerIndexField.class, "producerIndex");</code>
*
* @param type
* @param name
* @param initializer
* @param modifiers
* @return
*/
private static FieldDeclaration fieldDeclarationWithInitialiser(Type type, String name, Expression initializer,
Modifier... modifiers) {
FieldDeclaration fieldDeclaration = new FieldDeclaration();
VariableDeclarator variable = new VariableDeclarator(type, name, initializer);
fieldDeclaration.getVariables().add(variable);
EnumSet<Modifier> modifierSet = EnumSet.copyOf(Arrays.asList(modifiers));
fieldDeclaration.setModifiers(modifierSet);
return fieldDeclaration;
}
/**
* Generates something like
* <code>private static final AtomicLongFieldUpdater<MpmcAtomicArrayQueueProducerIndexField> P_INDEX_UPDATER = AtomicLongFieldUpdater.newUpdater(MpmcAtomicArrayQueueProducerIndexField.class, "producerIndex");</code>
*
* @param className
* @param variableName
* @return
*/
private static FieldDeclaration declareLongFieldUpdater(String className, String variableName) {
MethodCallExpr initializer = newAtomicLongFieldUpdater(className, variableName);
ClassOrInterfaceType type = simpleParametricType("AtomicLongFieldUpdater", className);
FieldDeclaration newField = fieldDeclarationWithInitialiser(type, fieldUpdaterFieldName(variableName),
initializer, Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL);
return newField;
}
private static MethodCallExpr newAtomicLongFieldUpdater(String className, String variableName) {
return methodCallExpr("AtomicLongFieldUpdater", "newUpdater", new ClassExpr(classType(className)),
new StringLiteralExpr(variableName));
}
/**
* Generates something like <code>return field</code>
*
* @param fieldName
* @return
*/
private static BlockStmt returnField(String fieldName) {
BlockStmt body = new BlockStmt();
body.addStatement(new ReturnStmt(fieldName));
return body;
}
private static boolean isRefArray(Type in, String refClassName) {
if (in instanceof ArrayType) {
ArrayType aType = (ArrayType) in;
return isRefType(aType.getComponentType(), refClassName);
}
return false;
}
private static boolean isRefType(Type in, String className) {
// Does not check type parameters
if (in instanceof ClassOrInterfaceType) {
return (className.equals(((ClassOrInterfaceType) in).getNameAsString()));
}
return false;
}
private static boolean isLongArray(Type in) {
if (in instanceof ArrayType) {
ArrayType aType = (ArrayType) in;
return PrimitiveType.longType().equals(aType.getComponentType());
}
return false;
}
private static ClassOrInterfaceType atomicRefArrayType(ArrayType in) {
ClassOrInterfaceType out = new ClassOrInterfaceType(null, "AtomicReferenceArray");
out.setTypeArguments(in.getComponentType());
return out;
}
private static ClassOrInterfaceType atomicLongArrayType() {
return new ClassOrInterfaceType(null, "AtomicLongArray");
}
private static MethodCallExpr methodCallExpr(String owner, String method, Expression... args) {
MethodCallExpr methodCallExpr = new MethodCallExpr(new NameExpr(owner), method);
for (Expression expr : args) {
methodCallExpr.addArgument(expr);
}
return methodCallExpr;
}
private static ClassOrInterfaceType simpleParametricType(String className, String... typeArgs) {
NodeList<Type> typeArguments = new NodeList<Type>();
for (String typeArg : typeArgs) {
typeArguments.add(classType(typeArg));
}
return new ClassOrInterfaceType(null, new SimpleName(className), typeArguments);
}
private static ClassOrInterfaceType classType(String className) {
return new ClassOrInterfaceType(null, className);
}
private static ImportDeclaration importDeclaration(String name) {
return new ImportDeclaration(new Name(name), false, false);
}
private static String translateQueueName(String originalQueueName) {
if (originalQueueName.length() < 5) {
return originalQueueName;
}
String start = originalQueueName.substring(0, 4);
String end = originalQueueName.substring(4);
if ((start.equals("Spsc") || start.equals("Spmc") || start.equals("Mpsc") || start.equals("Mpmc"))
&& end.startsWith("ArrayQueue")) {
return start + "Atomic" + end;
}
return originalQueueName;
}
private static String fieldUpdaterFieldName(String fieldName) {
switch (fieldName) {
case "producerIndex":
return "P_INDEX_UPDATER";
case "consumerIndex":
return "C_INDEX_UPDATER";
case "producerLimit":
return "P_LIMIT_UPDATER";
default:
throw new IllegalArgumentException("Unhandled field: " + fieldName);
}
}
private static void organiseImports(CompilationUnit cu) {
List<ImportDeclaration> importDecls = new ArrayList<>();
for (ImportDeclaration importDeclaration : cu.getImports()) {
if (importDeclaration.getNameAsString().startsWith("org.jctools.util.Unsafe")) {
continue;
}
importDecls.add(importDeclaration);
}
cu.getImports().clear();
for (ImportDeclaration importDecl : importDecls) {
cu.addImport(importDecl);
}
cu.addImport(importDeclaration("java.util.concurrent.atomic.AtomicLongFieldUpdater"));
cu.addImport(importDeclaration("java.util.concurrent.atomic.AtomicReferenceArray"));
cu.addImport(importDeclaration("java.util.concurrent.atomic.AtomicLongArray"));
}
private static void processSpecialNodeTypes(Parameter node) {
processSpecialNodeTypes(node, node.getNameAsString());
}
private static void processSpecialNodeTypes(VariableDeclarator node) {
processSpecialNodeTypes(node, node.getNameAsString());
}
/**
* Given a variable declaration of some sort, check it's name and type and
* if it looks like any of the key type changes between unsafe and atomic
* queues, perform the conversion to change it's type.
*
* @param node
* @param name
*/
private static void processSpecialNodeTypes(NodeWithType<?, Type> node, String name) {
Type type = node.getType();
if ("buffer".equals(name) && isRefArray(type, "E")) {
node.setType(atomicRefArrayType((ArrayType) type));
} else if ("sBuffer".equals(name) && isLongArray(type)) {
node.setType(atomicLongArrayType());
} else if (PrimitiveType.longType().equals(type)) {
switch(name) {
case "mask":
case "offset":
case "seqOffset":
case "lookAheadSeqOffset":
case "lookAheadElementOffset":
node.setType(PrimitiveType.intType());
}
}
}
public static void main(String[] args) throws Exception {
if (args.length < 2) {
throw new IllegalArgumentException("Usage: outputDirectory inputSourceFiles");
}
File outputDirectory = new File(args[0]);
for (int i = 1; i < args.length; i++) {
File file = new File(args[i]);
System.out.println("Processing " + file);
CompilationUnit cu = JavaParser.parse(file);
new JavaParsingAtomicArrayQueueGenerator(file.getName()).visit(cu, null);
organiseImports(cu);
FileWriter writer = null;
String outputFileName = file.getName();
if (outputFileName.endsWith(".java")) {
outputFileName = translateQueueName(outputFileName.replace(".java", ""));
} else {
outputFileName = translateQueueName(outputFileName);
}
outputFileName += ".java";
try {
writer = new FileWriter(new File(outputDirectory, outputFileName));
writer.write(cu.toString());
} finally {
if (writer != null) {
writer.close();
}
}
}
}
}