Search code examples
androidgotolabeled-statements

Android - How to use labeled statements


I am working on a project for which I have reverse engineered the code of other project. But, the code contains so much goto statements and a label with it.

I tried to rearrange the code as per the labels used, but not getting the proper output. I know this could be out of scope for you guys because you don't know the code.

My query is regarding how can I use the labeled statements in Android, as I am unable to find any specific code or demo examples.

Below is the code snippet of the code on which I am working.

    public static String computeIMEI()
{
    String s1 = ((TelephonyManager)getInstance().getSystemService("phone")).getDeviceId();
    if (s1 != null) goto _L2; else goto _L1
 _L1:
    String s = "not available";
 _L4:
    Log.d("IMEI", (new StringBuilder()).append("got deviceID='").append(s).append("'").toString());
    return s;
 _L2:
    s = s1;
    if (s1.equals("000000000000000"))
    {
        s = "1971b8df0a9dccfd";
    }
    if (true) goto _L4; else goto _L3
_L3:
}

Your little help will be much appreciated, thank you.


Solution

  • OMG! Where did you get it? :)
    Normally nobody uses goto statements. The code with it quite hard to read and understand.

    if (s1 != null) goto _L2; else goto _L1 pretty evident. If s1 equals null, we go to _L1 label, and then to _L4 and return from method.

    If s1 not equals null, we go to _L2 label, and then to _L4 again (if (true) goto _L4; else goto _L3, else branch never will be executed) and return from method.

    Your code in "translated" form:

    public static String computeIMEI() {
        String s1 = ((TelephonyManager)getInstance().getSystemService("phone")).getDeviceId();
        if (s1 != null) {
            s = s1;
            if (s1.equals("000000000000000")) {
                s = "1971b8df0a9dccfd";
            }
        } else {
            String s = "not available";
        }
    
        Log.d("IMEI", (new StringBuilder()).append("got deviceID='").append(s).append("'").toString());
        return s;
    }