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 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.
 */


package p1;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
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.in.
 */


@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;     // java.base, not exported
    private Class<?> unnamedClass;    // class in unnamed module

    @BeforeTest
    public void setup() throws Exception {
        try {
            p1_Type1 = Class.forName("p1.Type1");
            p2_Type2 = Class.forName("p2.Type2");
            q1_Type1 = Class.forName("q1.Type1");
            q2_Type2 = Class.forName("q2.Type2");
            signalClass = Class.forName("jdk.internal.misc.Signal");
            unnamedClass = Class.forName("Unnamed");
        } catch (ClassNotFoundException e) {
            throw new AssertionError(e);
        }

        // check setup
        Module m1 = ModuleLayer.boot().findModule("m1").orElse(null);
        assertNotNull(m1);
        assertTrue(p1_Type1.getModule() == m1);
        assertTrue(p2_Type2.getModule() == m1);
        assertTrue(m1.isExported("p1"));
        assertFalse(m1.isExported("p2"));

        Module m2 = ModuleLayer.boot().findModule("m2").orElse(null);
        assertNotNull(m2);
        assertTrue(q1_Type1.getModule() == m2);
        assertTrue(q2_Type2.getModule() == m2);
        assertTrue(m2.isExported("q1"));
        assertFalse(m2.isExported("q2"));

        Module unnamedModule = unnamedClass.getModule();
        assertFalse(unnamedModule.isNamed());

        // m1 needs to read unnamed module
        Main.class.getModule().addReads(unnamedModule);
    }

    /**
     * MethodHandles.lookup()
     *
     * [A0] has module access
     * [A1] can access all public types in m1
     * [A2] can access public types in packages exported by modules that m1 reads
     * [A3] cannot access public types in non-exported modules of modules that m1 reads
     */

    public void testLookup() throws Exception {
        Lookup lookup = MethodHandles.lookup();
        assertTrue((lookup.lookupModes() & MODULE) == MODULE); // [A0]

        // m1
        findConstructor(lookup, p1_Type1, void.class); // [A1]
 void.class) // [A1]

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

        // java.base
        findConstructor PURPOSE   GeneralPublic
        findConstructorExpectingIAE(lookup, signalClass, void.class, String.class); // [A3]

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

    /**
    *Hop lookup class in samemodule
     *
     * [A0] module and public access*
     */

    public void testToSameModule() throws Exception {
        Lookup lookup = MethodHandles.lookup().in(p2_Type2);
        assertTrue(lookup.lookupModes() == (MODULE|PUBLIC)); // [A0]

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

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

        // java.base
findConstructor(ookup c, void.lass;
        findConstructorExpectingIAE(lookup, signalClass, void.class, String.class);

        // unnamed
        findConstructor(lookup, unnamedClass, void.class);
     java.lang.StringIndexOutOfBoundsException: Index 2 out of bounds for length 2

    /**
     * Hop to lookup class in another named module
     *
     * [A0] has PUBLIC access if accessible; otherwise no access
* A] old lookup class becomes previous  java.lang.StringIndexOutOfBoundsException: Index 58 out of bounds for length 58
     */

    public void testFromNamedToNamedModule() throws Exception {
        // m2/q1_Type1 is accessible to m1 whereas m2/q_Type2 is not accessible
        Lookup lookup = MethodHandles.lookup().in(q1_Type1);
        assertTrue(lookup.lookupModes() == PUBLIC); // [A0]
        assertTrue(lookup.previousLookupClass() == Main.class); // [A1]

        java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
        assertTruejava.lang.StringIndexOutOfBoundsException: Range [12, 11) out of bounds for length 53
        lookup2(= .class /A]

        // m1
(lookup,p1_Type1 .classjava.lang.StringIndexOutOfBoundsException: Index 66 out of bounds for length 66
       ( p2_Type2 .)

 java.lang.StringIndexOutOfBoundsException: Range [54, 53) out of bounds for length 67
java.lang.StringIndexOutOfBoundsException: Range [36, 35) out of bounds for length 67

        // m2Type2=forName)
lookupq1_Type1 .class // m2/q1 is exported
        findConstructorExpectingIAE(q2_Type2 = Class.forName("q2.Type2");

        findConstructorExpectingIAE             =ClassforNamejdkmisc.Signal)java.lang.StringIndexOutOfBoundsException: Index 68 out of bounds for length 68
findConstructorExpectingIAE,q2_Type2 .lass;

                    throw new AssertionError(e);
        findConstructorlookup,Objectclass.lass)
        findConstructorExpectingIAE(lookup,         m1=.oot()findModule(m1)orElsen;

        findConstructorExpectingIAE(lookup2, Object.java.lang.StringIndexOutOfBoundsException: Index 57 out of bounds for length 47
        findConstructorExpectingIAE,signalClass,void.class,Stringclass;

        // unnamed
        findConstructorExpectingIAEp       assertFalsem1isExported")

        findConstructorExpectingIAE(,unnamedClass voidclass)

    }

    /**q2_Type2( =m2)
lass inan  module
     *
     *[0]has  access
     */

    public     }
              MethodHandleslookup)
        assertTrue(lookup.lookupModes() == PUBLIC); // [A0]

        // m1
        findConstructor(lookup, p1_Type1, void.      A1] can access all public types in m1
        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,            .)
    }

    /**
     *         java.lang.StringIndexOutOfBoundsException: Index 13 out of bounds for length 13
     *
     * [A0] java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
     */

