org.osgi.framework.Bundle.loadClass()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(13.2k)|赞(0)|评价(0)|浏览(169)

本文整理了Java中org.osgi.framework.Bundle.loadClass()方法的一些代码示例,展示了Bundle.loadClass()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Bundle.loadClass()方法的具体详情如下:
包路径:org.osgi.framework.Bundle
类名称:Bundle
方法名:loadClass

Bundle.loadClass介绍

[英]Loads the specified class using this bundle's class loader.

If this bundle is a fragment bundle then this method must throw a ClassNotFoundException.

If this bundle's state is INSTALLED, this method must attempt to resolve this bundle before attempting to load the class.

If this bundle cannot be resolved, a Framework event of type FrameworkEvent#ERROR is fired containing a BundleException with details of the reason this bundle could not be resolved. This method must then throw a ClassNotFoundException.

If this bundle's state is UNINSTALLED, then an IllegalStateException is thrown.
[中]使用此捆绑包的类加载器加载指定的类。
如果此捆绑包是片段捆绑包,则此方法必须抛出ClassNotFoundException。
如果此捆绑包的状态已安装,则此方法必须在尝试加载类之前尝试解析此捆绑包。
如果无法解析此捆绑包,将触发FrameworkEvent#ERROR类型的框架事件,其中包含BundleException,详细说明无法解析此捆绑包的原因。然后,此方法必须抛出ClassNotFoundException。
如果此捆绑包的状态已卸载,则会引发IllegalStateException。

代码示例

代码示例来源:origin: jersey/jersey

@Override
  public Class<?> run() throws ClassNotFoundException {
    return bundle.loadClass(className);
  }
});

代码示例来源:origin: jersey/jersey

@Override
  public Class<?> run() throws ClassNotFoundException {
    return bundle.loadClass(className);
  }
});

代码示例来源:origin: apache/ignite

/** {@inheritDoc} */
@Override protected Class<?> findClass(String name) throws ClassNotFoundException {
  return bundle.loadClass(name);
}

代码示例来源:origin: gocd/gocd

Class<?> loadClass(Bundle bundle, String classFilePath) {
  String className = classFilePath.replaceFirst("^/", "").replace('/', '.').replaceFirst(".class$", "");
  try {
    return bundle.loadClass(className);
  } catch (Throwable e) {
    errors.add(String.format("Class [%s] could not be loaded. Message: [%s].", className, e.getMessage()));
  }
  return null;
}

代码示例来源:origin: DozerMapper/dozer

@Override
public Class<?> loadClass(String className) {
  try {
    return context.getBundle().loadClass(className);
  } catch (ClassNotFoundException e) {
    throw new MappingException(e);
  }
}

代码示例来源:origin: gocd/gocd

@Test
public void shouldRegisterServiceWithBothPluginIDAndExtensionTypeAsProperties() throws Exception {
  setupClassesInBundle("PublicGoExtensionClassWhichWillLoadSuccessfullyAndProvideAValidIdentifier.class");
  when(bundle.loadClass("PublicGoExtensionClassWhichWillLoadSuccessfullyAndProvideAValidIdentifier")).thenReturn((Class) PublicGoExtensionClassWhichWillLoadSuccessfullyAndProvideAValidIdentifier.class);
  Hashtable<String, String> expectedPropertiesUponRegistration = new Hashtable<>();
  expectedPropertiesUponRegistration.put(Constants.BUNDLE_SYMBOLICNAME, PLUGIN_ID);
  expectedPropertiesUponRegistration.put(Constants.BUNDLE_CATEGORY, "test-extension");
  activator.start(context);
  assertThat(activator.hasErrors(), is(false));
  verify(context).registerService(eq(GoPlugin.class), any(GoPlugin.class), eq(expectedPropertiesUponRegistration));
}

代码示例来源:origin: gocd/gocd

