Skip to content

Commit

Permalink
minor items
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Nov 19, 2022
1 parent b170a90 commit ca06efa
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2823,4 +2823,37 @@ public void testUpperBounds5() {
"Groovy:The type Integer is not a valid substitute for the bounded parameter <T extends java.lang.Number & p.I>\n" +
"----------\n");
}

@Test // https://issues.apache.org/jira/browse/GROOVY-10797
public void testUpperBounds6() {
//@formatter:off
String[] sources = {
"Main.java",
"public class Main {\n" +
" public static void main(String[] args) {\n" +
" p.C.test();\n" +
" }\n" +
"}\n",

"p/A.java",
"package p;\n" +
"public class A<T> {\n" +
"}\n",

"p/B.java",
"package p;\n" +
"public class B<T extends A<?>> {\n" +
"}\n",

"p/C.groovy",
"package p\n" +
"class C {\n" +
" static <T extends A> B<T> test() {\n" +
" }\n" +
"}\n",
};
//@formatter:on

runConformTest(sources);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,56 @@ public void testTypeChecked28() {
runConformTest(sources, "[foo:1, bar:2, baz:3.14]");
}

@Test
public void testTypeChecked29() {
//@formatter:off
String[] sources = {
"Main.groovy",
"@groovy.transform.TypeChecked\n" +
"class C {\n" +
" def m() {\n" +
" }\n" +
" static {\n" +
" m()\n" +
" }\n" +
"}\n",
};
//@formatter:on

runNegativeTest(sources,
"----------\n" +
"1. ERROR in Main.groovy (at line 6)\n" +
"\tm()\n" +
"\t^^^\n" +
"Groovy:[Static type checking] - Non-static method C#m cannot be called from static context\n" +
"----------\n");
}

@Test
public void testTypeChecked30() {
//@formatter:off
String[] sources = {
"Main.groovy",
"@groovy.transform.TypeChecked\n" +
"class C {\n" +
" def m() {\n" +
" }\n" +
" static sm() {\n" +
" m()\n" +
" }\n" +
"}\n",
};
//@formatter:on

runNegativeTest(sources,
"----------\n" +
"1. ERROR in Main.groovy (at line 6)\n" +
"\tm()\n" +
"\t^^^\n" +
"Groovy:[Static type checking] - Non-static method C#m cannot be called from static context\n" +
"----------\n");
}

@Test
public void testTypeChecked5450() {
//@formatter:off
Expand Down Expand Up @@ -2541,6 +2591,35 @@ public void testTypeChecked9412() {
runConformTest(sources);
}

@Test
public void testTypeChecked9415() {
//@formatter:off
String[] sources = {
"Main.groovy",
"class C {\n" +
" static <T> T getAt(T t) { t }\n" +
"}\n" +
"@groovy.transform.TypeChecked\n" +
"void test() {\n" +
" print C[1]\n" +
"}\n" +
"test()\n",
};
//@formatter:on

if (isAtLeastGroovy(40)) {
runConformTest(sources, "1");
} else {
runNegativeTest(sources,
"----------\n" +
"1. ERROR in Main.groovy (at line 6)\n" +
"\tprint C[1]\n" +
"\t ^^^^\n" +
"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" +
"----------\n");
}
}

@Test
public void testTypeChecked9460() {
//@formatter:off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toMap;
import static java.util.stream.Collectors.*;
import static org.apache.groovy.ast.tools.ClassNodeUtils.samePackageName;
import static org.apache.groovy.ast.tools.ExpressionUtils.isThisExpression;
import static org.apache.groovy.ast.tools.ExpressionUtils.isSuperExpression;
Expand Down Expand Up @@ -2024,6 +2024,7 @@ private ClassNode getTypeForMapPropertyExpression(ClassNode testClass, ClassNode
private <T> T allowStaticAccessToMember(T member, boolean staticOnly) {
if (member == null) return null;
if (!staticOnly) return member;
/* GRECLIPSE edit
boolean isStatic;
if (member instanceof Variable) {
Variable v = (Variable) member;
Expand All @@ -2040,6 +2041,20 @@ private <T> T allowStaticAccessToMember(T member, boolean staticOnly) {
}
if (staticOnly && !isStatic) return null;
return member;
*/
if (member instanceof List) {
return (T) ((List<MethodNode>) member).stream()
.map(m -> allowStaticAccessToMember(m,true))
.filter(Objects::nonNull).collect(toList());
}
boolean isStatic;
if (member instanceof Variable) {
isStatic = Modifier.isStatic(((Variable) member).getModifiers());
} else { // assume member instanceof MethodNode
isStatic = member instanceof ExtensionMethodNode ? ((ExtensionMethodNode) member).isStaticExtension() : ((MethodNode) member).isStatic();
}
return (isStatic ? member : null);
// GRECLIPSE end
}

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

Expand Down Expand Up @@ -4104,7 +4119,7 @@ public void visitMethodCallExpression(MethodCallExpression call) {
!directMethodCallCandidate.isStatic() && objectExpression instanceof ClassExpression &&
!"java.lang.Class".equals(directMethodCallCandidate.getDeclaringClass().getName())) {
ClassNode owner = directMethodCallCandidate.getDeclaringClass();
addStaticTypeError("Non static method " + owner.getName() + "#" + directMethodCallCandidate.getName() + " cannot be called from static context", call);
addStaticTypeError("Non-static method " + owner.getName() + "#" + directMethodCallCandidate.getName() + " cannot be called from static context", call);
}
// GRECLIPSE add -- GROOVY-10341
else if (directMethodCallCandidate.isAbstract() && isSuperExpression(objectExpression))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1965,6 +1965,7 @@ private ClassNode getTypeForMapPropertyExpression(final ClassNode testClass, fin
private <T> T allowStaticAccessToMember(final T member, final boolean staticOnly) {
if (member == null) return null;
if (!staticOnly) return member;
/* GRECLIPSE edit
boolean isStatic;
if (member instanceof Variable) {
Variable v = (Variable) member;
Expand All @@ -1981,6 +1982,20 @@ private <T> T allowStaticAccessToMember(final T member, final boolean staticOnly
}
if (staticOnly && !isStatic) return null;
return member;
*/
if (member instanceof List) {
return (T) ((List<MethodNode>) member).stream()
.map(m -> allowStaticAccessToMember(m,true))
.filter(Objects::nonNull).collect(toList());
}
boolean isStatic;
if (member instanceof Variable) {
isStatic = Modifier.isStatic(((Variable) member).getModifiers());
} else { // assume member instanceof MethodNode
isStatic = member instanceof ExtensionMethodNode ? ((ExtensionMethodNode) member).isStaticExtension() : ((MethodNode) member).isStatic();
}
return (isStatic ? member : null);
// GRECLIPSE end
}

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

Expand Down Expand Up @@ -3873,7 +3888,7 @@ public void visitMethodCallExpression(final MethodCallExpression call) {
&& !directMethodCallCandidate.isStatic() && objectExpression instanceof ClassExpression
&& !"java.lang.Class".equals(directMethodCallCandidate.getDeclaringClass().getName())) {
ClassNode owner = directMethodCallCandidate.getDeclaringClass();
addStaticTypeError("Non static method " + owner.getName() + "#" + directMethodCallCandidate.getName() + " cannot be called from static context", call);
addStaticTypeError("Non-static method " + owner.getName() + "#" + directMethodCallCandidate.getName() + " cannot be called from static context", call);
}
// GRECLIPSE add -- GROOVY-10341
else if (directMethodCallCandidate.isAbstract() && isSuperExpression(objectExpression))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1812,10 +1812,10 @@ private <T> T allowStaticAccessToMember(final T member, final boolean staticOnly
boolean isStatic;
if (member instanceof FieldNode) {
isStatic = ((FieldNode) member).isStatic();
} else if (member instanceof MethodNode) {
isStatic = ((MethodNode) member).isStatic();
} else {
} else if (member instanceof PropertyNode) {
isStatic = ((PropertyNode) member).isStatic();
} else { // assume member instanceof MethodNode
isStatic = member instanceof ExtensionMethodNode ? ((ExtensionMethodNode) member).isStaticExtension() : ((MethodNode) member).isStatic();
}
return (isStatic ? member : null);
}
Expand Down

0 comments on commit ca06efa

Please sign in to comment.