Skip to content

Commit 9a48349

Browse files
committed
Parse more of the gir structure
Specifically: - Track and map types - Parse record fields - Parse classes (very similar to records) - Generate diagnostic lists of defined and referenced types (in the subset of the gir we can parse)
1 parent 6c6c3ca commit 9a48349

File tree

8 files changed

+456
-9
lines changed

8 files changed

+456
-9
lines changed

src/gir2java/ConvertedType.java

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package gir2java;
2+
3+
import java.util.Arrays;
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
7+
import org.bridj.Pointer;
8+
9+
import com.sun.codemodel.JClass;
10+
import com.sun.codemodel.JCodeModel;
11+
import com.sun.codemodel.JType;
12+
13+
/**
14+
* Tell various information about types found in .girs that help generating appropriate
15+
* bindings for the elements that use it.
16+
*
17+
* @author relek
18+
*
19+
*/
20+
public class ConvertedType {
21+
22+
private static Set<String> knownBridjPrimitives = new HashSet<String>(Arrays.asList(
23+
"Boolean",
24+
"Byte",
25+
"Char",
26+
"CLong",
27+
"Double",
28+
"Enum",
29+
"Float",
30+
"Int",
31+
"Long",
32+
"NativeObject",
33+
"Pointer",
34+
"Short",
35+
"SizeT",
36+
"TypedPointer"
37+
));
38+
39+
private String namespace;
40+
private String type;
41+
private String ctype;
42+
private boolean isEnum;
43+
private JCodeModel cm;
44+
private JType jType;
45+
46+
public ConvertedType(JCodeModel cm, String namespace, String type, String ctype, boolean isEnum) {
47+
this.namespace = namespace;
48+
this.type = type;
49+
this.ctype = ctype;
50+
this.cm = cm;
51+
this.isEnum = isEnum;
52+
}
53+
54+
public boolean isPointer() {
55+
return (ctype != null) && (ctype.contains("*"));
56+
}
57+
58+
public boolean isEnum() {
59+
return isEnum;
60+
}
61+
62+
public String getType() {
63+
return type;
64+
}
65+
66+
public String getCtype() {
67+
return ctype;
68+
}
69+
70+
public String getNamespace() {
71+
return namespace;
72+
}
73+
74+
public JType getJType() {
75+
return jType;
76+
}
77+
78+
public void setJType(JType jType) {
79+
this.jType = jType;
80+
}
81+
82+
public String bridjMethodifyTypeName() {
83+
if (isEnum()) {
84+
return "Enum";
85+
// } else if (type.isAssignableFrom(TypedPointer.class)) {
86+
// return "TypedPointer";
87+
} else if (isPointer()) {
88+
return "Pointer";
89+
}
90+
91+
String camelType = NameUtils.toCamel(getType());
92+
93+
if (knownBridjPrimitives.contains(camelType)) {
94+
return camelType;
95+
} else {
96+
return "NativeObject";
97+
}
98+
}
99+
}

src/gir2java/Gir2Java.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ private void run(String[] args) {
7878
System.err.println("Could not save Java from codebase, reason:");
7979
e.printStackTrace();
8080
}
81+
82+
codebase.saveTypes(new File("found-types.txt"), new File("referenced-types.txt"));
8183
}
8284

8385
public static void main(String[] args) {

src/gir2java/GirCodebase.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package gir2java;
22
import java.io.File;
3+
import java.io.FileWriter;
34
import java.io.IOException;
5+
import java.util.HashSet;
6+
import java.util.Set;
47

58
import nu.xom.Document;
69

@@ -40,5 +43,9 @@ public void addGir(Document gir) {
4043
public void saveJava(File javadir) throws IOException {
4144
cm.build(javadir);
4245
}
46+
47+
public void saveTypes(File found, File referenced) {
48+
parser.outputTypes(found, referenced);
49+
}
4350

4451
}

0 commit comments

Comments
 (0)