Java Code Examples for jdk.jfr.consumer.RecordedClass#getClassLoader()

The following examples show how to use jdk.jfr.consumer.RecordedClass#getClassLoader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PrettyWriter.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private void printClass(RecordedClass clazz, String postFix) {
    RecordedClassLoader classLoader = clazz.getClassLoader();
    String classLoaderName = "null";
    if (classLoader != null) {
        if (classLoader.getName() != null) {
            classLoaderName = classLoader.getName();
        } else {
            classLoaderName = classLoader.getType().getName();
        }
    }
    String className = clazz.getName();
    if (className.startsWith("[")) {
        className = decodeDescriptors(className, "").get(0);
    }
    println(className + " (classLoader = " + classLoaderName + ")" + postFix);
}
 
Example 2
Source File: PrettyWriter.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private void printClass(RecordedClass clazz, String postFix) {
    RecordedClassLoader classLoader = clazz.getClassLoader();
    String classLoaderName = "null";
    if (classLoader != null) {
        if (classLoader.getName() != null) {
            classLoaderName = classLoader.getName();
        } else {
            classLoaderName = classLoader.getType().getName();
        }
    }
    String className = clazz.getName();
    if (className.startsWith("[")) {
        className = decodeDescriptors(className, "").get(0);
    }
    println(className + " (classLoader = " + classLoaderName + ")" + postFix);
}
 
Example 3
Source File: TestClassUnloadEvent.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Throwable {
    Recording recording = new Recording();
    recording.enable(EVENT_PATH).withThreshold(Duration.ofMillis(0));
    unloadableClassLoader = new TestClassLoader();
    recording.start();
    unloadableClassLoader.loadClass(TEST_CLASS_NAME);
    unloadableClassLoader = null;
    System.gc();
    recording.stop();

    List<RecordedEvent> events = Events.fromRecording(recording);
    Events.hasEvents(events);
    boolean isAnyFound = false;
    for (RecordedEvent event : events) {
        System.out.println("Event:" + event);
        RecordedClass unloadedClass = event.getValue("unloadedClass");
        if (TEST_CLASS_NAME.equals(unloadedClass.getName())) {
            RecordedClassLoader definingClassLoader = unloadedClass.getClassLoader();
            Asserts.assertEquals(TestClassLoader.class.getName(), definingClassLoader.getType().getName(),
                "Expected " + TestClassLoader.class.getName() + ", got " + definingClassLoader.getType().getName());
            //Asserts.assertEquals(TestClassLoader.CLASS_LOADER_NAME, definingClassLoader.getName(),
              //  "Expected " + TestClassLoader.CLASS_LOADER_NAME + ", got " + definingClassLoader.getName());
            Asserts.assertFalse(isAnyFound, "Found more than 1 event");
            isAnyFound = true;
        }
    }
    Asserts.assertTrue(isAnyFound, "No events found");
}
 
Example 4
Source File: TestMetadataRetention.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static void validateClassUnloadEvent(List<RecordedEvent> events) throws Throwable {
    for (RecordedEvent event : events) {
        if (event.getEventType().getName().equals(EventNames.ClassUnload)) {
            RecordedClass unloadedClass = event.getValue("unloadedClass");
            if (TEST_CLASS_NAME.equals(unloadedClass.getName())) {
                RecordedClassLoader definingClassLoader = unloadedClass.getClassLoader();
                Asserts.assertEquals(TEST_CLASS_LOADER_NAME, definingClassLoader.getName(), "Expected " + TEST_CLASS_LOADER_NAME + ", got " + definingClassLoader.getType().getName());
                return;
            }
        }
    }
}
 