@Test
public void shouldFailToRegisterServiceWhenExtensionTypeCannotBeSuccessfullyRetrieved() throws Exception {
  setupClassesInBundle("PublicGoExtensionClassWhichWillLoadSuccessfullyButThrowWhenAskedForPluginIdentifier.class");
  when(bundle.loadClass("PublicGoExtensionClassWhichWillLoadSuccessfullyButThrowWhenAskedForPluginIdentifier")).thenReturn((Class) PublicGoExtensionClassWhichWillLoadSuccessfullyButThrowWhenAskedForPluginIdentifier.class);
  activator.start(context);
  assertThat(activator.hasErrors(), is(true));
  verifyErrorReportedContains("Unable to find extension type from plugin identifier in class com.thoughtworks.go.plugin.activation.PublicGoExtensionClassWhichWillLoadSuccessfullyButThrowWhenAskedForPluginIdentifier");
  verify(context, times(0)).registerService(eq(GoPlugin.class), any(GoPlugin.class), any());
}

代码示例来源:origin: gocd/gocd

private void assertLoadUnloadInvocationCount(Class<?> testExtensionClass, int invocationCount) throws Exception {
  String simpleNameOfTestExtensionClass = testExtensionClass.getSimpleName();
  setupClassesInBundle(simpleNameOfTestExtensionClass + ".class");
  when(bundle.loadClass(contains(simpleNameOfTestExtensionClass))).thenReturn((Class) testExtensionClass);
  activator.start(context);
  assertThat(testExtensionClass.getField("loadInvoked").getInt(null), is(invocationCount));
  activator.stop(context);
  assertThat(testExtensionClass.getField("unLoadInvoked").getInt(null), is(invocationCount));
}

代码示例来源:origin: gocd/gocd

@Test
public void shouldReportMultipleClassLoadErrorsToThePluginHealthService() throws Exception {
  setupClassesInBundle("SomeClass.class", "SomeOtherClass.class");
  when(bundle.loadClass(anyString())).thenThrow(new ClassNotFoundException("Ouch! Failed"));
  activator.start(context);
  verifyErrorsReported("Class [SomeClass] could not be loaded. Message: [Ouch! Failed].", "Class [SomeOtherClass] could not be loaded. Message: [Ouch! Failed]."
      , NO_EXT_ERR_MSG);
}

代码示例来源:origin: gocd/gocd

@Test
public void shouldReportAClassLoadErrorToThePluginHealthService() throws Exception {
  setupClassesInBundle("SomeClass.class");
  when(bundle.loadClass(anyString())).thenThrow(new ClassNotFoundException("Ouch! Failed"));
  activator.start(context);
  verifyErrorsReported("Class [SomeClass] could not be loaded. Message: [Ouch! Failed].", NO_EXT_ERR_MSG);
}

代码示例来源:origin: gocd/gocd

@Test
public void loggerShouldBeAvailableToBeUsedInStaticBlocksAndConstructorAndLoadUnloadMethodsOfPluginExtensionClasses() throws Exception {
  setupClassesInBundle("PublicGoExtensionClassWhichLogsInAStaticBlock.class");
  when(bundle.loadClass(contains("PublicGoExtensionClassWhichLogsInAStaticBlock"))).thenReturn((Class) PublicGoExtensionClassWhichLogsInAStaticBlock.class);
  activator.start(context);
  activator.stop(context);
  verify(loggingService).info(PLUGIN_ID, PublicGoExtensionClassWhichLogsInAStaticBlock.class.getName(), "HELLO from static block in PublicGoExtensionClassWhichLogsInAStaticBlock");
  verify(loggingService).info(PLUGIN_ID, PublicGoExtensionClassWhichLogsInAStaticBlock.class.getName(), "HELLO from constructor in PublicGoExtensionClassWhichLogsInAStaticBlock");
  verify(loggingService).info(PLUGIN_ID, PublicGoExtensionClassWhichLogsInAStaticBlock.class.getName(), "HELLO from load in PublicGoExtensionClassWhichLogsInAStaticBlock");
  verify(loggingService).info(PLUGIN_ID, PublicGoExtensionClassWhichLogsInAStaticBlock.class.getName(), "HELLO from unload in PublicGoExtensionClassWhichLogsInAStaticBlock");
}

代码示例来源:origin: gocd/gocd

