Ejemplo n.º 1
0
  public AVResampleLibrary() throws IOException {
    lib = BridJ.getNativeLibrary(Lib.class);

    int libVersion = avresample_version();

    majorVersion = (libVersion >> 16) & 0xff;
    minorVersion = (libVersion >> 8) & 0xff;
    microVersion = libVersion & 0xff;
    String version = String.format("%d.%d.%d", majorVersion, minorVersion, microVersion);

    File libFile = BridJ.getNativeLibraryFile(LIB_NAME);

    Logger.getLogger(getClass().getName())
        .log(
            Level.INFO,
            "Loading {0} library, version {1}...",
            new Object[] {libFile.getAbsolutePath(), version});

    if (majorVersion < MIN_MAJOR_VERSION || majorVersion > MAX_MAJOR_VERSION)
      throw new UnsatisfiedLinkError(
          "Unsupported version of the "
              + LIB_NAME
              + " native library. ("
              + MIN_MAJOR_VERSION
              + ".x.x <= required <= "
              + MAX_MAJOR_VERSION
              + ".x.x, found "
              + version
              + ")");
  }
Ejemplo n.º 2
0
  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 !!
      }

      // Setting the C++ template parameters in the instance :
      int templateParametersCount = getTemplateParametersCount(typeClass);
      if (templateParametersCount > 0) {
        Object[] templateArgs = new Object[templateParametersCount];
        System.arraycopy(args, 0, templateArgs, 0, templateParametersCount);
        setTemplateParameters(instance, typeClass, templateArgs);
      }

      // Calling the constructor with the non-template parameters :
      if (constructor != null) {
        Object[] consThisArgs = new Object[args.length - templateParametersCount + 1];
        consThisArgs[0] = peer;
        System.arraycopy(
            args, templateParametersCount, consThisArgs, 1, args.length - templateParametersCount);

        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);
    }
  }
Ejemplo n.º 3
0
 Pointer.Releaser newCPPReleaser(final Type type) {
   try {
     final Class<?> typeClass = Utils.getClass(type);
     NativeLibrary lib = BridJ.getNativeLibrary(typeClass);
     return newCPPReleaser(type, typeClass, lib);
   } catch (Throwable th) {
     throw new RuntimeException(
         "Failed to create a C++ destructor for type " + Utils.toString(type) + " : " + th, th);
   }
 }
Ejemplo n.º 4
0
 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;
 }
Ejemplo n.º 5
0
  private Bundle getBundle(String bundleName) {
    Bundle bundle = bundles.get(bundleName);
    if (bundle == null) {
      /*
      NativeLibrary.addSearchPath(bundleName, ".");

      String module_path = System.getenv("GSP_MODULES_PATH");
      if (module_path != null && !module_path.isEmpty()) {
          String delims = ":";
          String[] tokens = module_path.split(delims);
          for (int i = 0; i < tokens.length; i++) {
              if(!tokens[i].isEmpty())
                  NativeLibrary.addSearchPath(bundleName, tokens[i]);
          }
      }

      module_path = System.getProperty("gsp.module.path");
      if (module_path != null && !module_path.isEmpty()) {
          String delims = ":";
          String[] tokens = module_path.split(delims);
          for (int i = 0; i < tokens.length; i++) {
              if(!tokens[i].isEmpty())
                  System.err.println("ADDED "+tokens[i]);
                  NativeLibrary.addSearchPath(bundleName, tokens[i]);
          }
      }

      try {
          bundle = new Bundle(NativeLibrary.getInstance(bundleName), NativeFunctionFinder.create(bundleName));
          bundles.put(bundleName, bundle);
      } catch (Exception e) {
          // TODO test NativeFunctionFinder also (add exceptions in it, or in the create or ...)

          // cannot load
          // TODO report
      }*/
      try {
        bundle =
            new Bundle(BridJ.getNativeLibrary(bundleName), NativeFunctionFinder.create(bundleName));
        bundles.put(bundleName, bundle);
        // bundle.jnalibrary = NativeLibrary.getInstance(bundleName);
      } catch (Exception e) {
        throw ex("Could not load native bundle/library with name '" + bundleName + "'", e);
        // TODO test NativeFunctionFinder also (add exceptions in it, or in the create or ...)
        // cannot load
        // TODO report
      }
    }
    return bundle;
  }
