Path: blob/master/test/jdk/java/lang/constant/SymbolicDescTest.java
41149 views
/*1* Copyright (c) 2018, 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*/2223import java.lang.Class;24import java.lang.invoke.MethodHandles;25import java.lang.invoke.MethodType;26import java.lang.constant.ClassDesc;27import java.lang.constant.Constable;28import java.lang.constant.ConstantDesc;29import java.lang.constant.ConstantDescs;30import java.util.List;31import java.util.Optional;32import java.util.stream.Stream;3334import static org.testng.Assert.assertEquals;3536/**37* Base class for XxxDesc tests38*/39public abstract class SymbolicDescTest {4041public static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();4243static List<String> someDescs = List.of("Ljava/lang/String;", "Ljava/util/List;");44static String[] basicDescs = Stream.concat(Stream.of(Primitives.values())45.filter(p -> p != Primitives.VOID)46.map(p -> p.descriptor),47someDescs.stream())48.toArray(String[]::new);49static String[] paramDescs = Stream.of(basicDescs)50.flatMap(d -> Stream.of(d, "[" + d))51.toArray(String[]::new);52static String[] returnDescs = Stream.concat(Stream.of(paramDescs), Stream.of("V")).toArray(String[]::new);5354enum Primitives {55INT("I", "int", int.class, int[].class, ConstantDescs.CD_int),56LONG("J", "long", long.class, long[].class, ConstantDescs.CD_long),57SHORT("S", "short", short.class, short[].class, ConstantDescs.CD_short),58BYTE("B", "byte", byte.class, byte[].class, ConstantDescs.CD_byte),59CHAR("C", "char", char.class, char[].class, ConstantDescs.CD_char),60FLOAT("F", "float", float.class, float[].class, ConstantDescs.CD_float),61DOUBLE("D", "double", double.class, double[].class, ConstantDescs.CD_double),62BOOLEAN("Z", "boolean", boolean.class, boolean[].class, ConstantDescs.CD_boolean),63VOID("V", "void", void.class, null, ConstantDescs.CD_void);6465public final String descriptor;66public final String name;67public final Class<?> clazz;68public final Class<?> arrayClass;69public final ClassDesc classDesc;7071Primitives(String descriptor, String name, Class<?> clazz, Class<?> arrayClass, ClassDesc desc) {72this.descriptor = descriptor;73this.name = name;74this.clazz = clazz;75this.arrayClass = arrayClass;76classDesc = desc;77}78}7980static String classToDescriptor(Class<?> clazz) {81return MethodType.methodType(clazz).toMethodDescriptorString().substring(2);82}8384static ClassDesc classToDesc(Class<?> c) {85return ClassDesc.ofDescriptor(c.descriptorString());86}8788static<T> void testSymbolicDesc(ConstantDesc desc) throws ReflectiveOperationException {89testSymbolicDesc(desc, false);90}9192static<T> void testSymbolicDescForwardOnly(ConstantDesc desc) throws ReflectiveOperationException {93testSymbolicDesc(desc, true);94}9596private static<T> void testSymbolicDesc(ConstantDesc desc, boolean forwardOnly) throws ReflectiveOperationException {97// Round trip sym -> resolve -> toSymbolicDesc98Constable constable = (Constable) desc.resolveConstantDesc(LOOKUP);99Optional<? extends ConstantDesc> described = constable.describeConstable();100if (!forwardOnly) {101assertEquals(desc, described.orElseThrow());102}103104// Round trip sym -> quoted sym -> resolve105if (desc instanceof Constable) {106Optional<ConstantDesc> opt = (Optional<ConstantDesc>) ((Constable) desc).describeConstable();107ConstantDesc sr = (ConstantDesc) opt.orElseThrow().resolveConstantDesc(LOOKUP);108assertEquals(sr, desc);109}110}111}112113114