mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-20 04:09:57 +00:00
Lambda impl Phase 1: Track inner callables as nested scopes so parameters and locals resolve correctly inside the callable body,
This commit is contained in:
1 parent
6c5a269966
commit
9e04a1184c
3 files changed
+605
No files matched your search
@@ -0,0 +1,99 @@
|
||||
package noppes.npcs.client.gui.util.script.interpreter;
|
||||
|
||||
import noppes.npcs.client.gui.util.script.interpreter.field.FieldInfo;
|
||||
import noppes.npcs.client.gui.util.script.interpreter.type.TypeInfo;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
|
||||
/**
|
||||
* Represents an inner callable scope (lambda or JS function expression).
|
||||
* These are NOT added to the methods list - they're resolution-only scopes.
|
||||
*/
|
||||
public class InnerCallableScope {
|
||||
|
||||
public enum Kind {
|
||||
JAVA_LAMBDA,
|
||||
JS_FUNCTION_EXPR
|
||||
}
|
||||
|
||||
private final Kind kind;
|
||||
private final int headerStart; // Start of "(params) ->" or "function(params)"
|
||||
private final int headerEnd; // End of header (position of "->" for lambda, ")" for JS func)
|
||||
private final int bodyStart; // Start of body (expression start or '{')
|
||||
private final int bodyEnd; // End of body (expression end or '}')
|
||||
private final List<FieldInfo> parameters = new ArrayList<>();
|
||||
private final Map<String, FieldInfo> locals = new HashMap<>();
|
||||
private InnerCallableScope parentScope; // Enclosing inner scope (for nested lambdas)
|
||||
private TypeInfo expectedType; // The expected functional interface type (set during resolution)
|
||||
|
||||
public InnerCallableScope(Kind kind, int headerStart, int headerEnd, int bodyStart, int bodyEnd) {
|
||||
this.kind = kind;
|
||||
this.headerStart = headerStart;
|
||||
this.headerEnd = headerEnd;
|
||||
this.bodyStart = bodyStart;
|
||||
this.bodyEnd = bodyEnd;
|
||||
}
|
||||
|
||||
// Getters
|
||||
public Kind getKind() { return kind; }
|
||||
public int getHeaderStart() { return headerStart; }
|
||||
public int getHeaderEnd() { return headerEnd; }
|
||||
public int getBodyStart() { return bodyStart; }
|
||||
public int getBodyEnd() { return bodyEnd; }
|
||||
public List<FieldInfo> getParameters() { return parameters; }
|
||||
public Map<String, FieldInfo> getLocals() { return locals; }
|
||||
public InnerCallableScope getParentScope() { return parentScope; }
|
||||
public TypeInfo getExpectedType() { return expectedType; }
|
||||
|
||||
// Setters
|
||||
public void setParentScope(InnerCallableScope parent) { this.parentScope = parent; }
|
||||
public void setExpectedType(TypeInfo type) { this.expectedType = type; }
|
||||
|
||||
public void addParameter(FieldInfo param) {
|
||||
parameters.add(param);
|
||||
}
|
||||
|
||||
public void addLocal(String name, FieldInfo local) {
|
||||
locals.put(name, local);
|
||||
}
|
||||
|
||||
public FieldInfo getParameter(String name) {
|
||||
for (FieldInfo param : parameters) {
|
||||
if (param.getName().equals(name)) {
|
||||
return param;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean hasParameter(String name) {
|
||||
return getParameter(name) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a position is inside this scope's body.
|
||||
*/
|
||||
public boolean containsPosition(int position) {
|
||||
return position >= bodyStart && position < bodyEnd;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a position is inside this scope's header (parameter list).
|
||||
*/
|
||||
public boolean containsHeaderPosition(int position) {
|
||||
return position >= headerStart && position < headerEnd;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the full range (header + body).
|
||||
*/
|
||||
public int getFullStart() {
|
||||
return headerStart;
|
||||
}
|
||||
|
||||
public int getFullEnd() {
|
||||
return bodyEnd;
|
||||
}
|
||||
}
|
||||
@@ -106,6 +106,9 @@ public class ScriptDocument {
|
||||
// Local variables per method (methodStartOffset -> {varName -> FieldInfo})
|
||||
private final Map<Integer, Map<String, FieldInfo>> methodLocals = new HashMap<>();
|
||||
|
||||
// Inner callable scopes (lambdas, JS function expressions) - NOT in methods list
|
||||
private final List<InnerCallableScope> innerScopes = new ArrayList<>();
|
||||
|
||||
// Method calls - stores all parsed method call information
|
||||
private final List<MethodCallInfo> methodCalls = new ArrayList<>();
|
||||
// Field accesses - stores all parsed field access information
|
||||
@@ -331,6 +334,7 @@ public class ScriptDocument {
|
||||
excludedRanges.clear();
|
||||
methodLocals.clear();
|
||||
scriptTypes.clear();
|
||||
innerScopes.clear();
|
||||
methodCalls.clear();
|
||||
externalFieldAssignments.clear();
|
||||
declarationErrors.clear();
|
||||
@@ -653,6 +657,9 @@ public class ScriptDocument {
|
||||
// Parse methods/functions - UNIFIED for both languages
|
||||
parseMethodDeclarations();
|
||||
|
||||
// Parse inner callable scopes (lambdas, JS function expressions)
|
||||
parseInnerCallableScopes();
|
||||
|
||||
// Parse local variables inside methods/functions - UNIFIED for both languages
|
||||
parseLocalVariables();
|
||||
|
||||
@@ -1532,6 +1539,322 @@ public class ScriptDocument {
|
||||
return currentType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse inner callable scopes (lambdas and JS function expressions).
|
||||
* These are NOT added to the methods list - they're resolution-only scopes.
|
||||
*/
|
||||
private void parseInnerCallableScopes() {
|
||||
// For Java: detect (params) -> expr and (params) -> { ... } and param -> expr
|
||||
// For JS: detect function(params) { ... } that are expressions (not declarations)
|
||||
|
||||
if (isJavaScript()) {
|
||||
parseJSFunctionExpressions();
|
||||
} else {
|
||||
parseJavaLambdas();
|
||||
}
|
||||
|
||||
// Sort by header start and set up parent relationships
|
||||
innerScopes.sort((a, b) -> Integer.compare(a.getHeaderStart(), b.getHeaderStart()));
|
||||
setupScopeParents();
|
||||
}
|
||||
|
||||
private void parseJavaLambdas() {
|
||||
// Pattern: (params) -> or identifier ->
|
||||
// Look for -> that's not inside strings/comments
|
||||
String arrowPattern = "->";
|
||||
int pos = 0;
|
||||
while ((pos = text.indexOf(arrowPattern, pos)) >= 0) {
|
||||
if (isExcluded(pos)) {
|
||||
pos += 2;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Found a potential lambda arrow
|
||||
int arrowPos = pos;
|
||||
|
||||
// Look backwards to find the parameter list
|
||||
int headerStart = findLambdaHeaderStart(arrowPos);
|
||||
if (headerStart < 0) {
|
||||
pos += 2;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Look forward to find the body end
|
||||
int bodyStart = arrowPos + 2;
|
||||
// Skip whitespace after ->
|
||||
while (bodyStart < text.length() && Character.isWhitespace(text.charAt(bodyStart))) {
|
||||
bodyStart++;
|
||||
}
|
||||
|
||||
if (bodyStart >= text.length()) {
|
||||
pos += 2;
|
||||
continue;
|
||||
}
|
||||
|
||||
int bodyEnd;
|
||||
if (text.charAt(bodyStart) == '{') {
|
||||
// Block lambda
|
||||
bodyEnd = findMatchingBrace(bodyStart);
|
||||
if (bodyEnd < 0) bodyEnd = text.length();
|
||||
else bodyEnd++; // Include the closing brace
|
||||
} else {
|
||||
// Expression lambda - find end of expression
|
||||
bodyEnd = findLambdaExpressionEnd(bodyStart);
|
||||
}
|
||||
|
||||
InnerCallableScope scope = new InnerCallableScope(
|
||||
InnerCallableScope.Kind.JAVA_LAMBDA,
|
||||
headerStart,
|
||||
arrowPos + 2,
|
||||
bodyStart,
|
||||
bodyEnd
|
||||
);
|
||||
|
||||
// Parse parameters (will be typed later during resolution)
|
||||
parseLambdaParameters(scope, headerStart, arrowPos);
|
||||
|
||||
innerScopes.add(scope);
|
||||
pos = bodyEnd;
|
||||
}
|
||||
}
|
||||
|
||||
private int findLambdaHeaderStart(int arrowPos) {
|
||||
// Work backwards from arrow to find start of parameter list
|
||||
int pos = arrowPos - 1;
|
||||
|
||||
// Skip whitespace
|
||||
while (pos >= 0 && Character.isWhitespace(text.charAt(pos))) {
|
||||
pos--;
|
||||
}
|
||||
|
||||
if (pos < 0) return -1;
|
||||
|
||||
if (text.charAt(pos) == ')') {
|
||||
// Parenthesized params: find matching (
|
||||
int depth = 1;
|
||||
pos--;
|
||||
while (pos >= 0 && depth > 0) {
|
||||
if (isExcluded(pos)) {
|
||||
pos--;
|
||||
continue;
|
||||
}
|
||||
char c = text.charAt(pos);
|
||||
if (c == ')') depth++;
|
||||
else if (c == '(') depth--;
|
||||
pos--;
|
||||
}
|
||||
return pos + 1; // Position of '('
|
||||
} else if (Character.isJavaIdentifierPart(text.charAt(pos))) {
|
||||
// Single identifier param (no parens): identifier ->
|
||||
while (pos >= 0 && Character.isJavaIdentifierPart(text.charAt(pos))) {
|
||||
pos--;
|
||||
}
|
||||
return pos + 1;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
private int findLambdaExpressionEnd(int start) {
|
||||
// Find end of expression lambda body
|
||||
// Ends at: ; , ) ] } (at depth 0) or end of text
|
||||
int depth = 0;
|
||||
int angleDepth = 0;
|
||||
int pos = start;
|
||||
boolean inString = false;
|
||||
char stringChar = 0;
|
||||
|
||||
while (pos < text.length()) {
|
||||
char c = text.charAt(pos);
|
||||
|
||||
// Handle strings
|
||||
if (!inString && (c == '"' || c == '\'')) {
|
||||
inString = true;
|
||||
stringChar = c;
|
||||
pos++;
|
||||
continue;
|
||||
}
|
||||
if (inString) {
|
||||
if (c == stringChar && (pos == 0 || text.charAt(pos - 1) != '\\')) {
|
||||
inString = false;
|
||||
}
|
||||
pos++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Track nesting
|
||||
if (c == '(' || c == '[' || c == '{') depth++;
|
||||
else if (c == ')' || c == ']' || c == '}') {
|
||||
if (depth == 0) return pos;
|
||||
depth--;
|
||||
}
|
||||
else if (c == '<') angleDepth++;
|
||||
else if (c == '>') angleDepth = Math.max(0, angleDepth - 1);
|
||||
else if ((c == ';' || c == ',') && depth == 0 && angleDepth == 0) {
|
||||
return pos;
|
||||
}
|
||||
|
||||
pos++;
|
||||
}
|
||||
|
||||
return pos;
|
||||
}
|
||||
|
||||
private void parseLambdaParameters(InnerCallableScope scope, int headerStart, int arrowPos) {
|
||||
String headerText = text.substring(headerStart, arrowPos).trim();
|
||||
|
||||
if (headerText.startsWith("(") && headerText.endsWith(")")) {
|
||||
// Parenthesized: (a, b) or (Type a, Type b)
|
||||
String paramsText = headerText.substring(1, headerText.length() - 1).trim();
|
||||
if (!paramsText.isEmpty()) {
|
||||
String[] params = paramsText.split(",");
|
||||
int offset = headerStart + 1; // After '('
|
||||
for (String param : params) {
|
||||
param = param.trim();
|
||||
if (param.isEmpty()) continue;
|
||||
|
||||
// Find position in original text
|
||||
int paramOffset = text.indexOf(param, offset);
|
||||
if (paramOffset < 0) paramOffset = offset;
|
||||
|
||||
String[] parts = param.split("\\s+");
|
||||
String paramName;
|
||||
TypeInfo paramType = null;
|
||||
|
||||
if (parts.length >= 2) {
|
||||
// Typed: Type name
|
||||
paramName = parts[parts.length - 1];
|
||||
String typeName = parts[parts.length - 2];
|
||||
paramType = resolveType(typeName);
|
||||
} else {
|
||||
// Untyped: just name (type will be inferred from expected FI)
|
||||
paramName = parts[0];
|
||||
}
|
||||
|
||||
int nameOffset = text.indexOf(paramName, paramOffset);
|
||||
if (nameOffset < 0) nameOffset = paramOffset;
|
||||
|
||||
FieldInfo paramInfo = FieldInfo.parameter(paramName, paramType, nameOffset, null);
|
||||
scope.addParameter(paramInfo);
|
||||
|
||||
offset = paramOffset + param.length();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Single identifier: x ->
|
||||
String paramName = headerText;
|
||||
int nameOffset = text.indexOf(paramName, headerStart);
|
||||
if (nameOffset < 0) nameOffset = headerStart;
|
||||
|
||||
FieldInfo paramInfo = FieldInfo.parameter(paramName, null, nameOffset, null);
|
||||
scope.addParameter(paramInfo);
|
||||
}
|
||||
}
|
||||
|
||||
private void parseJSFunctionExpressions() {
|
||||
// Pattern: function(params) { ... } or function name(params) { ... }
|
||||
// But NOT function declarations at top level (those are already in methods)
|
||||
Pattern funcExprPattern = Pattern.compile("function\\s*(?:\\w+)?\\s*\\(([^)]*)\\)\\s*\\{");
|
||||
Matcher m = funcExprPattern.matcher(text);
|
||||
|
||||
while (m.find()) {
|
||||
int start = m.start();
|
||||
if (isExcluded(start)) continue;
|
||||
|
||||
// Check if this is a function expression (not a declaration)
|
||||
// A function declaration is at statement level; expression is inside parens, after =, etc.
|
||||
if (isFunctionDeclaration(start)) continue;
|
||||
|
||||
int headerStart = start;
|
||||
int headerEnd = m.end() - 1; // Position of '{'
|
||||
int bodyStart = m.end() - 1;
|
||||
int bodyEnd = findMatchingBrace(bodyStart);
|
||||
if (bodyEnd < 0) bodyEnd = text.length();
|
||||
else bodyEnd++; // Include closing brace
|
||||
|
||||
InnerCallableScope scope = new InnerCallableScope(
|
||||
InnerCallableScope.Kind.JS_FUNCTION_EXPR,
|
||||
headerStart,
|
||||
headerEnd,
|
||||
bodyStart,
|
||||
bodyEnd
|
||||
);
|
||||
|
||||
// Parse parameters
|
||||
String paramsText = m.group(1).trim();
|
||||
if (!paramsText.isEmpty()) {
|
||||
String[] params = paramsText.split(",");
|
||||
int offset = m.start(1);
|
||||
for (String param : params) {
|
||||
param = param.trim();
|
||||
if (param.isEmpty()) continue;
|
||||
|
||||
int nameOffset = text.indexOf(param, offset);
|
||||
if (nameOffset < 0) nameOffset = offset;
|
||||
|
||||
// JS params don't have types by default; will infer from expected FI
|
||||
FieldInfo paramInfo = FieldInfo.parameter(param, TypeInfo.ANY, nameOffset, null);
|
||||
scope.addParameter(paramInfo);
|
||||
|
||||
offset = nameOffset + param.length();
|
||||
}
|
||||
}
|
||||
|
||||
innerScopes.add(scope);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isFunctionDeclaration(int funcStart) {
|
||||
// A function declaration is a statement, check context:
|
||||
// - If preceded by = or ( or , or : or [ it's an expression
|
||||
// - If at line start (possibly with whitespace) or after { or ; it's a declaration
|
||||
|
||||
int pos = funcStart - 1;
|
||||
while (pos >= 0 && Character.isWhitespace(text.charAt(pos)) && text.charAt(pos) != '\n') {
|
||||
pos--;
|
||||
}
|
||||
|
||||
if (pos < 0) return true; // Start of file = declaration
|
||||
|
||||
char prev = text.charAt(pos);
|
||||
// These characters indicate it's an expression, not a declaration
|
||||
if (prev == '=' || prev == '(' || prev == ',' || prev == ':' || prev == '[' || prev == '?') {
|
||||
return false;
|
||||
}
|
||||
// After newline + whitespace could be declaration
|
||||
if (prev == '\n' || prev == '{' || prev == ';' || prev == '}') {
|
||||
return true;
|
||||
}
|
||||
|
||||
return true; // Default to declaration to avoid false positives in inner scopes
|
||||
}
|
||||
|
||||
private void setupScopeParents() {
|
||||
// For each scope, find its immediate parent (smallest enclosing scope)
|
||||
for (int i = 0; i < innerScopes.size(); i++) {
|
||||
InnerCallableScope scope = innerScopes.get(i);
|
||||
InnerCallableScope parent = null;
|
||||
|
||||
for (int j = 0; j < innerScopes.size(); j++) {
|
||||
if (i == j) continue;
|
||||
InnerCallableScope candidate = innerScopes.get(j);
|
||||
|
||||
// Check if candidate contains scope
|
||||
if (candidate.getBodyStart() < scope.getHeaderStart() &&
|
||||
candidate.getBodyEnd() > scope.getFullEnd()) {
|
||||
// candidate contains scope
|
||||
if (parent == null ||
|
||||
(candidate.getBodyStart() > parent.getBodyStart())) {
|
||||
// candidate is smaller (more immediate) than current parent
|
||||
parent = candidate;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
scope.setParentScope(parent);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse global fields/variables (outside methods) - UNIFIED for both Java and JavaScript.
|
||||
* Stores results in the shared 'globalFields' map.
|
||||
@@ -5857,6 +6180,29 @@ public class ScriptDocument {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the innermost scope (method or inner callable) at a position.
|
||||
* Returns null if not inside any scope.
|
||||
*/
|
||||
public Object findInnermostScopeAt(int position) {
|
||||
// First check inner callable scopes (most specific)
|
||||
InnerCallableScope innermost = null;
|
||||
for (InnerCallableScope scope : innerScopes) {
|
||||
if (scope.containsPosition(position)) {
|
||||
if (innermost == null || scope.getBodyStart() > innermost.getBodyStart()) {
|
||||
innermost = scope;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (innermost != null) {
|
||||
return innermost;
|
||||
}
|
||||
|
||||
// Fall back to method scope
|
||||
return findMethodAtPosition(position);
|
||||
}
|
||||
|
||||
private int findMatchingBrace(int openBraceIndex) {
|
||||
if (openBraceIndex < 0 || openBraceIndex >= text.length())
|
||||
return -1;
|
||||
@@ -5892,6 +6238,10 @@ public class ScriptDocument {
|
||||
return Collections.unmodifiableList(methods);
|
||||
}
|
||||
|
||||
public List<InnerCallableScope> getInnerScopes() {
|
||||
return Collections.unmodifiableList(innerScopes);
|
||||
}
|
||||
|
||||
public List<ScriptTypeInfo> getScriptTypes() {
|
||||
return scriptTypes.values().stream().collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@@ -82,6 +82,10 @@ public class TypeInfo {
|
||||
// Documentation (script-defined types)
|
||||
private JSDocInfo jsDocInfo;
|
||||
|
||||
// SAM (Single Abstract Method) caching for functional interface detection
|
||||
private MethodInfo cachedSAM;
|
||||
private boolean samCacheResolved = false;
|
||||
|
||||
private TypeInfo(String simpleName, String fullName, String packageName,
|
||||
Kind kind, Class<?> javaClass, boolean resolved, TypeInfo enclosingType) {
|
||||
this(simpleName, fullName, packageName, kind, javaClass, resolved, enclosingType, null, null, null);
|
||||
@@ -937,6 +941,158 @@ public class TypeInfo {
|
||||
// Default: no validation for Java types
|
||||
}
|
||||
|
||||
// ==================== Functional Interface (SAM) Detection ====================
|
||||
|
||||
/**
|
||||
* Determine whether this type is a functional interface and return its single abstract method (SAM).
|
||||
*
|
||||
* For Java reflection types (javaClass != null):
|
||||
* - Must be an interface
|
||||
* - Collects all public methods from javaClass.getMethods()
|
||||
* - Filters out: static methods, default methods, and Object methods
|
||||
* - If exactly one abstract instance method remains, returns it as a MethodInfo
|
||||
*
|
||||
* For script-defined interfaces (ScriptTypeInfo with kind == INTERFACE):
|
||||
* - Identifies abstract methods (methods without body)
|
||||
* - If exactly one exists, returns it as a MethodInfo
|
||||
*
|
||||
* @return MethodInfo for the single abstract method, or null if not a functional interface
|
||||
*/
|
||||
public MethodInfo getSingleAbstractMethod() {
|
||||
// Return cached result if already resolved
|
||||
if (samCacheResolved) {
|
||||
return cachedSAM;
|
||||
}
|
||||
|
||||
// Mark as resolved to prevent re-computation
|
||||
samCacheResolved = true;
|
||||
|
||||
try {
|
||||
// Handle Java reflection types
|
||||
if (javaClass != null) {
|
||||
// Only interfaces can be functional interfaces (for simplicity)
|
||||
if (!javaClass.isInterface()) {
|
||||
cachedSAM = null;
|
||||
return null;
|
||||
}
|
||||
|
||||
// Collect all public methods
|
||||
java.lang.reflect.Method[] methods = javaClass.getMethods();
|
||||
java.lang.reflect.Method singleAbstractMethod = null;
|
||||
|
||||
for (java.lang.reflect.Method method : methods) {
|
||||
int modifiers = method.getModifiers();
|
||||
|
||||
// Skip static methods
|
||||
if (java.lang.reflect.Modifier.isStatic(modifiers)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skip default methods (Java 8+)
|
||||
if (method.isDefault()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skip methods inherited from Object
|
||||
String methodName = method.getName();
|
||||
if (isObjectMethod(methodName, method.getParameterCount())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// This is an abstract instance method
|
||||
if (singleAbstractMethod != null) {
|
||||
// More than one abstract method - not a functional interface
|
||||
cachedSAM = null;
|
||||
return null;
|
||||
}
|
||||
|
||||
singleAbstractMethod = method;
|
||||
}
|
||||
|
||||
// If exactly one abstract method found, create MethodInfo with generic substitution
|
||||
if (singleAbstractMethod != null) {
|
||||
cachedSAM = MethodInfo.fromReflection(singleAbstractMethod, this);
|
||||
return cachedSAM;
|
||||
}
|
||||
|
||||
cachedSAM = null;
|
||||
return null;
|
||||
}
|
||||
|
||||
// Handle script-defined interfaces
|
||||
if (this instanceof ScriptTypeInfo) {
|
||||
ScriptTypeInfo scriptType = (ScriptTypeInfo) this;
|
||||
|
||||
// Only check interfaces
|
||||
if (scriptType.getKind() != Kind.INTERFACE) {
|
||||
cachedSAM = null;
|
||||
return null;
|
||||
}
|
||||
|
||||
// Find all abstract methods (methods without body)
|
||||
// For script-defined interfaces, all declared methods are implicitly abstract
|
||||
java.util.List<MethodInfo> allMethods = scriptType.getAllMethodsFlat();
|
||||
|
||||
if (allMethods.size() == 1) {
|
||||
// Exactly one method - it's the SAM
|
||||
cachedSAM = allMethods.get(0);
|
||||
return cachedSAM;
|
||||
} else if (allMethods.size() > 1) {
|
||||
// More than one method - not a functional interface
|
||||
cachedSAM = null;
|
||||
return null;
|
||||
}
|
||||
|
||||
cachedSAM = null;
|
||||
return null;
|
||||
}
|
||||
|
||||
// Not a functional interface
|
||||
cachedSAM = null;
|
||||
return null;
|
||||
|
||||
} catch (Exception e) {
|
||||
// Any reflection or security error - treat as not a functional interface
|
||||
cachedSAM = null;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this type is a functional interface (has a single abstract method).
|
||||
*
|
||||
* @return true if this is a functional interface, false otherwise
|
||||
*/
|
||||
public boolean isFunctionalInterface() {
|
||||
return getSingleAbstractMethod() != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to check if a method is inherited from java.lang.Object.
|
||||
* These methods don't count toward the SAM requirement.
|
||||
*
|
||||
* @param methodName The name of the method
|
||||
* @param paramCount The number of parameters
|
||||
* @return true if this is an Object method
|
||||
*/
|
||||
private boolean isObjectMethod(String methodName, int paramCount) {
|
||||
switch (methodName) {
|
||||
case "equals":
|
||||
return paramCount == 1;
|
||||
case "hashCode":
|
||||
case "toString":
|
||||
case "getClass":
|
||||
case "notify":
|
||||
case "notifyAll":
|
||||
return paramCount == 0;
|
||||
case "wait":
|
||||
// wait() has overloads with 0, 1, and 2 parameters
|
||||
return paramCount == 0 || paramCount == 1 || paramCount == 2;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== Type Parameter Methods ====================
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user