< prev index next >

test/hotspot/jtreg/runtime/appcds/OldClassTest.java

Print this page




   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 /*
  26  * @test
  27  * @summary classes with major version < JDK_6 (50) should not be included in CDS
  28  * @requires vm.cds
  29  * @library /test/lib
  30  * @modules java.base/jdk.internal.org.objectweb.asm
  31  *          java.base/jdk.internal.misc
  32  *          java.management
  33  *          jdk.jartool/sun.tools.jar
  34  * @compile test-classes/Hello.java
  35  * @run build TestCommon JarBuilder
  36  * @run driver OldClassTest
  37  */
  38 
  39 import java.io.File;
  40 import java.io.FileOutputStream;
  41 import jdk.test.lib.process.OutputAnalyzer;
  42 import java.nio.file.Files;
  43 
  44 import java.util.*;
  45 import jdk.internal.org.objectweb.asm.*;
  46 
  47 public class OldClassTest implements Opcodes {
  48 
  49   public static void main(String[] args) throws Exception {
  50     File jarSrcFile = new File(JarBuilder.getOrCreateHelloJar());
  51 
  52     File dir = new File(System.getProperty("test.classes", "."));
  53     File jarFile = new File(dir, "OldClassTest_old.jar");
  54     String jar = jarFile.getPath();
  55 
  56     if (!jarFile.exists() || jarFile.lastModified() < jarSrcFile.lastModified()) {
  57       createTestJarFile(jarSrcFile, jarFile);
  58     } else {
  59       System.out.println("Already up-to-date: " + jarFile);
  60     }
  61 
  62     String appClasses[] = TestCommon.list("Hello");
  63 
  64     // CASE 1: pre-JDK 6 compiled classes should be excluded from the dump
  65     OutputAnalyzer output = TestCommon.dump(jar, appClasses);
  66     TestCommon.checkExecReturn(output, 0, true, "Pre JDK 6 class not supported by CDS");
  67 
  68     TestCommon.run(
  69         "-cp", jar,
  70         "Hello")
  71       .assertNormalExit("Hello Unicode world (Old)");
  72 
  73     // CASE 2: if we exlcude old version of this class, we should not pick up
  74     //         the newer version of this class in a subsequent classpath element.
  75     String classpath = jar + File.pathSeparator + jarSrcFile.getPath();
  76     output = TestCommon.dump(classpath, appClasses);
  77     TestCommon.checkExecReturn(output, 0, true, "Pre JDK 6 class not supported by CDS");
  78 
  79     TestCommon.run(
  80         "-cp", classpath,
  81         "Hello")
  82       .assertNormalExit("Hello Unicode world (Old)");
  83   }
  84 
  85   static void createTestJarFile(File jarSrcFile, File jarFile) throws Exception {
  86     jarFile.delete();
  87     Files.copy(jarSrcFile.toPath(), jarFile.toPath());
  88 
  89     File dir = new File(System.getProperty("test.classes", "."));
  90     File outdir = new File(dir, "old_class_test_classes");
  91     outdir.delete();
  92     outdir.mkdir();
  93 
  94     writeClassFile(new File(outdir, "Hello.class"), makeOldHello());
  95 
  96     JarBuilder.update(jarFile.getPath(), outdir.getPath());
  97   }


 110 public class Hello {
 111     public static void main(String args[]) {
 112         System.out.println(\u1234());
 113     }
 114     static String \u1234() {
 115         return "Hello Unicode world (Old)";
 116     }
 117 }
 118 EOF
 119 javac Hello.java
 120 java jdk.internal.org.objectweb.asm.util.ASMifier Hello.class
 121 
 122  */
 123 
 124   static byte[] makeOldHello() throws Exception {
 125     ClassWriter cw = new ClassWriter(0);
 126     FieldVisitor fv;
 127     MethodVisitor mv;
 128     AnnotationVisitor av0;
 129 
 130       cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, "Hello", null, "java/lang/Object", null);

 131 
 132     {
 133       mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
 134       mv.visitCode();
 135       mv.visitVarInsn(ALOAD, 0);
 136       mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
 137       mv.visitInsn(RETURN);
 138       mv.visitMaxs(1, 1);
 139       mv.visitEnd();
 140     }
 141     {
 142       mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
 143       mv.visitCode();
 144       mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
 145       mv.visitMethodInsn(INVOKESTATIC, "Hello", "\u1234", "()Ljava/lang/String;", false);
 146       mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
 147       mv.visitInsn(RETURN);
 148       mv.visitMaxs(2, 1);
 149       mv.visitEnd();
 150     }


   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 /*
  26  * @test
  27  * @summary classes with major version < JDK_1.5 (48) should not be included in CDS
  28  * @requires vm.cds
  29  * @library /test/lib
  30  * @modules java.base/jdk.internal.org.objectweb.asm
  31  *          java.base/jdk.internal.misc
  32  *          java.management
  33  *          jdk.jartool/sun.tools.jar
  34  * @compile test-classes/Hello.java
  35  * @run build TestCommon JarBuilder
  36  * @run driver OldClassTest
  37  */
  38 
  39 import java.io.File;
  40 import java.io.FileOutputStream;
  41 import jdk.test.lib.process.OutputAnalyzer;
  42 import java.nio.file.Files;
  43 
  44 import java.util.*;
  45 import jdk.internal.org.objectweb.asm.*;
  46 
  47 public class OldClassTest implements Opcodes {
  48 
  49   public static void main(String[] args) throws Exception {
  50     File jarSrcFile = new File(JarBuilder.getOrCreateHelloJar());
  51 
  52     File dir = new File(System.getProperty("test.classes", "."));
  53     File jarFile = new File(dir, "OldClassTest_old.jar");
  54     String jar = jarFile.getPath();
  55 
  56     if (!jarFile.exists() || jarFile.lastModified() < jarSrcFile.lastModified()) {
  57       createTestJarFile(jarSrcFile, jarFile);
  58     } else {
  59       System.out.println("Already up-to-date: " + jarFile);
  60     }
  61 
  62     String appClasses[] = TestCommon.list("Hello");
  63 
  64     // CASE 1: pre-JDK 1.5 compiled classes should be excluded from the dump
  65     OutputAnalyzer output = TestCommon.dump(jar, appClasses);
  66     TestCommon.checkExecReturn(output, 0, true, "Pre JDK 1.5 class not supported by CDS");
  67 
  68     TestCommon.run(
  69         "-cp", jar,
  70         "Hello")
  71       .assertNormalExit("Hello Unicode world (Old)");
  72 
  73     // CASE 2: if we exlcude old version of this class, we should not pick up
  74     //         the newer version of this class in a subsequent classpath element.
  75     String classpath = jar + File.pathSeparator + jarSrcFile.getPath();
  76     output = TestCommon.dump(classpath, appClasses);
  77     TestCommon.checkExecReturn(output, 0, true, "Pre JDK 1.5 class not supported by CDS");
  78 
  79     TestCommon.run(
  80         "-cp", classpath,
  81         "Hello")
  82       .assertNormalExit("Hello Unicode world (Old)");
  83   }
  84 
  85   static void createTestJarFile(File jarSrcFile, File jarFile) throws Exception {
  86     jarFile.delete();
  87     Files.copy(jarSrcFile.toPath(), jarFile.toPath());
  88 
  89     File dir = new File(System.getProperty("test.classes", "."));
  90     File outdir = new File(dir, "old_class_test_classes");
  91     outdir.delete();
  92     outdir.mkdir();
  93 
  94     writeClassFile(new File(outdir, "Hello.class"), makeOldHello());
  95 
  96     JarBuilder.update(jarFile.getPath(), outdir.getPath());
  97   }


 110 public class Hello {
 111     public static void main(String args[]) {
 112         System.out.println(\u1234());
 113     }
 114     static String \u1234() {
 115         return "Hello Unicode world (Old)";
 116     }
 117 }
 118 EOF
 119 javac Hello.java
 120 java jdk.internal.org.objectweb.asm.util.ASMifier Hello.class
 121 
 122  */
 123 
 124   static byte[] makeOldHello() throws Exception {
 125     ClassWriter cw = new ClassWriter(0);
 126     FieldVisitor fv;
 127     MethodVisitor mv;
 128     AnnotationVisitor av0;
 129 
 130 //WAS cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, "Hello", null, "java/lang/Object", null);
 131       cw.visit(V1_4, ACC_PUBLIC + ACC_SUPER, "Hello", null, "java/lang/Object", null);
 132 
 133     {
 134       mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
 135       mv.visitCode();
 136       mv.visitVarInsn(ALOAD, 0);
 137       mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
 138       mv.visitInsn(RETURN);
 139       mv.visitMaxs(1, 1);
 140       mv.visitEnd();
 141     }
 142     {
 143       mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
 144       mv.visitCode();
 145       mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
 146       mv.visitMethodInsn(INVOKESTATIC, "Hello", "\u1234", "()Ljava/lang/String;", false);
 147       mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
 148       mv.visitInsn(RETURN);
 149       mv.visitMaxs(2, 1);
 150       mv.visitEnd();
 151     }
< prev index next >