< prev index next >

src/jdk.jextract/share/classes/com/sun/tools/jextract/TypeDictionary.java

Print this page

        

@@ -48,46 +48,37 @@
 
 /**
  * A dictionary that find Java type for a given native type.
  * Each instance of TypeDictionary presents types for a given java package.
  */
-public final class TypeDictionary {
-    // package name to TypeDictionary
-    final static Map<String, TypeDictionary> tdMap;
-    final Logger logger = Logger.getLogger(getClass().getPackage().getName());
-
-    final String pkgName;
+final class TypeDictionary {
+    private final Logger logger = Logger.getLogger(getClass().getPackage().getName());
+    private Context ctx;
+    private final String pkgName;
     // clang.Type.spelling() to java type
-    final Map<String, JType> typeMap;
-
-    static {
-        tdMap = new HashMap<>();
-    }
-
-    final AtomicInteger serialNo;
+    private final Map<String, JType> typeMap;
+    private final AtomicInteger serialNo;
 
     private int serialNo() {
         return serialNo.incrementAndGet();
     }
 
-    private TypeDictionary(String pkg) {
-        pkgName = pkg;
-        typeMap = new HashMap<>();
-        serialNo = new AtomicInteger();
+    TypeDictionary(Context ctx, String pkg) {
+        this.ctx = ctx;
+        this.pkgName = pkg;
+        this.typeMap = new HashMap<>();
+        this.serialNo = new AtomicInteger();
     }
 
-    public static TypeDictionary of(String pkg) {
-        return tdMap.computeIfAbsent(pkg, TypeDictionary::new);
-    }
 /*
-    public static boolean addType(Type t, Class<?> cls) {
+    public static boolean addType(Context ctx, Type t, Class<?> cls) {
         if (cls.isAnnotation() || cls.isArray()) {
             throw new IllegalArgumentException();
         }
 
         String pkg = cls.getPackage().getName();
-        TypeDictionary dict = TypeDictionary.of(pkg);
+        TypeDictionary dict = ctx.typeDictionaryFor(pkg);
 
         String type = t.spelling();
         return dict.addWithClass(type, cls);
     }
 

@@ -107,24 +98,26 @@
         return (null == typeMap.putIfAbsent(type, JType.of(cls)));
     }
 */
 
     static class JarClassStreamer extends ClassLoader {
+        private final Context ctx;
         private final HashMap<String, byte[]> bytecodes = new HashMap<>();
 
-        JarClassStreamer(Path jar) {
+        JarClassStreamer(Context ctx, Path jar) {
+            this.ctx = ctx;
             try (JarInputStream jis = new JarInputStream(Files.newInputStream(jar, READ))) {
                 // List all classes in the jar
                 for (JarEntry e = jis.getNextJarEntry(); e != null; e = jis.getNextJarEntry()) {
                     if (e.isDirectory()) {
                         jis.closeEntry();
                         continue;
                     }
                     String name = e.getName();
                     if (! name.endsWith(".class")) {
                         // Should not have file not class files
-                        Context.getInstance().err.println("Warning: unexpected file " + name);
+                        ctx.err.println("Warning: unexpected file " + name);
                     }
                     name = name.substring(0, name.length() - 6);
                     byte[] buf = new byte[4096];
                     ByteArrayOutputStream bos = new ByteArrayOutputStream();
                     int n;

@@ -132,11 +125,11 @@
                         bos.write(buf, 0, n);
                     }
                     bytecodes.put(name.replace(File.separatorChar, '.'), bos.toByteArray());
                 }
             } catch (IOException ioe) {
-                Context.getInstance().err.println("Failed to load types from jar file: " + jar.toString());
+                ctx.err.println("Failed to load types from jar file: " + jar.toString());
             }
         }
 
         public Stream<Class<?>> stream() {
             return bytecodes.entrySet().stream()

@@ -145,15 +138,15 @@
                         return defineClass(e.getKey(), bytecode, 0, bytecode.length);
                     });
         }
     }
 
-    public static void loadJar(Path jar) {
-        JarClassStreamer cl = new JarClassStreamer(jar);
+    void loadJar(Path jar) {
+        JarClassStreamer cl = new JarClassStreamer(ctx, jar);
         cl.stream().forEach(cls -> {
             Package pkg = cls.getPackage();
-            TypeDictionary dict = TypeDictionary.of(pkg.getName());
+            TypeDictionary dict = ctx.typeDictionaryFor(pkg.getName());
             NativeType nt = cls.getAnnotation(NativeType.class);
             dict.typeMap.putIfAbsent(nt.ctype(), JType.of(cls));
         });
     }
 

@@ -215,11 +208,11 @@
 
     /**
      * @param t
      * @return
      */
-    public final JType get(Type t) {
+    final JType get(Type t) {
         JType jt;
 
         switch(t.kind()) {
             case Unexposed:
                 // Always look at canonical type

@@ -275,11 +268,11 @@
             jt = (jt instanceof JType2) ? jt : JType2.bind(jt, t, t.getDeclarationCursor());
         }
         return jt;
     }
 
-    public final JType computeIfAbsent(Type t, Function<Type, JType> fn) {
+    final JType computeIfAbsent(Type t, Function<Type, JType> fn) {
         JType jt = get(t);
         if (jt != null) {
             return jt;
         }
 

@@ -295,22 +288,22 @@
      * look into the origin(declaring) TypeDictionary.
      * @param t
      * @return
      * @throws com.sun.tools.jextract.TypeDictionary.NotDeclaredException
      */
-    public final JType lookup(Type t) throws NotDeclaredException {
+    final JType lookup(Type t) throws NotDeclaredException {
         JType jt = get(t);
         if (jt == null && t.kind() != TypeKind.Pointer) {
             // Pointer type need to check with pointee type, as the declaration
             // might still be in same TypeDictionary
             Cursor c = t.getDeclarationCursor();
             if (c.isInvalid()) {
                 logger.info(() -> "Type " + t.spelling() + " has invalid declaration cursor.");
                 logger.fine(() -> Printer.Stringifier(p -> p.dumpType(t)));
                 throw new NotDeclaredException(t);
             }
-            jt = Context.getInstance().getJType(t.getDeclarationCursor());
+            jt = ctx.getJType(t.getDeclarationCursor());
         }
         return jt;
     }
 
     static class NotDeclaredException extends RuntimeException {
< prev index next >