Fixed StackOverflows

This commit is contained in:
bigguy345 committed 2026-01-07 20:31:02 +02:00
1 parent c433593459
commit 7d6bacb824
3 files changed
+71 -11

No files matched your search

@@ -101,13 +101,19 @@ public class JSAutocompleteProvider extends JavaAutocompleteProvider {
* Recursively add methods from a type and its parents.
*/
protected void addMethodsFromType(JSTypeInfo type, List<AutocompleteItem> items, Set<String> added) {
addMethodsFromType(type, items, added, 0);
addMethodsFromType(type, items, added, 0, new HashSet<>());
}
/**
* Recursively add methods from a type and its parents with inheritance depth tracking.
* Recursively add methods from a type and its parents with inheritance depth tracking and cycle detection.
*/
private void addMethodsFromType(JSTypeInfo type, List<AutocompleteItem> items, Set<String> added, int depth) {
private void addMethodsFromType(JSTypeInfo type, List<AutocompleteItem> items, Set<String> added, int depth, Set<String> visited) {
// Prevent infinite recursion from circular inheritance
if (visited.contains(type.getFullName())) {
return;
}
visited.add(type.getFullName());
for (JSMethodInfo method : type.getMethods().values()) {
String name = method.getName();
// Skip overload markers (name$1, name$2, etc.)
@@ -122,7 +128,7 @@ public class JSAutocompleteProvider extends JavaAutocompleteProvider {
// Add from parent type with incremented depth
if (type.getResolvedParent() != null) {
addMethodsFromType(type.getResolvedParent(), items, added, depth + 1);
addMethodsFromType(type.getResolvedParent(), items, added, depth + 1, visited);
}
}
@@ -130,13 +136,19 @@ public class JSAutocompleteProvider extends JavaAutocompleteProvider {
* Recursively add fields from a type and its parents.
*/
protected void addFieldsFromType(JSTypeInfo type, List<AutocompleteItem> items, Set<String> added) {
addFieldsFromType(type, items, added, 0);
addFieldsFromType(type, items, added, 0, new HashSet<>());
}
/**
* Recursively add fields from a type and its parents with inheritance depth tracking.
* Recursively add fields from a type and its parents with inheritance depth tracking and cycle detection.
*/
private void addFieldsFromType(JSTypeInfo type, List<AutocompleteItem> items, Set<String> added, int depth) {
private void addFieldsFromType(JSTypeInfo type, List<AutocompleteItem> items, Set<String> added, int depth, Set<String> visited) {
// Prevent infinite recursion from circular inheritance
if (visited.contains(type.getFullName())) {
return;
}
visited.add(type.getFullName());
for (JSFieldInfo field : type.getFields().values()) {
if (!added.contains(field.getName())) {
added.add(field.getName());
@@ -146,7 +158,7 @@ public class JSAutocompleteProvider extends JavaAutocompleteProvider {
// Add from parent type with incremented depth
if (type.getResolvedParent() != null) {
addFieldsFromType(type.getResolvedParent(), items, added, depth + 1);
addFieldsFromType(type.getResolvedParent(), items, added, depth + 1, visited);
}
}
@@ -89,12 +89,25 @@ public class JSTypeInfo {
* Get a method by name, including inherited methods.
*/
public JSMethodInfo getMethod(String name) {
return getMethod(name, new HashSet<>());
}
/**
* Internal method to get method with cycle detection.
*/
private JSMethodInfo getMethod(String name, Set<String> visited) {
// Prevent infinite recursion from circular inheritance
if (visited.contains(this.fullName)) {
return null;
}
visited.add(this.fullName);
JSMethodInfo method = methods.get(name);
if (method != null) return method;
// Check parent
if (resolvedParent != null) {
return resolvedParent.getMethod(name);
return resolvedParent.getMethod(name, visited);
}
return null;
}
@@ -103,8 +116,21 @@ public class JSTypeInfo {
* Get all methods with a given name (for overloads).
*/
public List<JSMethodInfo> getMethodOverloads(String name) {
return getMethodOverloads(name, new HashSet<>());
}
/**
* Internal method to get method overloads with cycle detection.
*/
private List<JSMethodInfo> getMethodOverloads(String name, Set<String> visited) {
List<JSMethodInfo> overloads = new ArrayList<>();
// Prevent infinite recursion from circular inheritance
if (visited.contains(this.fullName)) {
return overloads;
}
visited.add(this.fullName);
// Get from this type
if (methods.containsKey(name)) {
overloads.add(methods.get(name));
@@ -118,7 +144,7 @@ public class JSTypeInfo {
// Get from parent
if (resolvedParent != null) {
overloads.addAll(resolvedParent.getMethodOverloads(name));
overloads.addAll(resolvedParent.getMethodOverloads(name, visited));
}
return overloads;
@@ -135,12 +161,25 @@ public class JSTypeInfo {
* Get a field by name, including inherited fields.
*/
public JSFieldInfo getField(String name) {
return getField(name, new HashSet<>());
}
/**
* Internal method to get field with cycle detection.
*/
private JSFieldInfo getField(String name, Set<String> visited) {
// Prevent infinite recursion from circular inheritance
if (visited.contains(this.fullName)) {
return null;
}
visited.add(this.fullName);
JSFieldInfo field = fields.get(name);
if (field != null) return field;
// Check parent
if (resolvedParent != null) {
return resolvedParent.getField(name);
return resolvedParent.getField(name, visited);
}
return null;
}
@@ -368,7 +368,16 @@ public class JSTypeRegistry {
public void resolveInheritance() {
for (JSTypeInfo type : types.values()) {
JSTypeInfo child = type;
Set<String> visited = new HashSet<>();
while (child != null && child.getExtendsType() != null && child.getResolvedParent() == null) {
// Prevent circular inheritance
if (visited.contains(child.getFullName())) {
System.out.println("WARNING: Circular inheritance detected: " + child.getFullName() +
" extends " + child.getExtendsType());
break;
}
visited.add(child.getFullName());
JSTypeInfo parent = getType(child.getExtendsType());
if (parent != null) {
child.setResolvedParent(parent);