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

x33g5p2x  于2022-01-25 转载在 其他  
字(6.8k)|赞(0)|评价(0)|浏览(219)

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

Package.getPackage介绍

[英]Attempts to locate the requested package in the caller's class loader. If no package information can be located, null is returned.
[中]尝试在调用方的类加载器中定位请求的包。如果找不到包信息,则返回null。

代码示例

代码示例来源:origin: oblac/jodd

/**
   * Packages#getPackages().
   */
  public static Package of(final ClassLoader classLoader, final String name) {
    return Package.getPackage(name);
  }
}

代码示例来源:origin: stackoverflow.com

Package pkg = Package.getPackage("com.abc");
System.out.println(pkg);
Class<A> a = A.class;
pkg = Package.getPackage("com.abc");
System.out.println(pkg);

代码示例来源:origin: com.google.inject/guice

public Object readResolve() {
 return inPackage(Package.getPackage(packageName));
}

代码示例来源:origin: EngineHub/WorldEdit

/**
 * Get the version.
 *
 * @return the version of WorldEdit
 */
public static String getVersion() {
  if (version != null) {
    return version;
  }
  Package p = WorldEdit.class.getPackage();
  if (p == null) {
    p = Package.getPackage("com.sk89q.worldedit");
  }
  if (p == null) {
    version = "(unknown)";
  } else {
    version = p.getImplementationVersion();
    if (version == null) {
      version = "(unknown)";
    }
  }
  return version;
}

代码示例来源:origin: org.testng/testng

private static Ignore findAnnotation(Package testPackage) {
    if (testPackage == null) {
      return null;
    }
    Ignore result = testPackage.getAnnotation(Ignore.class);
    if (result != null) {
      return result;
    }
    String[] parts = testPackage.getName().split("\\.");
    String[] parentParts = Arrays.copyOf(parts, parts.length - 1);
    String parentPackageName = Strings.join(".", parentParts);
    if (parentPackageName.isEmpty()) {
      return null;
    }
    return findAnnotation(Package.getPackage(parentPackageName));
  }
}

代码示例来源:origin: org.springframework.boot/spring-boot

private boolean isLoadCandidate(Resource resource) {
  if (resource == null || !resource.exists()) {
    return false;
  }
  if (resource instanceof ClassPathResource) {
    // A simple package without a '.' may accidentally get loaded as an XML
    // document if we're not careful. The result of getInputStream() will be
    // a file list of the package content. We double check here that it's not
    // actually a package.
    String path = ((ClassPathResource) resource).getPath();
    if (path.indexOf('.') == -1) {
      try {
        return Package.getPackage(path) == null;
      }
      catch (Exception ex) {
        // Ignore
      }
    }
  }
  return true;
}

代码示例来源:origin: alibaba/jstorm

