Skip to content

Commit becb71e

Browse files
committed
Make generated methods without an instance-parameter static
This is still not a perfect way of deciding what should be static and what not. It results in things like GList.nth_data(list) instead of the sensible list.nth_data() because while list here is obviously an instance parameter, it is not declared so in the gir file. But, rather have this than GList.alloc() not being static, or everything being static, I think.
1 parent 5433bff commit becb71e

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

src/gir2java/GirParser.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -780,13 +780,15 @@ private void parseMethodOrFunction(Element root, ParsingContext context) {
780780
}
781781

782782
int nativeVisibility = (returnsPointer || takesPointer) ? JMod.PROTECTED : JMod.PUBLIC;
783+
int staticModifier = ParameterDescriptor.containsInstanceParameter(parametersList) ? 0 : JMod.STATIC;
784+
int nativeModifiers = nativeVisibility | staticModifier | JMod.NATIVE;
783785

784786
JMethod nativeMethod;
785787
if (returnsPointer) {
786-
nativeMethod = enclosing.method(nativeVisibility | JMod.NATIVE, long.class, nativeName);
788+
nativeMethod = enclosing.method(nativeModifiers | JMod.NATIVE, long.class, nativeName);
787789
nativeMethod.annotate(Ptr.class);
788790
} else {
789-
nativeMethod = enclosing.method(nativeVisibility | JMod.NATIVE, returnType.getJType(), nativeName);
791+
nativeMethod = enclosing.method(nativeModifiers | JMod.NATIVE, returnType.getJType(), nativeName);
790792
}
791793

792794
if (parametersList != null) { //null if the function has no arguments
@@ -812,9 +814,14 @@ private void parseMethodOrFunction(Element root, ParsingContext context) {
812814
name = nativeName;
813815
}
814816
name = NameUtils.neutralizeKeyword(name);
815-
JMethod wrapper = enclosing.method(JMod.PUBLIC, returnType.getJType(), name);
817+
JMethod wrapper = enclosing.method(JMod.PUBLIC | staticModifier, returnType.getJType(), name);
816818

817-
JInvocation nativeCall = JExpr._this().invoke(nativeMethod);
819+
JInvocation nativeCall;
820+
if (staticModifier == 0) {
821+
nativeCall = JExpr._this().invoke(nativeMethod);
822+
} else {
823+
nativeCall = enclosing.staticInvoke(nativeMethod);
824+
}
818825
if (parametersList != null) {
819826
for (ParameterDescriptor paramDesc : parametersList) {
820827
if (paramDesc.isInstance()) {

src/gir2java/ParameterDescriptor.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,16 @@ public static boolean containsPointer(List<ParameterDescriptor> list) {
3737
}
3838
return false;
3939
}
40+
41+
public static boolean containsInstanceParameter(List<ParameterDescriptor> list) {
42+
if (list == null) {
43+
return false;
44+
}
45+
for (ParameterDescriptor desc : list) {
46+
if (desc.isInstance()) {
47+
return true;
48+
}
49+
}
50+
return false;
51+
}
4052
}

0 commit comments

Comments
 (0)