jnr.ffi.provider.jffi.AsmBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jnr-ffi Show documentation
Show all versions of jnr-ffi Show documentation
A library for invoking native functions from java
package jnr.ffi.provider.jffi;
import com.kenai.jffi.CallContext;
import com.kenai.jffi.Function;
import com.kenai.jffi.ObjectParameterInfo;
import jnr.ffi.mapper.FromNativeConverter;
import jnr.ffi.mapper.ToNativeConverter;
import org.objectweb.asm.ClassVisitor;
import java.util.*;
/**
*
*/
class AsmBuilder {
private final String classNamePath;
private final ClassVisitor classVisitor;
private final ObjectNameGenerator functionId = new ObjectNameGenerator("functionAddress");
private final ObjectNameGenerator contextId = new ObjectNameGenerator("callContext");
private final ObjectNameGenerator toNativeConverterId = new ObjectNameGenerator("toNativeConverter");
private final ObjectNameGenerator fromNativeConverterId = new ObjectNameGenerator("fromNativeConverter");
private final ObjectNameGenerator objectParameterInfoId = new ObjectNameGenerator("objectParameterInfo");
private final Map toNativeConverters = new IdentityHashMap();
private final Map fromNativeConverters = new IdentityHashMap();
private final Map objectParameterInfo = new HashMap();
private final Map callContextMap = new HashMap();
private final Map functionAddresses = new HashMap();
private final List objectFields = new ArrayList();
AsmBuilder(String classNamePath, ClassVisitor classVisitor) {
this.classNamePath = classNamePath;
this.classVisitor = classVisitor;
}
public String getClassNamePath() {
return classNamePath;
}
ClassVisitor getClassVisitor() {
return classVisitor;
}
private static final class ObjectNameGenerator {
private final String baseName;
private int value;
ObjectNameGenerator(String baseName) {
this.baseName = baseName;
this.value = 0;
}
String generateName() {
return baseName + "_" + ++value;
}
}
ObjectField addField(Map map, T value, Class klass, ObjectNameGenerator objectNameGenerator) {
ObjectField field = new ObjectField(objectNameGenerator.generateName(), value, klass);
objectFields.add(field);
map.put(value, field);
return field;
}
ObjectField getField(Map map, T value, Class klass, ObjectNameGenerator objectNameGenerator) {
ObjectField field = map.get(value);
return field != null ? field : addField(map, value, klass, objectNameGenerator);
}
String getCallContextFieldName(Function function) {
return getField(callContextMap, function.getCallContext(), CallContext.class, contextId).name;
}
String getFunctionAddressFieldName(Function function) {
return getField(functionAddresses, function.getFunctionAddress(), long.class, functionId).name;
}
String getFromNativeConverterName(FromNativeConverter converter) {
return getField(fromNativeConverters, converter, FromNativeConverter.class, fromNativeConverterId).name;
}
String getToNativeConverterName(ToNativeConverter converter) {
return getField(toNativeConverters, converter, ToNativeConverter.class, toNativeConverterId).name;
}
String getObjectParameterInfoName(ObjectParameterInfo info) {
return getField(objectParameterInfo, info, ObjectParameterInfo.class, objectParameterInfoId).name;
}
public static final class ObjectField {
public final String name;
public final Object value;
public final Class klass;
public ObjectField(String fieldName, Object fieldValue, Class fieldClass) {
this.name = fieldName;
this.value = fieldValue;
this.klass = fieldClass;
}
}
ObjectField[] getObjectFieldArray() {
return objectFields.toArray(new ObjectField[objectFields.size()]);
}
}