< prev index next >
src/jdk.jextract/share/classes/com/sun/tools/jextract/Context.java
Print this page
@@ -42,41 +42,43 @@
import static java.nio.file.StandardOpenOption.*;
/**
* The setup for the tool execution
*/
-public class Context {
+public final class Context {
+ // package name to TypeDictionary
+ private final Map<String, TypeDictionary> tdMap;
// The folder path mapping to package name
private final Map<Path, String> pkgMap;
// The header file parsed
private final Map<Path, HeaderFile> headerMap;
// The args for parsing C
- final List<String> clangArgs;
+ private final List<String> clangArgs;
// The set of source header files
- final Set<Path> sources;
+ private final Set<Path> sources;
// The list of library names
- final List<String> libraryNames;
+ private final List<String> libraryNames;
// The list of library paths
- final List<String> libraryPaths;
+ private final List<String> libraryPaths;
// The list of library paths for link checks
- final List<String> linkCheckPaths;
+ private final List<String> linkCheckPaths;
final PrintWriter out;
final PrintWriter err;
// check if a symbol is found in any of the libraries or not.
- public static interface SymbolChecker {
+ static interface SymbolChecker {
public boolean lookup(String name);
}
- SymbolChecker symChecker;
+ private SymbolChecker symChecker;
- final static String defaultPkg = "jextract.dump";
- private static Context instance;
- public final Logger logger = Logger.getLogger(getClass().getPackage().getName());
+ private final static String defaultPkg = "jextract.dump";
+ final Logger logger = Logger.getLogger(getClass().getPackage().getName());
- private Context(PrintWriter out, PrintWriter err) {
+ public Context(PrintWriter out, PrintWriter err) {
+ this.tdMap = new HashMap<>();
this.pkgMap = new HashMap<>();
this.headerMap = new HashMap<>();
this.clangArgs = new ArrayList<>();
this.sources = new TreeSet<>();
this.libraryNames = new ArrayList<>();
@@ -84,27 +86,38 @@
this.linkCheckPaths = new ArrayList<>();
this.out = out;
this.err = err;
}
- // used by jtreg tests
- public static Context newInstance() {
- return newInstance(new PrintWriter(System.out, true), new PrintWriter(System.err, true));
+ public Context() {
+ this(new PrintWriter(System.out, true), new PrintWriter(System.err, true));
}
- static Context newInstance(PrintWriter out, PrintWriter err) {
- return instance = new Context(out, err);
+ TypeDictionary typeDictionaryFor(String pkg) {
+ return tdMap.computeIfAbsent(pkg, p->new TypeDictionary(this, p));
}
- static Context getInstance() {
- return instance;
+ void addClangArg(String arg) {
+ clangArgs.add(arg);
}
public void addSource(Path path) {
sources.add(path);
}
+ void addLibraryName(String name) {
+ libraryNames.add(name);
+ }
+
+ void addLibraryPath(String path) {
+ libraryPaths.add(path);
+ }
+
+ void addLinkCheckPath(String path) {
+ linkCheckPaths.add(path);
+ }
+
/**
* Setup a package name for a given folder.
*
* @param folder The path to the folder, use null to set catch-all.
* @param pkg The package name
@@ -127,13 +140,13 @@
logger.warning(() -> "Package " + existing + " had been selected for " + finalFolder + ", request to use " + pkg + " is ignored.");
return false;
}
}
- public static class Entity {
- public final String pkg;
- public final String entity;
+ static class Entity {
+ final String pkg;
+ final String entity;
Entity(String pkg, String entity) {
this.pkg = pkg;
this.entity = entity;
}
@@ -147,11 +160,11 @@
*
* @param origin The source path
* @return The Entity
* @see Context::usePackageForFolder(Path, String)
*/
- public Entity whatis(Path origin) {
+ Entity whatis(Path origin) {
// normalize to absolute path
origin = origin.toAbsolutePath();
String filename = null;
if (!Files.isDirectory(origin)) {
// ensure it's a folder name
@@ -205,11 +218,11 @@
logger.warning(() -> "Not a regular file: " + header.toString());
throw new IllegalArgumentException(header.toString());
}
final Context.Entity e = whatis(header);
- return new HeaderFile(header, e.pkg, e.entity, main, symChecker);
+ return new HeaderFile(this, header, e.pkg, e.entity, main, symChecker);
}
void processCursor(Cursor c, HeaderFile main, Function<HeaderFile, CodeFactory> fn) {
SourceLocation loc = c.getSourceLocation();
if (loc == null) {
@@ -254,10 +267,14 @@
}
header.processCursor(c, main, isBuiltIn);
}
+ public void parse() {
+ parse(header -> new AsmCodeFactory(this, header));
+ }
+
public void parse(Function<HeaderFile, CodeFactory> fn) {
if (!libraryNames.isEmpty() && !linkCheckPaths.isEmpty()) {
Library[] libs = NativeLibraryImpl.loadLibraries(
linkCheckPaths.toArray(new String[0]),
libraryNames.toArray(new String[0]));
@@ -316,11 +333,11 @@
private Map<String, List<CodeFactory>> getPkgCfMap() {
final Map<String, List<CodeFactory>> mapPkgCf = new HashMap<>();
// Build the pkg to CodeFactory map
headerMap.values().forEach(header -> {
- CodeFactory cf = header.cf;
+ CodeFactory cf = header.getCodeFactory();
String pkg = header.pkgName;
logger.config(() -> "File " + header + " is in package: " + pkg);
if (cf == null) {
logger.config(() -> "File " + header + " code generation is not activated!");
return;
@@ -374,11 +391,11 @@
mapPkgCf.getOrDefault(pkg_name, Collections.emptyList())
.forEach(cf -> writeJar(cf, jos));
}
}
- public void collectJarFile(final Path jar, String... pkgs) throws IOException {
+ void collectJarFile(final Path jar, String... pkgs) throws IOException {
logger.info(() -> "Collecting jar file " + jar);
try (OutputStream os = Files.newOutputStream(jar, CREATE, TRUNCATE_EXISTING, WRITE);
JarOutputStream jo = new JarOutputStream(os)) {
collectJarFile(jo, pkgs);
} catch (UncheckedIOException uioe) {
@@ -411,11 +428,11 @@
* Perform a global lookup
*
* @param c The cursor define or declare the type.
* @return
*/
- public JType getJType(final Cursor c) {
+ JType getJType(final Cursor c) {
if (c.isInvalid()) {
throw new IllegalArgumentException();
}
SourceLocation loc = c.getSourceLocation();
if (null == loc) {
< prev index next >