diff --git a/base/org.codehaus.groovy24/src/org/codehaus/groovy/classgen/asm/sc/StaticPropertyAccessHelper.java b/base/org.codehaus.groovy24/src/org/codehaus/groovy/classgen/asm/sc/StaticPropertyAccessHelper.java index e7946070d3..7e2bd837a2 100644 --- a/base/org.codehaus.groovy24/src/org/codehaus/groovy/classgen/asm/sc/StaticPropertyAccessHelper.java +++ b/base/org.codehaus.groovy24/src/org/codehaus/groovy/classgen/asm/sc/StaticPropertyAccessHelper.java @@ -29,12 +29,13 @@ import java.util.Arrays; +import static java.beans.Introspector.decapitalize; + /** - * Contains helper methods aimed at facilitating the generation of statically compiled bytecode for property access. - * - * @since 2.4.0 + * Facilitates the generation of statically-compiled bytecode for property access. */ public abstract class StaticPropertyAccessHelper { + public static Expression transformToSetterCall( Expression receiver, MethodNode setterMethod, @@ -103,7 +104,10 @@ private static class PoppingMethodCallExpression extends MethodCallExpression { private final TemporaryVariableExpression tmp; public PoppingMethodCallExpression(final Expression receiver, final MethodNode setterMethod, final TemporaryVariableExpression tmp) { - super(receiver, setterMethod.getName(), tmp); + // GRECLIPSE edit -- retaian property semantics on the method expression + super(receiver, decapitalize(setterMethod.getName().substring(3)), tmp); + //super(receiver, setterMethod.getName(), tmp); + // GRECLIPSE end this.receiver = receiver; this.setter = setterMethod; this.tmp = tmp; diff --git a/base/org.codehaus.groovy25/.checkstyle b/base/org.codehaus.groovy25/.checkstyle index ac18217701..fef4cff19f 100644 --- a/base/org.codehaus.groovy25/.checkstyle +++ b/base/org.codehaus.groovy25/.checkstyle @@ -48,6 +48,7 @@ + diff --git a/base/org.codehaus.groovy25/src/org/codehaus/groovy/classgen/asm/sc/StaticPropertyAccessHelper.java b/base/org.codehaus.groovy25/src/org/codehaus/groovy/classgen/asm/sc/StaticPropertyAccessHelper.java new file mode 100644 index 0000000000..4635ff8f73 --- /dev/null +++ b/base/org.codehaus.groovy25/src/org/codehaus/groovy/classgen/asm/sc/StaticPropertyAccessHelper.java @@ -0,0 +1,137 @@ +/* + * 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.classgen.asm.sc; + +import org.codehaus.groovy.ast.GroovyCodeVisitor; +import org.codehaus.groovy.ast.MethodNode; +import org.codehaus.groovy.ast.expr.Expression; +import org.codehaus.groovy.ast.expr.ExpressionTransformer; +import org.codehaus.groovy.ast.expr.MethodCallExpression; +import org.codehaus.groovy.classgen.AsmClassGenerator; +import org.codehaus.groovy.transform.sc.ListOfExpressionsExpression; +import org.codehaus.groovy.transform.sc.TemporaryVariableExpression; + +import java.util.Arrays; + +import static java.beans.Introspector.decapitalize; + +/** + * Facilitates the generation of statically-compiled bytecode for property access. + */ +public abstract class StaticPropertyAccessHelper { + + public static Expression transformToSetterCall( + Expression receiver, + MethodNode setterMethod, + final Expression arguments, + boolean implicitThis, + boolean safe, + boolean spreadSafe, + boolean requiresReturnValue, + Expression location) { + if (requiresReturnValue) { + TemporaryVariableExpression tmp = new TemporaryVariableExpression(arguments); + PoppingMethodCallExpression call = new PoppingMethodCallExpression(receiver, setterMethod, tmp); + call.setImplicitThis(implicitThis); + call.setSafe(safe); + call.setSpreadSafe(spreadSafe); + call.setSourcePosition(location); + PoppingListOfExpressionsExpression result = new PoppingListOfExpressionsExpression(tmp, call); + result.setSourcePosition(location); + return result; + } else { + MethodCallExpression call = new MethodCallExpression( + receiver, + setterMethod.getName(), + arguments + ); + call.setImplicitThis(implicitThis); + call.setSafe(safe); + call.setSpreadSafe(spreadSafe); + call.setMethodTarget(setterMethod); + call.setSourcePosition(location); + return call; + } + } + + private static class PoppingListOfExpressionsExpression extends ListOfExpressionsExpression { + private final TemporaryVariableExpression tmp; + private final PoppingMethodCallExpression call; + + public PoppingListOfExpressionsExpression(final TemporaryVariableExpression tmp, final PoppingMethodCallExpression call) { + super(Arrays.asList( + tmp, + call + )); + this.tmp = tmp; + this.call = call; + } + + @Override + public Expression transformExpression(final ExpressionTransformer transformer) { + PoppingMethodCallExpression tcall = (PoppingMethodCallExpression) call.transformExpression(transformer); + return new PoppingListOfExpressionsExpression(tcall.tmp, tcall); + } + + @Override + public void visit(final GroovyCodeVisitor visitor) { + super.visit(visitor); + if (visitor instanceof AsmClassGenerator) { + tmp.remove(((AsmClassGenerator) visitor).getController()); + } + } + } + + private static class PoppingMethodCallExpression extends MethodCallExpression { + private final Expression receiver; + private final MethodNode setter; + private final TemporaryVariableExpression tmp; + + public PoppingMethodCallExpression(final Expression receiver, final MethodNode setterMethod, final TemporaryVariableExpression tmp) { + // GRECLIPSE edit -- retaian property semantics on the method expression + super(receiver, decapitalize(setterMethod.getName().substring(3)), tmp); + //super(receiver, setterMethod.getName(), tmp); + // GRECLIPSE end + this.receiver = receiver; + this.setter = setterMethod; + this.tmp = tmp; + setMethodTarget(setterMethod); + } + + @Override + public Expression transformExpression(final ExpressionTransformer transformer) { + PoppingMethodCallExpression trn = new PoppingMethodCallExpression(receiver.transformExpression(transformer), setter, (TemporaryVariableExpression) tmp.transformExpression(transformer)); + trn.copyNodeMetaData(this); + trn.setSourcePosition(this); + trn.setImplicitThis(isImplicitThis()); + trn.setSafe(isSafe()); + trn.setSpreadSafe(isSpreadSafe()); + return trn; + } + + @Override + public void visit(final GroovyCodeVisitor visitor) { + super.visit(visitor); + if (visitor instanceof AsmClassGenerator) { + // ignore the return of the call + ((AsmClassGenerator) visitor).getController().getOperandStack().pop(); + } + } + } +} diff --git a/base/org.codehaus.groovy30/.checkstyle b/base/org.codehaus.groovy30/.checkstyle index e0d258bece..7e6e89578e 100644 --- a/base/org.codehaus.groovy30/.checkstyle +++ b/base/org.codehaus.groovy30/.checkstyle @@ -50,6 +50,7 @@ + diff --git a/base/org.codehaus.groovy30/src/org/codehaus/groovy/classgen/asm/sc/StaticPropertyAccessHelper.java b/base/org.codehaus.groovy30/src/org/codehaus/groovy/classgen/asm/sc/StaticPropertyAccessHelper.java new file mode 100644 index 0000000000..4635ff8f73 --- /dev/null +++ b/base/org.codehaus.groovy30/src/org/codehaus/groovy/classgen/asm/sc/StaticPropertyAccessHelper.java @@ -0,0 +1,137 @@ +/* + * 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.classgen.asm.sc; + +import org.codehaus.groovy.ast.GroovyCodeVisitor; +import org.codehaus.groovy.ast.MethodNode; +import org.codehaus.groovy.ast.expr.Expression; +import org.codehaus.groovy.ast.expr.ExpressionTransformer; +import org.codehaus.groovy.ast.expr.MethodCallExpression; +import org.codehaus.groovy.classgen.AsmClassGenerator; +import org.codehaus.groovy.transform.sc.ListOfExpressionsExpression; +import org.codehaus.groovy.transform.sc.TemporaryVariableExpression; + +import java.util.Arrays; + +import static java.beans.Introspector.decapitalize; + +/** + * Facilitates the generation of statically-compiled bytecode for property access. + */ +public abstract class StaticPropertyAccessHelper { + + public static Expression transformToSetterCall( + Expression receiver, + MethodNode setterMethod, + final Expression arguments, + boolean implicitThis, + boolean safe, + boolean spreadSafe, + boolean requiresReturnValue, + Expression location) { + if (requiresReturnValue) { + TemporaryVariableExpression tmp = new TemporaryVariableExpression(arguments); + PoppingMethodCallExpression call = new PoppingMethodCallExpression(receiver, setterMethod, tmp); + call.setImplicitThis(implicitThis); + call.setSafe(safe); + call.setSpreadSafe(spreadSafe); + call.setSourcePosition(location); + PoppingListOfExpressionsExpression result = new PoppingListOfExpressionsExpression(tmp, call); + result.setSourcePosition(location); + return result; + } else { + MethodCallExpression call = new MethodCallExpression( + receiver, + setterMethod.getName(), + arguments + ); + call.setImplicitThis(implicitThis); + call.setSafe(safe); + call.setSpreadSafe(spreadSafe); + call.setMethodTarget(setterMethod); + call.setSourcePosition(location); + return call; + } + } + + private static class PoppingListOfExpressionsExpression extends ListOfExpressionsExpression { + private final TemporaryVariableExpression tmp; + private final PoppingMethodCallExpression call; + + public PoppingListOfExpressionsExpression(final TemporaryVariableExpression tmp, final PoppingMethodCallExpression call) { + super(Arrays.asList( + tmp, + call + )); + this.tmp = tmp; + this.call = call; + } + + @Override + public Expression transformExpression(final ExpressionTransformer transformer) { + PoppingMethodCallExpression tcall = (PoppingMethodCallExpression) call.transformExpression(transformer); + return new PoppingListOfExpressionsExpression(tcall.tmp, tcall); + } + + @Override + public void visit(final GroovyCodeVisitor visitor) { + super.visit(visitor); + if (visitor instanceof AsmClassGenerator) { + tmp.remove(((AsmClassGenerator) visitor).getController()); + } + } + } + + private static class PoppingMethodCallExpression extends MethodCallExpression { + private final Expression receiver; + private final MethodNode setter; + private final TemporaryVariableExpression tmp; + + public PoppingMethodCallExpression(final Expression receiver, final MethodNode setterMethod, final TemporaryVariableExpression tmp) { + // GRECLIPSE edit -- retaian property semantics on the method expression + super(receiver, decapitalize(setterMethod.getName().substring(3)), tmp); + //super(receiver, setterMethod.getName(), tmp); + // GRECLIPSE end + this.receiver = receiver; + this.setter = setterMethod; + this.tmp = tmp; + setMethodTarget(setterMethod); + } + + @Override + public Expression transformExpression(final ExpressionTransformer transformer) { + PoppingMethodCallExpression trn = new PoppingMethodCallExpression(receiver.transformExpression(transformer), setter, (TemporaryVariableExpression) tmp.transformExpression(transformer)); + trn.copyNodeMetaData(this); + trn.setSourcePosition(this); + trn.setImplicitThis(isImplicitThis()); + trn.setSafe(isSafe()); + trn.setSpreadSafe(isSpreadSafe()); + return trn; + } + + @Override + public void visit(final GroovyCodeVisitor visitor) { + super.visit(visitor); + if (visitor instanceof AsmClassGenerator) { + // ignore the return of the call + ((AsmClassGenerator) visitor).getController().getOperandStack().pop(); + } + } + } +} diff --git a/ide/org.codehaus.groovy.eclipse.core/src/org/codehaus/groovy/eclipse/core/inference/STCTypeLookup.java b/ide/org.codehaus.groovy.eclipse.core/src/org/codehaus/groovy/eclipse/core/inference/STCTypeLookup.java index 2252a40d8e..9186dafe2c 100644 --- a/ide/org.codehaus.groovy.eclipse.core/src/org/codehaus/groovy/eclipse/core/inference/STCTypeLookup.java +++ b/ide/org.codehaus.groovy.eclipse.core/src/org/codehaus/groovy/eclipse/core/inference/STCTypeLookup.java @@ -100,6 +100,7 @@ public TypeLookupResult lookupType(final Expression expr, final VariableScope sc } else if (enclosingNode instanceof MethodCallExpression && ((MethodCallExpression) enclosingNode).getMethod() == expr) { methodTarget = enclosingNode.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET); if (methodTarget == null) methodTarget = getMopMethodTarget((MethodCallExpression) enclosingNode); + if (methodTarget == null) methodTarget = ((MethodCallExpression) enclosingNode).getMethodTarget(); } if (methodTarget instanceof ExtensionMethodNode) {