Skip to content

Commit d53cebd

Browse files
committed
minor items
1 parent b170a90 commit d53cebd

File tree

6 files changed

+151
-9
lines changed

6 files changed

+151
-9
lines changed

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

+33
Original file line numberDiff line numberDiff line change
@@ -2823,4 +2823,37 @@ public void testUpperBounds5() {
28232823
"Groovy:The type Integer is not a valid substitute for the bounded parameter <T extends java.lang.Number & p.I>\n" +
28242824
"----------\n");
28252825
}
2826+
2827+
@Test // https://issues.apache.org/jira/browse/GROOVY-10797
2828+
public void testUpperBounds6() {
2829+
//@formatter:off
2830+
String[] sources = {
2831+
"Main.java",
2832+
"public class Main {\n" +
2833+
" public static void main(String[] args) {\n" +
2834+
" p.C.test();\n" +
2835+
" }\n" +
2836+
"}\n",
2837+
2838+
"p/A.java",
2839+
"package p;\n" +
2840+
"public class A<T> {\n" +
2841+
"}\n",
2842+
2843+
"p/B.java",
2844+
"package p;\n" +
2845+
"public class B<T extends A<?>> {\n" +
2846+
"}\n",
2847+
2848+
"p/C.groovy",
2849+
"package p\n" +
2850+
"class C {\n" +
2851+
" static <T extends A> B<T> test() {\n" +
2852+
" }\n" +
2853+
"}\n",
2854+
};
2855+
//@formatter:on
2856+
2857+
runConformTest(sources);
2858+
}
28262859
}

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

+79
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,56 @@ public void testTypeChecked28() {
695695
runConformTest(sources, "[foo:1, bar:2, baz:3.14]");
696696
}
697697

