Skip to content

Commit

Permalink
fix static class handling
Browse files Browse the repository at this point in the history
  • Loading branch information
msbarry committed Dec 8, 2023
1 parent e23c637 commit aa9d7b7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,19 +243,21 @@ private LuaTypeDefinition generateLuaTypeDefinition(Class<?> clazz, String suffi
TypeToken<?> type = TypeToken.of(clazz);
var definition = new LuaTypeDefinition(type, suffix, isStatic);

Type superclass = clazz.getGenericSuperclass();
if (superclass != null && superclass != Object.class) {
definition.addParent(type.resolveType(superclass));
}
for (var iface : clazz.getGenericInterfaces()) {
definition.addParent(type.resolveType(iface));
if (!isStatic) {
Type superclass = clazz.getGenericSuperclass();
if (superclass != null && superclass != Object.class) {
definition.addParent(type.resolveType(superclass));
}
for (var iface : clazz.getGenericInterfaces()) {
definition.addParent(type.resolveType(iface));
}
}

for (var field : clazz.getFields()) {
TypeToken<?> rawType = TypeToken.of(field.getDeclaringClass()).resolveType(field.getGenericType());
TypeToken<?> typeOnThisClass = type.resolveType(field.getGenericType());
if (Modifier.isPublic(field.getModifiers()) && isStatic == Modifier.isStatic(field.getModifiers()) &&
(field.getDeclaringClass() == clazz || !rawType.equals(typeOnThisClass))) {
(isStatic || field.getDeclaringClass() == clazz || !rawType.equals(typeOnThisClass))) {
definition.addField(field);
}
}
Expand All @@ -268,7 +270,7 @@ private LuaTypeDefinition generateLuaTypeDefinition(Class<?> clazz, String suffi
for (var method : clazz.getMethods()) {
if (Modifier.isPublic(method.getModifiers()) && isStatic == Modifier.isStatic(method.getModifiers())) {
Invokable<?, ?> invokable = type.method(method);
if (!invokable.getOwnerType().equals(type) && !differentFromParents(invokable, type)) {
if (!isStatic && !invokable.getOwnerType().equals(type) && !differentFromParents(invokable, type)) {
continue;
}
if (hasMethod(Object.class, method)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,20 @@ class LuaValues {
LuaValues.class);
}

public static class StaticClass {
public static class StaticClassSuper {
public static final int SUPER_CONSTANT = 1;
public final int superInstanceField = 1;

public int superInstance(int arg) {
return 1;
}

public static String superStaticMethod(String arg) {
return "";
}
}

public static class StaticClass extends StaticClassSuper {
public static final int CONSTANT = 1;
public static int staticField;
public int instanceField;
Expand All @@ -366,6 +379,7 @@ void testStaticLuaInstanceWithConsructors() {
"""
---@class (exact) com_onthegomap_planetiler_experimental_lua_GenerateLuaTypesTest_StaticClass__class
---@field CONSTANT integer
---@field SUPER_CONSTANT integer
---@field static_field integer
types.com_onthegomap_planetiler_experimental_lua_GenerateLuaTypesTest_StaticClass__class = {}
---@param i integer
Expand All @@ -376,6 +390,9 @@ void testStaticLuaInstanceWithConsructors() {
---@param arg integer
---@return integer
function types.com_onthegomap_planetiler_experimental_lua_GenerateLuaTypesTest_StaticClass__class:static_method(arg) end
---@param arg string
---@return string
function types.com_onthegomap_planetiler_experimental_lua_GenerateLuaTypesTest_StaticClass__class:super_static_method(arg) end
"""
.trim(),
StaticClass.class);
Expand Down

0 comments on commit aa9d7b7

Please sign in to comment.