@Test
public void shouldReportAClassWhichIsAnnotatedAsAnExtensionIfItFailsDuringConstruction() throws Exception {
  setupClassesInBundle("PublicGoExtensionClassWhichThrowsAnExceptionInItsConstructor.class");
  when(bundle.loadClass(contains("PublicGoExtensionClassWhichThrowsAnExceptionInItsConstructor"))).thenReturn((Class) PublicGoExtensionClassWhichThrowsAnExceptionInItsConstructor.class);
  activator.start(context);
  verifyErrorsReported(
      format("Class [PublicGoExtensionClassWhichThrowsAnExceptionInItsConstructor] is annotated with @Extension but cannot be constructed. Reason: java.lang.RuntimeException: %s.",
          CONSTRUCTOR_FAIL_MSG), NO_EXT_ERR_MSG);
}

代码示例来源:origin: gocd/gocd

@Test
public void shouldNotReportAClassWhichIsNotAnnotatedAsAnExtensionEvenIfItIsNotPublic() throws Exception {
  setupClassesInBundle("NonPublicClassWhichIsNotAGoExtension.class");
  when(bundle.loadClass(contains("NonPublicClassWhichIsNotAGoExtension"))).thenReturn((Class) NonPublicClassWhichIsNotAGoExtension.class);
  activator.start(context);
  verifyErrorsReported(NO_EXT_ERR_MSG);
}

代码示例来源:origin: gocd/gocd

@Test
public void shouldReportAClassWhichIsAnnotatedAsAnExtensionIfItIsAbstract() throws Exception {
  setupClassesInBundle("PublicAbstractGoExtensionClass.class");
  when(bundle.loadClass(contains("PublicAbstractGoExtensionClass"))).thenReturn((Class) PublicAbstractGoExtensionClass.class);
  activator.start(context);
  verifyErrorsReported("Class [PublicAbstractGoExtensionClass] is annotated with @Extension but is abstract.", NO_EXT_ERR_MSG);
}

代码示例来源:origin: gocd/gocd

@Test
public void shouldReportAClassWhichIsAnnotatedAsAnExtensionIfItIsNotInstantiable() throws Exception {
  setupClassesInBundle("PublicGoExtensionClassWhichDoesNotHaveADefaultConstructor.class");
  when(bundle.loadClass(contains("PublicGoExtensionClassWhichDoesNotHaveADefaultConstructor"))).thenReturn((Class) PublicGoExtensionClassWhichDoesNotHaveADefaultConstructor.class);
  activator.start(context);
  verifyErrorsReported("Class [PublicGoExtensionClassWhichDoesNotHaveADefaultConstructor] is annotated with @Extension but cannot be constructed. "
      + "Make sure it and all of its parent classes have a default constructor.", NO_EXT_ERR_MSG);
}

代码示例来源:origin: gocd/gocd

@Test
public void shouldReportAClassWhichIsAnnotatedAsAnExtensionIfItIsNotPublic() throws Exception {
  setupClassesInBundle("NonPublicGoExtensionClass.class");
  when(bundle.loadClass(contains("NonPublicGoExtensionClass"))).thenReturn((Class) NonPublicGoExtensionClass.class);
  activator.start(context);
  verifyErrorsReported("Class [NonPublicGoExtensionClass] is annotated with @Extension but is not public.", NO_EXT_ERR_MSG);
}

代码示例来源:origin: gocd/gocd

@Test
public void shouldInvokeMethodWithLoadUnloadAnnotationAtPluginStart() throws Exception {
  setupClassesInBundle("GoExtensionWithLoadUnloadAnnotation.class");
  when(bundle.loadClass(contains("GoExtensionWithLoadUnloadAnnotation"))).thenReturn((Class) GoExtensionWithLoadUnloadAnnotation.class);
  activator.start(context);
  assertThat(GoExtensionWithLoadUnloadAnnotation.loadInvoked, is(1));
  activator.stop(context);
  assertThat(GoExtensionWithLoadUnloadAnnotation.unLoadInvoked, is(1));
}

代码示例来源:origin: gocd/gocd

