Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Android/art/art/test/142-classloader2/src/   (Android Betriebssystem Version 17©)  Datei vom 26.5.2026 mit Größe 3 kB image not shown  

Quelle  Main.java

  Sprache: JAVA
 

/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import java.lang.reflect.Constructor;
import java.lang.reflect.Field;

/**
 * PathClassLoader test.
 */

public class Main {

    private static ClassLoader createClassLoader(String dexPath, ClassLoader parent) {
        try {
            Class<?> myClassLoaderClass = Class.forName("MyPathClassLoader");
            Constructor<?> constructor = myClassLoaderClass.getConstructor(String.class,
                                                                           ClassLoader.class);
            return (ClassLoader)constructor.newInstance(dexPath, parent);
        } catch (Exception e) {
            // Ups, not available?!?!
            throw new RuntimeException(e);
        }
    }

    /**
     * Main entry point.
     */

    public static void main(String[] args) throws Exception {
        // Check the class-path for the second file. We'll use that one as the source of the
        // new classloader.
        String cp = System.getProperty("java.class.path");
        if (cp.split(System.getProperty("path.separator")).length != 1) {
            throw new IllegalStateException("Didn't find exactly one classpath element in " + cp);
        }
        if (!cp.endsWith("classloader2.jar")) {
            throw new IllegalStateException("Don't understand classpath " + cp);
        }
        cp = cp.replace("classloader2.jar""classloader2-ex.jar");

        ClassLoader myClassLoader = createClassLoader(
                cp, ClassLoader.getSystemClassLoader().getParent());

        // Now load our test class.
        Class<?> srcClass = A.class;
        Class<?> exClass = myClassLoader.loadClass("A");

        // First check: classes should be different.
        if (srcClass == exClass) {
            throw new IllegalStateException("Loaded class instances are the same");
        }

        // Secondary checks: get the static field values and make sure they aren't the same.
        String srcValue = (String)srcClass.getDeclaredField("value").get(null);
        if (!"Src-A".equals(srcValue)) {
            throw new IllegalStateException("Expected Src-A, found " + srcValue);
        }
        String exValue = (String)exClass.getDeclaredField("value").get(null);
        if (!"Ex-A".equals(exValue)) {
            throw new IllegalStateException("Expected Ex-A, found " + exValue);
        }

        // Try to load a dex file with bad dex code. Use new instance to force verification.
        VerifyError existing = null;
        try {
          Class<?> badClass = Main.class.getClassLoader().loadClass("B");
          System.out.println("Loaded class B.");
          badClass.newInstance();
          System.out.println("Should not be able to instantiate B with bad dex bytecode.");
        } catch (VerifyError e) {
          System.out.println("Caught VerifyError.");
          existing = e;
        }

        // Make sure the same error is rethrown when reloading the bad class.
        try {
          Class<?> badClass = Main.class.getClassLoader().loadClass("B");
          System.out.println("Loaded class B.");
          badClass.newInstance();
          System.out.println("Should not be able to instantiate B with bad dex bytecode.");
        } catch (VerifyError e) {
          if (e == existing) {
            System.out.println("Caught existing VerifyError.");
          } else {
            e.printStackTrace(System.out);
          }
        }

        System.out.println("Everything OK.");
    }
}

Messung V0.5 in Prozent
C=91 H=94 G=92

¤ Dauer der Verarbeitung: 0.16 Sekunden  (vorverarbeitet am  2026-06-29) ¤

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