Skip to content

Commit 2759818

Browse files
committed
GROOVY-9093
1 parent 514bfbe commit 2759818

File tree

3 files changed

+37
-47
lines changed

3 files changed

+37
-47
lines changed

base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/StaticCompilationTests.java

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2463,22 +2463,19 @@ public void testCompileStatic9007or9043_enumConstToPrivate1() {
24632463
"}\n" +
24642464
"@groovy.transform.CompileStatic\n" +
24652465
"void test() {\n" +
2466-
" print E.ONE.name\n" +
2466+
" E.ONE.name\n" +
24672467
"}\n" +
24682468
"test()\n",
24692469
};
24702470
//@formatter:on
24712471

2472-
runConformTest(sources, "", "groovy.lang.MissingPropertyException: No such property: name for class: E");
2473-
/* TODO: https://issues.apache.org/jira/browse/GROOVY-9093
24742472
runNegativeTest(sources,
24752473
"----------\n" +
24762474
"1. ERROR in Main.groovy (at line 6)\n" +
24772475
"\tE.ONE.name\n" +
24782476
"\t^^^^^\n" +
2479-
"Groovy:Access to E#name is forbidden @ line 6, column 3.\n" +
2477+
"Groovy:Access to E#name is forbidden\n" +
24802478
"----------\n");
2481-
*/
24822479
}
24832480

24842481
@Test
@@ -2491,22 +2488,19 @@ public void testCompileStatic9007or9043_enumConstToPrivate2() {
24912488
"}\n" +
24922489
"@groovy.transform.CompileStatic\n" +
24932490
"void test() {\n" +
2494-
" print E.ONE.ordinal\n" +
2491+
" E.ONE.ordinal\n" +
24952492
"}\n" +
24962493
"test()\n",
24972494
};
24982495
//@formatter:on
24992496

2500-
runConformTest(sources, "", "groovy.lang.MissingPropertyException: No such property: ordinal for class: E");
2501-
/* TODO: https://issues.apache.org/jira/browse/GROOVY-9093
25022497
runNegativeTest(sources,
25032498
"----------\n" +
25042499
"1. ERROR in Main.groovy (at line 6)\n" +
25052500
"\tE.ONE.ordinal\n" +
25062501
"\t^^^^^\n" +
2507-
"Groovy:Access to E#ordinal is forbidden @ line 6, column 3.\n" +
2502+
"Groovy:Access to E#ordinal is forbidden\n" +
25082503
"----------\n");
2509-
*/
25102504
}
25112505

25122506
@Test
@@ -3023,16 +3017,13 @@ public void testCompileStatic9043_subToPackage2() {
30233017
};
30243018
//@formatter:on
30253019

3026-
runConformTest(sources, "");
3027-
/* TODO: https://issues.apache.org/jira/browse/GROOVY-9093
30283020
runNegativeTest(sources,
30293021
"----------\n" +
30303022
"1. ERROR in q\\More.groovy (at line 5)\n" +
30313023
"\tprint VALUE\n" +
30323024
"\t ^^^^^\n" +
3033-
"Groovy:Access to q.More#VALUE is forbidden @ line 5, column 11.\n" +
3025+
"Groovy:Access to q.More#VALUE is forbidden\n" +
30343026
"----------\n");
3035-
*/
30363027
}
30373028

30383029
@Test
@@ -3174,16 +3165,13 @@ public void testCompileStatic9043_subToPrivate2() {
31743165
};
31753166
//@formatter:on
31763167

3177-
runConformTest(sources, "");
3178-
/* TODO: https://issues.apache.org/jira/browse/GROOVY-9093
31793168
runNegativeTest(sources,
31803169
"----------\n" +
31813170
"1. ERROR in q\\More.groovy (at line 5)\n" +
31823171
"\tprint VALUE\n" +
31833172
"\t ^^^^^\n" +
3184-
"Groovy:Access to q.More#VALUE is forbidden @ line 5, column 11.\n" +
3173+
"Groovy:Access to q.More#VALUE is forbidden\n" +
31853174
"----------\n");
3186-
*/
31873175
}
31883176

