mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-12 20:07:32 +00:00
Non-static method call from static context validation
This commit is contained in:
1 parent
9c1fadf493
commit
61fb092c82
3 files changed
+20
-17
No files matched your search
@@ -1939,7 +1939,6 @@ public class ScriptDocument {
|
||||
// Resolve receiver using existing chain-based resolver and detect static access
|
||||
TypeInfo receiverType = resolveReceiverChain(nameStart);
|
||||
MethodInfo resolvedMethod = null;
|
||||
boolean computedStaticAccess = isStaticAccessCall(nameStart);
|
||||
|
||||
if (receiverType != null) {
|
||||
if (receiverType.hasMethod(methodName)) {
|
||||
@@ -1947,23 +1946,26 @@ public class ScriptDocument {
|
||||
TypeInfo[] argTypes = arguments.stream().map(MethodCallInfo.Argument::getResolvedType).toArray(TypeInfo[]::new);
|
||||
// Get best method overload based on argument types
|
||||
resolvedMethod = receiverType.getBestMethodOverload(methodName, argTypes);
|
||||
|
||||
MethodCallInfo callInfo = new MethodCallInfo(
|
||||
methodName, nameStart, nameEnd, openParen, closeParen,
|
||||
arguments, receiverType, resolvedMethod, computedStaticAccess
|
||||
);
|
||||
|
||||
// Set expected type for validation if this is the final expression
|
||||
if (!isFollowedByDot(closeParen)) {
|
||||
TypeInfo expectedType = findExpectedTypeAtPosition(nameStart);
|
||||
if (expectedType != null) {
|
||||
callInfo.setExpectedType(expectedType);
|
||||
if (isStaticAccess && resolvedMethod != null && !resolvedMethod.isStatic()) {
|
||||
TokenErrorMessage errorMsg = TokenErrorMessage
|
||||
.from("Cannot call non-static method '" + methodName + "' from static context '" + receiverType.getSimpleName() + "'")
|
||||
.clearOtherErrors();
|
||||
marks.add(new ScriptLine.Mark(nameStart, nameEnd, TokenType.UNDEFINED_VAR, errorMsg));
|
||||
} else {
|
||||
MethodCallInfo callInfo = new MethodCallInfo(methodName, nameStart, nameEnd, openParen,
|
||||
closeParen, arguments, receiverType, resolvedMethod, isStaticAccess);
|
||||
// Set expected type for validation if this is the final expression
|
||||
if (!isFollowedByDot(closeParen)) {
|
||||
TypeInfo expectedType = findExpectedTypeAtPosition(nameStart);
|
||||
if (expectedType != null) {
|
||||
callInfo.setExpectedType(expectedType);
|
||||
}
|
||||
}
|
||||
|
||||
callInfo.validate();
|
||||
methodCalls.add(callInfo);
|
||||
marks.add(new ScriptLine.Mark(nameStart, nameEnd, TokenType.METHOD_CALL, callInfo));
|
||||
}
|
||||
|
||||
callInfo.validate();
|
||||
methodCalls.add(callInfo);
|
||||
marks.add(new ScriptLine.Mark(nameStart, nameEnd, TokenType.METHOD_CALL, callInfo));
|
||||
} else {
|
||||
marks.add(new ScriptLine.Mark(nameStart, nameEnd, TokenType.UNDEFINED_VAR));
|
||||
}
|
||||
|
||||
+1
@@ -324,6 +324,7 @@ public class MethodCallInfo {
|
||||
}
|
||||
|
||||
// Check static/instance access (skip for constructors)
|
||||
// NO LONGER CHECKED HERE, BUT DIRECTLY AT MARK CREATION
|
||||
if (!isConstructor && isStaticAccess && !resolvedMethod.isStatic()) {
|
||||
setError(ErrorType.STATIC_ACCESS_ERROR,
|
||||
"Cannot call instance method '" + methodName + "' on a class type");
|
||||
|
||||
+1
-1
@@ -126,7 +126,7 @@ public class ScriptTypeInfo extends TypeInfo {
|
||||
/**
|
||||
* Get all method overloads with the given name.
|
||||
*/
|
||||
public List<MethodInfo> getMethodOverloads(String methodName) {
|
||||
public List<MethodInfo> getAllMethodOverloads(String methodName) {
|
||||
return methods.getOrDefault(methodName, new ArrayList<>());
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user