82
83 JavaSourceFactory(Context ctx, HeaderFile header) {
84 this.log = ctx.log;
85 log.print(Level.INFO, () -> "Instantiate JavaSourceFactory for " + header.path);
86 this.headerFile = header;
87 this.headerClassName = headerFile.pkgName + "." + headerFile.headerClsName;
88 this.types = new HashMap<>();
89 this.libraryNames = ctx.options.libraryNames;
90 this.libraryPaths = ctx.options.recordLibraryPath? ctx.options.libraryPaths : null;
91 this.noNativeLocations = ctx.options.noNativeLocations;
92 this.global_jsb = new JavaSourceBuilder();
93 this.srcDir = Paths.get(ctx.options.srcDumpDir)
94 .resolve(headerFile.pkgName.replace('.', File.separatorChar));
95 }
96
97 // main entry point that generates & saves .java files for the header file
98 public void generate(List<Tree> decls) {
99 global_jsb.addPackagePrefix(headerFile.pkgName);
100
101 Map<String, Object> header = new HashMap<>();
102 header.put("path", headerFile.path.toAbsolutePath().toString().replace("\\", "\\\\"));
103 if (!libraryNames.isEmpty()) {
104 header.put("libraries", libraryNames.toArray(new String[0]));
105 if (libraryPaths != null && !libraryPaths.isEmpty()) {
106 header.put("libraryPaths", libraryPaths.toArray(new String[0]));
107 }
108 }
109
110 JType.ClassType[] classes = headerFile.dictionary().resolutionRoots()
111 .toArray(JType.ClassType[]::new);
112 if (classes.length != 0) {
113 header.put("resolutionContext", classes);
114 }
115
116 Set<Layout> global_layouts = new LinkedHashSet<>();
117 for (Tree tr : decls) {
118 if (tr instanceof VarTree) {
119 VarTree varTree = (VarTree)tr;
120 global_layouts.add(varTree.layout().withAnnotation(Layout.NAME, varTree.name()));
121 }
122 }
151 }
152
153 protected void handleException(Exception ex) {
154 log.printError("cannot.write.class.file", headerFile.pkgName + "." + headerFile.headerClsName, ex);
155 log.printStackTrace(ex);
156 }
157
158 private void addNativeLocation(JavaSourceBuilder jsb, Tree tree) {
159 addNativeLocation(jsb, tree.location());
160 }
161
162 private void addNativeLocation(JavaSourceBuilder jsb, SourceLocation src) {
163 addNativeLocation(true, jsb, src);
164 }
165
166 private void addNativeLocation(boolean align, JavaSourceBuilder jsb, SourceLocation src) {
167 if (! noNativeLocations) {
168 SourceLocation.Location loc = src.getFileLocation();
169 Path p = loc.path();
170 Map<String, Object> fields = new HashMap<>();
171 fields.put("file", p == null ? "<builtin>" : p.toAbsolutePath().toString().replace("\\", "\\\\"));
172 fields.put("line", loc.line());
173 fields.put("column", loc.column());
174 jsb.addAnnotation(align, NATIVE_LOCATION, fields);
175 }
176 }
177
178 private void addClassIfNeeded(String clsName, JavaSourceBuilder jsb) {
179 if (null != types.put(clsName, jsb)) {
180 log.printWarning("warn.class.overwritten", clsName);
181 }
182 }
183
184 private static boolean isBitField(Tree tree) {
185 return tree instanceof FieldTree && ((FieldTree)tree).isBitField();
186 }
187
188 /**
189 *
190 * @param jsb JavaSourceBuilder for the struct
191 * @param tree The Tree
|
82
83 JavaSourceFactory(Context ctx, HeaderFile header) {
84 this.log = ctx.log;
85 log.print(Level.INFO, () -> "Instantiate JavaSourceFactory for " + header.path);
86 this.headerFile = header;
87 this.headerClassName = headerFile.pkgName + "." + headerFile.headerClsName;
88 this.types = new HashMap<>();
89 this.libraryNames = ctx.options.libraryNames;
90 this.libraryPaths = ctx.options.recordLibraryPath? ctx.options.libraryPaths : null;
91 this.noNativeLocations = ctx.options.noNativeLocations;
92 this.global_jsb = new JavaSourceBuilder();
93 this.srcDir = Paths.get(ctx.options.srcDumpDir)
94 .resolve(headerFile.pkgName.replace('.', File.separatorChar));
95 }
96
97 // main entry point that generates & saves .java files for the header file
98 public void generate(List<Tree> decls) {
99 global_jsb.addPackagePrefix(headerFile.pkgName);
100
101 Map<String, Object> header = new HashMap<>();
102 header.put("path", headerFile.path.toAbsolutePath().toString());
103 if (!libraryNames.isEmpty()) {
104 header.put("libraries", libraryNames.toArray(new String[0]));
105 if (libraryPaths != null && !libraryPaths.isEmpty()) {
106 header.put("libraryPaths", libraryPaths.toArray(new String[0]));
107 }
108 }
109
110 JType.ClassType[] classes = headerFile.dictionary().resolutionRoots()
111 .toArray(JType.ClassType[]::new);
112 if (classes.length != 0) {
113 header.put("resolutionContext", classes);
114 }
115
116 Set<Layout> global_layouts = new LinkedHashSet<>();
117 for (Tree tr : decls) {
118 if (tr instanceof VarTree) {
119 VarTree varTree = (VarTree)tr;
120 global_layouts.add(varTree.layout().withAnnotation(Layout.NAME, varTree.name()));
121 }
122 }
151 }
152
153 protected void handleException(Exception ex) {
154 log.printError("cannot.write.class.file", headerFile.pkgName + "." + headerFile.headerClsName, ex);
155 log.printStackTrace(ex);
156 }
157
158 private void addNativeLocation(JavaSourceBuilder jsb, Tree tree) {
159 addNativeLocation(jsb, tree.location());
160 }
161
162 private void addNativeLocation(JavaSourceBuilder jsb, SourceLocation src) {
163 addNativeLocation(true, jsb, src);
164 }
165
166 private void addNativeLocation(boolean align, JavaSourceBuilder jsb, SourceLocation src) {
167 if (! noNativeLocations) {
168 SourceLocation.Location loc = src.getFileLocation();
169 Path p = loc.path();
170 Map<String, Object> fields = new HashMap<>();
171 fields.put("file", p == null ? "<builtin>" : p.toAbsolutePath().toString());
172 fields.put("line", loc.line());
173 fields.put("column", loc.column());
174 jsb.addAnnotation(align, NATIVE_LOCATION, fields);
175 }
176 }
177
178 private void addClassIfNeeded(String clsName, JavaSourceBuilder jsb) {
179 if (null != types.put(clsName, jsb)) {
180 log.printWarning("warn.class.overwritten", clsName);
181 }
182 }
183
184 private static boolean isBitField(Tree tree) {
185 return tree instanceof FieldTree && ((FieldTree)tree).isBitField();
186 }
187
188 /**
189 *
190 * @param jsb JavaSourceBuilder for the struct
191 * @param tree The Tree
|