Path: blob/master/test/functional/cmdLineTests/lockWordAlignment/src/CreateTestObjectJar.java
6004 views
/*******************************************************************************1* Copyright (c) 2015, 2021 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/2122import java.lang.IllegalArgumentException;23import java.io.File;2425import static jdk.internal.org.objectweb.asm.Opcodes.ACC_PRIVATE;2627import java.io.FileOutputStream;28import java.io.ByteArrayInputStream;29import java.util.zip.ZipOutputStream;30import java.util.zip.ZipEntry;3132import jdk.internal.org.objectweb.asm.ClassWriter;33import jdk.internal.org.objectweb.asm.FieldVisitor;34import jdk.internal.org.objectweb.asm.ClassReader;3536public class CreateTestObjectJar {3738/* Length of buffer used to write JAR file. It can be set to any value */39private final static int bufLen = 1024;4041private static void printUsage() {42System.out.println("CreateTestObjectJar Usage:");43System.out.println();44System.out.println("CreateTestObjectJar <dir> <testType>");45System.out.println();46System.out.println("dir : Directory where the test object jar file will be written to");47System.out.println("testType : Must be one of the following: 'i', 'ii', 'iii', or 'd'");48System.out.println();49}5051public static void main(String[] args) throws Throwable {5253File testDir = new File(args[0]);54String testType = args[1];5556String jarFileName = null;5758ClassReader cr = new ClassReader("java/lang/Object");59ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);60FieldVisitor fv = null;6162cr.accept(cw, 0);6364if (!testDir.isDirectory()) {65printUsage();66throw new IllegalArgumentException("Invalid directory: " + testDir.getPath());67}6869/* Modify the Object class according with the test type */70if (testType.equalsIgnoreCase("d")) {71fv = cw.visitField(ACC_PRIVATE, "d", "D", null, null);72jarFileName = "object_d.jar";73} else if (testType.equalsIgnoreCase("i")) {74fv = cw.visitField(ACC_PRIVATE, "i", "I", null, null);75jarFileName = "object_i.jar";76} else if (testType.equalsIgnoreCase("ii")) {77fv = cw.visitField(ACC_PRIVATE, "i", "I", null, null);78fv = cw.visitField(ACC_PRIVATE, "ii", "I", null, null);79jarFileName = "object_ii.jar";80} else if (testType.equalsIgnoreCase("iii")) {81fv = cw.visitField(ACC_PRIVATE, "i", "I", null, null);82fv = cw.visitField(ACC_PRIVATE, "ii", "I", null, null);83fv = cw.visitField(ACC_PRIVATE, "iii", "I", null, null);84jarFileName = "object_iii.jar";85} else {86printUsage();87throw new IllegalArgumentException("Invalid test type: " + testType);88}8990if (null != fv) {91fv.visitEnd();92}9394/* Write the final jar file with the modified Object class in it */95if (null != jarFileName) {96ZipOutputStream outJar = new ZipOutputStream(new FileOutputStream(testDir.getPath() + "/" + jarFileName));97outJar.putNextEntry(new ZipEntry("java/lang/Object.class"));98ByteArrayInputStream inJar = new ByteArrayInputStream(cw.toByteArray());99int len;100byte[] buf = new byte[bufLen];101while ((len = inJar.read(buf)) > 0) {102outJar.write(buf, 0, len);103}104outJar.closeEntry();105outJar.close();106}107108}109110}111112113114