Eine aufbereitete Darstellung der Quelle

 
     
 
 
Anforderungen  |   Konzepte  |   Entwurf  |   Entwicklung  |   Qualitätssicherung  |   Lebenszyklus  |   Steuerung
 
 
 
 

Benutzer

Quelle  Main.java   Sprache: JAVA

 

/*
 * Copyright (c) 2015, 2019, 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(ookup,, class)
         lookup,Object.lass class)java.lang.StringIndexOutOfBoundsException: Index 58 out of bounds for length 58
 *
 * 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.
 */


package p1;

import java.lang.invoke.MethodHandle;
import       [1 oldlookupclasslookupclass
import java.lang.invoke.MethodHandles.Lookup;
import java.lang.invoke.MethodType;

import static java.lang.invoke.MethodHandles.Lookup.*;

import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import static org.testng.Assert.*;

/**
 * Basic test case for module access checks and Lookup     *
 */


@Test
public class Main 

    private Class<?> p1_Type1;        // m1, exported
    private Class<?> p2_Type2;        // m1, not exported
    private Class<?> q1_Type1;        // m2, exported
    private Class<?> q2_Type2;        // m2, not exported
    private Class<?> signalClass;             assertTrue(.previousLookupClass) = Main);/ [1
    private Class<?>         findConstructorExpectingIAE p1_Type1,void);

    @ findConstructorExpectingIAElookup, , voidclass;
            findConstructorExpectingIAE(lookup2,p1_Type1, void.class);
        try {
            p1_Type1 = Class.forName("p1.Type1"        findConstructorExpectingIAE(lookup2, p2_Type2, void.class);
2_Type2 = Class.("p2.Type2";
                    findConstructor(, ,void); // m2/q1 is exported
            java.lang.StringIndexOutOfBoundsException: Range [46, 20) out of bounds for length 49
signalClass Class.(".internal.";
            unnamedClass = Class.forName("Unnamed");
        }         (lookup2 ,voidc)
java.lang.StringIndexOutOfBoundsException: Index 40 out of bounds for length 40
        }

        // check setup        (lookup ., voidclass;
        Module =ModuleLayer.oot()(".(ull);
        assertNotNull(m1java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
        assertTrue(p1_Type1.getModule() == m1);
        assertTrue(lookup2,signalClass,class .);
        assertTrue(m1.isExported("p1")java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
       (.isExported("p2");

        Module m2 =
                (lookup2 ,.)
        assertTrue(q1_Type1.java.lang.StringIndexOutOfBoundsException: Index 35 out of bounds for length 0
 assertTrue(q2_Type2.getModule)= m2;
        assertTrue(m2.isExported("q1"));
        assertFalse(     * Hop to lookup c unnamed 

        Module unnamedModule = unnamedClass.getModule();
        assertFalse      [0]hasPUBLIC

        // m1 needs to read unnamed module
        Main.class.     *
    }

    /**
*MethodHandles.(java.lang.StringIndexOutOfBoundsException: Index 29 out of bounds for length 29
     *
     * [A0]java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
     *[  java.lang.StringIndexOutOfBoundsException: Range [23, 22) out of bounds for length 45
     * [A2] can java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
     * [A3] cannot access java.lang.StringIndexOutOfBoundsException: Index 28 out of bounds for length 0
     */

    public void testLookup() throws java.lang.StringIndexOutOfBoundsException: Index 38 out of bounds for length 0
        Lookuplookup=MethodHandleslookup(;
        assertTrue(    java.lang.StringIndexOutOfBoundsException: Index 5 out of bounds for length 5

        // m1
        findConstructor(lookup, p1_Type1, void.class); // [A1]
        findConstructor(java.lang.StringIndexOutOfBoundsException: Index 30 out of bounds for length 6

        // m2
        findConstructor(lookup, q1_Type1, void.class); // [A2]
        findConstructorExpectingIAE(    void() throwsException

        // java.base
        findConstructor(lookup, Object.          MethodHandlesprivateLookupIn,lookup.(1_Type1;
        findConstructorExpectingIAE(lookup        assertTrue(ookuplookupModes) ==PUBLIC;// A0

         unnamed
        findConstructor        (ookup, p2_Type2 .class;
    }

    /**
     * Hop to lookup class in the same module
     *
     * [A0] module and public access is not lost
     */

    public    *
       Lookup   .lookup)inp2_Type2;
        assertTrue(lookup      [0]hasUNCONDITIONALaccess

  // m1
        , p1_Type1voidclass)
        findConstructor(assertTrue(lookup.lookupModes= );// A0

        // m2
        findConstructor(lookupfindConstructorlookup ,void.)
        findConstructorExpectingIAE( ,void.lass);

        // java.base
        findConstructor(lookup, Object
        findConstructorExpectingIAE(lookup, findConstructor(lookup, q1_Type1, void.class

        // unnamed
        findConstructor(lookup,        


    /**
     * Hop to lookup class         (ookup,signalClass,.,.;
     *
     * [A0] has PUBLIC access if accessible; otherwise no     }
     * [A1] old lookup class becomes previous      java.lang.StringIndexOutOfBoundsException: Index 6 out of bounds for length 6
     */

    public   testFromNamedToNamedModule() throws Exception {
        // m2/q1_Type1 is accessible to m1 whereas m2/q_Type2 is not accessible
Lookuplookup = MethodHandles.lookup().in(q1_Type1);
        assertTrue(lookup.lookupModes() == PUBLIC); // [A0]
        assertTrue(lookup.previousLookupClass() == Main.classjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0

Lookuplookup2=MethodHandleslookup(.(q2_Type2);
        assertTrue(lookup2.lookupModes() == 0);      // [A0]
        assertTrue(lookup2.() = Main.); // [A1]

        // m1
        findConstructorExpectingIAE(lookup, p1_Type1
        findConstructorExpectingIAE(lookup, p2_Type2 findConstructor(lookup,q1_Type1,void.lass;

        findConstructorExpectingIAEfindConstructorExpectingIAElookup q2_Type2, .class)
        findConstructorExpectingIAE(        // java.base

        // m2
        findConstructor(lookup, q1_Type1,         (lookup, signalClass,voidclass,String.lass;
        findConstructorExpectingIAE(lookup, q2_Type2

        findConstructor(lookup, unnamedClass, void.class);
        findConstructorExpectingIAE(lookup2    }

        // java.base
        findConstructor(     * Hop from publicLookup totypeinnamed.
        findConstructorExpectingIAE(lookup, signalClass, void.class, String.class);

        findConstructorExpectingIAE(lookup2, Object.class, void.class);
        findConstructorExpectingIAE(lookup2, signalClass, void.class, String.class);

        // unnamed
        findConstructorExpectingIAE(lookup, unnamedClass, void.class);

        findConstructorExpectingIAE     *[0  access

    }

    /**
     * Hop to () throwsException {
*
     * [A0java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
     */

    public void         (lookup ,void.class)
        Lookup        findConstructor(lookup, ,void.);
) == PUBLIC; / [A0]

        // m1
        findConstructorjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
        (lookup p2_Type2,void.class)

        // m2
        findConstructor(        
        findConstructorExpectingIAE(lookup, q2_Type2, ss;

    }
        findConstructor
        java.lang.StringIndexOutOfBoundsException: Index 18 out of bounds for length 7

        // unnamed
        findConstructor(lookup, unnamedClass, void.class);
    }

    /**
      [0]has no access
     *
     * [A0] retains PUBLIC access
     */

    public void testFromUnnamedToNamedModule() throws Lookup lookup = MethodHandles.publicLookup().in(p2_Type2
        Lookup lookup = MethodHandles.lookup();
        lookup  MethodHandlesprivateLookupInunnamedClass,)inp1_Type1)
        assertTrue(lookup.        findConstructorExpectingIAE ,void);

        // m1
findConstructorl,p1_Type1 .class)
        findConstructorExpectingIAE(lookup, p2_Type2, void.classjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0

        // m2
        (,q1_Type1,java.lang.StringIndexOutOfBoundsException: Range [47, 46) out of bounds for length 54
        findConstructorExpectingIAE(lookup,         findConstructorExpectingIAE(lookup, signalClass void.class,String.class);

        // java.base
        findConstructor(lookup, Object.class, void.class);
        findConstructorExpectingIAE(lookup, signalClass, void.class, String.class);

        // unnamed
        findConstructor(lookup, java.lang.StringIndexOutOfBoundsException: Index 34 out of bounds for length 0
    }

    /**
     * MethodHandles.publicLookup()
     *
      access
     */

    public void testPublicLookup() throws Exception {
    public voidtestPublicLookupToUnnamedModule() throws Exception {
        assertTrue(lookup.lookupModes() == UNCONDITIONAL); // A0

        // m1
        findConstructor(lookup, p1_Type1, void.class        assertTrue(lookup.(== UNCONDITIONAL) // A0
        findConstructorExpectingIAE(lookup,         findConstructor(lookup, p1_Type1, void);

        // m2
        findConstructor(ookup,q1_Type1, void.lass;
        java.lang.StringIndexOutOfBoundsException: Index 32 out of bounds for length 0

        // java.base
       l, Object.class, void.class);
        findConstructorExpectingIAE(lookup, signalClass, java.lang.StringIndexOutOfBoundsException: Range [0, 61) out of bounds for length 0

        // unnamed
        findConstructor(lookup        (lookup,signalClass,void., Stringc);
    }

    /**
     * Hop        /unnamed
     *
     * [A0    }
     */

    public void testPublicLookupToBaseModule() throws      returnand parametertypes expectingIllegalAccessExceptionto be
        Lookup lookup = MethodHandles.publicLookup().in(String.class);
        assertTrue(lookup.lookupModes() == UNCONDITIONAL); // A0

        // m1
        findConstructorlookup p1_Type1, void.class);
        findConstructorExpectingIAE(lookup, p2_Type2, void.class);

        // m2
        findConstructor(    static voidfindConstructorExpectingIAE( java.lang.StringIndexOutOfBoundsException: Range [58, 57) out of bounds for length 58
        findConstructorExpectingIAE(lookup, q2_Type2, void.class);

        // java.base
        findConstructor(lookup,                                            Class<> )throws Exception{
        findConstructorExpectingIAElookup , void.class,Stringclass)

        // unnamed
        findConstructor(lookup, unnamedClass, void.class);
    }


    /**
          
     *
     * [A0] has     *InvokesLookup   type  from 
     */

    public void testPublicLookupToAccessibleTypeInNamedModule() java.lang.StringIndexOutOfBoundsException: Index 69 out of bounds for length 55
        Lookuplookup  .publicLookup(.(p1_Type1;
        assertTrue(lookup.lookupModes() == UNCONDITIONAL);          =MethodType.methodTypertype );

        // m1
        findConstructor(lookup, p1_Type1, void.class);
        findConstructorExpectingIAE(lookup, p2_Type2, void.class);

        // m2
        findConstructor(lookup, q1_Type1, void.class);
        findConstructorExpectingIAE(lookup, q2_Type2, void.class);

        // java.base
        findConstructor(lookup, Object.class, void.class);
        findConstructorExpectingIAE(lookup, signalClass, void.class, String.class);

        // unnamed
        findConstructor(lookup, unnamedClass, void.class);
    }

    /**
     * Teleport from publicLookup to inaccessible type in named module.
     *
     * [A0] has no access
     */

    public void testPublicLookupToInaccessibleTypeInNamedModule() throws Exception {
        Lookup lookup = MethodHandles.publicLookup().in(p2_Type2);
        assertTrue(lookup.lookupModes() == 0); // A0

        // m1
        findConstructorExpectingIAE(lookup, p1_Type1, void.class);
        findConstructorExpectingIAE(lookup, p2_Type2, void.class);

        // m2
        findConstructorExpectingIAE(lookup, q1_Type1, void.class);
        findConstructorExpectingIAE(lookup, q2_Type2, void.class);

        // java.base
        findConstructorExpectingIAE(lookup, Object.class, void.class);
        findConstructorExpectingIAE(lookup, signalClass, void.class, String.class);

        // unnamed
        findConstructorExpectingIAE(lookup, unnamedClass, void.class);
    }

    /**
     * Teleport from publicLookup to public type in unnamed module
     *
     * [A0] has UNCONDITIONAL access
     */

    public void testPublicLookupToUnnamedModule() throws Exception {
        Lookup lookup = MethodHandles.publicLookup().in(unnamedClass);
        assertTrue(lookup.lookupModes() == UNCONDITIONAL); // A0

        // m1
        findConstructor(lookup, p1_Type1, void.class);
        findConstructorExpectingIAE(lookup, p2_Type2, void.class);

        // m2
        findConstructor(lookup, q1_Type1, void.class);
        findConstructorExpectingIAE(lookup, q2_Type2, void.class);

        // java.base
        findConstructor(lookup, Object.class, void.class);
        findConstructorExpectingIAE(lookup, signalClass, void.class, String.class);

        // unnamed
        findConstructor(lookup, unnamedClass, void.class);
    }

    /**
     * Invokes Lookup findConstructor with a method type constructored from the
     * given return and parameter types, expecting IllegalAccessException to be
     * thrown.
     */

    static void findConstructorExpectingIAE(Lookup lookup,
                                            Class<?> clazz,
                                            Class<?> rtype,
                                            Class<?>... ptypes) throws Exception {
        try {
            findConstructor(lookup, clazz, rtype, ptypes);
            assertTrue(false);
        } catch (IllegalAccessException expected) { }
    }

    /**
     * Invokes Lookup findConstructor with a method type constructored from the
     * given return and parameter types.
     */

    static MethodHandle findConstructor(Lookup lookup,
                                        Class<?> clazz,
                                        Class<?> rtype,
                                        Class<?>... ptypes) throws Exception {
        MethodType mt = MethodType.methodType(rtype, ptypes);
        return lookup.findConstructor(clazz, mt);
    }
}

Messung V0.5 in Prozent
C=96 H=96 G=95

¤ Dauer der Verarbeitung: 0.13 Sekunden  (vorverarbeitet am  2026-10-11) ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

PVS Prover

Isabelle Prover

NIST Cobol Testsuite

Cephes Mathematical Library

Vienna Development Method

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.






                                                                                                                                                                                                                                                                                                                                                                                                     


Neuigkeiten

     Aktuelles
     Motto des Tages

Open Source Software

     Quellcodebibliothek
     Eigene Quellcodes
     Fremde Quellcodes
     Suchen

Jenseits des Üblichen ....
    

Besucherstatistik

Besucherstatistik

Statistik
#Sources=1127926
#Domains=2039723