caoccao / Javet

Javet is Java + V8 (JAVa + V + EighT). It is an awesome way of embedding Node.js and V8 in Java.
https://www.caoccao.com/Javet/
Apache License 2.0
702 stars 68 forks source link

JavetBuddy is unable to generate bytecode #403

Open AsyncProxy opened 3 days ago

AsyncProxy commented 3 days ago

1.Follow your example and write an anonymous class.

public class TestJava {

public int add() {
    return 2;
}

}

  1. Initialize the proxy plugin.

javetProxyConverter.getConfig().setReflectionObjectFactory(JavetReflectionObjectFactory.getInstance()); IJavetAnonymous anonymous = new IJavetAnonymous() { @V8Function public void JavaAptder(TestJava testJava) throws Exception { testJava.add(); } };

JS: Java.JavaAptder({

add() {
    return 1000;
}

})

3.Error: failed with error message Attempt to invoke virtual method 'int com.editor.async.script.TestJava.add()' on a null object reference.

caoccao commented 3 days ago

Please follow the doc strictly and try again. The JS code should look like the following.

{
  add: (a, b) => a + b,
}
AsyncProxy commented 3 days ago

请严格按照文档进行操作,然后重试。JS 代码应如下所示。

{
  add: (a, b) => a + b,
}

Please ask me what I did wrong, give a reasonable explanation, I have done it strictly according to the example of the document. 1 2 3 4 5

AsyncProxy commented 3 days ago

请严格按照文档进行操作,然后重试。JS 代码应如下所示。

{
  add: (a, b) => a + b,
}

I tried java.io.File at the beginning, he reported an error with multiple constructors, which was thrown by ByteBuddy internally, and I later opened the DynamicObjectAutoCloseableInvocationHandler class from your source code to try to generate bytecode, but it didn't work, the same error was thrown, Android architecture.

caoccao commented 3 days ago

It only works for classes with default constructor. New features are not planned.

AsyncProxy commented 3 days ago

它仅适用于具有 default 构造函数的类。新功能未计划。

But I wrote TestJava according to your example, which is the same as your example, why is it also wrong, if it says java.io.File, then it is very reasonable, but TestJava I came according to your documentation, what is going on?

AsyncProxy commented 3 days ago

它仅适用于具有 default 构造函数的类。新功能未计划。

It's the same as the default constructor class, but it doesn't work, and I see from your anonymous implementation, it seems that you need to pass a class object instead of V8Object, when I pass a specified class object in the test function, he doesn't report an error, but the function is completely inconsistent.

anonyein commented 3 days ago

@AsyncProxy net.bytebuddy.ByteBuddy please check this! Maybe it cannot fit Android!

caoccao commented 3 days ago

Well, it seems JavetBuddy needs to adapt to Android in the future.

AsyncProxy commented 3 days ago

好吧,看来 JavetBuddy 将来需要适应 Android。

I've also used ByteBuddy before, it seems to be stuck in the getLoaded place, when I tried to generate class before, it can only work on generating empty shell classes, r8 compiles to dex loading, so I think ByteBuddy may not be compatible with android, but I don't know if there is any other way to quickly generate bytecode.