@Test
public void shouldHandleExceptionGeneratedByLoadMethodAtPluginStart() throws Exception {
  setupClassesInBundle("GoExtensionWithLoadAnnotationMethodThrowingException.class");
  when(bundle.loadClass(contains("GoExtensionWithLoadAnnotationMethodThrowingException"))).thenReturn((Class) GoExtensionWithLoadAnnotationMethodThrowingException.class);
  activator.start(context);
  assertThat(activator.hasErrors(), is(true));
  verifyErrorsReported("Class [GoExtensionWithLoadAnnotationMethodThrowingException] is annotated with @Extension but cannot be registered. "
      + "Reason: java.io.IOException: Load Dummy Checked Exception.");
}

代码示例来源:origin: gocd/gocd

@Test
public void shouldHandleExceptionGeneratedByUnLoadMethodAtPluginStop() throws Exception {
  setupClassesInBundle("GoExtensionWithUnloadAnnotationMethodThrowingException.class");
  when(bundle.loadClass(contains("GoExtensionWithUnloadAnnotationMethodThrowingException"))).thenReturn((Class) GoExtensionWithUnloadAnnotationMethodThrowingException.class);
  activator.start(context);
  assertThat(activator.hasErrors(), is(false));
  activator.stop(context);
  assertThat(activator.hasErrors(), is(true));
  verifyErrorsReported("Invocation of unload method [public int com.thoughtworks.go.plugin.activation.GoExtensionWithUnloadAnnotationMethodThrowingException"
      + ".throwExceptionAgain(com.thoughtworks.go.plugin.api.info.PluginContext) "
      + "throws java.io.IOException]. "
      + "Reason: java.io.IOException: Unload Dummy Checked Exception.");
}

代码示例来源:origin: gocd/gocd

@Test
public void shouldGenerateExceptionWhenThereAreMoreThanOneLoadAnnotationsAtPluginStart() throws Exception {
  String expectedErrorMessageWithMethodsWithIncreasingOrder = "Class [GoExtensionWithMultipleLoadUnloadAnnotation] is annotated with @Extension will not be registered. "
      + "Reason: java.lang.RuntimeException: More than one method with @Load annotation not allowed. "
      + "Methods Found: [public void com.thoughtworks.go.plugin.activation.GoExtensionWithMultipleLoadUnloadAnnotation.setupData1(com.thoughtworks.go.plugin.api.info.PluginContext), "
      + "public void com.thoughtworks.go.plugin.activation.GoExtensionWithMultipleLoadUnloadAnnotation.setupData2(com.thoughtworks.go.plugin.api.info.PluginContext)].";
  String expectedErrorMessageWithMethodsWithDecreasingOrder = "Class [GoExtensionWithMultipleLoadUnloadAnnotation] is annotated with @Extension will not be registered. "
      + "Reason: java.lang.RuntimeException: More than one method with @Load annotation not allowed. "
      + "Methods Found: [public void com.thoughtworks.go.plugin.activation.GoExtensionWithMultipleLoadUnloadAnnotation.setupData2(com.thoughtworks.go.plugin.api.info.PluginContext), "
      + "public void com.thoughtworks.go.plugin.activation.GoExtensionWithMultipleLoadUnloadAnnotation.setupData1(com.thoughtworks.go.plugin.api.info.PluginContext)].";
  setupClassesInBundle("GoExtensionWithMultipleLoadUnloadAnnotation.class");
  when(bundle.loadClass(contains("GoExtensionWithMultipleLoadUnloadAnnotation"))).thenReturn((Class) GoExtensionWithMultipleLoadUnloadAnnotation.class);
  activator.start(context);
  assertThat(activator.hasErrors(), is(true));
  verifyThatOneOfTheErrorMessagesIsPresent(expectedErrorMessageWithMethodsWithIncreasingOrder, expectedErrorMessageWithMethodsWithDecreasingOrder);
  activator.stop(context);
  verifyThatOneOfTheErrorMessagesIsPresent(expectedErrorMessageWithMethodsWithIncreasingOrder, expectedErrorMessageWithMethodsWithDecreasingOrder);
}

相关文章