JNI:将C函数转换为Java风格函数 背景

JNI:将C函数转换为Java风格函数 背景,java,android,c,function,pointers,Java,Android,C,Function,Pointers,我使用的函数将参数作为指针传递。我需要将用C编写的函数转换为JAVA,其行为与C中的相同。我正在Windows下的Eclipse中为Android开发 C函数示例 此函数将三个指针指向char值并对其进行初始化,因此在C中,我可以编写如下内容来初始化所有三个指针: char* v1, v2, v3; int res = testFunction(v1, v2, v3, 54); 调用testFunction后,变量v1、v2、v3将用一些值初始化 JAVA函数示例 这与C中的函数相同,但是是用

我使用的函数将参数作为指针传递。我需要将用C编写的函数转换为JAVA,其行为与C中的相同。我正在Windows下的Eclipse中为Android开发

C函数示例 此函数将三个指针指向char值并对其进行初始化,因此在C中,我可以编写如下内容来初始化所有三个指针:

char* v1, v2, v3;
int res = testFunction(v1, v2, v3, 54);
调用
testFunction
后,变量
v1、v2、v3
将用一些值初始化

JAVA函数示例 这与C中的函数相同,但是是用JAVA编写的,但是JAVA中没有指针,这意味着我无法编写类似的内容:

String v1, v2, v3;
testFunction(v1, v2, v3, 54);
class Holder <T>
{
  public T element;
  public T value() { return element; }
  public void setValue(T t ) { element = t; }
}
public class Person {
    public String firstName;
    public String secondName;
    public String lastName;
}

int testFunction(Person person, int age) {
    ...
    // fill person fields
}

Person person = new Person();
int res = testFunction(person, 54);
问题:
我该怎么做才能获得与C示例中相同的JAVA函数初始化行为?这可能吗?

您可以做一些事情。您可以创建一个包含要创建的字段的bean类,例如:

 public class Names {
     String firstName;
     String secondName;
     String lastName;
     int returnValue;

     // getters/constructors etc ommitted...
 }
Holder<String> name = new Holder<String>();

int result = testFunction(name, ...);
System.out.println(name.value());
然后,testFunction可以实例化Names对象并返回它,即

 Names names = testFunction(int age);
 String firstName = names.getFirstName();
 // etc... 
如果处理字符串,另一种方法是通过StringBuilder对象,即

int testFunction(StringBuilder firstName, StringBuilder secondName, StringBuilder lastName, int age) {
   // Use StringBuilder methods such as append to create the strings eg...
   firstName.append("Bob");
}
然后在代码的其他地方您可以使用

StringBuilder firstNameBuilder = new StringBuilder();
StringBuilder secondNameBuilder = new StringBuilder();
StringBuilder thirdNameBuilder = new StringBuilder();

testFunction(firstNameBuilder, secondNameBuilder, thirdNameBuilder, 84);

String firstName = firstNameBuilder.toString();  // firstName now equals "Bob"
/// etc...
您必须应用“Holder”模式。粗略地说(我现在不能测试代码,但你会明白的),你可以这样做:

String v1, v2, v3;
testFunction(v1, v2, v3, 54);
class Holder <T>
{
  public T element;
  public T value() { return element; }
  public void setValue(T t ) { element = t; }
}
public class Person {
    public String firstName;
    public String secondName;
    public String lastName;
}

int testFunction(Person person, int age) {
    ...
    // fill person fields
}

Person person = new Person();
int res = testFunction(person, 54);
testFunction
的实现是严格的:

int testFunction(Holder<String> name, ...)
{
   name.setValue("whatever");
   // ...
}
int testFunction(持有者名称,…)
{
name.setValue(“任意”);
// ...
}

您可以让您的
testFunction(…)
返回一个包含三个(字符原语/字符串对象)的对象


您必须执行以下操作:

String v1, v2, v3;
testFunction(v1, v2, v3, 54);
class Holder <T>
{
  public T element;
  public T value() { return element; }
  public void setValue(T t ) { element = t; }
}
public class Person {
    public String firstName;
    public String secondName;
    public String lastName;
}

int testFunction(Person person, int age) {
    ...
    // fill person fields
}

Person person = new Person();
int res = testFunction(person, 54);

您的C函数
testFunction
并不像您想象的那样。它根本不会更改
v1
v2
v3
的值。它们在调用前未初始化,调用后也未初始化。