14eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light/*
24eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light * Copyright (C) 2017 The Android Open Source Project
34eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light *
44eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light * Licensed under the Apache License, Version 2.0 (the "License");
54eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light * you may not use this file except in compliance with the License.
64eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light * You may obtain a copy of the License at
74eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light *
84eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light *      http://www.apache.org/licenses/LICENSE-2.0
94eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light *
104eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light * Unless required by applicable law or agreed to in writing, software
114eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light * distributed under the License is distributed on an "AS IS" BASIS,
124eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light * See the License for the specific language governing permissions and
144eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light * limitations under the License.
154eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light */
164eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light
174eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Lightpackage art;
184eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light
194eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Lightimport java.util.ArrayList;
204eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light// Common Redefinition functions. Placed here for use by CTS
214eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Lightpublic class Redefinition {
224eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light  public static final class CommonClassDefinition {
234eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light    public final Class<?> target;
244eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light    public final byte[] class_file_bytes;
254eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light    public final byte[] dex_file_bytes;
264eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light
274eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light    public CommonClassDefinition(Class<?> target, byte[] class_file_bytes, byte[] dex_file_bytes) {
284eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light      this.target = target;
294eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light      this.class_file_bytes = class_file_bytes;
304eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light      this.dex_file_bytes = dex_file_bytes;
314eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light    }
324eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light  }
334eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light
343ccf980505e8b62280cb08616efce28113037f28Alex Light  // A set of possible test configurations. Test should set this if they need to.
353ccf980505e8b62280cb08616efce28113037f28Alex Light  // This must be kept in sync with the defines in ti-agent/common_helper.cc
363ccf980505e8b62280cb08616efce28113037f28Alex Light  public static enum Config {
373ccf980505e8b62280cb08616efce28113037f28Alex Light    COMMON_REDEFINE(0),
383ccf980505e8b62280cb08616efce28113037f28Alex Light    COMMON_RETRANSFORM(1),
393ccf980505e8b62280cb08616efce28113037f28Alex Light    COMMON_TRANSFORM(2);
403ccf980505e8b62280cb08616efce28113037f28Alex Light
413ccf980505e8b62280cb08616efce28113037f28Alex Light    private final int val;
423ccf980505e8b62280cb08616efce28113037f28Alex Light    private Config(int val) {
433ccf980505e8b62280cb08616efce28113037f28Alex Light      this.val = val;
443ccf980505e8b62280cb08616efce28113037f28Alex Light    }
453ccf980505e8b62280cb08616efce28113037f28Alex Light  }
463ccf980505e8b62280cb08616efce28113037f28Alex Light
473ccf980505e8b62280cb08616efce28113037f28Alex Light  public static void setTestConfiguration(Config type) {
483ccf980505e8b62280cb08616efce28113037f28Alex Light    nativeSetTestConfiguration(type.val);
493ccf980505e8b62280cb08616efce28113037f28Alex Light  }
503ccf980505e8b62280cb08616efce28113037f28Alex Light
513ccf980505e8b62280cb08616efce28113037f28Alex Light  private static native void nativeSetTestConfiguration(int type);
523ccf980505e8b62280cb08616efce28113037f28Alex Light
534eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light  // Transforms the class
544eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light  public static native void doCommonClassRedefinition(Class<?> target,
554eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light                                                      byte[] classfile,
564eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light                                                      byte[] dexfile);
574eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light
584eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light  public static void doMultiClassRedefinition(CommonClassDefinition... defs) {
594eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light    ArrayList<Class<?>> classes = new ArrayList<>();
604eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light    ArrayList<byte[]> class_files = new ArrayList<>();
614eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light    ArrayList<byte[]> dex_files = new ArrayList<>();
624eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light
634eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light    for (CommonClassDefinition d : defs) {
644eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light      classes.add(d.target);
654eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light      class_files.add(d.class_file_bytes);
664eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light      dex_files.add(d.dex_file_bytes);
674eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light    }
684eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light    doCommonMultiClassRedefinition(classes.toArray(new Class<?>[0]),
694eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light                                   class_files.toArray(new byte[0][]),
704eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light                                   dex_files.toArray(new byte[0][]));
714eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light  }
724eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light
734eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light  public static void addMultiTransformationResults(CommonClassDefinition... defs) {
744eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light    for (CommonClassDefinition d : defs) {
754eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light      addCommonTransformationResult(d.target.getCanonicalName(),
764eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light                                    d.class_file_bytes,
774eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light                                    d.dex_file_bytes);
784eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light    }
794eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light  }
804eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light
814eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light  public static native void doCommonMultiClassRedefinition(Class<?>[] targets,
824eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light                                                           byte[][] classfiles,
834eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light                                                           byte[][] dexfiles);
844eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light  public static native void doCommonClassRetransformation(Class<?>... target);
854eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light  public static native void setPopRetransformations(boolean pop);
864eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light  public static native void popTransformationFor(String name);
874eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light  public static native void enableCommonRetransformation(boolean enable);
884eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light  public static native void addCommonTransformationResult(String target_name,
894eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light                                                          byte[] class_bytes,
904eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light                                                          byte[] dex_bytes);
914eec3c5a28ce1592ff53b11270fd430d1217aa07Alex Light}
92