< prev index next >
src/jdk.jextract/share/classes/com/sun/tools/jextract/TypeDictionary.java
Print this page
*** 48,93 ****
/**
* 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;
// clang.Type.spelling() to java type
! final Map<String, JType> typeMap;
!
! static {
! tdMap = new HashMap<>();
! }
!
! final AtomicInteger serialNo;
private int serialNo() {
return serialNo.incrementAndGet();
}
! private TypeDictionary(String pkg) {
! pkgName = pkg;
! typeMap = new HashMap<>();
! serialNo = new AtomicInteger();
}
- public static TypeDictionary of(String pkg) {
- return tdMap.computeIfAbsent(pkg, TypeDictionary::new);
- }
/*
! public static boolean addType(Type t, Class<?> cls) {
if (cls.isAnnotation() || cls.isArray()) {
throw new IllegalArgumentException();
}
String pkg = cls.getPackage().getName();
! TypeDictionary dict = TypeDictionary.of(pkg);
String type = t.spelling();
return dict.addWithClass(type, cls);
}
--- 48,84 ----
/**
* A dictionary that find Java type for a given native type.
* Each instance of TypeDictionary presents types for a given java package.
*/
! 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
! private final Map<String, JType> typeMap;
! private final AtomicInteger serialNo;
private int serialNo() {
return serialNo.incrementAndGet();
}
! TypeDictionary(Context ctx, String pkg) {
! this.ctx = ctx;
! this.pkgName = pkg;
! this.typeMap = new HashMap<>();
! this.serialNo = new AtomicInteger();
}
/*
! 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 = ctx.typeDictionaryFor(pkg);
String type = t.spelling();
return dict.addWithClass(type, cls);
}
*** 107,130 ****
return (null == typeMap.putIfAbsent(type, JType.of(cls)));
}
*/
static class JarClassStreamer extends ClassLoader {
private final HashMap<String, byte[]> bytecodes = new HashMap<>();
! JarClassStreamer(Path jar) {
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);
}
name = name.substring(0, name.length() - 6);
byte[] buf = new byte[4096];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int n;
--- 98,123 ----
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(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
! 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,142 ****
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());
}
}
public Stream<Class<?>> stream() {
return bytecodes.entrySet().stream()
--- 125,135 ----
bos.write(buf, 0, n);
}
bytecodes.put(name.replace(File.separatorChar, '.'), bos.toByteArray());
}
} catch (IOException ioe) {
! ctx.err.println("Failed to load types from jar file: " + jar.toString());
}
}
public Stream<Class<?>> stream() {
return bytecodes.entrySet().stream()
*** 145,159 ****
return defineClass(e.getKey(), bytecode, 0, bytecode.length);
});
}
}
! public static void loadJar(Path jar) {
! JarClassStreamer cl = new JarClassStreamer(jar);
cl.stream().forEach(cls -> {
Package pkg = cls.getPackage();
! TypeDictionary dict = TypeDictionary.of(pkg.getName());
NativeType nt = cls.getAnnotation(NativeType.class);
dict.typeMap.putIfAbsent(nt.ctype(), JType.of(cls));
});
}
--- 138,152 ----
return defineClass(e.getKey(), bytecode, 0, bytecode.length);
});
}
}
! void loadJar(Path jar) {
! JarClassStreamer cl = new JarClassStreamer(ctx, jar);
cl.stream().forEach(cls -> {
Package pkg = cls.getPackage();
! TypeDictionary dict = ctx.typeDictionaryFor(pkg.getName());
NativeType nt = cls.getAnnotation(NativeType.class);
dict.typeMap.putIfAbsent(nt.ctype(), JType.of(cls));
});
}
*** 215,225 ****
/**
* @param t
* @return
*/
! public final JType get(Type t) {
JType jt;
switch(t.kind()) {
case Unexposed:
// Always look at canonical type
--- 208,218 ----
/**
* @param t
* @return
*/
! final JType get(Type t) {
JType jt;
switch(t.kind()) {
case Unexposed:
// Always look at canonical type
*** 275,285 ****
jt = (jt instanceof JType2) ? jt : JType2.bind(jt, t, t.getDeclarationCursor());
}
return jt;
}
! public final JType computeIfAbsent(Type t, Function<Type, JType> fn) {
JType jt = get(t);
if (jt != null) {
return jt;
}
--- 268,278 ----
jt = (jt instanceof JType2) ? jt : JType2.bind(jt, t, t.getDeclarationCursor());
}
return jt;
}
! final JType computeIfAbsent(Type t, Function<Type, JType> fn) {
JType jt = get(t);
if (jt != null) {
return jt;
}
*** 295,316 ****
* look into the origin(declaring) TypeDictionary.
* @param t
* @return
* @throws com.sun.tools.jextract.TypeDictionary.NotDeclaredException
*/
! public 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());
}
return jt;
}
static class NotDeclaredException extends RuntimeException {
--- 288,309 ----
* look into the origin(declaring) TypeDictionary.
* @param t
* @return
* @throws com.sun.tools.jextract.TypeDictionary.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 = ctx.getJType(t.getDeclarationCursor());
}
return jt;
}
static class NotDeclaredException extends RuntimeException {
< prev index next >