|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.codehaus.groovy.classgen.asm.sc; |
| 20 | + |
| 21 | +import org.codehaus.groovy.GroovyBugError; |
| 22 | +import org.codehaus.groovy.ast.ClassCodeVisitorSupport; |
| 23 | +import org.codehaus.groovy.ast.ClassHelper; |
| 24 | +import org.codehaus.groovy.ast.ClassNode; |
| 25 | +import org.codehaus.groovy.ast.MethodNode; |
| 26 | +import org.codehaus.groovy.ast.Parameter; |
| 27 | +import org.codehaus.groovy.ast.expr.ArgumentListExpression; |
| 28 | +import org.codehaus.groovy.ast.expr.ArrayExpression; |
| 29 | +import org.codehaus.groovy.ast.expr.ClosureExpression; |
| 30 | +import org.codehaus.groovy.ast.expr.ConstantExpression; |
| 31 | +import org.codehaus.groovy.ast.expr.Expression; |
| 32 | +import org.codehaus.groovy.ast.expr.MethodCallExpression; |
| 33 | +import org.codehaus.groovy.ast.expr.VariableExpression; |
| 34 | +import org.codehaus.groovy.ast.stmt.ReturnStatement; |
| 35 | +import org.codehaus.groovy.classgen.asm.ClosureWriter; |
| 36 | +import org.codehaus.groovy.classgen.asm.WriterController; |
| 37 | +import org.codehaus.groovy.control.SourceUnit; |
| 38 | +import org.codehaus.groovy.transform.sc.StaticCompilationMetadataKeys; |
| 39 | +import org.codehaus.groovy.transform.stc.StaticTypesMarker; |
| 40 | +import groovyjarjarasm.asm.Opcodes; |
| 41 | + |
| 42 | +import java.util.Collections; |
| 43 | +import java.util.List; |
| 44 | + |
| 45 | +/** |
| 46 | + * Writer responsible for generating closure classes in statically compiled mode. |
| 47 | + */ |
| 48 | +public class StaticTypesClosureWriter extends ClosureWriter { |
| 49 | + public StaticTypesClosureWriter(WriterController wc) { |
| 50 | + super(wc); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + protected ClassNode createClosureClass(final ClosureExpression expression, final int mods) { |
| 55 | + ClassNode closureClass = super.createClosureClass(expression, mods); |
| 56 | + List<MethodNode> methods = closureClass.getDeclaredMethods("call"); |
| 57 | + List<MethodNode> doCall = closureClass.getMethods("doCall"); |
| 58 | + if (doCall.size() != 1) { |
| 59 | + throw new GroovyBugError("Expected to find one (1) doCall method on generated closure, but found " + doCall.size()); |
| 60 | + } |
| 61 | + MethodNode doCallMethod = doCall.get(0); |
| 62 | + if (methods.isEmpty() && doCallMethod.getParameters().length == 1) { |
| 63 | + createDirectCallMethod(closureClass, doCallMethod); |
| 64 | + } |
| 65 | + MethodTargetCompletionVisitor visitor = new MethodTargetCompletionVisitor(doCallMethod); |
| 66 | + Object dynamic = expression.getNodeMetaData(StaticTypesMarker.DYNAMIC_RESOLUTION); |
| 67 | + if (dynamic != null) { |
| 68 | + doCallMethod.putNodeMetaData(StaticTypesMarker.DYNAMIC_RESOLUTION, dynamic); |
| 69 | + } |
| 70 | + for (MethodNode method : methods) { |
| 71 | + visitor.visitMethod(method); |
| 72 | + } |
| 73 | + closureClass.putNodeMetaData(StaticCompilationMetadataKeys.STATIC_COMPILE_NODE, Boolean.TRUE); |
| 74 | + return closureClass; |
| 75 | + } |
| 76 | + |
| 77 | + private static void createDirectCallMethod(final ClassNode closureClass, final MethodNode doCallMethod) { |
| 78 | + // in case there is no "call" method on the closure, we can create a "fast invocation" paths |
| 79 | + // to avoid going through ClosureMetaClass by call(Object...) method |
| 80 | + |
| 81 | + // we can't have a specialized version of call(Object...) because the dispatch logic in ClosureMetaClass |
| 82 | + // is too complex! |
| 83 | + |
| 84 | + // call(Object) |
| 85 | + /* GRECLIPSE edit -- GROOVY-10071 |
| 86 | + Parameter args = new Parameter(ClassHelper.OBJECT_TYPE, "args"); |
| 87 | + */ |
| 88 | + Parameter doCallParam = doCallMethod.getParameters()[0]; |
| 89 | + Parameter args = new Parameter(doCallParam.getType(), "args"); |
| 90 | + // GRECLIPSE end |
| 91 | + MethodCallExpression doCall1arg = new MethodCallExpression( |
| 92 | + new VariableExpression("this", closureClass), |
| 93 | + "doCall", |
| 94 | + new ArgumentListExpression(new VariableExpression(args)) |
| 95 | + ); |
| 96 | + doCall1arg.setImplicitThis(true); |
| 97 | + doCall1arg.setMethodTarget(doCallMethod); |
| 98 | + closureClass.addMethod( |
| 99 | + new MethodNode("call", |
| 100 | + Opcodes.ACC_PUBLIC, |
| 101 | + ClassHelper.OBJECT_TYPE, |
| 102 | + new Parameter[]{args}, |
| 103 | + ClassNode.EMPTY_ARRAY, |
| 104 | + new ReturnStatement(doCall1arg))); |
| 105 | + |
| 106 | + // call() |
| 107 | + /* GRECLIPSE edit -- GROOVY-10071, GROOVY-10072 |
| 108 | + MethodCallExpression doCallNoArgs = new MethodCallExpression(new VariableExpression("this", closureClass), "doCall", new ArgumentListExpression(new ConstantExpression(null))); |
| 109 | + */ |
| 110 | + Expression argument; |
| 111 | + if (doCallParam.hasInitialExpression()) { |
| 112 | + argument = doCallParam.getInitialExpression(); |
| 113 | + } else if (doCallParam.getType().isArray()) { |
| 114 | + ClassNode elementType = doCallParam.getType().getComponentType(); |
| 115 | + argument = new ArrayExpression(elementType, null, Collections.singletonList(new ConstantExpression(0, true))); |
| 116 | + } else { |
| 117 | + argument = new ConstantExpression(null); |
| 118 | + } |
| 119 | + MethodCallExpression doCallNoArgs = new MethodCallExpression(new VariableExpression("this", closureClass), "doCall", new ArgumentListExpression(argument)); |
| 120 | + // GRECLIPSE end |
| 121 | + doCallNoArgs.setImplicitThis(true); |
| 122 | + doCallNoArgs.setMethodTarget(doCallMethod); |
| 123 | + closureClass.addMethod( |
| 124 | + new MethodNode("call", |
| 125 | + Opcodes.ACC_PUBLIC, |
| 126 | + ClassHelper.OBJECT_TYPE, |
| 127 | + Parameter.EMPTY_ARRAY, |
| 128 | + ClassNode.EMPTY_ARRAY, |
| 129 | + new ReturnStatement(doCallNoArgs))); |
| 130 | + } |
| 131 | + |
| 132 | + private static final class MethodTargetCompletionVisitor extends ClassCodeVisitorSupport { |
| 133 | + |
| 134 | + private final MethodNode doCallMethod; |
| 135 | + |
| 136 | + private MethodTargetCompletionVisitor(final MethodNode doCallMethod) { |
| 137 | + this.doCallMethod = doCallMethod; |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + protected SourceUnit getSourceUnit() { |
| 142 | + return null; |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public void visitMethodCallExpression(final MethodCallExpression call) { |
| 147 | + super.visitMethodCallExpression(call); |
| 148 | + MethodNode mn = call.getMethodTarget(); |
| 149 | + if (mn == null) { |
| 150 | + call.setMethodTarget(doCallMethod); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments