Skip to content

Commit 24282be

Browse files
committed
GROOVY-10939
1 parent c2b3534 commit 24282be

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

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

+10-7
Original file line numberDiff line numberDiff line change
@@ -4111,9 +4111,8 @@ public void visitMethodCallExpression(MethodCallExpression call) {
41114111
mn = accessibleMethods;
41124112
if (accessibleMethods.isEmpty()) {
41134113
// choose an arbitrary method to display an error message
4114-
MethodNode node = inaccessibleMethods.get(0);
4115-
ClassNode owner = node.getDeclaringClass();
4116-
addStaticTypeError("Non-static method " + owner.getName() + "#" + node.getName() + " cannot be called from static context", call);
4114+
MethodNode inaccessibleMethod = inaccessibleMethods.get(0);
4115+
addStaticTypeError("Non-static method " + prettyPrintTypeName(inaccessibleMethod.getDeclaringClass()) + "#" + inaccessibleMethod.getName() + " cannot be called from static context", call);
41174116
}
41184117
}
41194118

@@ -4141,19 +4140,24 @@ public void visitMethodCallExpression(MethodCallExpression call) {
41414140
mn = disambiguateMethods(mn, chosenReceiver != null ? chosenReceiver.getType() : null, args, call);
41424141
if (mn.size() == 1) {
41434142
MethodNode directMethodCallCandidate = mn.get(0);
4143+
ClassNode declaringClass = directMethodCallCandidate.getDeclaringClass();
4144+
if (chosenReceiver == null) chosenReceiver = Receiver.make(declaringClass);
4145+
/* GRECLIPSE edit -- GROOVY-10939
41444146
if (call.getNodeMetaData(StaticTypesMarker.DYNAMIC_RESOLUTION) == null &&
41454147
!directMethodCallCandidate.isStatic() && objectExpression instanceof ClassExpression &&
41464148
!"java.lang.Class".equals(directMethodCallCandidate.getDeclaringClass().getName())) {
41474149
ClassNode owner = directMethodCallCandidate.getDeclaringClass();
41484150
addStaticTypeError("Non-static method " + owner.getName() + "#" + directMethodCallCandidate.getName() + " cannot be called from static context", call);
41494151
}
4152+
*/
4153+
if (!directMethodCallCandidate.isStatic() && !(declaringClass.equals(CLASS_Type) || declaringClass.equals(OBJECT_TYPE))
4154+
&& receiver.equals(CLASS_Type) && chosenReceiver.getData() == null && !Boolean.TRUE.equals(call.getNodeMetaData(StaticTypesMarker.DYNAMIC_RESOLUTION))) {
4155+
addStaticTypeError("Non-static method " + prettyPrintTypeName(declaringClass) + "#" + directMethodCallCandidate.getName() + " cannot be called from static context", call);
4156+
}
41504157
// GRECLIPSE add -- GROOVY-10341
41514158
else if (directMethodCallCandidate.isAbstract() && isSuperExpression(objectExpression))
41524159
addStaticTypeError("Abstract method " + toMethodParametersString(directMethodCallCandidate.getName(), extractTypesFromParameters(directMethodCallCandidate.getParameters())) + " cannot be called directly", call);
41534160
// GRECLIPSE end
4154-
if (chosenReceiver == null) {
4155-
chosenReceiver = Receiver.make(directMethodCallCandidate.getDeclaringClass());
4156-
}
41574161

41584162
ClassNode returnType = getType(directMethodCallCandidate);
41594163

@@ -4190,7 +4194,6 @@ else if (directMethodCallCandidate.isAbstract() && isSuperExpression(objectExpre
41904194

41914195
storeType(call, returnType);
41924196
storeTargetMethod(call, directMethodCallCandidate);
4193-
ClassNode declaringClass = directMethodCallCandidate.getDeclaringClass();
41944197
if (declaringClass.isInterface() && directMethodCallCandidate.isStatic() && !(directMethodCallCandidate instanceof ExtensionMethodNode)) {
41954198
typeCheckingContext.getEnclosingClassNode().putNodeMetaData(MINIMUM_BYTECODE_VERSION, Opcodes.V1_8);
41964199
}

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

+10-6
Original file line numberDiff line numberDiff line change
@@ -3859,9 +3859,8 @@ public void visitMethodCallExpression(final MethodCallExpression call) {
38593859
mn = accessibleMethods;
38603860
if (accessibleMethods.isEmpty()) {
38613861
// choose an arbitrary method to display an error message
3862-
MethodNode node = inaccessibleMethods.get(0);
3863-
ClassNode owner = node.getDeclaringClass();
3864-
addStaticTypeError("Non-static method " + owner.getName() + "#" + node.getName() + " cannot be called from static context", call);
3862+
MethodNode inaccessibleMethod = inaccessibleMethods.get(0);
3863+
addStaticTypeError("Non-static method " + prettyPrintTypeName(inaccessibleMethod.getDeclaringClass()) + "#" + inaccessibleMethod.getName() + " cannot be called from static context", call);
38653864
}
38663865
}
38673866

@@ -3890,19 +3889,24 @@ public void visitMethodCallExpression(final MethodCallExpression call) {
38903889

38913890
if (mn.size() == 1) {
38923891
MethodNode directMethodCallCandidate = mn.get(0);
3892+
ClassNode declaringClass = directMethodCallCandidate.getDeclaringClass();
3893+
if (chosenReceiver == null) chosenReceiver = Receiver.make(declaringClass);
3894+
/* GRECLIPSE edit -- GROOVY-10939
38933895
if (call.getNodeMetaData(DYNAMIC_RESOLUTION) == null
38943896
&& !directMethodCallCandidate.isStatic() && objectExpression instanceof ClassExpression
38953897
&& !"java.lang.Class".equals(directMethodCallCandidate.getDeclaringClass().getName())) {
38963898
ClassNode owner = directMethodCallCandidate.getDeclaringClass();
38973899
addStaticTypeError("Non-static method " + owner.getName() + "#" + directMethodCallCandidate.getName() + " cannot be called from static context", call);
38983900
}
3901+
*/
3902+
if (!directMethodCallCandidate.isStatic() && !(declaringClass.equals(CLASS_Type) || declaringClass.equals(OBJECT_TYPE))
3903+
&& receiver.equals(CLASS_Type) && chosenReceiver.getData() == null && !Boolean.TRUE.equals(call.getNodeMetaData(DYNAMIC_RESOLUTION))) {
3904+
addStaticTypeError("Non-static method " + prettyPrintTypeName(declaringClass) + "#" + directMethodCallCandidate.getName() + " cannot be called from static context", call);
3905+
}
38993906
// GRECLIPSE add -- GROOVY-10341
39003907
else if (directMethodCallCandidate.isAbstract() && isSuperExpression(objectExpression))
39013908
addStaticTypeError("Abstract method " + toMethodParametersString(directMethodCallCandidate.getName(), extractTypesFromParameters(directMethodCallCandidate.getParameters())) + " cannot be called directly", call);
39023909
// GRECLIPSE end
3903-
if (chosenReceiver == null) {
3904-
chosenReceiver = Receiver.make(directMethodCallCandidate.getDeclaringClass());
3905-
}
39063910

39073911
ClassNode returnType = getType(directMethodCallCandidate);
39083912
if (isUsingGenericsOrIsArrayUsingGenerics(returnType)) {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -3503,8 +3503,8 @@ public void visitMethodCallExpression(final MethodCallExpression call) {
35033503
if (chosenReceiver == null) {
35043504
chosenReceiver = Receiver.make(declaringClass.getPlainNodeReference());
35053505
}
3506-
if (!targetMethodCandidate.isStatic() && !isClassType(declaringClass) && isClassType(receiver)
3507-
&& chosenReceiver.getData() == null && call.getNodeMetaData(DYNAMIC_RESOLUTION) == null) {
3506+
if (!targetMethodCandidate.isStatic() && !(isClassType(declaringClass) || isObjectType(declaringClass)) // GROOVY-10939: Class or Object
3507+
&& isClassType(receiver) && chosenReceiver.getData() == null && !Boolean.TRUE.equals(call.getNodeMetaData(DYNAMIC_RESOLUTION))) {
35083508
addStaticTypeError("Non-static method " + prettyPrintTypeName(declaringClass) + "#" + targetMethodCandidate.getName() + " cannot be called from static context", call);
35093509
} else if (targetMethodCandidate.isAbstract() && isSuperExpression(objectExpression)) { // GROOVY-10341
35103510
String target = toMethodParametersString(targetMethodCandidate.getName(), extractTypesFromParameters(targetMethodCandidate.getParameters()));

0 commit comments

Comments
 (0)