698+
@Test
699+
public void testTypeChecked29() {
700+
//@formatter:off
701+
String[] sources = {
702+
"Main.groovy",
703+
"@groovy.transform.TypeChecked\n" +
704+
"class C {\n" +
705+
" def m() {\n" +
706+
" }\n" +
707+
" static {\n" +
708+
" m()\n" +
709+
" }\n" +
710+
"}\n",
711+
};
712+
//@formatter:on
713+
714+
runNegativeTest(sources,
715+
"----------\n" +
716+
"1. ERROR in Main.groovy (at line 6)\n" +
717+
"\tm()\n" +
718+
"\t^^^\n" +
719+
"Groovy:[Static type checking] - Non-static method C#m cannot be called from static context\n" +
720+
"----------\n");
721+
}
722+
723+
@Test
724+
public void testTypeChecked30() {
725+
//@formatter:off
726+
String[] sources = {
727+
"Main.groovy",
728+
"@groovy.transform.TypeChecked\n" +
729+
"class C {\n" +
730+
" def m() {\n" +
731+
" }\n" +
732+
" static sm() {\n" +
733+
" m()\n" +
734+
" }\n" +
735+
"}\n",
736+
};
737+
//@formatter:on
738+
739+
runNegativeTest(sources,
740+
"----------\n" +
741+
"1. ERROR in Main.groovy (at line 6)\n" +
742+
"\tm()\n" +
743+
"\t^^^\n" +
744+
"Groovy:[Static type checking] - Non-static method C#m cannot be called from static context\n" +
745+
"----------\n");
746+
}
747+
698748
@Test
699749
public void testTypeChecked5450() {
700750
//@formatter:off
@@ -2541,6 +2591,35 @@ public void testTypeChecked9412() {
25412591
runConformTest(sources);
25422592
}
25432593

2594+
@Test
2595+
public void testTypeChecked9415() {
2596+
//@formatter:off
2597+
String[] sources = {
2598+
"Main.groovy",
2599+
"class C {\n" +
2600+
" static <T> T getAt(T t) { t }\n" +
2601+
"}\n" +
2602+
"@groovy.transform.TypeChecked\n" +
2603+
"void test() {\n" +
2604+
" print C[1]\n" +
2605+
"}\n" +
2606+
"test()\n",
2607+
};
2608+
//@formatter:on
2609+
2610+
if (isAtLeastGroovy(40)) {
2611+
runConformTest(sources, "1");
2612+
} else {
2613+
runNegativeTest(sources,
2614+
"----------\n" +
2615+
"1. ERROR in Main.groovy (at line 6)\n" +
2616+
"\tprint C[1]\n" +
2617+
"\t ^^^^\n" +
2618+
"Groovy:[Static type checking] - Cannot find matching method java.lang.Class#getAt(int). Please check if the declared type is correct and if the method exists.\n" +
2619+
"----------\n");
2620+
}
2621+
}
2622+
25442623
@Test
25452624
public void testTypeChecked9460() {
25462625
//@formatter:off

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

+18-3
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@
141141
import java.util.stream.IntStream;
142142
import java.util.stream.Stream;
143143

144-
import static java.util.stream.Collectors.toMap;
144+
import static java.util.stream.Collectors.*;
145145
import static org.apache.groovy.ast.tools.ClassNodeUtils.samePackageName;
146146
import static org.apache.groovy.ast.tools.ExpressionUtils.isThisExpression;
147147
import static org.apache.groovy.ast.tools.ExpressionUtils.isSuperExpression;
@@ -2024,6 +2024,7 @@ private ClassNode getTypeForMapPropertyExpression(ClassNode testClass, ClassNode
20242024
private <T> T allowStaticAccessToMember(T member, boolean staticOnly) {
20252025
if (member == null) return null;
20262026
if (!staticOnly) return member;
2027+
/* GRECLIPSE edit
20272028
boolean isStatic;
20282029
if (member instanceof Variable) {
20292030
Variable v = (Variable) member;
@@ -2040,6 +2041,20 @@ private <T> T allowStaticAccessToMember(T member, boolean staticOnly) {
20402041
}
20412042
if (staticOnly && !isStatic) return null;
20422043
return member;
2044+
*/
2045+
if (member instanceof List) {
2046+
return (T) ((List<MethodNode>) member).stream()
2047+
.map(m -> allowStaticAccessToMember(m,true))
2048+
.filter(Objects::nonNull).collect(toList());
2049+
}
2050+
boolean isStatic;
2051+
if (member instanceof Variable) {
2052+
isStatic = Modifier.isStatic(((Variable) member).getModifiers());
2053+
} else { // assume member instanceof MethodNode
2054+
isStatic = member instanceof ExtensionMethodNode ? ((ExtensionMethodNode) member).isStaticExtension() : ((MethodNode) member).isStatic();
2055+
}
2056+
return (isStatic ? member : null);
2057+
// GRECLIPSE end
20432058
}
20442059

20452060
private void storeWithResolve(ClassNode typeToResolve, ClassNode receiver, ClassNode declaringClass, boolean isStatic, PropertyExpression expressionToStoreOn) {
@@ -4072,7 +4087,7 @@ public void visitMethodCallExpression(MethodCallExpression call) {
40724087
// choose an arbitrary method to display an error message
40734088
MethodNode node = inaccessibleMethods.get(0);
40744089
ClassNode owner = node.getDeclaringClass();
4075-
addStaticTypeError("Non static method " + owner.getName() + "#" + node.getName() + " cannot be called from static context", call);
4090+
addStaticTypeError("Non-static method " + owner.getName() + "#" + node.getName() + " cannot be called from static context", call);
40764091
}
40774092
}
40784093

@@ -4104,7 +4119,7 @@ public void visitMethodCallExpression(MethodCallExpression call) {
41044119
!directMethodCallCandidate.isStatic() && objectExpression instanceof ClassExpression &&
41054120
!"java.lang.Class".equals(directMethodCallCandidate.getDeclaringClass().getName())) {
41064121
ClassNode owner = directMethodCallCandidate.getDeclaringClass();
4107-
addStaticTypeError("Non static method " + owner.getName() + "#" + directMethodCallCandidate.getName() + " cannot be called from static context", call);
4122+
addStaticTypeError("Non-static method " + owner.getName() + "#" + directMethodCallCandidate.getName() + " cannot be called from static context", call);
41084123
}
41094124
// GRECLIPSE add -- GROOVY-10341
41104125
else if (directMethodCallCandidate.isAbstract() && isSuperExpression(objectExpression))

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

+17-2
Original file line numberDiff line numberDiff line change
@@ -1965,6 +1965,7 @@ private ClassNode getTypeForMapPropertyExpression(final ClassNode testClass, fin
19651965
private <T> T allowStaticAccessToMember(final T member, final boolean staticOnly) {
19661966
if (member == null) return null;
19671967
if (!staticOnly) return member;
1968+
/* GRECLIPSE edit
19681969
boolean isStatic;
19691970
if (member instanceof Variable) {
19701971
Variable v = (Variable) member;
@@ -1981,6 +1982,20 @@ private <T> T allowStaticAccessToMember(final T member, final boolean staticOnly
19811982
}
19821983
if (staticOnly && !isStatic) return null;
19831984
return member;
1985+
*/
1986+
if (member instanceof List) {
1987+
return (T) ((List<MethodNode>) member).stream()
1988+
.map(m -> allowStaticAccessToMember(m,true))
1989+
.filter(Objects::nonNull).collect(toList());
1990+
}
1991+
boolean isStatic;
1992+
if (member instanceof Variable) {
1993+
isStatic = Modifier.isStatic(((Variable) member).getModifiers());
1994+
} else { // assume member instanceof MethodNode
1995+
isStatic = member instanceof ExtensionMethodNode ? ((ExtensionMethodNode) member).isStaticExtension() : ((MethodNode) member).isStatic();
1996+
}
1997+
return (isStatic ? member : null);
1998+
// GRECLIPSE end
19841999
}
19852000

19862001
private void storeWithResolve(ClassNode type, final ClassNode receiver, final ClassNode declaringClass, final boolean isStatic, final Expression expressionToStoreOn) {
@@ -3840,7 +3855,7 @@ public void visitMethodCallExpression(final MethodCallExpression call) {
38403855
// choose an arbitrary method to display an error message
38413856
MethodNode node = inaccessibleMethods.get(0);
38423857
ClassNode owner = node.getDeclaringClass();
3843-
addStaticTypeError("Non static method " + owner.getName() + "#" + node.getName() + " cannot be called from static context", call);
3858+
addStaticTypeError("Non-static method " + owner.getName() + "#" + node.getName() + " cannot be called from static context", call);
38443859
}
38453860
}
38463861

@@ -3873,7 +3888,7 @@ public void visitMethodCallExpression(final MethodCallExpression call) {
38733888
&& !directMethodCallCandidate.isStatic() && objectExpression instanceof ClassExpression
38743889
&& !"java.lang.Class".equals(directMethodCallCandidate.getDeclaringClass().getName())) {
38753890
ClassNode owner = directMethodCallCandidate.getDeclaringClass();
3876-
addStaticTypeError("Non static method " + owner.getName() + "#" + directMethodCallCandidate.getName() + " cannot be called from static context", call);
3891+
addStaticTypeError("Non-static method " + owner.getName() + "#" + directMethodCallCandidate.getName() + " cannot be called from static context", call);
38773892
}
38783893
// GRECLIPSE add -- GROOVY-10341
38793894
else if (directMethodCallCandidate.isAbstract() && isSuperExpression(objectExpression))

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1812,10 +1812,10 @@ private <T> T allowStaticAccessToMember(final T member, final boolean staticOnly
18121812
boolean isStatic;
18131813
if (member instanceof FieldNode) {
18141814
isStatic = ((FieldNode) member).isStatic();
1815-
} else if (member instanceof MethodNode) {
1816-
isStatic = ((MethodNode) member).isStatic();
1817-
} else {
1815+
} else if (member instanceof PropertyNode) {
18181816
isStatic = ((PropertyNode) member).isStatic();
1817+
} else { // assume member instanceof MethodNode
1818+
isStatic = member instanceof ExtensionMethodNode ? ((ExtensionMethodNode) member).isStaticExtension() : ((MethodNode) member).isStatic();
18191819
}
18201820
return (isStatic ? member : null);
18211821
}

base/org.eclipse.jdt.groovy.core/src/org/codehaus/jdt/groovy/model/GroovyCompilationUnit.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ protected boolean buildStructure(OpenableElementInfo info, IProgressMonitor pm,
220220
options.put(CompilerOptions.OPTIONG_GroovyProjectName, project.getElementName());
221221
options.put(CompilerOptions.OPTIONG_BuildGroovyFiles, CompilerOptions.ENABLED);
222222
if (!computeProblems) {
223-
// disable task tags checking to speed up parsing
223+
// disable task tags checking for faster parsing
224224
options.remove(CompilerOptions.OPTION_TaskTags);
225225
}
226226
options.putAll(getCustomOptions());

0 commit comments

Comments
 (0)