Java Code Examples for java.util.jar.Attributes#get()

The following examples show how to use java.util.jar.Attributes#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Demo.java    From picocli with Apache License 2.0 6 votes vote down vote up
public String[] getVersion() throws Exception {
    Enumeration<URL> resources = CommandLine.class.getClassLoader().getResources("META-INF/MANIFEST.MF");
    while (resources.hasMoreElements()) {
        URL url = resources.nextElement();
        try {
            Manifest manifest = new Manifest(url.openStream());
            if (isApplicableManifest(manifest)) {
                Attributes attributes = manifest.getMainAttributes();
                return new String[] { attributes.get(key("Implementation-Title")) + " version \"" + attributes.get(key("Implementation-Version")) + "\"" };
            }
        } catch (IOException ex) {
            return new String[] { "Unable to read from " + url + ": " + ex };
        }
    }
    return new String[0];
}
 
Example 2
Source File: StrictJavaDepsPlugin.java    From bazel with Apache License 2.0 6 votes vote down vote up
private static JarOwner readJarOwnerFromManifest(Path jarPath) {
  try (JarFile jarFile = new JarFile(jarPath.toFile())) {
    Manifest manifest = jarFile.getManifest();
    if (manifest == null) {
      return JarOwner.create(jarPath);
    }
    Attributes attributes = manifest.getMainAttributes();
    String label = (String) attributes.get(TARGET_LABEL);
    if (label == null) {
      return JarOwner.create(jarPath);
    }
    String injectingRuleKind = (String) attributes.get(INJECTING_RULE_KIND);
    return JarOwner.create(jarPath, label, Optional.ofNullable(injectingRuleKind));
  } catch (IOException e) {
    // This jar file pretty much has to exist, we just used it in the compiler. Throw unchecked.
    throw new UncheckedIOException(e);
  }
}
 
Example 3
Source File: ImportDepsChecker.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Nullable
private static String extractLabel(Path jarPath) {
  try (JarFile jar = new JarFile(jarPath.toFile())) {
    Manifest manifest = jar.getManifest();
    if (manifest == null) {
      return null;
    }
    Attributes attributes = manifest.getMainAttributes();
    if (attributes == null) {
      return null;
    }
    String targetLabel = (String) attributes.get(TARGET_LABEL);
    String injectingRuleKind = (String) attributes.get(INJECTING_RULE_KIND);
    if (injectingRuleKind == null) {
      return targetLabel;
    } else {
      return String.format("\"%s %s\"", targetLabel, injectingRuleKind);
    }
  } catch (IOException e) {
    throw new UncheckedIOException(e);
  }
}
 
Example 4
Source File: ManifestMergerTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testMergeRequiredBundles() throws Exception {
	String packageName = getClass().getPackage().getName().replace('.', '/');
	InputStream resourceAsStream = getClass().getResourceAsStream("/" + packageName + "/Test_Manifest.MF");
	MergeableManifest manifest = new MergeableManifest(resourceAsStream);
	Attributes attrs = manifest.getMainAttributes();
	String before = ((String) attrs.get(MergeableManifest.REQUIRE_BUNDLE)).replaceAll("\\s","");
	manifest.addRequiredBundles(Collections.singleton("foo.bar.baz"));
	String after = (String) attrs.get(MergeableManifest.REQUIRE_BUNDLE);
	assertEquals(before + ",foo.bar.baz", after.replaceAll("\\s",""));
}
 
Example 5
Source File: ManifestMergerTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testNoChanges() throws Exception {
	String packageName = getClass().getPackage().getName().replace('.', '/');
	InputStream resourceAsStream = getClass().getResourceAsStream("/" + packageName + "/Test_Manifest.MF");
	MergeableManifest manifest = new MergeableManifest(resourceAsStream);
	Attributes attrs = manifest.getMainAttributes();
	String before = ((String) attrs.get(MergeableManifest.EXPORT_PACKAGE)).replaceAll("\\s","");
	manifest.addExportedPackages(Collections.singleton("foo.bar.baz"));
	String after = (String) attrs.get(MergeableManifest.EXPORT_PACKAGE);
	assertEquals(before + ",foo.bar.baz", after.replaceAll("\\s",""));
}
 
Example 6
Source File: VersionProviderDemo2.java    From picocli with Apache License 2.0 4 votes vote down vote up
private static Object get(Attributes attributes, String key) {
    return attributes.get(new Attributes.Name(key));
}
 
Example 7
Source File: ManifestVersionProvider.java    From commons-rng with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the named object from the attributes using the key.
 *
 * @param attributes The attributes.
 * @param key The key.
 * @return the object
 */
private static Object get(Attributes attributes, String key) {
    return attributes.get(new Attributes.Name(key));
}
 
Example 8
Source File: ManifestVersionProvider.java    From commons-rng with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the named object from the attributes using the key.
 *
 * @param attributes The attributes.
 * @param key The key.
 * @return the object
 */
private static Object get(Attributes attributes, String key) {
    return attributes.get(new Attributes.Name(key));
}