31893177
@Test

base/org.codehaus.groovy25/src/org/codehaus/groovy/classgen/asm/sc/StaticTypesCallSiteWriter.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -444,20 +444,19 @@ private boolean makeGetPrivateFieldWithBridgeMethod(final Expression receiver, f
444444
}
445445

446446
@Override
447-
public void makeGroovyObjectGetPropertySite(final Expression receiver, final String methodName, final boolean safe, final boolean implicitThis) {
447+
public void makeGroovyObjectGetPropertySite(final Expression receiver, final String propertyName, final boolean safe, final boolean implicitThis) {
448448
TypeChooser typeChooser = controller.getTypeChooser();
449449
ClassNode classNode = controller.getClassNode();
450450
ClassNode receiverType = typeChooser.resolveType(receiver, classNode);
451451
if (receiver instanceof VariableExpression && ((VariableExpression) receiver).isThisExpression() && !controller.isInClosure()) {
452452
receiverType = classNode;
453453
}
454-
455-
String property = methodName;
454+
456455
if (implicitThis) {
457456
if (controller.getInvocationWriter() instanceof StaticInvocationWriter) {
458457
MethodCallExpression currentCall = ((StaticInvocationWriter) controller.getInvocationWriter()).getCurrentCall();
459458
if (currentCall != null && currentCall.getNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER) != null) {
460-
property = currentCall.getNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER);
459+
String property = currentCall.getNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER);
461460
String[] props = property.split("\\.");
462461
BytecodeExpression thisLoader = new BytecodeExpression() {
463462
@Override
@@ -478,14 +477,23 @@ public void visit(final MethodVisitor mv) {
478477
}
479478
}
480479

481-
if (makeGetPropertyWithGetter(receiver, receiverType, property, safe, implicitThis)) return;
482-
if (makeGetPrivateFieldWithBridgeMethod(receiver, receiverType, property, safe, implicitThis)) return;
483-
if (makeGetField(receiver, receiverType, property, safe, implicitThis)) return;
480+
if (makeGetPropertyWithGetter(receiver, receiverType, propertyName, safe, implicitThis)) return;
481+
if (makeGetPrivateFieldWithBridgeMethod(receiver, receiverType, propertyName, safe, implicitThis)) return;
482+
if (makeGetField(receiver, receiverType, propertyName, safe, implicitThis)) return;
483+
484+
// GRECLIPSE add -- GROOVY-9093
485+
boolean isScriptVariable = (receiverType.isScript() && receiver instanceof VariableExpression && ((VariableExpression) receiver).getAccessedVariable() == null);
486+
if (!isScriptVariable && controller.getClassNode().getOuterClass() == null) { // inner class still needs dynamic property sequence
487+
String receiverName = (receiver instanceof ClassExpression ? receiver.getType() : receiverType).toString(false);
488+
String message = "Access to "+ receiverName + "#" + propertyName + " is forbidden";
489+
controller.getSourceUnit().addError(new SyntaxException(message, receiver));
490+
}
491+
// GRECLIPSE end
484492

485493
MethodCallExpression call = new MethodCallExpression(
486494
receiver,
487495
"getProperty",
488-
new ArgumentListExpression(new ConstantExpression(property))
496+
new ArgumentListExpression(new ConstantExpression(propertyName))
489497
);
490498
call.setImplicitThis(implicitThis);
491499
call.setSafe(safe);

base/org.codehaus.groovy30/src/org/codehaus/groovy/classgen/asm/sc/StaticTypesCallSiteWriter.java

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import java.util.Map;
5757
import java.util.Objects;
5858

59+
import static org.apache.groovy.ast.tools.ClassNodeUtils.getField;
5960
import static org.apache.groovy.ast.tools.ExpressionUtils.isThisExpression;
6061
import static org.apache.groovy.util.BeanUtils.capitalize;
6162
import static org.codehaus.groovy.ast.ClassHelper.BigDecimal_TYPE;
@@ -432,11 +433,10 @@ public void makeGroovyObjectGetPropertySite(final Expression receiver, final Str
432433
receiverType = controller.getTypeChooser().resolveType(receiver, receiverType);
433434
}
434435

435-
String property = propertyName;
436436
if (implicitThis && controller.getInvocationWriter() instanceof StaticInvocationWriter) {
437437
Expression currentCall = ((StaticInvocationWriter) controller.getInvocationWriter()).getCurrentCall();
438438
if (currentCall != null && currentCall.getNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER) != null) {
439-
property = currentCall.getNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER);
439+
String property = currentCall.getNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER);
440440
String[] props = property.split("\\.");
441441
BytecodeExpression thisLoader = bytecodeX(CLOSURE_TYPE, mv -> mv.visitVarInsn(ALOAD, 0));
442442
PropertyExpression pexp = propX(thisLoader, constX(props[0]), safe);
@@ -449,11 +449,18 @@ public void makeGroovyObjectGetPropertySite(final Expression receiver, final Str
449449
}
450450
}
451451