String logLevelUpper = (logLevel == null) ? "OFF" : logLevel.toUpperCase();
try {
  Package log4jPackage = Package.getPackage(LOG4J_CLASSIC);
  if (log4jPackage == null) {
    LOG.warn("Log4j is not in the classpath!");

代码示例来源:origin: org.springframework.boot/spring-boot

private Package findPackage(CharSequence source) {
  Package pkg = Package.getPackage(source.toString());
  if (pkg != null) {
    return pkg;
  }
  try {
    // Attempt to find a class in this package
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(
        getClass().getClassLoader());
    Resource[] resources = resolver.getResources(
        ClassUtils.convertClassNameToResourcePath(source.toString())
            + "/*.class");
    for (Resource resource : resources) {
      String className = StringUtils
          .stripFilenameExtension(resource.getFilename());
      load(Class.forName(source.toString() + "." + className));
      break;
    }
  }
  catch (Exception ex) {
    // swallow exception and continue
  }
  return Package.getPackage(source.toString());
}

代码示例来源:origin: alibaba/jstorm

String logLevelUpper = (logLevel == null) ? "OFF" : logLevel.toUpperCase();
try {
  Package logbackPackage = Package.getPackage(LOGBACK_CLASSIC);
  if (logbackPackage == null) {
    LOG.warn("Logback is not in the classpath!");

代码示例来源:origin: cbeust/testng

private static Ignore findAnnotation(Package testPackage) {
  if (testPackage == null) {
   return null;
  }
  Ignore result = testPackage.getAnnotation(Ignore.class);
  if (result != null) {
   return result;
  }
  String[] parts = testPackage.getName().split("\\.");
  String[] parentParts = Arrays.copyOf(parts, parts.length - 1);
  String parentPackageName = Strings.join(".", parentParts);
  if (parentPackageName.isEmpty()) {
   return null;
  }
  return findAnnotation(Package.getPackage(parentPackageName));
 }
}

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

@Test public void testPackagesFromParentClassLoaderAreMadeAvailableByName() throws Exception {
  assertThat(Test.class.getPackage()).isNotNull();
  assertThat(Package.getPackage("org.junit")).isNotNull();
  assertThat(Package.getPackage("org.junit")).isEqualTo(Test.class.getPackage());
 }
}

代码示例来源:origin: org.joda/joda-convert

@Override
  public Object convertFromString(Class<?> cls, String str) {
    return Package.getPackage(str);
  }
},

代码示例来源:origin: INRIA/spoon

@Override
public Package getActualPackage() {
  return Package.getPackage(getSimpleName());
}

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

static String shortClassName(Class<?> type) {
  Package pkg = type.getPackage();
  if (ClassUtils.isPrimitiveOrWrapper(type)) {
    if (!type.isPrimitive()) {
      type = ClassUtils.wrapperToPrimitive(type);
    }
    return type.getName();
  } else if (pkg == null || pkg.equals(Package.getPackage("java.lang"))) {
    return type.getSimpleName();
  } else {
    String[] words = type.getName().split(" ");
    String fullClassName = words[words.length - 1];
    String[] path = fullClassName.split("\\.");
    int i = 0;
    while (i + 1 < path.length && !Character.isUpperCase(path[i + 1].charAt(0))) {
      path[i] = path[i].substring(0, 1);
      i++;
    }
    return StringUtils.join(path, ".");
  }
}

代码示例来源:origin: org.elasticsearch/elasticsearch

@SuppressForbidden(reason = "ClassLoader.getDefinedPackage not available yet")
  public Object readResolve() {
    // TODO minJava >= 9 : use ClassLoader.getDefinedPackage and remove @SuppressForbidden
    return inPackage(Package.getPackage(packageName));
  }
}

代码示例来源:origin: haraldk/TwelveMonkeys

@Test
public void testCreateNorma() {
  new ProviderInfo(Package.getPackage("java.util"));
}

代码示例来源:origin: yahoo/elide

private static Package getParentPackage(Package pkg) {
  String name = pkg.getName();
  int idx = name.lastIndexOf('.');
  return idx == -1 ? null : Package.getPackage(name.substring(0, idx));
}

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

public static void main(String args[]) throws Exception {
  Package p = Package.getPackage("org.apache.karaf.shell.impl.console.standalone");
  if (p != null && p.getImplementationVersion() != null) {
    System.setProperty("karaf.version", p.getImplementationVersion());
  }
  Main main = new Main();
  main.run(args);
}

代码示例来源:origin: org.jruby/jruby-complete

@JRubyMethod(name = "sealed?")
public IRubyObject sealed_p(ThreadContext context) {
  final Package pkg = Package.getPackage(packageName);
  if ( pkg == null ) return context.nil;
  return context.runtime.newBoolean(pkg.isSealed());
}

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

protected void applyDefaultVersion(Swagger data) {
  if (applyDefaultVersion && data.getInfo() != null && data.getInfo().getVersion() == null
      && beanConfig != null && beanConfig.getResourcePackage() != null) {
    Package resourcePackage = Package.getPackage(beanConfig.getResourcePackage());
    if (resourcePackage != null) {
      data.getInfo().setVersion(resourcePackage.getImplementationVersion());
    }
  }
}

相关文章