/* * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * @test * @summary Test SignatureParser * @author Jean-Francois Denise * @modules java.base/jdk.internal.jimage.decompressor * @run main SignatureParserTest */ import java.util.Arrays; import java.util.Objects; import jdk.internal.jimage.decompressor.SignatureParser; public class SignatureParserTest { private int passed = 0; private int failed = 0; public static void main(String[] args) { new SignatureParserTest().test(); } private void test() { test("[Ljava/lang/String;", "[L;", "java/lang/String"); test("[[[[[[[[[[Ljava/lang/String;", "[[[[[[[[[[L;", "java/lang/String"); test(";>" + "(Ljava/lang/String;Ljava/lang/Class;TT;Ljava/lang/Comparable<-TT;>;" + "Ljava/lang/Comparable<-TT;>;ZZ)V", ";>(L;L;TT;L<-TT;>;L<-TT;>;ZZ)V", "java/lang/Object", "java/lang/Comparable", "java/lang/String", "java/lang/Class", "java/lang/Comparable", "java/lang/Comparable"); test("(Ljava/lang/String;ZLjava/util/EventListener;TTK;)V", "(L;ZL;TTK;)V", "java/lang/String", "java/util/EventListener"); test("", "", "java/lang/String"); test("", "", "java/lang/String", "java/util/EventListener"); test(";>", ";>", "java/lang/String", "java/util/EventListener", "java/lang/Comparable", "java/lang/String"); test(";>", ";>", "java/lang/String", "java/lang/Comparable", "java/lang/String", "java/lang/Float"); test(";>;>", ";>;>", "java/lang/String", "java/lang/Comparable", "java/lang/String", "java/lang/Float", "java/lang/Object"); test("Ljava/util/Set;", "L;", "java/util/Set"); test("Ljavaapplication20/Titi<[Ljava/lang/String;Ljava/lang/Integer;>;", "L<[L;L;>;", "javaapplication20/Titi", "java/lang/String", "java/lang/Integer"); test("Ljava/lang/Comparable;", "L;", "java/lang/Comparable"); test("Ljava/io/Serializable;Ljava/lang/Comparable;", "L;L;", "java/io/Serializable", "java/lang/Comparable"); test(";>" + "Ljavaapplication20/Titi<[Ljava/lang/String;Ljava/lang/Integer;TZ;>;" + "Ljava/io/Serializable;Ljava/lang/Comparable;", ";>L<[L;L;TZ;>;L;L;", "java/lang/String", "java/util/EventListener", "java/util/EventListener", "java/lang/Comparable", "java/lang/String", "javaapplication20/Titi", "java/lang/String", "java/lang/Integer", "java/io/Serializable", "java/lang/Comparable"); test("(Ljava/lang/Integer;TPO;)Ljava/lang/Integer;", "(L;TPO;)L;", "java/lang/Object", "java/lang/Integer", "java/lang/Integer"); test("(Ljava/lang/Integer;TPO;)TPO;", "(L;TPO;)TPO;", "java/lang/Object", "java/lang/Integer"); test("(Ljava/lang/Class;)[TT;", "(L;)[TT;", "java/util/EventListener", "java/lang/Class"); test("(Ljava/lang/Integer;ITPO;)Z", "(L;ITPO;)Z", "Titi", "java/lang/Integer"); test("Ljava/lang/Object;", "L;", "java/lang/Object", "java/lang/Object", "java/lang/Object"); test("Ljava/util/LinkedHashMap.LinkedHashIterator;Ljava/util/Iterator;", "L.L;L;", "java/util/LinkedHashMap", "inkedHashIterator", "java/util/Iterator"); test("LToto;", "L;", "Toto", "java/lang/String"); test("Ljavaapplication20/Titi<[Ljava/lang/String;Ljava/lang/Integer;TZ;>;", "L<[L;L;TZ;>;", "javaapplication20/Titi", "java/lang/String", "java/lang/Integer", "Toto"); test("LX<[LQ;LW;TZ;>;", "L<[L;L;TZ;>;", "X", "Q", "W", "Toto"); test("Ljava/lang/String<*>;", "L<*>;", "java/lang/String"); test("Ljava/util/List<[B>;", "L<[B>;", "java/util/List"); test(";>Ljava/lang/Object;Ljava/util/stream/Node;", ";>L;L;", "java/lang/Object", "java/util/stream/Node", "java/lang/Object", "java/util/stream/Node"); test("Ljavaapplication20/Titi<[Ljava/lang/String;>;", "L<[L;>;", "javaapplication20/Titi", "java/lang/String"); test(";>" + "Ljava/lang/Object;Ljava/lang/reflect/InvocationHandler;" + "Lcom/sun/codemodel/internal/JAnnotationWriter;", ";>L;L;L;", "java/lang/annotation/Annotation", "com/sun/codemodel/internal/JAnnotationWriter", "java/lang/Object", "java/lang/reflect/InvocationHandler", "com/sun/codemodel/internal/JAnnotationWriter"); test(";>(Ljava/lang/Class;" + "Lcom/sun/codemodel/internal/JAnnotatable;)TW;", ";>(L;L;)TW;", "com/sun/codemodel/internal/JAnnotationWriter", "java/lang/Class", "com/sun/codemodel/internal/JAnnotatable"); test("Ljava/util/Set.Edge;>;", "L.Edge;>;", "java/util/Set", "com/sun/tools/jdeps/JdepsTask$DotGraph"); test(";>Ljava/lang/Object;", ";>L;", "com/sun/xml/internal/rngom/ast/om/ParsedElementAnnotation", "", "com/sun/xml/internal/rngom/ast/om/Location", "", "com/sun/xml/internal/rngom/ast/builder/CommentList", "", "java/lang/Object"); test("(Ljava/util/List;TL;TA;)" + "Lcom/sun/xml/internal/rngom/nc/NameClass;", "(L;TL;TA;)L;", "java/util/List", "com/sun/xml/internal/rngom/nc/NameClass", "", "com/sun/xml/internal/rngom/nc/NameClass"); test("[Ljava/util/List;", "[L;", "java/util/List"); test("[Ljava/util/List<+Lcom/sun/jdi/request/EventRequest;>;", "[L<+L;>;", "java/util/List", "com/sun/jdi/request/EventRequest"); test("Lcom/sun/xml/internal/bind/v2/util/QNameMap.HashIterator" + ";>;", "L.HashIterator;>;", "com/sun/xml/internal/bind/v2/util/QNameMap", "com/sun/xml/internal/bind/v2/util/QNameMap$Entry"); test("[Ljava/lang/String;", "[L;", "java/lang/String"); test("[Ljava/lang/String;>;", "[L;>;", "java/lang/String", "java/lang/Toto", "java/lang/Titi"); test("(ZCLjava/lang/Class;IJS)[TT;", "(ZCL;IJS)[TT;", "java/util/EventListener", "java/util/BOO", "java/lang/Class"); test("(TT;ILjava/lang/Long;)TT;", "(TT;IL;)TT;", "java/lang/Object", "java/lang/Long"); test("(TT;ILjava/lang/Long;)TT;^TT;", "(TT;IL;)TT;^TT;", "java/lang/Object", "java/lang/Long"); test("(TT;ILjava/lang/Long;)TT;^TT;^Ljava/lang/Exception;", "(TT;IL;)TT;^TT;^L;", "java/lang/Object", "java/lang/Long", "java/lang/Exception"); if (passed + failed == 0) { throw new AssertionError("No tests were run"); } String message = String.format("Passed: %d, failed: %d, total: %d", passed, failed, passed + failed); if (failed > 0) { throw new AssertionError("Test failed: " + message); } else { System.err.println(message); } } private void test(String type, String formatted, String...classNames) { try { SignatureParser.ParseResult result = SignatureParser.parseSignatureDescriptor(type); String[] parsedNames = parse(classNames); assertEquals(result.formatted, formatted, "Input: '" + type + "', checking 'formatted'"); assertEquals(result.types.size(), 2 * classNames.length, "Input: '" + type + "', checking the length of 'types':" + "\nexpected: " + Arrays.toString(parsedNames) + "\n got: " + result.types); for (int i = 0; i < result.types.size(); ++i) { assertEquals(result.types.get(i), parsedNames[i], "Input: '" + type + "', checking 'packageName' at index " + i / 2); ++i; assertEquals(result.types.get(i), parsedNames[i], "Input: '" + type + "', checking 'simpleName' at index " + i / 2); } String reconstructed = SignatureParser.reconstruct(result.formatted, result.types); assertEquals(reconstructed, type, "Input: '" + type + "', checking reconstruction from: " + result.formatted + " " + result.types); ++passed; } catch (Exception | AssertionError e) { e.printStackTrace(); ++failed; } } private void assertEquals(Object actual, Object expected, String message) { if (!Objects.equals(actual, expected)) { throw new AssertionError(message + ": expected: " + expected + ", actual: " + actual); } } private String[] parse(String[] classNames) { String[] result = new String[2 * classNames.length]; for (int i = 0; i < classNames.length; ++i) { int index = classNames[i].lastIndexOf("/"); result[2 * i] = index == -1 ? "" : classNames[i].substring(0, index); result[2 *i + 1] = classNames[i].substring(index + 1); } return result; } }