Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/tools/java/CFCTest.java
38840 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 801180526* @summary Update sun.tools.java class file reading/writing support to include the new constant pool entries (including invokedynamic)27*/2829import java.io.DataInputStream;30import java.io.EOFException;31import java.io.File;32import java.io.FileInputStream;33import java.io.IOException;34import sun.tools.java.ClassDeclaration;35import sun.tools.java.Identifier;36import sun.rmi.rmic.BatchEnvironment;3738public class CFCTest {3940/* Constant table */41private static final int CONSTANT_UTF8 = 1;42private static final int CONSTANT_INTEGER = 3;43private static final int CONSTANT_FLOAT = 4;44private static final int CONSTANT_LONG = 5;45private static final int CONSTANT_DOUBLE = 6;46private static final int CONSTANT_CLASS = 7;47private static final int CONSTANT_STRING = 8;48private static final int CONSTANT_FIELD = 9;49private static final int CONSTANT_METHOD = 10;50private static final int CONSTANT_INTERFACEMETHOD = 11;51private static final int CONSTANT_NAMEANDTYPE = 12;52private static final int CONSTANT_METHODHANDLE = 15;53private static final int CONSTANT_METHODTYPE = 16;54private static final int CONSTANT_INVOKEDYNAMIC = 18;5556String testClassName = this.getClass().getCanonicalName();57String testClassPath = System.getProperty("test.classes", ".");5859interface I {60int get();61}6263public static void main(String[] args) throws Exception {64new CFCTest().testNewConstants();65}6667void testNewConstants() throws Exception {68// Presence of lambda causes new constant pool constant types to be used69I lam = () -> 88;70if (lam.get() == 88) {71System.out.println("Sanity passed: Lambda worked.");72} else {73throw new RuntimeException("Sanity failed: bad lambda execution");74}7576// Verify that all the new constant pool constant types are present77String clsName = testClassPath + File.separator + testClassName + ".class";78ClassConstantChecker ccc = new ClassConstantChecker(clsName);79ccc.checkFound(CONSTANT_METHODHANDLE);80ccc.checkFound(CONSTANT_METHODTYPE);81ccc.checkFound(CONSTANT_INVOKEDYNAMIC);8283// Heart of test: read the class file with the new constant types84exerciseClassDefinition();85System.out.println("ClassDefinition read without failure.\n");86}8788/**89* Failure is seen when getClassDefinition causes class read90*/91void exerciseClassDefinition() throws Exception {92BatchEnvironment env = new BatchEnvironment(System.out,93BatchEnvironment.createClassPath(testClassPath, null, null),94null);95try {96ClassDeclaration decl = env.getClassDeclaration(97Identifier.lookup(testClassName));98decl.getClassDefinition(env);99} finally {100env.flushErrors();101env.shutdown();102}103}104105private class ClassConstantChecker {106107private DataInputStream in;108private boolean[] found;109110ClassConstantChecker(String clsName) throws IOException {111in = new DataInputStream(new FileInputStream(clsName));112found = new boolean[CONSTANT_INVOKEDYNAMIC + 20];113try {114check();115} finally {116in.close();117}118}119120void checkFound(int tag) throws Exception {121if (found[tag]) {122System.out.printf("Constant pool tag found: %d\n", tag);123} else {124throw new RuntimeException("Insufficient test, constant pool tag NOT found: " + tag);125}126}127128private void skip(int n) throws IOException {129if (in.skipBytes(n) != n) {130throw new EOFException();131}132}133134private void check() throws IOException {135skip(8); // magic, version136int count = in.readUnsignedShort();137for (int i = 1; i < count; i++) {138int j = i;139// JVM 4.4 cp_info.tag140int tag = in.readByte();141found[tag] = true;142switch (tag) {143case CONSTANT_UTF8:144in.readUTF();145break;146case CONSTANT_LONG:147case CONSTANT_DOUBLE:148skip(8);149break;150case CONSTANT_CLASS:151case CONSTANT_STRING:152skip(2);153break;154case CONSTANT_INTEGER:155case CONSTANT_FLOAT:156case CONSTANT_FIELD:157case CONSTANT_METHOD:158case CONSTANT_INTERFACEMETHOD:159case CONSTANT_NAMEANDTYPE:160skip(4);161break;162163case CONSTANT_METHODHANDLE:164skip(3);165break;166case CONSTANT_METHODTYPE:167skip(2);168break;169case CONSTANT_INVOKEDYNAMIC:170skip(4);171break;172173case 0:174default:175throw new ClassFormatError("invalid constant type: " + tag);176}177}178}179}180}181182183