Skip to content

Commit 8b35a8d

Browse files
committed
GROOVY-10845
1 parent 7e12c28 commit 8b35a8d

File tree

4 files changed

+86
-3
lines changed

4 files changed

+86
-3
lines changed

base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/basic/EnumerationTests.java

+23
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,29 @@ public void testEnum10823() {
872872
runConformTest(sources, "42null");
873873
}
874874

875+
@Test
876+
public void testEnum10845() {
877+
//@formatter:off
878+
String[] sources = {
879+
"Script.groovy",
880+
"@groovy.transform.TupleConstructor(defaults=false)\n" +
881+
"@groovy.transform.TypeChecked\n" +
882+
"enum E {\n" +
883+
" X\n" +
884+
" final Number number\n" +
885+
"}\n",
886+
};
887+
//@formatter:on
888+
889+
runNegativeTest(sources,
890+
"----------\n" +
891+
"1. ERROR in Script.groovy (at line 4)\n" +
892+
"\tX\n" +
893+
"\t^\n" +
894+
"Groovy:[Static type checking] - Cannot find matching constructor E()\n" +
895+
"----------\n");
896+
}
897+
875898
@Test
876899
public void testEnumValues_GRE1071() {
877900
//@formatter:off

base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java

+21-1
Original file line numberDiff line numberDiff line change
@@ -3115,7 +3115,19 @@ public void visitStaticMethodCallExpression(final StaticMethodCallExpression cal
31153115
addStaticTypeError("cannot resolve dynamic method name at compile time.", call);
31163116
return;
31173117
}
3118-
3118+
// GRECLIPSE add -- GROOVY-10845: $INIT(Object[]) delegates
3119+
if (name.equals("$INIT") && call.getOwnerType().isEnum()) {
3120+
FieldExpression target = (FieldExpression) typeCheckingContext.getEnclosingBinaryExpression().getLeftExpression();
3121+
ConstructorCallExpression cce = new ConstructorCallExpression(call.getOwnerType(), call.getArguments());
3122+
cce.setSourcePosition(target.getField());
3123+
visitConstructorCallExpression(cce);
3124+
//
3125+
MethodNode init = call.getOwnerType().getDeclaredMethods("$INIT").get(0);
3126+
call.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, init.getReturnType());
3127+
call.putNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET, init);
3128+
return;
3129+
}
3130+
// GRECLIPSE end
31193131
if (extension.beforeMethodCall(call)) {
31203132
extension.afterMethodCall(call);
31213133
return;
@@ -6612,6 +6624,14 @@ protected void addStaticTypeError(final String msg, final ASTNode expr) {
66126624
}
66136625

66146626
protected void addNoMatchingMethodError(ClassNode receiver, final String name, final ClassNode[] args, final Expression call) {
6627+
// GRECLIPSE add -- GROOVY-10845
6628+
if ("<init>".equals(name)) {
6629+
// remove implicit agruments [String, int] from enum constant construction
6630+
ClassNode[] actual = (receiver.isEnum() && args.length >= 2) ? Arrays.copyOfRange(args, 2, args.length) : args;
6631+
addStaticTypeError("Cannot find matching constructor " + prettyPrintTypeName(receiver) + toMethodParametersString("", actual), call);
6632+
return;
6633+
}
6634+
// GRECLIPSE end
66156635
if (isClassClassNodeWrappingConcreteType(receiver)) {
66166636
receiver = receiver.getGenericsTypes()[0].getType();
66176637
}

base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java

+21-1
Original file line numberDiff line numberDiff line change
@@ -2957,7 +2957,19 @@ public void visitStaticMethodCallExpression(final StaticMethodCallExpression cal
29572957
addStaticTypeError("cannot resolve dynamic method name at compile time.", call);
29582958
return;
29592959
}
2960-
2960+
// GRECLIPSE add -- GROOVY-10845: $INIT(Object[]) delegates
2961+
if (name.equals("$INIT") && call.getOwnerType().isEnum()) {
2962+
FieldExpression target = (FieldExpression) typeCheckingContext.getEnclosingBinaryExpression().getLeftExpression();
2963+
ConstructorCallExpression cce = new ConstructorCallExpression(call.getOwnerType(), call.getArguments());
2964+
cce.setSourcePosition(target.getField());
2965+
visitConstructorCallExpression(cce);
2966+
//
2967+
MethodNode init = call.getOwnerType().getDeclaredMethods("$INIT").get(0);
2968+
call.putNodeMetaData(INFERRED_TYPE, init.getReturnType());
2969+
call.putNodeMetaData(DIRECT_METHOD_CALL_TARGET, init);
2970+
return;
2971+
}
2972+
// GRECLIPSE end
29612973
if (extension.beforeMethodCall(call)) {
29622974
extension.afterMethodCall(call);
29632975
return;
@@ -6384,6 +6396,14 @@ protected void addStaticTypeError(final String msg, final ASTNode expr) {
63846396
}
63856397

63866398
protected void addNoMatchingMethodError(ClassNode receiver, final String name, final ClassNode[] args, final Expression call) {
6399+
// GRECLIPSE add -- GROOVY-10845
6400+
if ("<init>".equals(name)) {
6401+
// remove implicit agruments [String, int] from enum constant construction
6402+
ClassNode[] actual = (receiver.isEnum() && args.length >= 2) ? Arrays.copyOfRange(args, 2, args.length) : args;
6403+
addStaticTypeError("Cannot find matching constructor " + prettyPrintTypeName(receiver) + toMethodParametersString("", actual), call);
6404+
return;
6405+
}
6406+
// GRECLIPSE end
63876407
if (isClassClassNodeWrappingConcreteType(receiver)) {
63886408
receiver = receiver.getGenericsTypes()[0].getType();
63896409
}

base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java

+21-1
Original file line numberDiff line numberDiff line change
@@ -2706,7 +2706,19 @@ public void visitStaticMethodCallExpression(final StaticMethodCallExpression cal
27062706
addStaticTypeError("cannot resolve dynamic method name at compile time.", call);
27072707
return;
27082708
}
2709-
2709+
// GRECLIPSE add -- GROOVY-10845: $INIT(Object[]) delegates
2710+
if (name.equals("$INIT") && call.getOwnerType().isEnum()) {
2711+
FieldExpression target = (FieldExpression) typeCheckingContext.getEnclosingBinaryExpression().getLeftExpression();
2712+
ConstructorCallExpression cce = new ConstructorCallExpression(call.getOwnerType(), call.getArguments());
2713+
cce.setSourcePosition(target.getField());
2714+
visitConstructorCallExpression(cce);
2715+
//
2716+
MethodNode init = call.getOwnerType().getDeclaredMethods("$INIT").get(0);
2717+
call.putNodeMetaData(INFERRED_TYPE, init.getReturnType());
2718+
call.putNodeMetaData(DIRECT_METHOD_CALL_TARGET, init);
2719+
return;
2720+
}
2721+
// GRECLIPSE end
27102722
if (extension.beforeMethodCall(call)) {
27112723
extension.afterMethodCall(call);
27122724
return;
@@ -5912,6 +5924,14 @@ protected void addStaticTypeError(final String msg, final ASTNode expr) {
59125924
}
59135925

59145926
protected void addNoMatchingMethodError(ClassNode receiver, final String name, final ClassNode[] args, final Expression call) {
5927+
// GRECLIPSE add -- GROOVY-10845
5928+
if ("<init>".equals(name)) {
5929+
// remove implicit agruments [String, int] from enum constant construction
5930+
ClassNode[] actual = (receiver.isEnum() && args.length >= 2) ? Arrays.copyOfRange(args, 2, args.length) : args;
5931+
addStaticTypeError("Cannot find matching constructor " + prettyPrintTypeName(receiver) + toMethodParametersString("", actual), call);
5932+
return;
5933+
}
5934+
// GRECLIPSE end
59155935
if (isClassClassNodeWrappingConcreteType(receiver)) {
59165936
receiver = receiver.getGenericsTypes()[0].getType();
59175937
}

0 commit comments

Comments
 (0)