本文整理汇总了Java中org.bridj.NativeLibrary类的典型用法代码示例。如果您正苦于以下问题:Java NativeLibrary类的具体用法?Java NativeLibrary怎么用?Java NativeLibrary使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
NativeLibrary类属于org.bridj包,在下文中一共展示了NativeLibrary类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: MemoryOperators
import org.bridj.NativeLibrary; //导入依赖的package包/类
@SuppressWarnings({ "deprecation", "unchecked", "rawtypes" })
public MemoryOperators(NativeLibrary library) {
for (Symbol sym : library.getSymbols()) {
try {
MemberRef parsedRef = sym.getParsedRef();
IdentLike n = parsedRef.getMemberName();
if (SpecialName.New.equals(n)) {
newFct = pointerToAddress(sym.getAddress()).asDynamicFunction(null, Pointer.class, SizeT.class);
} else if (SpecialName.NewArray.equals(n)) {
newFct = pointerToAddress(sym.getAddress()).asDynamicFunction(null, Pointer.class, SizeT.class);
} else if (SpecialName.Delete.equals(n)) {
newFct = pointerToAddress(sym.getAddress()).asDynamicFunction(null, Void.class, Pointer.class);
} else if (SpecialName.DeleteArray.equals(n)) {
newFct = pointerToAddress(sym.getAddress()).asDynamicFunction(null, Void.class, Pointer.class);
}
} catch (Exception ex) {
}
}
}
示例2: newCPPReleaser
import org.bridj.NativeLibrary; //导入依赖的package包/类
Pointer.Releaser newCPPReleaser(final Type type, final Class<?> typeClass, NativeLibrary lib) throws FileNotFoundException {
Pointer.Releaser releaser = null;
//final Class<?> typeClass = Utils.getClass(type);
//NativeLibrary lib = BridJ.getNativeLibrary(typeClass);
if (lib != null && BridJ.enableDestructors) {
final CPPDestructor<?> destructor = getDestructor(typeClass, type, lib);
if (destructor != null) {
releaser = new Pointer.Releaser() { //@Override
public void release(Pointer<?> p) {
if (BridJ.debug) {
BridJ.info("Destructing instance of C++ type " + Utils.toString(type) + " (address = " + p + ", destructor = " + getPointer(destructor) + ")");
}
//System.out.println("Destructing instance of C++ type " + type + "...");
long peer = p.getPeer();
destructor.destroy(peer);
BridJ.setJavaObjectFromNativePeer(peer, null);
}
};
}
}
return releaser;
}
示例3: testLLVMSymbols
import org.bridj.NativeLibrary; //导入依赖的package包/类
public void testLLVMSymbols() {
NativeLibrary lib = null;
try {
lib = BridJ.getNativeLibrary("LLVM-" + LLVM_VERSION);
} catch (IOException e) {
fail(e.getMessage());
}
Collection<Symbol> symbols = lib.getSymbols();
Set<Symbol> llvmSymbols = new HashSet<Symbol>(symbols.size());
for (Symbol sym : symbols) {
if (sym.getName().startsWith("LLVM")) {
llvmSymbols.add(sym);
}
}
assertTrue(llvmSymbols.size() > 10);
}
示例4: getNativeLibrary
import org.bridj.NativeLibrary; //导入依赖的package包/类
@Override
protected NativeLibrary getNativeLibrary(Class<?> type) throws IOException {
Library libAnn = type.getAnnotation(Library.class);
if (libAnn != null) {
try {
String name = libAnn.value();
return BridJ.getNativeLibrary(name, new File("/System/Library/Frameworks/" + name + ".framework/" + name));
} catch (FileNotFoundException ex) {
}
}
return super.getNativeLibrary(type);
}
示例5: registerNativeMethod
import org.bridj.NativeLibrary; //导入依赖的package包/类
@Override
protected void registerNativeMethod(
Class<?> type,
NativeLibrary typeLibrary,
Method method,
NativeLibrary methodLibrary,
Builder builder,
MethodCallInfoBuilder methodCallInfoBuilder) throws FileNotFoundException {
if (method == null) {
return;
}
if (!ObjCObject.class.isAssignableFrom(type) || ObjCBlock.class.isAssignableFrom(type)) {
super.registerNativeMethod(type, typeLibrary, method, methodLibrary, builder, methodCallInfoBuilder);
return;
}
try {
MethodCallInfo mci = methodCallInfoBuilder.apply(method);
boolean isStatic = Modifier.isStatic(method.getModifiers());
if (isStatic) {
Pointer<ObjCClass> pObjcClass = getObjCClass((Class) type).as(ObjCClass.class);
ObjCClass objcClass = pObjcClass.get();
mci.setNativeClass(pObjcClass.getPeer());
}
mci.setSymbolName(getSelector(method));
builder.addObjCMethod(mci);
} catch (ClassNotFoundException ex) {
throw new RuntimeException("Failed to register method " + method + " : " + ex, ex);
}
}
示例6: getMemoryOperators
import org.bridj.NativeLibrary; //导入依赖的package包/类
public synchronized MemoryOperators getMemoryOperators() {
if (memoryOperators == null) {
try {
NativeLibrary libStdCpp = BridJ.getNativeLibrary("stdc++");
memoryOperators = new MemoryOperators(libStdCpp);
} catch (Exception ex) {
BridJ.error(null, ex);
}
}
return memoryOperators;
}
示例7: getPositionInVirtualTable
import org.bridj.NativeLibrary; //导入依赖的package包/类
public int getPositionInVirtualTable(Pointer<Pointer<?>> pVirtualTable, Method method, NativeLibrary library) {
//Pointer<?> typeInfo = pVirtualTable.get(1);
int methodsOffset = 0;//library.isMSVC() ? 0 : -2;///2;
String className = getClassName(method.getDeclaringClass());
for (int iVirtual = 0;; iVirtual++) {
Pointer<?> pMethod = pVirtualTable.get(methodsOffset + iVirtual);
String virtualMethodName = pMethod == null ? null : library.getSymbolName(pMethod.getPeer());
//System.out.println("#\n# At index " + methodsOffset + " + " + iVirtual + " of vptr for class " + className + ", found symbol " + Long.toHexString(pMethod.getPeer()) + " = '" + virtualMethodName + "'\n#");
if (virtualMethodName == null) {
if (debug) {
info("\tVtable(" + className + ")[" + iVirtual + "] = null");
}
}
if (pMethod == null) {
return -1;
} else if (virtualMethodName == null) {
continue;
}
try {
MemberRef mr = library.parseSymbol(virtualMethodName);
if (debug) {
info("\tVtable(" + className + ")[" + iVirtual + "] = " + virtualMethodName + " = " + mr);
}
if (mr != null && mr.matchesSignature(method)) {
return iVirtual;
} else if (library.isMSVC() && !mr.matchesEnclosingType(method)) {
break; // no NULL terminator in MSVC++ vtables, so we have to guess when we've reached the end
}
} catch (Demangler.DemanglingException ex) {
BridJ.warning("Failed to demangle '" + virtualMethodName + "' during inspection of virtual table for '" + method.toGenericString() + "' : " + ex);
}
}
return -1;
}
示例8: installRegularVTablePtr
import org.bridj.NativeLibrary; //导入依赖的package包/类
@SuppressWarnings("deprecation")
protected boolean installRegularVTablePtr(Type type, NativeLibrary library, Pointer<?> peer) {
long vtablePtr = getVirtualTable(type, library);
if (vtablePtr != 0) {
if (BridJ.debug) {
BridJ.info("Installing regular vtable pointer " + Pointer.pointerToAddress(vtablePtr) + " to instance at " + peer + " (type = " + Utils.toString(type) + ")");
}
peer.setSizeT(vtablePtr);
return true;
}
return false;
}
示例9: synthetizeVirtualTable
import org.bridj.NativeLibrary; //导入依赖的package包/类
@SuppressWarnings("rawtypes")
protected VTable synthetizeVirtualTable(Type type, Pointer<Pointer> parentVTablePtr, List<VirtMeth> methods, NativeLibrary library) {
int nMethods = methods.size();
//Pointer<Pointer> parentVTablePtr = pointerToAddress(getVirtualTable(Utils.getParent(type), library), Pointer.class);
VTable vtable = new VTable();
vtable.ptr = allocatePointers(nMethods + 2).next(2); // leave two null pointers at index -2 and -1, to say there's no runtime type information available.
Class<?> c = Utils.getClass(type);
for (int iMethod = 0; iMethod < nMethods; iMethod++) {
VirtMeth vm = methods.get(iMethod);
Pointer<?> pMethod;
if (Modifier.isNative(vm.implementation.getModifiers())) {
pMethod = parentVTablePtr == null ? null : parentVTablePtr.get(iMethod);
} else {
try {
MethodCallInfo mci = new MethodCallInfo(vm.implementation, vm.definition);
mci.setDeclaringClass(vm.implementation.getDeclaringClass());
pMethod = createCToJavaCallback(mci, c);
vtable.callbacks.put(vm.implementation, pMethod);
} catch (Throwable th) {
BridJ.error("Failed to register overridden method " + vm.implementation + " for type " + type + " (original method = " + vm.definition + ")", th);
pMethod = null;
}
}
vtable.ptr.set(iMethod, (Pointer) pMethod);
}
return vtable;
}
示例10: onCreate
import org.bridj.NativeLibrary; //导入依赖的package包/类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
helloLog(pointerToCString("Hello, World!"));
try {
NativeLibrary lib = BridJ.getNativeLibrary("example");
for (Symbol s : lib.getSymbols()) {
String p = s.getParsedRef() + "";
System.out.println(p);
}
} catch (IOException e) {
throw new RuntimeException("BridJ loading failed?", e);
}
setContentView(R.layout.activity_main);
}
示例11: registerNativeMethod
import org.bridj.NativeLibrary; //导入依赖的package包/类
@Override
protected void registerNativeMethod(Class<?> type, NativeLibrary typeLibrary, Method method, NativeLibrary methodLibrary, Builder builder, MethodCallInfoBuilder methodCallInfoBuilder) throws FileNotFoundException {
int modifiers = method.getModifiers();
boolean isCPPClass = CPPObject.class.isAssignableFrom(method.getDeclaringClass());
// Annotation[][] anns = method.getParameterAnnotations();
if (!isCPPClass) {
super.registerNativeMethod(type, typeLibrary, method, methodLibrary, builder, methodCallInfoBuilder);
return;
}
MethodCallInfo mci = methodCallInfoBuilder.apply(method);
Virtual va = method.getAnnotation(Virtual.class);
if (va == null) {
Symbol symbol = methodLibrary.getSymbol(method);
mci.setForwardedPointer(symbol == null ? 0 : symbol.getAddress());
if (mci.getForwardedPointer() == 0) {
assert error("Method " + method.toGenericString() + " is not virtual but its address could not be resolved in the library.");
return;
}
if (Modifier.isStatic(modifiers)) {
builder.addFunction(mci);
if (debug) {
info("Registering " + method + " as function or static C++ method " + symbol.getName());
}
} else {
builder.addFunction(mci);
if (debug) {
info("Registering " + method + " as C++ method " + symbol.getName());
}
}
} else {
if (Modifier.isStatic(modifiers)) {
warning("Method " + method.toGenericString() + " is native and maps to a function, but is not static.");
}
int theoreticalVirtualIndex = va.value();
int theoreticalAbsoluteVirtualIndex = theoreticalVirtualIndex < 0 ? - 1 : getAbsoluteVirtualIndex(method, theoreticalVirtualIndex, type);
int absoluteVirtualIndex;
Pointer<Pointer<?>> pVirtualTable = isCPPClass && typeLibrary != null ? (Pointer) pointerToAddress(getVirtualTable(type, typeLibrary), Pointer.class) : null;
if (pVirtualTable == null) {
if (theoreticalAbsoluteVirtualIndex < 0) {
error("Method " + method.toGenericString() + " is virtual but the virtual table of class " + type.getName() + " was not found and the virtual method index is not provided in its @Virtual annotation.");
return;
}
absoluteVirtualIndex = theoreticalAbsoluteVirtualIndex;
} else {
int guessedAbsoluteVirtualIndex = getPositionInVirtualTable(pVirtualTable, method, typeLibrary);
if (guessedAbsoluteVirtualIndex < 0) {
if (theoreticalAbsoluteVirtualIndex < 0) {
error("Method " + method.toGenericString() + " is virtual but its position could not be found in the virtual table.");
return;
} else {
absoluteVirtualIndex = theoreticalAbsoluteVirtualIndex;
}
} else {
if (theoreticalAbsoluteVirtualIndex >= 0 && guessedAbsoluteVirtualIndex != theoreticalAbsoluteVirtualIndex) {
warning("Method " + method.toGenericString() + " has @Virtual annotation indicating virtual index " + theoreticalAbsoluteVirtualIndex + ", but analysis of the actual virtual table rather indicates it has index " + guessedAbsoluteVirtualIndex + " (using the guess)");
}
absoluteVirtualIndex = guessedAbsoluteVirtualIndex;
}
}
mci.setVirtualIndex(absoluteVirtualIndex);
if (debug) {
info("Registering " + method.toGenericString() + " as virtual C++ method with absolute virtual table index = " + absoluteVirtualIndex);
}
builder.addVirtualMethod(mci);
}
}
示例12: ptrToString
import org.bridj.NativeLibrary; //导入依赖的package包/类
private String ptrToString(Pointer<?> ptr, NativeLibrary library) {
return ptr == null ? "null" : Long.toHexString(ptr.getPeer()) + " (" + library.getSymbolName(ptr.getPeer()) + ")";
}
示例13: installSyntheticVTablePtr
import org.bridj.NativeLibrary; //导入依赖的package包/类
protected boolean installSyntheticVTablePtr(Type type, NativeLibrary library, Pointer<?> peer) {
synchronized (syntheticVirtualTables) {
VTable vtable = syntheticVirtualTables.get(type);
if (vtable == null) {
if (!typesThatDontNeedASyntheticVirtualTable.contains(type)) {
List<VirtMeth> methods = new ArrayList<VirtMeth>();
listVirtualMethods(Utils.getClass(type), methods);
boolean needsASyntheticVirtualTable = false;
for (VirtMeth method : methods) {
if (!Modifier.isNative(method.implementation.getModifiers())) {
needsASyntheticVirtualTable = true;
break;
}
}
if (needsASyntheticVirtualTable) {
Type parentType = Utils.getParent(type);
@SuppressWarnings("rawtypes")
Pointer<Pointer> parentVTablePtr = null;
if (CPPObject.class.isAssignableFrom(Utils.getClass(parentType))) {
parentVTablePtr = peer.getPointer(Pointer.class);
if (BridJ.debug) {
BridJ.info("Found parent virtual table pointer = " + ptrToString(parentVTablePtr, library));
/*Pointer<Pointer> expectedParentVTablePtr = pointerToAddress(getVirtualTable(parentType, library), Pointer.class);
if (expectedParentVTablePtr != null && !Utils.eq(parentVTablePtr, expectedParentVTablePtr))
BridJ.warning("Weird parent virtual table pointer : expected " + ptrToString(expectedParentVTablePtr, library) + ", got " + ptrToString(parentVTablePtr, library));
*/
}
//parentVTablePtr = pointerToAddress(getVirtualTable(parentType, library), Pointer.class);
}
syntheticVirtualTables.put(type, vtable = synthetizeVirtualTable(type, parentVTablePtr, methods, library));
} else {
typesThatDontNeedASyntheticVirtualTable.add(type);
}
}
}
if (vtable != null) {
if (BridJ.debug) {
BridJ.info("Installing synthetic vtable pointer " + vtable.ptr + " to instance at " + peer + " (type = " + Utils.toString(type) + ", " + vtable.callbacks.size() + " callbacks)");
}
peer.setPointer(vtable.ptr);
return vtable.ptr != null;
} else {
return false;
}
}
}
示例14: newCPPInstance
import org.bridj.NativeLibrary; //导入依赖的package包/类
protected <T extends CPPObject> Pointer<T> newCPPInstance(T instance, final Type type, int constructorId, Object... args) {
Pointer<T> peer = null;
try {
final Class<T> typeClass = Utils.getClass(type);
NativeLibrary lib = BridJ.getNativeLibrary(typeClass);
if (BridJ.debug) {
info("Creating C++ instance of type " + type + " with args " + Arrays.asList(args));
}
Pointer.Releaser releaser = newCPPReleaser(type, typeClass, lib);
long size = sizeOf(type, null);
peer = (Pointer) Pointer.allocateBytes(PointerIO.getInstance(type), size, releaser).as(type);
DynamicFunction<?> constructor = constructorId == SKIP_CONSTRUCTOR ? null : getConstructor(typeClass, type, lib, constructorId);
if (lib != null && CPPObject.class.isAssignableFrom(typeClass)) {
installRegularVTablePtr(type, lib, peer);
} else {
// TODO ObjCObject : call alloc on class type !!
}
// Calling the constructor with the non-template parameters :
if (constructor != null) {
Object[] consThisArgs = new Object[1 + args.length];
consThisArgs[0] = peer;
System.arraycopy(args, 0, consThisArgs, 1, args.length);
constructor.apply(consThisArgs);
}
// Install synthetic virtual table and associate the Java instance to the corresponding native pointer :
if (CPPObject.class.isAssignableFrom(typeClass)) {
if (installSyntheticVTablePtr(type, lib, peer)) {
BridJ.setJavaObjectFromNativePeer(peer.getPeer(), instance);
}
} else {
// TODO ObjCObject : call alloc on class type !!
}
return peer;
} catch (Exception ex) {
ex.printStackTrace();
if (peer != null) {
peer.release();
}
throw new RuntimeException("Failed to allocate new instance of type " + type, ex);
}
}
示例15: VC9Demangler
import org.bridj.NativeLibrary; //导入依赖的package包/类
public VC9Demangler(NativeLibrary library, String str) {
super(library, str);
}