Java Class.getPackage Examples

Java Class.getPackage - 30 examples found. These are the top rated real world Java examples of Class.getPackage extracted from open source projects. You can rate examples to help us improve the quality of examples.
 private void checkForAdapter(Class<?> clazz, Annotation[] anns) {
   if (anns != null) {
     for (Annotation a : anns) {
       if (XmlJavaTypeAdapter.class.isAssignableFrom(a.annotationType())) {
         Type t = getTypeFromXmlAdapter((XmlJavaTypeAdapter) a);
         if (t != null) {
           addType(t);
         }
       }
     }
   }
   XmlJavaTypeAdapter xjta = clazz.getAnnotation(XmlJavaTypeAdapter.class);
   if (xjta != null) {
     Type t = getTypeFromXmlAdapter(xjta);
     if (t != null) {
       addType(t);
     }
   }
   if (clazz.getPackage() != null) {
     XmlJavaTypeAdapters adapt = clazz.getPackage().getAnnotation(XmlJavaTypeAdapters.class);
     if (adapt != null) {
       for (XmlJavaTypeAdapter a : adapt.value()) {
         globalAdapters.add(a.type());
       }
       for (XmlJavaTypeAdapter a : adapt.value()) {
         Type t = getTypeFromXmlAdapter(a);
         if (t != null) {
           addType(t);
         }
       }
     }
   }
 }
Example #2
0
 /**
  * 获取版本号
  *
  * @param clazz
  * @param defaultVersion
  * @return
  */
 public static String getVersion(Class clazz, String defaultVersion) {
   try {
     // 首先查找MANIFEST.MF规范中的版本号
     String version = clazz.getPackage().getImplementationVersion();
     if (version == null || version.length() == 0) {
       version = clazz.getPackage().getSpecificationVersion();
     }
     if (version == null || version.length() == 0) {
       // 如果MANIFEST.MF规范中没有版本号,基于jar包名获取版本号
       String file = clazz.getProtectionDomain().getCodeSource().getLocation().getFile();
       if (file != null && file.length() > 0 && file.endsWith(".jar")) {
         Matcher matcher = VERSION_PATTERN.matcher(file);
         while (matcher.find() && matcher.groupCount() > 0) {
           version = matcher.group(1);
         }
       }
     }
     // 返回版本号,如果为空返回缺省版本号
     return version == null || version.length() == 0 ? defaultVersion : version;
   } catch (Throwable e) { // 防御性容错
     // 忽略异常,返回缺省版本号
     logger.error(e.getMessage(), e);
     return defaultVersion;
   }
 }
Example #3
0
 protected void maybeAddAncestors(Gwtc gwtc, Class<?> c) {
   addGwtcClass(gwtc, c);
   for (AncestorMode mode : gwtc.inheritanceMode()) {
     switch (mode) {
       case INHERIT_ONE_PARENT:
         Package pkg = c.getPackage();
         gwtc = pkg.getAnnotation(Gwtc.class);
         if (gwtc != null && addPackage(pkg)) {
           addGwtcPackage(gwtc, pkg, false);
         }
         break;
       case INHERIT_ALL_PARENTS:
         addAllPackages(c.getPackage());
         break;
       case INHERIT_ENCLOSING_CLASSES:
         addEnclosingClasses(c);
         break;
       case INHERIT_SUPER_CLASSES:
         addSuperclasses(c);
         break;
       default:
         X_Log.warn("Unsupported mode type", mode, "for class", c);
     }
   }
 }
 @SuppressWarnings("rawtypes")
 public String getVersionInformation(Class clazz) {
   // The following two lines read from the MANIFEST.MF
   String implTitle = clazz.getPackage().getImplementationTitle();
   String implVersion = clazz.getPackage().getImplementationVersion();
   if (implVersion != null) {
     // If we're in a .jar file, then we can return the version information
     // from the .jar file.
     return implTitle + " " + implVersion; // $NON-NLS-1$
   } else {
     // We're not in a .jar file - try to find the version file and
     // read the version information from that.
     try {
       ResourceBundle bundle = ResourceBundle.getBundle("version"); // $NON-NLS-1$
       StringBuffer buff = new StringBuffer();
       buff.append(bundle.getString("impl.title"))
           .append(' ')
           .append(bundle.getString("release.major.number"))
           .append('.')
           .append(
               bundle.getString(
                   "release.minor.number")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
       buff.append('.')
           .append(bundle.getString("release.milestone.number"))
           .append('.')
           .append(bundle.getString("release.build.number"))
           .append(" (class)"); // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
       return buff.toString();
     } catch (Exception ex) {
       return "Pentaho BI Platform - No Version Information Available"; //$NON-NLS-1$
     }
   }
 }
  protected static ClassLoadingMetaData createMetaData(
      PredeterminedManagedObjectAttachments deployment,
      String name,
      Version version,
      boolean useVersionOnPackages,
      Class<?>... packages) {
    MockClassLoadingMetaData classLoadingMetaData = new MockClassLoadingMetaData(name, version);

    classLoadingMetaData.setPaths(packages);

    CapabilitiesMetaData capabilities = classLoadingMetaData.getCapabilities();
    Capability capability = classLoadingMetaDataFactory.createModule(name, version);
    capabilities.addCapability(capability);

    if (packages != null) {
      for (Class<?> pkg : packages) {
        if (useVersionOnPackages)
          capability =
              classLoadingMetaDataFactory.createPackage(pkg.getPackage().getName(), version);
        else capability = classLoadingMetaDataFactory.createPackage(pkg.getPackage().getName());
        capabilities.addCapability(capability);
      }
    }

    classLoadingMetaData.setCapabilities(capabilities);
    return classLoadingMetaData;
  }
 /**
  * Creates a new {@link MetricName} without a scope.
  *
  * @param klass the {@link Class} to which the {@link Metric} belongs
  * @param name the name of the {@link Metric}
  * @param scope the scope of the {@link Metric}
  */
 public MetricName(Class<?> klass, String name, String scope) {
   this(
       klass.getPackage() == null ? "" : klass.getPackage().getName(),
       klass.getSimpleName().replaceAll("\\$$", ""),
       name,
       scope);
 }