java.lang.Class.getPackage()方法的使用及代码示例

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

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

Class.getPackage介绍

[英]Returns the Package of which the class represented by this Class is a member. Returns null if no Packageobject was created by the class loader of the class.
[中]返回该类表示的类是其成员的包。如果类的类装入器未创建Packageobject,则返回null。

代码示例

代码示例来源:origin: spring-projects/spring-framework

/**
 * Return the full version string of the present Spring codebase,
 * or {@code null} if it cannot be determined.
 * @see Package#getImplementationVersion()
 */
@Nullable
public static String getVersion() {
  Package pkg = SpringVersion.class.getPackage();
  return (pkg != null ? pkg.getImplementationVersion() : null);
}

代码示例来源:origin: prestodb/presto

/**
 * @since 2.7
 */
public static String getPackageName(Class<?> cls) {
  Package pkg = cls.getPackage();
  return (pkg == null) ? null : pkg.getName();
}

代码示例来源:origin: ch.qos.logback/logback-classic

/**
 * {@inheritDoc}
 */
public void setLoggerContext(LoggerContext lc) {
  this.lc = lc;
  this.logger = lc.getLogger(getClass().getPackage().getName());
}

代码示例来源:origin: org.mockito/mockito-core

private boolean isComingFromJDK(Class<?> type) {
  // Comes from the manifest entry :
  // Implementation-Title: Java Runtime Environment
  // This entry is not necessarily present in every jar of the JDK
  return type.getPackage() != null && "Java Runtime Environment".equalsIgnoreCase(type.getPackage().getImplementationTitle())
    || type.getName().startsWith("java.")
    || type.getName().startsWith("javax.");
}

代码示例来源:origin: org.mockito/mockito-core

@Override
boolean isExported(Class<?> source) {
  if (source.getPackage() == null) {
    return true;
  }
  return (Boolean) invoke(isExportedUnqualified, invoke(getModule, source), source.getPackage().getName());
}

代码示例来源:origin: org.mockito/mockito-core

@Override
boolean isExported(Class<?> source, Class<?> target) {
  if (source.getPackage() == null) {
    return true;
  }
  return (Boolean) invoke(isExported, invoke(getModule, source), source.getPackage().getName(), invoke(getModule, target));
}

代码示例来源:origin: org.mockito/mockito-core

@Override
boolean isOpened(Class<?> source, Class<?> target) {
  if (source.getPackage() == null) {
    return true;
  }
  return (Boolean) invoke(isOpen, invoke(getModule, source), source.getPackage().getName(), invoke(getModule, target));
}

代码示例来源:origin: prestodb/presto

@Test(groups = CLI, timeOut = TIMEOUT)
public void shouldDisplayVersion()
    throws IOException
{
  launchPrestoCli("--version");
  String version = firstNonNull(Presto.class.getPackage().getImplementationVersion(), "(version unknown)");
  assertThat(presto.readRemainingOutputLines()).containsExactly("Presto CLI " + version);
}

代码示例来源:origin: google/guava

private List<Class<?>> loadClassesInPackage() throws IOException {
 List<Class<?>> classes = Lists.newArrayList();
 String packageName = getClass().getPackage().getName();
 for (ClassPath.ClassInfo classInfo :
   ClassPath.from(getClass().getClassLoader()).getTopLevelClasses(packageName)) {
  Class<?> cls;
  try {
   cls = classInfo.load();
  } catch (NoClassDefFoundError e) {
   // In case there were linking problems, this is probably not a class we care to test anyway.
   logger.log(Level.SEVERE, "Cannot load class " + classInfo + ", skipping...", e);
   continue;
  }
  if (!cls.isInterface()) {
   classes.add(cls);
  }
 }
 return classes;
}

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

