仅将java类实例转换为基本类型

仅将java类实例转换为基本类型,java,transform,converter,primitive-types,flatten,Java,Transform,Converter,Primitive Types,Flatten,我想“展平”/将一个实例从给定的Java类转换为另一个只包含基本类型的实例 对象中尚未成为基元类型的每个字段也必须递归展平,以便生成的对象包含原始对象的展平视图(仅包含基元类型) 例如,给定以下类别: class Element { int id_; Position position_; } class Position { int x_; int y_; } 结果实例的类型为: class FlattenedElement { int id_; int x_;

我想“展平”/将一个实例从给定的Java类转换为另一个只包含基本类型的实例

对象中尚未成为基元类型的每个字段也必须递归展平,以便生成的对象包含原始对象的展平视图(仅包含基元类型)

例如,给定以下类别:

class Element {
  int id_;
  Position position_;
}

class Position {
  int x_;
  int y_;
}
结果实例的类型为:

class FlattenedElement {
  int id_;

  int x_;
  int y_;
}
我期待这样的行为:

Element e = new Person ( 42, new Position(0, 0) );
FlattenedElement fe = e.flatten();
有任何工具/库可以这样做吗?

或者我需要使用反射API编写自己的平坦器吗

--编辑以下大多数评论--

我更改了示例以避免与
字符串
字节
混淆(我不必处理字符串)

我需要在结果转换中保留数据的语义。输出的原语类型数据将直接参与使用OpenCL的并行计算,OpenCL只理解原语数据类型


换句话说,我需要的是打破封装,直接操作嵌入在类层次结构中的基本数据类型。

好吧,从我的观点来看,一种方法是:

public class Person {
    String name;
    Atring address;

    public FlattenedPerson flatten() {
        FlattenedPerson fp = new FlattenedPerson(this.name.getBytes(), this.houseNumber, this.address.getStreet.getBytes(), this.address.getTown.getBytes());
        return fp;
    }
}

你必须使用反射

看一下,返回字段的名称


因此,要获取此字段的值,您必须返回
f[i]。获取(Object)

您可以这样做,但您需要第三方库来创建新类,如Javaassist。要反思要展平的类,您将使用反射,正如一些用户已经说过的那样。下面是我为您编写的另一个示例:

public static Class flatten( Class classToFlatten ) {

    for ( Field f : classToFlatten.getDeclaredFields() ) {

        Class type = f.getType();
        String name = f.getName();
        int modifiersConfig = f.getModifiers();

        // extract all modifiers here (using the isXXX methods of the Modifier class)
        System.out.print( Modifier.isPrivate( modifiersConfig ) ? "private" : "" );
        System.out.print( " " + type );
        System.out.print( " " + name );
        System.out.println();

        if ( type.isPrimitive() ) {
            // primitive type.
            // does not need to be converted.
            // insert in the new class as it is (using Javaassist).
        } else {

            // no primitive type.
            // needs to be converted and tested.
            // insert in the new class after the conversion (using Javaassist).

            // convert rules...
            switch ( type.getSimpleName() ) {

                case "String":
                    // insert in the new class a array of chars.
                    break;

                // cases to test if the field is a wrapper (Integer, Long, etc.).

                // other rules tha you need here...

            }

            // here you need to use recursion...
            // you can use this method, but you will need to create
            // a new parameter, passing the class that is been built
            // to insert new fields in it (relative to the type that is been
            // reflected)

        }
    }

    // return the new class here and outside the method, use the newInstance
    // method to create new instances of this class
    return null;

}
有了这个,我想你可以做你需要的,但我在想你为什么需要这个。当您使用某种类型的数组进行递归Flette处理时,您会遇到一些问题,因为您可能需要“回溯”来解析它们的类型,或者有时需要运行展平算法。对于泛型、内部类、方法和所有类型的类成员,您将遇到相同的问题。我并不是说这是不可能的,但它会占用你很多时间,也许你可以像一些用户已经说过的那样,用一种更简单的方式解决你的问题。嗯,我希望你能做你需要的,如果可能的话,我想知道结果!也许你的解决方案可以成为一个新图书馆的种子!好的,头脑风暴已经够多了:这里有一些链接


我建议您仅在开始时使用它,因为自创建本教程以来,Javaassist API可能已经得到了改进,因此,在阅读本教程之后,重构代码(如果可能),阅读库文档。

您没有将字符串展平为
char[]
?是的。我想使用一个名为Aparapi的库,它只支持基本类型,但不支持char;)使用反射编写您自己的平坦器。使用反射应该足够简单。但更好的问题是,你为什么想要这样的东西?我想不出一个理由来说明这一点。我想我们可以很肯定,你试图解决的问题是错误的。这是一个非常专业的任务,你要求的,不要希望有一个图书馆。实际上,您正在以非常非标准的方式序列化对象。也许将所有内容转换为
字节[]
——这就是序列化为您所做的工作。谢谢您的回答,但这不是我所期望的。我需要一个能适应任何课程的动态行为。啊,你可能想编辑你的问题,你正在寻找一个库/方法来为任何课程做这件事。(我假设您希望保持原语类型不变,并将其他所有内容转换为字节数组?)