Ejemplo n.º 6
0
  public static void main(String[] args) throws IOException {
    try {
      {
        JFrame f = new JFrame();
        f.setVisible(true);
        long hwndVal =
            JAWTUtils.getNativePeerHandle(f); // TODO com.sun.jna.Native.getComponentID(this);
        System.out.println("#\n# hwndVal = " + hwndVal + "\n#");

        BridJ.register();
        ValuedEnum<ETest> t = testEnum(ETest.eFirst);
        if (t != ETest.eFirst) throw new RuntimeException();
      }
      testSetGetIntBuffer(10);
      //			if (Platform.isMacOSX()) {
      //				new NSCalendar();
      //				new NSAutoReleasePool();
      //			}

      //        	NativeLibrary lib = BridJ.getNativeLibrary("OpenCL", new
      // File("/System/Library/Frameworks/OpenCL.framework/OpenCL"));
      //        	NativeLibrary lib = BridJ.getNativeLibrary("OpenCL", new
      // File("/usr/lib/libobjc.dylib"));
      //        	NativeLibrary lib = BridJ.getNativeLibrary("OpenCL", new
      // File("/Users/ochafik/nativelibs4java/Runtime/BridJ/src/test/cpp/test/build_out/darwin_universal_gcc_debug/libtest.dylib"));

      //        	Collection<Symbol> symbols = lib.getSymbols();
      BridJ.register(MyCallback.class);
      BridJ.register();

      int ra = testAddDyncall(10, 4);
      if (ra != 14) throw new RuntimeException("Expected 14, got " + ra);
      ra = testAddDyncall(10, 4);
      if (ra != 14) throw new RuntimeException("Expected 14, got " + ra);

      testNativeTargetCallbacks();
      testJavaTargetCallbacks();

      if (true) return;

      long crea = Ctest.createTest();
      crea = Pointer.pointerToAddress(crea).getPointer().getPeer();
      print("Ctest.createTest()", crea, 10, 0);
      Ctest test = new Ctest();
      // long thisPtr = test.$this.getPeer();
      // System.out.println(hex(thisPtr));
      print("Ctest.this", Pointer.pointerTo(test, Ctest.class).getPointer().getPeer(), 10, 2);
      int res = test.testAdd(1, 2);
      System.out.println("res = " + res);
      res = test.testVirtualAdd(1, 2);
      System.out.println("res = " + res);

      res = test.testVirtualAddStdCall(null, 1, 2);
      System.out.println("res = " + res);
      res = test.testAddStdCall(null, 1, 2);
      System.out.println("res = " + res);

      TaskbarListDemo.main(null);

      IShellWindows win = COMRuntime.newInstance(IShellWindows.class);
      IUnknown iu = win.QueryInterface(IUnknown.class);
      if (iu == null) throw new RuntimeException("Interface does not handle IUnknown !");
      win.Release();

      try {
        new FunctionTest().add();

        MyStruct s = new MyStruct();
        s.a(10);
        System.out.println("Created MyStruct and set it to 10");
        int a = Pointer.pointerTo(s).getInt();
        a = s.a();
        Pointer.pointerTo(s).setInt(10);
        a = Pointer.pointerTo(s).getInt();
        a = s.a();
        if (s.a() != 10) throw new RuntimeException("invalid value = " + a);
        s.b(100.0);
        if (s.b() != 100.0) throw new RuntimeException("invalid value = " + a);

      } catch (Throwable ex) {
        ex.printStackTrace();
      }

      library = BridJ.getNativeLibrary("test");
      // NativeLibrary.load(libraryPath);

      new VC9Demangler(null, "?sinInt@@YANH@Z").parseSymbol();
      new VC9Demangler(null, "?forwardCall@@YAHP6AHHH@ZHH@Z").parseSymbol();

      BridJ.register();

      // new VC9Demangler(null, "??0Ctest2@@QEAA@XZ").parseSymbol();

      for (Demangler.Symbol symbol : library.getSymbols()) {
        String name = symbol.getName();
        long addr = symbol.getAddress();
        System.out.println(name + " = \t" + hex(addr));

        if (name.startsWith("_ZTV") || name.startsWith("_ZTI") || name.startsWith("??_")) {
          print("vtable", addr, 10, 1);
        } else System.out.println("'" + name + "'");
      }

      boolean is64 = Platform.is64Bits();

    } catch (Throwable ex) {
      ex.printStackTrace();
    } finally {
      System.in.read();
    }
    /*double dres = PerfLib.testASinB(1, 2);
    res = PerfLib.testAddJNI(1, 2);
    System.out.println("Done");*/
  }