Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/Java/Openjdk/test/jdk/java/lang/constant/   (Sun/Oracle ©)  Datei vom 13.11.2022 mit Größe 4 kB image not shown  

Quelle  SymbolicDescTest.java   Sprache: JAVA

 
/*
 * Copyright (c) 2018, 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.
 */


import java.lang.Class;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.constant.ClassDesc;
import java.lang.constant.Constable;
import java.lang.constant.ConstantDesc;
import java.lang.constant.ConstantDescs;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

import static org.testng.Assert.assertEquals;

/**
 * Base class for XxxDesc tests
 */

public abstract class SymbolicDescTest {

    public static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();

    static List<String> someDescs = List.of("Ljava/lang/String;""Ljava/util/List;");
    static String[] basicDescs = Stream.concat(Stream.of(Primitives.values())
                                                     .filter(p -> p != Primitives.VOID)
                                                     .map(p -> p.descriptor),
                                               someDescs.stream())
                                       .toArray(String[]::new);
    static String[] paramDescs = Stream.of(basicDescs)
                                       .flatMap(d -> Stream.of(d, "[" + d))
                                       .toArray(String[]::new);
    static String[] returnDescs = Stream.concat(Stream.of(paramDescs), Stream.of("V")).toArray(String[]::new);

    enum Primitives {
        INT("I""int"int.classint[].class, ConstantDescs.CD_int),
        LONG("J""long"long.classlong[].class, ConstantDescs.CD_long),
        SHORT("S""short"short.classshort[].class, ConstantDescs.CD_short),
        BYTE("B""byte"byte.classbyte[].class, ConstantDescs.CD_byte),
        CHAR("C""char"char.classchar[].class, ConstantDescs.CD_char),
        FLOAT("F""float"float.classfloat[].class, ConstantDescs.CD_float),
        DOUBLE("D""double"double.classdouble[].class, ConstantDescs.CD_double),
        BOOLEAN("Z""boolean"boolean.classboolean[].class, ConstantDescs.CD_boolean),
        VOID("V""void"void.classnull, ConstantDescs.CD_void);

        public final String descriptor;
        public final String name;
        public final Class<?> clazz;
        public final Class<?> arrayClass;
        public final ClassDesc classDesc;

        Primitives(String descriptor, String name, Class<?> clazz, Class<?> arrayClass, ClassDesc desc) {
            this.descriptor = descriptor;
            this.name = name;
            this.clazz = clazz;
            this.arrayClass = arrayClass;
            classDesc = desc;
        }
    }

    static String classToDescriptor(Class<?> clazz) {
        return MethodType.methodType(clazz).toMethodDescriptorString().substring(2);
    }

    static ClassDesc classToDesc(Class<?> c) {
        return ClassDesc.ofDescriptor(c.descriptorString());
    }

    static<T> void testSymbolicDesc(ConstantDesc desc) throws ReflectiveOperationException {
        testSymbolicDesc(desc, false);
    }

    static<T> void testSymbolicDescForwardOnly(ConstantDesc desc) throws ReflectiveOperationException {
        testSymbolicDesc(desc, true);
    }

    private static<T> void testSymbolicDesc(ConstantDesc desc, boolean forwardOnly) throws ReflectiveOperationException {
        // Round trip sym -> resolve -> toSymbolicDesc
        Constable constable = (Constable) desc.resolveConstantDesc(LOOKUP);
        Optional<? extends ConstantDesc> described = constable.describeConstable();
        if (!forwardOnly) {
            assertEquals(desc, described.orElseThrow());
        }

        // Round trip sym -> quoted sym -> resolve
        if (desc instanceof Constable) {
            Optional<ConstantDesc> opt = (Optional<ConstantDesc>) ((Constable) desc).describeConstable();
            ConstantDesc sr = (ConstantDesc) opt.orElseThrow().resolveConstantDesc(LOOKUP);
            assertEquals(sr, desc);
        }
    }
}

Messung V0.5
C=94 H=89 G=91

¤ Dauer der Verarbeitung: 0.3 Sekunden  ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

Beweissystem der NASA

Beweissystem Isabelle

NIST Cobol Testsuite

Cephes Mathematical Library

Wiener Entwicklungsmethode

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.