public File toGeneratedFile(Class<?> compiledClass) {
  File output = new File(OUTPUT_DIRECTORY, toPath(compiledClass.getPackage()) + "/" + compiledClass.getSimpleName() + getAndroidAnnotationsClassSuffix() + SOURCE_FILE_SUFFIX);
  return output;
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void shouldNotScanTwice() {
  TestImport.scanned = false;
  AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
  context.scan(TestImport.class.getPackage().getName());
  context.refresh();
  context.getBean(TestConfiguration.class);
}

代码示例来源:origin: bumptech/glide

@Test
public void testHasValidTag() {
 assertEquals(RequestManagerRetriever.class.getPackage().getName(),
   RequestManagerRetriever.FRAGMENT_TAG);
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Prior to the fix for SPR-8761, this test threw because the nested MyComponent
 * annotation was being falsely considered as a 'lite' Configuration class candidate.
 */
@Test
public void repro() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  ctx.scan(getClass().getPackage().getName());
  ctx.refresh();
  assertThat(ctx.containsBean("withNestedAnnotation"), is(true));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testGetPackageName() {
  assertEquals("java.lang", ClassUtils.getPackageName(String.class));
  assertEquals(getClass().getPackage().getName(), ClassUtils.getPackageName(getClass()));
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Prior to fixing SPR-10546 this might have succeeded depending on the ordering the
 * classes were picked up. If they are picked up in the same order as
 * {@link #enclosingConfigFirstParentDefinesBean()} then it would fail. This test is
 * mostly for illustration purposes, but doesn't hurt to continue using it.
 *
 * <p>We purposely use the {@link AEnclosingConfig} to make it alphabetically prior to the
 * {@link AEnclosingConfig.ChildConfig} which encourages this to occur with the
 * classpath scanning implementation being used by the author of this test.
 */
@Test
public void enclosingConfigFirstParentDefinesBeanWithScanning() {
  AnnotationConfigApplicationContext ctx= new AnnotationConfigApplicationContext();
  context = ctx;
  ctx.scan(AEnclosingConfig.class.getPackage().getName());
  ctx.refresh();
  assertThat(context.getBean("myBean",String.class), equalTo("myBean"));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void controlScan() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  ctx.scan(example.scannable._package.class.getPackage().getName());
  ctx.refresh();
  assertThat("control scan for example.scannable package failed to register FooServiceImpl bean",
      ctx.containsBean("fooServiceImpl"), is(true));
}

代码示例来源:origin: spring-projects/spring-framework

private ApplicationContext createContext(ScopedProxyMode scopedProxyMode) {
  GenericWebApplicationContext context = new GenericWebApplicationContext();
  ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
  scanner.setIncludeAnnotationConfig(false);
  scanner.setBeanNameGenerator((definition, registry) -> definition.getScope());
  scanner.setScopedProxyMode(scopedProxyMode);
  // Scan twice in order to find errors in the bean definition compatibility check.
  scanner.scan(getClass().getPackage().getName());
  scanner.scan(getClass().getPackage().getName());
  context.refresh();
  return context;
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void naming() throws MalformedObjectNameException {
  JmxTestBean bean = new JmxTestBean();
  IdentityNamingStrategy strategy = new IdentityNamingStrategy();
  ObjectName objectName = strategy.getObjectName(bean, "null");
  assertEquals("Domain is incorrect", bean.getClass().getPackage().getName(),
      objectName.getDomain());
  assertEquals("Type property is incorrect", ClassUtils.getShortName(bean.getClass()),
      objectName.getKeyProperty(IdentityNamingStrategy.TYPE_KEY));
  assertEquals("HashCode property is incorrect", ObjectUtils.getIdentityHexString(bean),
      objectName.getKeyProperty(IdentityNamingStrategy.HASH_CODE_KEY));
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testToFullyQualifiedNamePackageString() throws Exception {
  final String expected = "org.apache.commons.lang3.Test.properties";
  final String actual = ClassPathUtils.toFullyQualifiedName(ClassPathUtils.class.getPackage(), "Test.properties");
  assertEquals(expected, actual);
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
  public void testToFullyQualifiedPathPackage() throws Exception {
    final String expected = "org/apache/commons/lang3/Test.properties";
    final String actual = ClassPathUtils.toFullyQualifiedPath(ClassPathUtils.class.getPackage(), "Test.properties");

    assertEquals(expected, actual);
  }
}

相关文章

Class类方法