Example 5
Source File: TestClassDefineEvent.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Throwable {
    Recording recording = new Recording();
    recording.enable(EVENT_NAME);
    TestClassLoader cl = new TestClassLoader();
    recording.start();
    cl.loadClass(TEST_CLASS_NAME);
    recording.stop();

    List<RecordedEvent> events = Events.fromRecording(recording);
    boolean foundTestClasses = false;
    for (RecordedEvent event : events) {
        System.out.println(event);
        RecordedClass definedClass = event.getValue("definedClass");
        if (TEST_CLASS_NAME.equals(definedClass.getName())) {
            RecordedClassLoader definingClassLoader = definedClass.getClassLoader();
            Asserts.assertNotNull(definingClassLoader, "Defining Class Loader should not be null");
            RecordedClass definingClassLoaderType = definingClassLoader.getType();
            Asserts.assertNotNull(definingClassLoaderType, "The defining Class Loader type should not be null");
            Asserts.assertEquals(cl.getClass().getName(), definingClassLoaderType.getName(),
                "Expected type " + cl.getClass().getName() + ", got type " + definingClassLoaderType.getName());
            //Asserts.assertEquals(cl.getName(), definingClassLoader.getName(),
              //  "Defining Class Loader should have the same name as the original class loader");
            foundTestClasses = true;
        }
    }
    Asserts.assertTrue(foundTestClasses, "No class define event found for " + TEST_CLASS_NAME);
}
 
Example 6
Source File: TestClassUnloadEvent.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Throwable {
    Recording recording = new Recording();
    recording.enable(EVENT_PATH).withThreshold(Duration.ofMillis(0));
    unloadableClassLoader = new TestClassLoader();
    recording.start();
    unloadableClassLoader.loadClass(TEST_CLASS_NAME);
    unloadableClassLoader = null;
    System.gc();
    recording.stop();

    List<RecordedEvent> events = Events.fromRecording(recording);
    Events.hasEvents(events);
    boolean isAnyFound = false;
    for (RecordedEvent event : events) {
        System.out.println("Event:" + event);
        RecordedClass unloadedClass = event.getValue("unloadedClass");
        if (TEST_CLASS_NAME.equals(unloadedClass.getName())) {
            RecordedClassLoader definingClassLoader = unloadedClass.getClassLoader();
            Asserts.assertEquals(TestClassLoader.class.getName(), definingClassLoader.getType().getName(),
                "Expected " + TestClassLoader.class.getName() + ", got " + definingClassLoader.getType().getName());
            //Asserts.assertEquals(TestClassLoader.CLASS_LOADER_NAME, definingClassLoader.getName(),
              //  "Expected " + TestClassLoader.CLASS_LOADER_NAME + ", got " + definingClassLoader.getName());
            Asserts.assertFalse(isAnyFound, "Found more than 1 event");
            isAnyFound = true;
        }
    }
    Asserts.assertTrue(isAnyFound, "No events found");
}
 
Example 7
Source File: TestClassLoadEvent.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Throwable {
    Recording recording = new Recording();
    recording.enable(EVENT_NAME).withThreshold(Duration.ofMillis(0));
    TestClassLoader cl = new TestClassLoader();
    recording.start();
    cl.loadClass(TEST_CLASS_NAME);
    recording.stop();

    List<RecordedEvent> events = Events.fromRecording(recording);
    boolean isLoaded = false;
    for (RecordedEvent event : events) {
        RecordedClass loadedClass = event.getValue("loadedClass");
        if (SEARCH_CLASS_NAME.equals(loadedClass.getName())) {
            System.out.println(event);
            //neither package nor module events are available at 8
            //Events.assertClassPackage(loadedClass, SEARCH_PACKAGE_NAME);
            //Events.assertClassModule(loadedClass, SEARCH_MODULE_NAME);
            RecordedClassLoader initiatingClassLoader = event.getValue("initiatingClassLoader");
            Asserts.assertEquals(cl.getClass().getName(), initiatingClassLoader.getType().getName(),
                "Expected type " + cl.getClass().getName() + ", got type " + initiatingClassLoader.getType().getName());
            RecordedClassLoader definingClassLoader = loadedClass.getClassLoader();
            Asserts.assertEquals(BOOT_CLASS_LOADER_NAME, definingClassLoader.getName(),
                "Expected boot loader to be the defining class loader");
            Asserts.assertNull(definingClassLoader.getType(), "boot loader should not have a type");
            isLoaded = true;
        }
    }
    Asserts.assertTrue(isLoaded, "No class load event found for class " + SEARCH_CLASS_NAME);
}
 
Example 8
Source File: TestMetadataRetention.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void validateClassUnloadEvent(List<RecordedEvent> events) throws Throwable {
    for (RecordedEvent event : events) {
        if (event.getEventType().getName().equals(EventNames.ClassUnload)) {
            RecordedClass unloadedClass = event.getValue("unloadedClass");
            if (TEST_CLASS_NAME.equals(unloadedClass.getName())) {
                RecordedClassLoader definingClassLoader = unloadedClass.getClassLoader();
                Asserts.assertEquals(TEST_CLASS_LOADER_NAME, definingClassLoader.getName(), "Expected " + TEST_CLASS_LOADER_NAME + ", got " + definingClassLoader.getType().getName());
                return;
            }
        }
    }
}
 
Example 9
Source File: TestClassDefineEvent.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Throwable {
    Recording recording = new Recording();
    recording.enable(EVENT_NAME);
    TestClassLoader cl = new TestClassLoader();
    recording.start();
    cl.loadClass(TEST_CLASS_NAME);
    recording.stop();

    List<RecordedEvent> events = Events.fromRecording(recording);
    boolean foundTestClasses = false;
    for (RecordedEvent event : events) {
        System.out.println(event);
        RecordedClass definedClass = event.getValue("definedClass");
        if (TEST_CLASS_NAME.equals(definedClass.getName())) {
            RecordedClassLoader definingClassLoader = definedClass.getClassLoader();
            Asserts.assertNotNull(definingClassLoader, "Defining Class Loader should not be null");
            RecordedClass definingClassLoaderType = definingClassLoader.getType();
            Asserts.assertNotNull(definingClassLoaderType, "The defining Class Loader type should not be null");
            Asserts.assertEquals(cl.getClass().getName(), definingClassLoaderType.getName(),
                "Expected type " + cl.getClass().getName() + ", got type " + definingClassLoaderType.getName());
            //Asserts.assertEquals(cl.getName(), definingClassLoader.getName(),
              //  "Defining Class Loader should have the same name as the original class loader");
            foundTestClasses = true;
        }
    }
    Asserts.assertTrue(foundTestClasses, "No class define event found for " + TEST_CLASS_NAME);
}
 
Example 10
Source File: TestClassLoadEvent.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Throwable {
    Recording recording = new Recording();
    recording.enable(EVENT_NAME).withThreshold(Duration.ofMillis(0));
    TestClassLoader cl = new TestClassLoader();
    recording.start();
    cl.loadClass(TEST_CLASS_NAME);
    recording.stop();

    List<RecordedEvent> events = Events.fromRecording(recording);
    boolean isLoaded = false;
    for (RecordedEvent event : events) {
        RecordedClass loadedClass = event.getValue("loadedClass");
        if (SEARCH_CLASS_NAME.equals(loadedClass.getName())) {
            System.out.println(event);
            //neither package nor module events are available at 8
            //Events.assertClassPackage(loadedClass, SEARCH_PACKAGE_NAME);
            //Events.assertClassModule(loadedClass, SEARCH_MODULE_NAME);
            RecordedClassLoader initiatingClassLoader = event.getValue("initiatingClassLoader");
            Asserts.assertEquals(cl.getClass().getName(), initiatingClassLoader.getType().getName(),
                "Expected type " + cl.getClass().getName() + ", got type " + initiatingClassLoader.getType().getName());
            RecordedClassLoader definingClassLoader = loadedClass.getClassLoader();
            Asserts.assertEquals(BOOT_CLASS_LOADER_NAME, definingClassLoader.getName(),
                "Expected boot loader to be the defining class loader");
            Asserts.assertNull(definingClassLoader.getType(), "boot loader should not have a type");
            isLoaded = true;
        }
    }
    Asserts.assertTrue(isLoaded, "No class load event found for class " + SEARCH_CLASS_NAME);
}
 
Example 11
Source File: TestMetadataRetention.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void validateClassUnloadEvent(List<RecordedEvent> events) throws Throwable {
    for (RecordedEvent event : events) {
        if (event.getEventType().getName().equals(EventNames.ClassUnload)) {
            RecordedClass unloadedClass = event.getValue("unloadedClass");
            if (TEST_CLASS_NAME.equals(unloadedClass.getName())) {
                RecordedClassLoader definingClassLoader = unloadedClass.getClassLoader();
                //Asserts.assertEquals(TEST_CLASS_LOADER_NAME, definingClassLoader.getName(), "Expected " + TEST_CLASS_LOADER_NAME + ", got " + definingClassLoader.getType().getName());
                return;
            }
        }
    }
}
 
Example 12
Source File: TestClassDefineEvent.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Throwable {
    Recording recording = new Recording();
    recording.enable(EVENT_NAME);
    TestClassLoader cl = new TestClassLoader();
    recording.start();
    cl.loadClass(TEST_CLASS_NAME);
    recording.stop();

    List<RecordedEvent> events = Events.fromRecording(recording);
    boolean foundTestClasses = false;
    for (RecordedEvent event : events) {
        System.out.println(event);
        RecordedClass definedClass = event.getValue("definedClass");
        if (TEST_CLASS_NAME.equals(definedClass.getName())) {
            RecordedClassLoader definingClassLoader = definedClass.getClassLoader();
            Asserts.assertNotNull(definingClassLoader, "Defining Class Loader should not be null");
            RecordedClass definingClassLoaderType = definingClassLoader.getType();
            Asserts.assertNotNull(definingClassLoaderType, "The defining Class Loader type should not be null");
            Asserts.assertEquals(cl.getClass().getName(), definingClassLoaderType.getName(),
                "Expected type " + cl.getClass().getName() + ", got type " + definingClassLoaderType.getName());
            /*Asserts.assertEquals(cl.getName(), definingClassLoader.getName(),
                "Defining Class Loader should have the same name as the original class loader");*/
            foundTestClasses = true;
        }
    }
    Asserts.assertTrue(foundTestClasses, "No class define event found for " + TEST_CLASS_NAME);
}
 
Example 13
Source File: TestClassUnloadEvent.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Throwable {
    Recording recording = new Recording();
    recording.enable(EVENT_PATH).withThreshold(Duration.ofMillis(0));
    unloadableClassLoader = new TestClassLoader();
    recording.start();
    unloadableClassLoader.loadClass(TEST_CLASS_NAME);
    unloadableClassLoader = null;
    System.gc();
    recording.stop();

    List<RecordedEvent> events = Events.fromRecording(recording);
    Events.hasEvents(events);
    boolean isAnyFound = false;
    for (RecordedEvent event : events) {
        System.out.println("Event:" + event);
        RecordedClass unloadedClass = event.getValue("unloadedClass");
        if (TEST_CLASS_NAME.equals(unloadedClass.getName())) {
            RecordedClassLoader definingClassLoader = unloadedClass.getClassLoader();
            Asserts.assertEquals(TestClassLoader.class.getName(), definingClassLoader.getType().getName(),
                "Expected " + TestClassLoader.class.getName() + ", got " + definingClassLoader.getType().getName());
            /** unsupport class loader name in ajdk8 now
            Asserts.assertEquals(TestClassLoader.CLASS_LOADER_NAME, definingClassLoader.getName(),
                "Expected " + TestClassLoader.CLASS_LOADER_NAME + ", got " + definingClassLoader.getName());
            */
            Asserts.assertFalse(isAnyFound, "Found more than 1 event");
            isAnyFound = true;
        }
    }
    Asserts.assertTrue(isAnyFound, "No events found");
}
 
Example 14
Source File: TestClassLoadEvent.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Throwable {
    Recording recording = new Recording();
    recording.enable(EVENT_NAME).withThreshold(Duration.ofMillis(0));
    TestClassLoader cl = new TestClassLoader();
    recording.start();
    cl.loadClass(TEST_CLASS_NAME);
    recording.stop();

    List<RecordedEvent> events = Events.fromRecording(recording);
    boolean isLoaded = false;
    for (RecordedEvent event : events) {
        RecordedClass loadedClass = event.getValue("loadedClass");
        if (SEARCH_CLASS_NAME.equals(loadedClass.getName())) {
            System.out.println(event);
            /** unsupported now
            Events.assertClassPackage(loadedClass, SEARCH_PACKAGE_NAME);
            Events.assertClassModule(loadedClass, SEARCH_MODULE_NAME);
            */
            RecordedClassLoader initiatingClassLoader = event.getValue("initiatingClassLoader");
            Asserts.assertEquals(cl.getClass().getName(), initiatingClassLoader.getType().getName(),
                "Expected type " + cl.getClass().getName() + ", got type " + initiatingClassLoader.getType().getName());
            RecordedClassLoader definingClassLoader = loadedClass.getClassLoader();
            Asserts.assertEquals(BOOT_CLASS_LOADER_NAME, definingClassLoader.getName(),
                "Expected boot loader to be the defining class loader");
            Asserts.assertNull(definingClassLoader.getType(), "boot loader should not have a type");
            isLoaded = true;
        }
    }
    Asserts.assertTrue(isLoaded, "No class load event found for class " + SEARCH_CLASS_NAME);
}
 
Example 15
Source File: TestRecordedClassLoader.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    Recording recording = new Recording();
    recording.enable(EVENT_NAME).withoutStackTrace();
    TestClassLoader cl = new TestClassLoader();
    recording.start();
    cl.loadClass(TEST_CLASS_NAME);
    recording.stop();

    List<RecordedEvent> events = Events.fromRecording(recording);
    boolean isDefined = false;
    System.out.println(events);
    for (RecordedEvent event : events) {
        RecordedClass definedClass = event.getValue("definedClass");
        if (TEST_CLASS_NAME.equals(definedClass.getName())) {
            System.out.println(event);

            // get the RecordedClassLoader from the RecordedClass, the "definedClass"
            RecordedClassLoader definingClassLoader = definedClass.getClassLoader();
            Asserts.assertNotNull(definingClassLoader, "Defining Class Loader should not be null");

            // invoke RecordedClassLoader.getType() in order to validate the type of the RecordedClassLoader
            RecordedClass definingClassLoaderType = definingClassLoader.getType();
            Asserts.assertNotNull(definingClassLoaderType, "The defining Class Loader type should not be null");

            // verify matching types
            Asserts.assertEquals(cl.getClass().getName(), definingClassLoaderType.getName(),
                "Expected type " + cl.getClass().getName() + ", got type " + definingClassLoaderType.getName());

            // get a RecordedClassLoader directly from the "definingClassLoader" field as well
            RecordedClassLoader definingClassLoaderFromField = event.getValue("definingClassLoader");
            Asserts.assertNotNull(definingClassLoaderFromField,
                "Defining Class Loader instantatiated from field should not be null");

            // ensure that the class loader instance used in the test actually has a name
            /** unsupport now
            Asserts.assertNotNull(cl.getName(),
                "Expected a valid name for the TestClassLoader");

            // invoke RecordedClassLoader.getName() to get the name of the class loader instance
            Asserts.assertEquals(cl.getName(), definingClassLoader.getName(),
                "Defining Class Loader should have the same name as the original class loader");
            Asserts.assertEquals(definingClassLoaderFromField.getName(), definingClassLoader.getName(),
                "Defining Class Loader representations should have the same class loader name");
            */

            // invoke uniqueID()
            Asserts.assertGreaterThan(definingClassLoader.getId(), 0L, "Invalid id assignment");

            // second order class loader information ("check class loader of the class loader")
            RecordedClassLoader classLoaderOfDefClassLoader = definingClassLoaderType.getClassLoader();
            Asserts.assertNotNull(classLoaderOfDefClassLoader,
                "The class loader for the definining class loader should not be null");
            /** unsupport now
            Asserts.assertEquals(cl.getClass().getClassLoader().getName(), classLoaderOfDefClassLoader.getName(),
                "Expected class loader name " + cl.getClass().getClassLoader().getName() + ", got name " + classLoaderOfDefClassLoader.getName());
            */

            RecordedClass classLoaderOfDefClassLoaderType = classLoaderOfDefClassLoader.getType();
            Asserts.assertNotNull(classLoaderOfDefClassLoaderType,
                "The class loader type for the defining class loader should not be null");
            Asserts.assertEquals(cl.getClass().getClassLoader().getClass().getName(), classLoaderOfDefClassLoaderType.getName(),
                "Expected type " + cl.getClass().getClassLoader().getClass().getName() + ", got type " + classLoaderOfDefClassLoaderType.getName());

            Asserts.assertGreaterThan(definingClassLoader.getId(), classLoaderOfDefClassLoader.getId(),
                "expected id assignment invariant broken for Class Loaders");

            isDefined = true;
        }
    }
    Asserts.assertTrue(isDefined, "No class define event found to verify RecordedClassLoader");
}
 
Example 16
Source File: TestRecordedClassLoader.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    Recording recording = new Recording();
    recording.enable(EVENT_NAME).withoutStackTrace();
    TestClassLoader cl = new TestClassLoader();
    recording.start();
    cl.loadClass(TEST_CLASS_NAME);
    recording.stop();

    List<RecordedEvent> events = Events.fromRecording(recording);
    boolean isDefined = false;
    for (RecordedEvent event : events) {
        RecordedClass definedClass = event.getValue("definedClass");
        if (TEST_CLASS_NAME.equals(definedClass.getName())) {
            System.out.println(event);

            // get the RecordedClassLoader from the RecordedClass, the "definedClass"
            RecordedClassLoader definingClassLoader = definedClass.getClassLoader();
            Asserts.assertNotNull(definingClassLoader, "Defining Class Loader should not be null");

            // invoke RecordedClassLoader.getType() in order to validate the type of the RecordedClassLoader
            RecordedClass definingClassLoaderType = definingClassLoader.getType();
            Asserts.assertNotNull(definingClassLoaderType, "The defining Class Loader type should not be null");

            // verify matching types
            Asserts.assertEquals(cl.getClass().getName(), definingClassLoaderType.getName(),
                "Expected type " + cl.getClass().getName() + ", got type " + definingClassLoaderType.getName());

            // get a RecordedClassLoader directly from the "definingClassLoader" field as well
            RecordedClassLoader definingClassLoaderFromField = event.getValue("definingClassLoader");
            Asserts.assertNotNull(definingClassLoaderFromField,
                "Defining Class Loader instantatiated from field should not be null");

            // ensure that the class loader instance used in the test actually has a name
            //Asserts.assertNotNull(cl.getName(),
              //  "Expected a valid name for the TestClassLoader");

            // invoke RecordedClassLoader.getName() to get the name of the class loader instance
            //Asserts.assertEquals(cl.getName(), definingClassLoader.getName(),
              //  "Defining Class Loader should have the same name as the original class loader");
            Asserts.assertEquals(definingClassLoaderFromField.getName(), definingClassLoader.getName(),
                "Defining Class Loader representations should have the same class loader name");

            // invoke uniqueID()
            Asserts.assertGreaterThan(definingClassLoader.getId(), 0L, "Invalid id assignment");

            // second order class loader information ("check class loader of the class loader")
            RecordedClassLoader classLoaderOfDefClassLoader = definingClassLoaderType.getClassLoader();
            Asserts.assertNotNull(classLoaderOfDefClassLoader,
                "The class loader for the definining class loader should not be null");
           // Asserts.assertEquals(cl.getClass().getClassLoader().getName(), classLoaderOfDefClassLoader.getName(),
             //   "Expected class loader name " + cl.getClass().getClassLoader().getName() + ", got name " + classLoaderOfDefClassLoader.getName());

            RecordedClass classLoaderOfDefClassLoaderType = classLoaderOfDefClassLoader.getType();
            Asserts.assertNotNull(classLoaderOfDefClassLoaderType,
                "The class loader type for the defining class loader should not be null");
            Asserts.assertEquals(cl.getClass().getClassLoader().getClass().getName(), classLoaderOfDefClassLoaderType.getName(),
                "Expected type " + cl.getClass().getClassLoader().getClass().getName() + ", got type " + classLoaderOfDefClassLoaderType.getName());

            Asserts.assertGreaterThan(definingClassLoader.getId(), classLoaderOfDefClassLoader.getId(),
                "expected id assignment invariant broken for Class Loaders");

            isDefined = true;
        }
    }
    Asserts.assertTrue(isDefined, "No class define event found to verify RecordedClassLoader");
}
 
