1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package art;
18 
19 import java.util.Base64;
20 public class Test2003 {
21 
22   public static class Transform {
getGreeting()23     public String getGreeting() {
24       return "Hi";
25     }
26   }
27 
28   public static class SubTransform extends Transform {
29     private int count = 0;
sayHi()30     public void sayHi() {
31       System.out.println(getGreeting() + "(SubTransform called " + (++count) + " times)");
32     }
33   }
34   /**
35    * base64 encoded class/dex file for
36    * public static class SubTransform extends Transform {
37    *   private int count = 0;
38    *   public void sayHi() {
39    *     System.out.println(getGreeting() + "(SubTransform called " + (++count) + " times, Transform called " + getCount() + " times)");
40    *   }
41    * }
42    */
43   private static final byte[] SUBTRANSFORM_DEX_BYTES = Base64.getDecoder().decode(
44 "ZGV4CjAzNQCh/FHVi1J2bdQclTBNUHHeAbrR2Sy2cWXoBQAAcAAAAHhWNBIAAAAAAAAAACQFAAAh" +
45 "AAAAcAAAAAsAAAD0AAAABgAAACABAAACAAAAaAEAAAoAAAB4AQAAAQAAAMgBAAAABAAA6AEAAJ4C" +
46 "AACnAgAAwgIAANkCAADhAgAA5AIAAOcCAADrAgAA7wIAAAwDAAAmAwAANgMAAFoDAAB6AwAAkQMA" +
47 "AKUDAADAAwAA1AMAAOIDAADxAwAA9AMAAPgDAAAFBAAADQQAABQEAAAeBAAAKwQAADEEAAA2BAAA" +
48 "PwQAAEYEAABQBAAAVwQAAAQAAAAIAAAACQAAAAoAAAALAAAADAAAAA0AAAAOAAAADwAAABAAAAAT" +
49 "AAAABAAAAAAAAAAAAAAABQAAAAcAAAAAAA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GluZwAEbmFtZQADb3V0AAdwcmlu" +
63 "dGxuAAVzYXlIaQAIdG9TdHJpbmcABXZhbHVlAIsBfn5EOHsiY29tcGlsYXRpb24tbW9kZSI6ImRl" +
64 "YnVnIiwiaGFzLWNoZWNrc3VtcyI6ZmFsc2UsIm1pbi1hcGkiOjEsInNoYS0xIjoiODViZjE2Yzc1" +
65 "NjUzZDQwNGE0YzNlZDQzNjA3Yzc3Yjg1YmFmMzFlZSIsInZlcnNpb24iOiIyLjAuNS1kZXYifQAC" +
66 "BAEfGAMCBQIVBAkaFxEAAQEBAAIAgYAE6AMDAYgEAAAAAAIAAADlBAAA6wQAAAgFAAAAAAAAAAAA" +
67 "AAAAAAAQAAAAAAAAAAEAAAAAAAAAAQAAACEAAABwAAAAAgAAAAsAAAD0AAAAAwAAAAYAAAAgAQAA" +
68 "BAAAAAIAAABoAQAABQAAAAoAAAB4AQAABgAAAAEAAADIAQAAASAAAAIAAADoAQAAAyAAAAIAAACC" +
69 "AgAAARAAAAIAAACQAgAAAiAAACEAAACeAgAABCAAAAIAAADlBAAAACAAAAEAAAD0BAAAAxAAAAIA" +
70 "AAAEBQAABiAAAAEAAAAUBQAAABAAAAEAAAAkBQAA");
71 
72   /**
73    * base64 encoded class/dex file for
74    * public static class Transform {
75    *   private int count;
76    *   public String getGreeting() {
77    *     incrCount();
78    *     return "Hello";
79    *   }
80    *   protected void incrCount() {
81    *     ++count;
82    *   }
83    *   protected int getCount() {
84    *     return count;
85    *   }
86    * }
87    */
88   private static final byte[] TRANSFORM_DEX_BYTES = Base64.getDecoder().decode(
89 "ZGV4CjAzNQCAt16FlKvFzDaE6l56jUkorc7YXyrJmRpsBAAAcAAAAHhWNBIAAAAAAAAAALQDAAAV" +
90 "AAAAcAAAAAgAAADEAAAAAwAAAOQAAAABAAAACAEAAAUAAAAQAQAAAQAAADgBAAAUAwAAWAEAANQB" +
91 "AADcAQAA4wEAAOYBAADpAQAAAwIAABMCAAA3AgAAVwIAAGsCAAB/AgA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AfAC" +
105 "AQSkAwAAAAACAAAAbQMAAHMDAACYAwAAAAAAAAAAAAAAAAAADwAAAAAAAAABAAAAAAAAAAEAAAAV" +
106 "AAAAcAAAAAIAAAAIAAAAxAAAAAMAAAADAAAA5AAAAAQAAAABAAAACAEAAAUAAAAFAAAAEAEAAAYA" +
107 "AAABAAAAOAEAAAEgAAAEAAAAWAEAAAMgAAAEAAAAwgEAAAIgAAAVAAAA1AEAAAQgAAACAAAAbQMA" +
108 "AAAgAAABAAAAfAMAAAMQAAACAAAAlAMAAAYgAAABAAAApAMAAAAQAAABAAAAtAMAAA==");
109 
110 
run()111   public static void run() {
112     Redefinition.setTestConfiguration(Redefinition.Config.COMMON_REDEFINE);
113     doTest(new SubTransform());
114   }
115 
doTest(SubTransform t)116   public static void doTest(SubTransform t) {
117     t.sayHi();
118     t.sayHi();
119     t.sayHi();
120     Redefinition.doMultiStructuralClassRedefinition(
121         new Redefinition.CommonClassDefinition(SubTransform.class, null, SUBTRANSFORM_DEX_BYTES),
122         new Redefinition.CommonClassDefinition(Transform.class, null, TRANSFORM_DEX_BYTES));
123     t.sayHi();
124     t.sayHi();
125     t.sayHi();
126   }
127 }
128