简体   繁体   中英

New instance of inner static class has variables in different order in pre-lollipop devices

I am working on a bug in existing code-base of an android application. We have a static inner class like shown below:

public class MyOuterClass{

private MyInnerClass myInnerClassObject;
private int someOtherVariable;

// getter and setter of my inner class
public MyInnerClass getMyInnerClassObject(){
return myInnerClassObject;
}

public void setMyInnerClassObject(MyInnerClass myInnerClassObject){
this.myInnerClassObject = myInnerClassObject;
}

public static class MyInnerClass{

private int variable1;
private int variable2;
private int variable 3;
...

// here are getters and setters for above variables in same order which I have skipped.

}

Now the place where the above code is called is as below:

private MyInnerClass getMyInnerClassObject(AnotherObject anotherObject){

MyInnerClass myInnerClassObject = new MyInnerClass();
myInnerClassObject.setFirstVariable(anotherObject.getFirstValue);
myInnerClassObject.setSecondVariable(anotherObject.getSecondValue);
myInnerClassObject.setThirdVariable(anotherObject.getThirdValue);
...
return myInnerClassObject;
}

This object returned in the above function is mapped into XML in the order it is formed. Problem arises there. When running this code in devices having Android version more than or equal to lollipop, the order of these variables when seen in debug mode of Android Studio for this object is the same as we have defined in code. But when running the same code in devices having Android version less than lollipop like Kitkat, a strange phenomenon is observed. The same object when looked through debugger of Android Studio now has these variables in a completely different order.

This causes problem to me because the request XML of my web-service formed in pre-lollipop devices from the mapping has elements in different order, thus causing problem to me.

What is the reason of this strange phenomenon? Does Android have different behaviors for static inner classes call in different versions?

I have changed name of variables and classes for code confidentiality reason. Hope the flow I tried to explain is clear. Feel free to comment for any clarification.

This object returned in the above function is mapped into XML in the order it is formed.

That is a really bad idea.

What is the reason of this strange phenomenon?

You are using a flawed approach for generating XML.

Does Android have different behaviors for static inner classes call in different versions?

AFAIK, there is no requirement for the compiled code to have its fields and other members in any particular order. In addition, other tools, such as ProGuard, may alter the order as part of their processing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM