コード例 #1
0
  @Test
  public void can_load_a_class_in_memory_from_bytes() throws Exception {
    // given
    ClassLoader cl =
        ClassLoaders.inMemoryClassLoader()
            .withClassDefinition("yop.Dude", SimpleClassGenerator.makeMarkerInterface("yop.Dude"))
            .build();

    // when
    Class<?> aClass = cl.loadClass("yop.Dude");

    // then
    assertThat(aClass).isNotNull();
    assertThat(aClass.getClassLoader()).isEqualTo(cl);
    assertThat(aClass.getName()).isEqualTo("yop.Dude");
  }
コード例 #2
0
  @Test
  public void isolated_class_loader_can_load_all_classes_unless_all_classes_mathch_the_prefixes()
      throws Exception {
    // given
    ClassLoader cl =
        isolatedClassLoader()
            .withCurrentCodeSourceUrls()
            .withPrivateCopyOf(CLASS_NAME_USING_INTERFACE)
            .withPrivateCopyOf(INTERFACE_NAME)
            .build();

    // when
    Class<?> aClass = cl.loadClass(CLASS_NAME_USING_INTERFACE);

    // then
    assertThat(aClass).isNotNull();
    assertThat(aClass.getClassLoader()).isEqualTo(cl);
    assertThat(aClass.getInterfaces()[0].getClassLoader()).isEqualTo(cl);
  }