452-
if (makeGetPropertyWithGetter(receiver, receiverType, property, safe, implicitThis)) return;
453-
if (makeGetPrivateFieldWithBridgeMethod(receiver, receiverType, property, safe, implicitThis)) return;
454-
if (makeGetField(receiver, receiverType, property, safe, implicitThis)) return;
455-
456-
MethodCallExpression call = callX(receiver, "getProperty", args(constX(property)));
452+
if (makeGetPropertyWithGetter(receiver, receiverType, propertyName, safe, implicitThis)) return;
453+
if (makeGetPrivateFieldWithBridgeMethod(receiver, receiverType, propertyName, safe, implicitThis)) return;
454+
if (makeGetField(receiver, receiverType, propertyName, safe, implicitThis)) return;
455+
// GRECLIPSE add -- GROOVY-9093
456+
boolean isScriptVariable = (receiverType.isScript() && receiver instanceof VariableExpression && ((VariableExpression) receiver).getAccessedVariable() == null);
457+
if (!isScriptVariable && controller.getClassNode().getOuterClass() == null) { // inner class still needs dynamic property sequence
458+
String receiverName = (receiver instanceof ClassExpression ? receiver.getType() : receiverType).toString(false);
459+
String message = "Access to "+ receiverName + "#" + propertyName + " is forbidden";
460+
controller.getSourceUnit().addError(new SyntaxException(message, receiver));
461+
}
462+
// GRECLIPSE end
463+
MethodCallExpression call = callX(receiver, "getProperty", args(constX(propertyName)));
457464
call.setImplicitThis(implicitThis);
458465
call.setMethodTarget(GROOVYOBJECT_GETPROPERTY_METHOD);
459466
call.setSafe(safe);
@@ -529,8 +536,7 @@ private boolean makeGetPropertyWithGetter(final Expression receiver, final Class
529536
}
530537

531538
boolean makeGetField(final Expression receiver, final ClassNode receiverType, final String fieldName, final boolean safe, final boolean implicitThis) {
532-
FieldNode field = receiverType.getField(fieldName);
533-
539+
FieldNode field = getField(receiverType, fieldName); // GROOVY-7039: include interface constants
534540
if (field != null && isDirectAccessAllowed(field, controller.getClassNode())) {
535541
CompileStack compileStack = controller.getCompileStack();
536542
MethodVisitor mv = controller.getMethodVisitor();
@@ -573,18 +579,6 @@ boolean makeGetField(final Expression receiver, final ClassNode receiverType, fi
573579
operandStack.replace(replacementType);
574580
return true;
575581
}
576-
577-
for (ClassNode face : receiverType.getInterfaces()) {
578-
// GROOVY-7039
579-
if (face != receiverType && makeGetField(receiver, face, fieldName, safe, implicitThis)) {
580-
return true;
581-
}
582-
}
583-
584-
ClassNode superClass = receiverType.getSuperClass();
585-
if (superClass != null && !OBJECT_TYPE.equals(superClass)) {
586-
return makeGetField(receiver, superClass, fieldName, safe, implicitThis);
587-
}
588582
return false;
589583
}
590584

0 commit comments

Comments
 (0)