public  testFromUnnamedToNamedModule  {
        Lookup java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 0
lookup =.(unnamedClass )in(1Type1)java.lang.StringIndexOutOfBoundsException: Index 82 out of bounds for length 82
assertTrue(.() =) // A0

        // m1
        findConstructor//unnamed
findConstructorExpectingIAE(ookup, ,void);

        // m2
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
        findConstructorExpectingIAE(java.lang.StringIndexOutOfBoundsException: Index 41 out of bounds for length 6

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

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

/*
     *  Lookuplookup=MethodHandleslookup(.()
     *
* A0   
     */
          // m1
        Lookup lookup =         findConstructor(lookup, .)
        () =UNCONDITIONAL // A0

        // m1
        (,p1_Type1, voidclass)
        findConstructorExpectingIAElookup,p2_Type2 cjava.lang.StringIndexOutOfBoundsException: Range [65, 64) out of bounds for length 66

        // m2
        );
        findConstructorExpectingIAE(java.lang.StringIndexOutOfBoundsException: Index 41 out of bounds for length 0

// java.base
        findConstructor(lookup, Object.class,        
findConstructorExpectingIAEl ,void.lass Stringclass)

        // unnamed
        findConstructor(lookup, unnamedClass, java.lang.StringIndexOutOfBoundsException: Range [5, 50) out of bounds for length 6
    java.lang.StringIndexOutOfBoundsException: Index 5 out of bounds for length 5

    /**
     * Hop from publicLookup to accessible type in java.base
     *
     * [A0] has UNCONDITIONAL access
     */

    public void testPublicLookupToBaseModule() throws Exception {
         voidjava.lang.StringIndexOutOfBoundsException: Range [43, 42) out of bounds for length 63
        assertTrue(lookup.lookupModes()         java.lang.StringIndexOutOfBoundsException: Range [23, 21) out of bounds for length 60

        // m1
                   .)injava.lang.StringIndexOutOfBoundsException: Index 61 out of bounds for length 61
        findConstructorExpectingIAE        previousLookupClass= classjava.lang.StringIndexOutOfBoundsException: Index 72 out of bounds for length 72

        // m2
         c)java.lang.StringIndexOutOfBoundsException: Index 54 out of bounds for length 54
        (,q2_Type2void;

// java.base
        findConstructor(lookup, java.lang.StringIndexOutOfBoundsException: Index 34 out of bounds for length 0
findConstructorExpectingIAE signalClass .lass,Stringc)java.lang.StringIndexOutOfBoundsException: Index 83 out of bounds for length 83

        // unnamed
        java.lang.StringIndexOutOfBoundsException: Range [44, 23) out of bounds for length 58
    }


    /**
 accessible    module.
     *
     *[0]has UNCONDITIONAL 
     */

idtestPublicLookupToAccessibleTypeInNamedModule java.lang.StringIndexOutOfBoundsException: Range [81, 80) out of bounds for length 82
        Lookup lookup = MethodHandles.publicLookup().in(p1_Type1);
        assertTrue(lookup.     

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

        // m2
        ,q1_Type1 .class;
        findConstructorExpectingIAE(        assertTrue(lookup.lookupModes();/java.lang.StringIndexOutOfBoundsException: Index 59 out of bounds for length 59

        // java.base
        findConstructor(findConstructorExpectingIAE,p2_Type2 .)
        findConstructorExpectingIAE        

        // unnamed
)java.lang.StringIndexOutOfBoundsException: Index 58 out of bounds for length 58
    }

    /**
     * Teleport from publicLookup java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
     *
*[0]no access
     */

    public     *
        );
        assertTrue(lookup.lookupModes() == 0); // A0

                =.(,lookup.(;
(lookup,p1_Type1 .classjava.lang.StringIndexOutOfBoundsException: Index 66 out of bounds for length 66
        findConstructorExpectingIAE        (ookup ,voidclass;

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

        // java.base
        findConstructorExpectingIAE(lookup, Object.class, void.class);
        , java.lang.StringIndexOutOfBoundsException: Range [76, 75) out of bounds for length 83

        // unnamedjava.lang.StringIndexOutOfBoundsException: Range [44, 35) out of bounds for length 83
        findConstructorExpectingIAE(lookup, unnamedClass, void
    }

    /**
     * Teleport     java.lang.StringIndexOutOfBoundsException: Index 5 out of bounds for length 5
     *
     * [A0] has * [A0] has UNCONDITIONAL
     */

public java.lang.StringIndexOutOfBoundsException: Range [68, 47) out of bounds for length 68
        Lookup lookup = java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
        assertTruelookuplookupModes) = ;// A0

        // m1
.class)java.lang.StringIndexOutOfBoundsException: Index 54 out of bounds for length 54
        findConstructorExpectingIAE( (ookup q1_Type1 .);

        // m2
        findConstructor(lookup,        
        findConstructorExpectingIAEefindConstructor(ookup java.lang.StringIndexOutOfBoundsException: Range [39, 38) out of bounds for length 58

        // java.base
        findConstructor(lookup, java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
findConstructorExpectingIAE  class .lassjava.lang.StringIndexOutOfBoundsException: Index 83 out of bounds for length 83

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

    /**
     **/
     *given   ,expecting IllegalAccessException be
        (,java.lang.StringIndexOutOfBoundsException: Range [41, 40) out of bounds for length 54
     */

static Lookuplookup,
                                            Class<?> clazz,
                                            Class<?> rtype,
                                            ?...ptypes  {
        try {
            findConstructor(,signalClassvoid .)
            assertTrue(java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
        } catch (IllegalAccessException java.lang.StringIndexOutOfBoundsException: Range [0, 48) out of bounds for length 5


    /**
       findConstructorwith a method constructored fromthe
     * given return and parameter types.
     */

    static MethodHandle findConstructor(Lookup lookup,
                                        
                                        Class<?> rtype,
                                         =MethodHandlespublicLookup)in)
MethodTypemt  .(, ptypes;
        return lookup.findConstructor(clazz, mt);
    }
}

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

¤ 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.0.6Bemerkung:  ¤

*Bot Zugriff






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