forked from apache/groovy
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathModuleNode.java
More file actions
641 lines (558 loc) · 24.5 KB
/
ModuleNode.java
File metadata and controls
641 lines (558 loc) · 24.5 KB
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.codehaus.groovy.ast;
import org.apache.groovy.ast.tools.ClassNodeUtils;
import org.codehaus.groovy.ast.expr.ConstructorCallExpression;
import org.codehaus.groovy.ast.expr.DeclarationExpression;
import org.codehaus.groovy.ast.expr.Expression;
import org.codehaus.groovy.ast.expr.ListExpression;
import org.codehaus.groovy.ast.expr.VariableExpression;
import org.codehaus.groovy.ast.stmt.BlockStatement;
import org.codehaus.groovy.ast.stmt.ExpressionStatement;
import org.codehaus.groovy.ast.stmt.Statement;
import org.codehaus.groovy.classgen.GeneratorContext;
import org.codehaus.groovy.control.SourceUnit;
import org.codehaus.groovy.runtime.InvokerHelper;
import org.codehaus.groovy.syntax.SyntaxException;
import org.codehaus.groovy.transform.BaseScriptASTTransformation;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static org.codehaus.groovy.ast.tools.GeneralUtils.args;
import static org.codehaus.groovy.ast.tools.GeneralUtils.callX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.classX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.ctorSuperX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.param;
import static org.codehaus.groovy.ast.tools.GeneralUtils.params;
import static org.codehaus.groovy.ast.tools.GeneralUtils.stmt;
import static org.codehaus.groovy.ast.tools.GeneralUtils.varX;
import static org.objectweb.asm.Opcodes.ACC_ABSTRACT;
import static org.objectweb.asm.Opcodes.ACC_FINAL;
import static org.objectweb.asm.Opcodes.ACC_INTERFACE;
import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
import static org.objectweb.asm.Opcodes.ACC_STATIC;
/**
* Represents a module, which consists typically of a class declaration
* but could include some imports, some statements and multiple classes
* intermixed with statements like scripts in Python or Ruby
*/
public class ModuleNode extends ASTNode {
private List<ClassNode> classes = new LinkedList<>();
private final List<MethodNode> methods = new ArrayList<>();
private final List<ImportNode> imports = new ArrayList<>();
private final List<ImportNode> starImports = new ArrayList<>();
private final Map<String, ImportNode> staticImports = new LinkedHashMap<>();
private final Map<String, ImportNode> staticStarImports = new LinkedHashMap<>();
private CompileUnit unit;
private PackageNode packageNode;
private String description;
private boolean createClassForStatements = true;
private transient SourceUnit context;
private boolean importsResolved;
private ClassNode scriptDummy;
private String mainClassName;
private final BlockStatement statementBlock = new BlockStatement();
public ModuleNode(final SourceUnit context) {
this.context = context;
}
public ModuleNode(final CompileUnit unit) {
this.unit = unit;
}
public List<ClassNode> getClasses() {
if (createClassForStatements && (!statementBlock.isEmpty() || !methods.isEmpty() || isPackageInfo())) {
ClassNode mainClass = createStatementsClass();
mainClassName = mainClass.getName();
createClassForStatements = false;
classes.add(0, mainClass);
mainClass.setModule(this);
addToCompileUnit(mainClass);
}
return /*Collections.unmodifiableList(*/classes/*)*/; // modified by MacroClassTransform
}
/**
* @return the module's methods
*/
public List<MethodNode> getMethods() {
return methods;
}
/**
* @return a copy of the module's imports
*/
public List<ImportNode> getImports() {
return new ArrayList<>(imports);
}
/**
* @return the module's star imports
*/
public List<ImportNode> getStarImports() {
return starImports;
}
/**
* @return the module's static imports
*/
public Map<String, ImportNode> getStaticImports() {
return staticImports;
}
/**
* @return the module's static star imports
*/
public Map<String, ImportNode> getStaticStarImports() {
return staticStarImports;
}
/**
* @param alias the name of interest
* @return the import type for the given alias or null if none is available
*/
public ClassNode getImportType(final String alias) {
ImportNode node = getImport(alias);
return (node != null ? node.getType() : null);
}
/**
* @param alias the name of interest
* @return the import node for the given alias or null if none is available
*/
public ImportNode getImport(final String alias) {
Map<String, ImportNode> aliases = getNodeMetaData("import.aliases", x ->
imports.stream().collect(Collectors.toMap(ImportNode::getAlias, n -> n, (n, m) -> m)));
return aliases.get(alias);
}
public void addImport(final String name, final ClassNode type) {
addImport(name, type, Collections.emptyList());
}
public void addImport(final String name, final ClassNode type, final List<AnnotationNode> annotations) {
checkUsage(name, type); // GROOVY-8254
ImportNode importNode = new ImportNode(type, name);
importNode.addAnnotations(annotations);
imports.add(importNode);
removeNodeMetaData("import.aliases");
storeLastAddedImportNode(importNode);
}
public void addStarImport(final String packageName) {
addStarImport(packageName, Collections.emptyList());
}
public void addStarImport(final String packageName, final List<AnnotationNode> annotations) {
ImportNode importNode = new ImportNode(packageName);
importNode.addAnnotations(annotations);
starImports.add(importNode);
storeLastAddedImportNode(importNode);
}
public void addStaticImport(final ClassNode type, final String memberName, final String simpleName) {
addStaticImport(type, memberName, simpleName, Collections.emptyList());
}
public void addStaticImport(final ClassNode type, final String memberName, final String simpleName, final List<AnnotationNode> annotations) {
ClassNode memberType = new ClassNode(type.getName() + '.' + memberName, 0, null) {
@Override public ClassNode getOuterClass() { return type; }
};
memberType.setSourcePosition(type);
checkUsage(simpleName, memberType);
ImportNode node = new ImportNode(type, memberName, simpleName);
node.addAnnotations(annotations);
ImportNode prev = staticImports.put(simpleName, node);
if (prev != null) {
staticImports.put(prev.toString(), prev);
staticImports.put(simpleName, staticImports.remove(simpleName));
}
storeLastAddedImportNode(node);
}
public void addStaticStarImport(final String name, final ClassNode type) {
addStaticStarImport(name, type, Collections.emptyList());
}
public void addStaticStarImport(final String name, final ClassNode type, final List<AnnotationNode> annotations) {
ImportNode node = new ImportNode(type);
node.addAnnotations(annotations);
staticStarImports.put(name, node);
storeLastAddedImportNode(node);
}
public void addStatement(final Statement node) {
statementBlock.addStatement(node);
}
public void addClass(final ClassNode node) {
if (classes.isEmpty())
mainClassName = node.getName();
classes.add(node);
node.setModule(this);
addToCompileUnit(node);
checkUsage(node.getNameWithoutPackage(), node);
}
private void addToCompileUnit(final ClassNode node) {
// register the new class with the compile unit
if (unit != null) {
unit.addClass(node);
}
}
private void checkUsage(final String name, final ClassNode type) {
for (ClassNode node : classes) {
if (node.getNameWithoutPackage().equals(name) && !node.equals(type)) {
getContext().addErrorAndContinue(new SyntaxException("The name " + name + " is already declared", type));
return;
}
}
for (ImportNode node : imports) {
if (node.getAlias().equals(name) && !node.getType().equals(type)) {
getContext().addErrorAndContinue(new SyntaxException("The name " + name + " is already declared", type));
return;
}
}
{
ImportNode node = staticImports.get(name);
if (node != null && !node.getType().equals(type.getOuterClass())) {
getContext().addErrorAndContinue(new SyntaxException("The name " + name + " is already declared", type));
}
}
}
public void addMethod(final MethodNode node) {
methods.add(node);
}
@Override
public void visit(final GroovyCodeVisitor visitor) {
}
public String getPackageName() {
return packageNode == null ? null : packageNode.getName();
}
public PackageNode getPackage() {
return packageNode;
}
public boolean hasPackage() {
return (packageNode != null);
}
public void setPackage(final PackageNode packageNode) {
this.packageNode = packageNode;
}
public boolean hasPackageName() {
return (packageNode != null && packageNode.getName() != null);
}
public void setPackageName(final String packageName) {
setPackage(new PackageNode(packageName));
}
/**
* @return the underlying character stream description
*/
public String getDescription() {
if (context != null) {
return context.getName();
} else {
return description;
}
}
public void setDescription(final String description) {
this.description = description;
}
public CompileUnit getUnit() {
return unit;
}
void setUnit(final CompileUnit unit) {
this.unit = unit;
}
public SourceUnit getContext() {
return context;
}
private boolean isPackageInfo() {
return context != null && context.getName() != null && context.getName().endsWith("package-info.groovy");
}
public ClassNode getScriptClassDummy() {
if (scriptDummy != null) {
setScriptBaseClassFromConfig(scriptDummy);
return scriptDummy;
}
String name = getPackageName();
if (name == null) {
name = "";
}
// now let's use the file name to determine the class name
if (getDescription() == null) {
throw new RuntimeException("Cannot generate main(String[]) class for statements when we have no file description");
}
name += GeneratorContext.encodeAsValidClassName(extractClassFromFileDescription());
ClassNode classNode;
if (isPackageInfo()) {
classNode = new ClassNode(name, ACC_ABSTRACT | ACC_INTERFACE, ClassHelper.OBJECT_TYPE);
} else {
classNode = new ClassNode(name, ACC_PUBLIC, ClassHelper.SCRIPT_TYPE);
setScriptBaseClassFromConfig(classNode);
classNode.setScript(true);
classNode.setScriptBody(true);
}
scriptDummy = classNode;
return classNode;
}
private void setScriptBaseClassFromConfig(final ClassNode cn) {
String baseClassName = null;
ClassLoader bcLoader = null;
if (unit != null) {
bcLoader = unit.getClassLoader();
baseClassName = unit.getConfig().getScriptBaseClass();
} else if (context != null) {
bcLoader = context.getClassLoader();
baseClassName = context.getConfiguration().getScriptBaseClass();
}
if (baseClassName != null && !cn.getSuperClass().getName().equals(baseClassName)) {
cn.addAnnotation(new AnnotationNode(BaseScriptASTTransformation.MY_TYPE));
try { // GROOVY-8096
cn.setSuperClass(ClassHelper.make(bcLoader.loadClass(baseClassName)));
} catch (ReflectiveOperationException | RuntimeException e) {
cn.setSuperClass(ClassHelper.make(baseClassName));
}
}
}
private static Parameter[] finalParam(final ClassNode type, final String name) {
Parameter parameter = param(type, name);
parameter.setModifiers(ACC_FINAL);
return params(parameter);
}
protected ClassNode createStatementsClass() {
ClassNode classNode = getScriptClassDummy();
if (classNode.getName().endsWith("package-info")) {
return classNode;
}
MethodNode existingMain = handleMainMethodIfPresent(methods);
boolean hasUncontainedStatements = false;
List<FieldNode> fields = new ArrayList<>();
// check for uncontained statements (excluding decl statements)
for (Statement statement : statementBlock.getStatements()) {
if (!(statement instanceof ExpressionStatement es)) {
hasUncontainedStatements = true;
break;
}
Expression expression = es.getExpression();
if (!(expression instanceof DeclarationExpression de)) {
hasUncontainedStatements = true;
break;
}
if (de.isMultipleAssignmentDeclaration()) {
List<Expression> variables = de.getTupleExpression().getExpressions();
if (!(de.getRightExpression() instanceof ListExpression)) break;
List<Expression> values = ((ListExpression)de.getRightExpression()).getExpressions();
for (int i = 0; i < variables.size(); i++) {
VariableExpression var = (VariableExpression) variables.get(i);
Expression val = i >= values.size() ? null : values.get(i);
fields.add(new FieldNode(var.getName(), var.getModifiers(), var.getType(), null, val));
}
} else {
VariableExpression ve = de.getVariableExpression();
fields.add(new FieldNode(ve.getName(), ve.getModifiers(), ve.getType(), null, de.getRightExpression()));
}
}
if (existingMain != null && !hasUncontainedStatements) { // JEP 445 main
ClassNode result = new ClassNode(classNode.getName(), 0, ClassHelper.OBJECT_TYPE);
result.addAnnotations(existingMain.getAnnotations());
result.setScriptBody(false);
result.putNodeMetaData("_SKIPPABLE_ANNOTATIONS", Boolean.TRUE);
existingMain.putNodeMetaData("_SKIPPABLE_ANNOTATIONS", Boolean.TRUE);
methods.forEach(result::addMethod);
fields.forEach(result::addField);
return result;
}
MethodNode main = new MethodNode(
"main",
ACC_PUBLIC | ACC_STATIC,
ClassHelper.VOID_TYPE,
finalParam(ClassHelper.STRING_TYPE.makeArray(), "args"),
ClassNode.EMPTY_ARRAY,
stmt(
callX(
ClassHelper.make(InvokerHelper.class),
"runScript",
args(classX(classNode), varX("args"))
)
)
);
main.setIsScriptBody();
ClassNodeUtils.addGeneratedMethod(classNode, main, true);
// we add the run method unless we find a no-arg instance run method
// and there are no uncontained statements
MethodNode existingRun = hasUncontainedStatements ? null : findRun();
if (existingRun == null) {
MethodNode methodNode = new MethodNode("run", ACC_PUBLIC, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, statementBlock);
methodNode.setIsScriptBody();
if (existingMain != null) {
methodNode.addAnnotations(existingMain.getAnnotations());
}
ClassNodeUtils.addGeneratedMethod(classNode, methodNode, true);
new CodeVisitorSupport() {
@Override
public void visitConstructorCallExpression(final ConstructorCallExpression cce) {
if (cce.isUsingAnonymousInnerClass()) { // GROOVY-11846
cce.getType().setEnclosingMethod(methodNode);
}
super.visitConstructorCallExpression(cce);
}
}
.visit(statementBlock);
} else {
fields.forEach(classNode::addField);
classNode.addAnnotations(existingRun.getAnnotations());
classNode.putNodeMetaData("_SKIPPABLE_ANNOTATIONS", Boolean.TRUE);
existingRun.putNodeMetaData("_SKIPPABLE_ANNOTATIONS", Boolean.TRUE);
}
classNode.addConstructor(ACC_PUBLIC, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, new BlockStatement());
Statement stmt;
// A script's contextual constructor should call its super class' contextual constructor, if it has one.
// In practice this will always be true because currently this visitor is run before the AST transformations
// (like @BaseScript) that could change this. But this is cautious and anticipates possible compiler changes.
if (classNode.getSuperClass().getDeclaredConstructor(params(param(ClassHelper.BINDING_TYPE, "context"))) != null) {
stmt = stmt(ctorSuperX(args(varX("context"))));
} else {
// Fallback for non-standard base "script" classes with no context (Binding) constructor.
stmt = stmt(callX(varX("super"), "setBinding", args(varX("context"))));
}
classNode.addConstructor(ACC_PUBLIC, finalParam(ClassHelper.BINDING_TYPE, "context"), ClassNode.EMPTY_ARRAY, stmt);
for (MethodNode method : methods) {
if (method.isAbstract()) {
throw new RuntimeException("Cannot use abstract methods in a script" +
", they are only available inside classes. Method: " + method.getName());
}
classNode.addMethod(method);
}
return classNode;
}
private MethodNode findRun() {
for (MethodNode node : methods) {
if ("run".equals(node.getName()) && node.getParameters().length == 0) {
return node;
}
}
return null;
}
/*
* We retain the 'main' method if a compatible one is found.
* A compatible one has no parameters or 1 (Object or String[]) parameter.
* The return type must be void or Object.
* When multiple valid main methods exist, a warning is issued for those
* that would not be reachable from the command-line runner.
* Priority follows JEP-512: static before instance, args before no-args.
*/
private MethodNode handleMainMethodIfPresent(final List<MethodNode> methods) {
boolean foundInstance = false;
boolean foundStatic = false;
List<MethodNode> validMains = new ArrayList<>();
for (MethodNode node : methods) {
if ("main".equals(node.getName()) && !node.isPrivate()) {
int numParams = node.getParameters().length;
if (numParams < 2) {
ClassNode argType = numParams > 0 ? node.getParameters()[0].getType() : null;
ClassNode retType = node.getReturnType();
boolean argTypeMatches = argType == null || "Object".equals(argType.getNameWithoutPackage()) || (argType.isArray() && "String".equals(argType.getComponentType().getNameWithoutPackage()));
boolean retTypeMatches = ClassHelper.isPrimitiveVoid(retType) || "Object".equals(retType.getNameWithoutPackage());
if (retTypeMatches && argTypeMatches) {
if (node.isStatic() ? foundStatic : foundInstance) {
throw new RuntimeException("Repetitive main method found.");
}
validMains.add(node);
if (node.isStatic()) foundStatic = true;
else foundInstance = true;
}
}
}
}
if (validMains.isEmpty()) return null;
// Select winner using JEP-512 priority: static before instance, args before no-args
validMains.sort((a, b) -> {
if (a.isStatic() != b.isStatic()) return a.isStatic() ? -1 : 1;
return Integer.compare(b.getParameters().length, a.getParameters().length);
});
MethodNode result = validMains.get(0);
// Warn about unreachable main methods
for (int i = 1; i < validMains.size(); i++) {
MethodNode unreachable = validMains.get(i);
getContext().addWarning("Method '" + unreachable.getText()
+ "' is not reachable from the Groovy runner"
+ " because a higher-priority main method '"
+ result.getText() + "' exists", unreachable);
}
return result;
}
protected String extractClassFromFileDescription() {
String answer = getDescription();
try {
URI uri = new URI(answer);
String path = uri.getPath();
String schemeSpecific = uri.getSchemeSpecificPart();
if (path != null && !path.isEmpty()) {
answer = path;
} else if (schemeSpecific != null && !schemeSpecific.isEmpty()) {
answer = schemeSpecific;
}
} catch (URISyntaxException ignore) {}
// let's strip off everything after the last '.'
int slashIdx = answer.lastIndexOf('/');
int separatorIdx = answer.lastIndexOf(File.separatorChar);
int dotIdx = answer.lastIndexOf('.');
if (dotIdx > 0 && dotIdx > Math.max(slashIdx, separatorIdx)) {
answer = answer.substring(0, dotIdx);
}
// new let's strip everything up to and including the path separators
if (slashIdx >= 0) {
answer = answer.substring(slashIdx + 1);
}
// recalculate in case we have already done some stripping
separatorIdx = answer.lastIndexOf(File.separatorChar);
if (separatorIdx >= 0) {
answer = answer.substring(separatorIdx + 1);
}
return answer;
}
public boolean isEmpty() {
return classes.isEmpty() && statementBlock.getStatements().isEmpty();
}
public void sortClasses() {
if (isEmpty()) return;
List<ClassNode> classes = getClasses();
if (classes.size() == 1) return;
List<ClassNode> ordered = new LinkedList<>();
int level = 1;
while (!classes.isEmpty()) {
for (Iterator<ClassNode> it = classes.iterator(); it.hasNext(); ) {
ClassNode cn = it.next(), sc = cn.getSuperClass();
for (int i = 1; i < level && sc != null; i += 1) sc = sc.getSuperClass();
if (sc != null && sc.isPrimaryClassNode()) continue;
ordered.add(cn);
it.remove();
}
level += 1;
}
this.classes = ordered;
}
public boolean hasImportsResolved() {
return importsResolved;
}
public void setImportsResolved(final boolean importsResolved) {
this.importsResolved = importsResolved;
}
// This method only exists as a workaround for GROOVY-6094
// In order to keep binary compatibility
private void storeLastAddedImportNode(final ImportNode node) {
if (getNodeMetaData(ImportNode.class) == ImportNode.class) {
putNodeMetaData(ImportNode.class, node);
}
}
public String getMainClassName() {
return mainClassName;
}
public BlockStatement getStatementBlock() {
return statementBlock;
}
}