Skip to content

Commit

Permalink
Fix for #1013: retain property semantic for method expression
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Dec 31, 2019
1 parent 59c9127 commit ebfd4b4
Show file tree
Hide file tree
Showing 10 changed files with 374 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,32 @@ public void testFieldReferencesInClass6() {
assertLocation(searchRequestor.getMatch(2), contents.lastIndexOf("flag"), "flag".length());
}

@Test // https://github.com/groovy/groovy-eclipse/issues/935
public void testFieldReferencesInClass7() {
GroovyCompilationUnit pogo = createUnit("Pogo",
"class Pogo {\n" +
" boolean flag\n" +
"}\n");

String contents =
"@groovy.transform.CompileStatic\n" +
"class C {\n" +
" void meth(Pogo pogo) {\n" +
" pogo.flag = false\n" +
" }\n" +
"}\n";

IField field = findType("Pogo", pogo).getField("flag");
GroovyCompilationUnit unit = createUnit("C", contents);
MockPossibleMatch possibleMatch = new MockPossibleMatch(unit);
ITypeRequestor typeRequestor = new TypeRequestorFactory().createRequestor(possibleMatch,
SearchPattern.createPattern(field, IJavaSearchConstants.REFERENCES), searchRequestor);
factory.createVisitor(possibleMatch).visitCompilationUnit(typeRequestor);

assertEquals("Should have found 1 matches, but found:\n" + searchRequestor.printMatches(), 1, searchRequestor.getMatches().size());
assertLocation(searchRequestor.getMatch(0), contents.lastIndexOf("flag"), "flag".length());
}

@Test // https://github.com/groovy/groovy-eclipse/issues/939
public void testFieldReferencesInClosure() {
String contents =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.codehaus.groovy.ast.GroovyCodeVisitor;
import org.codehaus.groovy.ast.MethodNode;
import org.codehaus.groovy.ast.expr.ConstantExpression;
import org.codehaus.groovy.ast.expr.Expression;
import org.codehaus.groovy.ast.expr.ExpressionTransformer;
import org.codehaus.groovy.ast.expr.MethodCallExpression;
Expand All @@ -29,8 +30,12 @@

import java.util.Arrays;

import static java.beans.Introspector.decapitalize;

/**
* Facilitates the generation of statically-compiled bytecode for property access.
*
* @since 2.4.0
*/
public abstract class StaticPropertyAccessHelper {

Expand Down Expand Up @@ -102,7 +107,17 @@ private static class PoppingMethodCallExpression extends MethodCallExpression {
private final TemporaryVariableExpression tmp;

public PoppingMethodCallExpression(final Expression receiver, final MethodNode setterMethod, final TemporaryVariableExpression tmp) {
/* GRECLIPSE edit
super(receiver, setterMethod.getName(), tmp);
*/
super(receiver, new ConstantExpression(setterMethod.getName()) {
@Override
public String getText() {
// retaian property semantic for method expression
return decapitalize(super.getText().substring(3));
}
}, tmp);
// GRECLIPSE end
this.receiver = receiver;
this.setter = setterMethod;
this.tmp = tmp;
Expand Down
1 change: 1 addition & 0 deletions base/org.codehaus.groovy25/.checkstyle
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<file-match-pattern match-pattern="groovy/classgen/asm/CompileStack.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/classgen/asm/StatementWriter.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/classgen/asm/sc/StaticInvocationWriter.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/classgen/asm/sc/StaticPropertyAccessHelper.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/classgen/asm/sc/StaticTypesCallSiteWriter.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/control/CompilationUnit.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/control/CompilerConfiguration.java" include-pattern="false" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* 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.ConstantExpression;
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.
*
* @since 2.4.0
*/
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
super(receiver, setterMethod.getName(), tmp);
*/
super(receiver, new ConstantExpression(setterMethod.getName()) {
@Override
public String getText() {
// retaian property semantic for method expression
return decapitalize(super.getText().substring(3));
}
}, 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();
}
}
}
}
1 change: 1 addition & 0 deletions base/org.codehaus.groovy30/.checkstyle
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<file-match-pattern match-pattern="groovy/classgen/Verifier.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/classgen/asm/WriterController.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/classgen/asm/sc/StaticInvocationWriter.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/classgen/asm/sc/StaticPropertyAccessHelper.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/classgen/asm/sc/StaticTypesLambdaWriter.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/control/CompilationUnit.java" include-pattern="false" />
<file-match-pattern match-pattern="groovy/control/CompilerConfiguration.java" include-pattern="false" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* 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.ConstantExpression;
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.
*
* @since 2.4.0
*/
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
super(receiver, setterMethod.getName(), tmp);
*/
super(receiver, new ConstantExpression(setterMethod.getName()) {
@Override
public String getText() {
// retaian property semantic for method expression
return decapitalize(super.getText().substring(3));
}
}, 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();
}
}
}
}
Loading

0 comments on commit ebfd4b4

Please sign in to comment.