Example 17
Source File: TestRecordedClassLoader.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    Recording recording = new Recording();
    recording.enable(EVENT_NAME).withoutStackTrace();
    TestClassLoader cl = new TestClassLoader();
    recording.start();
    cl.loadClass(TEST_CLASS_NAME);
    recording.stop();

    List<RecordedEvent> events = Events.fromRecording(recording);
    boolean isDefined = false;
    for (RecordedEvent event : events) {
        RecordedClass definedClass = event.getValue("definedClass");
        if (TEST_CLASS_NAME.equals(definedClass.getName())) {
            System.out.println(event);

            // get the RecordedClassLoader from the RecordedClass, the "definedClass"
            RecordedClassLoader definingClassLoader = definedClass.getClassLoader();
            Asserts.assertNotNull(definingClassLoader, "Defining Class Loader should not be null");

            // invoke RecordedClassLoader.getType() in order to validate the type of the RecordedClassLoader
            RecordedClass definingClassLoaderType = definingClassLoader.getType();
            Asserts.assertNotNull(definingClassLoaderType, "The defining Class Loader type should not be null");

            // verify matching types
            Asserts.assertEquals(cl.getClass().getName(), definingClassLoaderType.getName(),
                "Expected type " + cl.getClass().getName() + ", got type " + definingClassLoaderType.getName());

            // get a RecordedClassLoader directly from the "definingClassLoader" field as well
            RecordedClassLoader definingClassLoaderFromField = event.getValue("definingClassLoader");
            Asserts.assertNotNull(definingClassLoaderFromField,
                "Defining Class Loader instantatiated from field should not be null");

            // ensure that the class loader instance used in the test actually has a name
            //Asserts.assertNotNull(cl.getName(),
              //  "Expected a valid name for the TestClassLoader");

            // invoke RecordedClassLoader.getName() to get the name of the class loader instance
            //Asserts.assertEquals(cl.getName(), definingClassLoader.getName(),
              //  "Defining Class Loader should have the same name as the original class loader");
            Asserts.assertEquals(definingClassLoaderFromField.getName(), definingClassLoader.getName(),
                "Defining Class Loader representations should have the same class loader name");

            // invoke uniqueID()
            Asserts.assertGreaterThan(definingClassLoader.getId(), 0L, "Invalid id assignment");

            // second order class loader information ("check class loader of the class loader")
            RecordedClassLoader classLoaderOfDefClassLoader = definingClassLoaderType.getClassLoader();
            Asserts.assertNotNull(classLoaderOfDefClassLoader,
                "The class loader for the definining class loader should not be null");
           // Asserts.assertEquals(cl.getClass().getClassLoader().getName(), classLoaderOfDefClassLoader.getName(),
             //   "Expected class loader name " + cl.getClass().getClassLoader().getName() + ", got name " + classLoaderOfDefClassLoader.getName());

            RecordedClass classLoaderOfDefClassLoaderType = classLoaderOfDefClassLoader.getType();
            Asserts.assertNotNull(classLoaderOfDefClassLoaderType,
                "The class loader type for the defining class loader should not be null");
            Asserts.assertEquals(cl.getClass().getClassLoader().getClass().getName(), classLoaderOfDefClassLoaderType.getName(),
                "Expected type " + cl.getClass().getClassLoader().getClass().getName() + ", got type " + classLoaderOfDefClassLoaderType.getName());

            Asserts.assertGreaterThan(definingClassLoader.getId(), classLoaderOfDefClassLoader.getId(),
                "expected id assignment invariant broken for Class Loaders");

            isDefined = true;
        }
    }
    Asserts.assertTrue(isDefined, "No class define event found to verify RecordedClassLoader");
}