From f26df87d3bfc3f6b701d119c26deae6598b35b78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Kubitz?= Date: Mon, 29 Jan 2024 14:54:40 +0100 Subject: [PATCH] use try-with-resource, java.nio.file.Files#readString --- .../api/tools/internal/APIFileGenerator.java | 45 +++------ .../tools/internal/ApiDescriptionManager.java | 8 +- .../internal/ApiDescriptionProcessor.java | 37 ++----- .../SystemApiDescriptionProcessor.java | 37 ++----- .../pde/api/tools/internal/util/Util.java | 8 +- .../internal/build/tasks/JNLPGenerator.java | 38 ++++---- .../internal/core/PDERegistryStrategy.java | 37 +++---- .../pde/internal/core/util/CoreUtility.java | 28 ++---- .../ui/shared/target/TargetContentsGroup.java | 54 +++++------ .../imports/PluginImportOperation.java | 97 ++++++++----------- 10 files changed, 147 insertions(+), 242 deletions(-) diff --git a/apitools/org.eclipse.pde.api.tools/src/org/eclipse/pde/api/tools/internal/APIFileGenerator.java b/apitools/org.eclipse.pde.api.tools/src/org/eclipse/pde/api/tools/internal/APIFileGenerator.java index 70b05ebaec..91f9dc24fa 100644 --- a/apitools/org.eclipse.pde.api.tools/src/org/eclipse/pde/api/tools/internal/APIFileGenerator.java +++ b/apitools/org.eclipse.pde.api.tools/src/org/eclipse/pde/api/tools/internal/APIFileGenerator.java @@ -18,10 +18,11 @@ import java.io.FileFilter; import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; import java.io.PrintWriter; import java.io.StringReader; import java.io.StringWriter; -import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.ArrayList; import java.util.Enumeration; import java.util.HashSet; @@ -212,39 +213,25 @@ public void generateAPIFile() { File currentManifest = new File(allManifestFile); Set currentApiPackages = null; if (currentManifest.exists()) { - BufferedInputStream inputStream = null; - ZipFile zipFile = null; try { if (isZipJarFile(currentManifest.getName())) { - zipFile = new ZipFile(currentManifest); - final ZipEntry entry = zipFile.getEntry("META-INF/MANIFEST.MF"); //$NON-NLS-1$ - if (entry != null) { - inputStream = new BufferedInputStream(zipFile.getInputStream(entry)); + try (ZipFile zipFile = new ZipFile(currentManifest)) { + final ZipEntry entry = zipFile.getEntry("META-INF/MANIFEST.MF"); //$NON-NLS-1$ + if (entry != null) { + InputStream inputStream = zipFile.getInputStream(entry); + manifestMap = ManifestElement.parseBundleManifest(inputStream, null); + currentApiPackages = collectApiPackageNames(manifestMap); + + } } } else { - inputStream = new BufferedInputStream(new FileInputStream(currentManifest)); - } - if (inputStream != null) { - manifestMap = ManifestElement.parseBundleManifest(inputStream, null); - currentApiPackages = collectApiPackageNames(manifestMap); + try (InputStream inputStream = new FileInputStream(currentManifest)) { + manifestMap = ManifestElement.parseBundleManifest(inputStream, null); + currentApiPackages = collectApiPackageNames(manifestMap); + } } } catch (IOException | BundleException e) { ApiPlugin.log(e); - } finally { - if (inputStream != null) { - try { - inputStream.close(); - } catch (IOException e) { - // ignore - } - } - if (zipFile != null) { - try { - zipFile.close(); - } catch (IOException e) { - // ignore - } - } } } if (currentApiPackages != null) { @@ -424,8 +411,8 @@ private boolean isAPIToolsNature(File dotProjectFile) { if (!dotProjectFile.exists()) { return false; } - try (BufferedInputStream stream = new BufferedInputStream(new FileInputStream(dotProjectFile))) { - String contents = new String(Util.getInputStreamAsCharArray(stream, StandardCharsets.UTF_8)); + try { + String contents = Files.readString(dotProjectFile.toPath()); return containsAPIToolsNature(contents); } catch (IOException e) { e.printStackTrace(); diff --git a/apitools/org.eclipse.pde.api.tools/src/org/eclipse/pde/api/tools/internal/ApiDescriptionManager.java b/apitools/org.eclipse.pde.api.tools/src/org/eclipse/pde/api/tools/internal/ApiDescriptionManager.java index bb51031112..ca63467199 100644 --- a/apitools/org.eclipse.pde.api.tools/src/org/eclipse/pde/api/tools/internal/ApiDescriptionManager.java +++ b/apitools/org.eclipse.pde.api.tools/src/org/eclipse/pde/api/tools/internal/ApiDescriptionManager.java @@ -13,11 +13,9 @@ *******************************************************************************/ package org.eclipse.pde.api.tools.internal; -import java.io.BufferedInputStream; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; -import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.text.MessageFormat; import java.util.ArrayList; import java.util.HashMap; @@ -251,8 +249,8 @@ public synchronized void saving(ISaveContext context) throws CoreException { private boolean restoreDescription(IJavaProject project, ProjectApiDescription description) throws CoreException { File file = API_DESCRIPTIONS_CONTAINER_PATH.append(project.getElementName()).append(IApiCoreConstants.API_DESCRIPTION_XML_NAME).toFile(); if (file.exists()) { - try (BufferedInputStream stream = new BufferedInputStream(new FileInputStream(file))) { - String xml = new String(Util.getInputStreamAsCharArray(stream, StandardCharsets.UTF_8)); + try { + String xml = Files.readString(file.toPath()); Element root = Util.parseDocument(xml); if (!root.getNodeName().equals(IApiXmlConstants.ELEMENT_COMPONENT)) { abort(ScannerMessages.ComponentXMLScanner_0, null); diff --git a/apitools/org.eclipse.pde.api.tools/src/org/eclipse/pde/api/tools/internal/ApiDescriptionProcessor.java b/apitools/org.eclipse.pde.api.tools/src/org/eclipse/pde/api/tools/internal/ApiDescriptionProcessor.java index c6b99d6653..53ef873445 100644 --- a/apitools/org.eclipse.pde.api.tools/src/org/eclipse/pde/api/tools/internal/ApiDescriptionProcessor.java +++ b/apitools/org.eclipse.pde.api.tools/src/org/eclipse/pde/api/tools/internal/ApiDescriptionProcessor.java @@ -14,10 +14,9 @@ package org.eclipse.pde.api.tools.internal; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; -import java.io.InputStream; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; @@ -443,46 +442,28 @@ private ApiDescriptionProcessor() { */ public static String serializeComponentXml(File location) { if (location.exists()) { - ZipFile jarFile = null; - InputStream stream = null; try { String extension = IPath.fromOSString(location.getName()).getFileExtension(); if (extension != null && extension.equals("jar") && location.isFile()) { //$NON-NLS-1$ - jarFile = new ZipFile(location, ZipFile.OPEN_READ); - ZipEntry manifestEntry = jarFile.getEntry(IApiCoreConstants.COMPONENT_XML_NAME); - if (manifestEntry != null) { - stream = jarFile.getInputStream(manifestEntry); + try (ZipFile jarFile = new ZipFile(location, ZipFile.OPEN_READ)) { + ZipEntry manifestEntry = jarFile.getEntry(IApiCoreConstants.COMPONENT_XML_NAME); + if (manifestEntry != null) { + byte[] allBytes = jarFile.getInputStream(manifestEntry).readAllBytes(); + return new String(allBytes, StandardCharsets.UTF_8); + } } } else if (location.isDirectory()) { File file = new File(location, IApiCoreConstants.COMPONENT_XML_NAME); if (file.exists()) { - stream = new FileInputStream(file); + return Files.readString(file.toPath()); } } else if (location.isFile()) { if (location.getName().equals(IApiCoreConstants.COMPONENT_XML_NAME)) { - stream = new FileInputStream(location); + return Files.readString(location.toPath()); } } - if (stream != null) { - return new String(Util.getInputStreamAsCharArray(stream, StandardCharsets.UTF_8)); - } } catch (IOException e) { ApiPlugin.log(e); - } finally { - try { - if (stream != null) { - stream.close(); - } - } catch (IOException e) { - ApiPlugin.log(e); - } - try { - if (jarFile != null) { - jarFile.close(); - } - } catch (IOException e) { - ApiPlugin.log(e); - } } } return null; diff --git a/apitools/org.eclipse.pde.api.tools/src/org/eclipse/pde/api/tools/internal/SystemApiDescriptionProcessor.java b/apitools/org.eclipse.pde.api.tools/src/org/eclipse/pde/api/tools/internal/SystemApiDescriptionProcessor.java index 05a90654c1..c234a33aee 100644 --- a/apitools/org.eclipse.pde.api.tools/src/org/eclipse/pde/api/tools/internal/SystemApiDescriptionProcessor.java +++ b/apitools/org.eclipse.pde.api.tools/src/org/eclipse/pde/api/tools/internal/SystemApiDescriptionProcessor.java @@ -14,10 +14,9 @@ package org.eclipse.pde.api.tools.internal; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; -import java.io.InputStream; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; @@ -65,46 +64,28 @@ private SystemApiDescriptionProcessor() { */ public static String serializeComponentXml(File location) { if (location.exists()) { - ZipFile jarFile = null; - InputStream stream = null; try { String extension = IPath.fromOSString(location.getName()).getFileExtension(); if (extension != null && extension.equals("jar") && location.isFile()) { //$NON-NLS-1$ - jarFile = new ZipFile(location, ZipFile.OPEN_READ); - ZipEntry manifestEntry = jarFile.getEntry(IApiCoreConstants.SYSTEM_API_DESCRIPTION_XML_NAME); - if (manifestEntry != null) { - stream = jarFile.getInputStream(manifestEntry); + try (ZipFile jarFile = new ZipFile(location, ZipFile.OPEN_READ)) { + ZipEntry manifestEntry = jarFile.getEntry(IApiCoreConstants.SYSTEM_API_DESCRIPTION_XML_NAME); + if (manifestEntry != null) { + byte[] allBytes = jarFile.getInputStream(manifestEntry).readAllBytes(); + return new String(allBytes, StandardCharsets.UTF_8); + } } } else if (location.isDirectory()) { File file = new File(location, IApiCoreConstants.SYSTEM_API_DESCRIPTION_XML_NAME); if (file.exists()) { - stream = new FileInputStream(file); + return Files.readString(file.toPath()); } } else if (location.isFile()) { if (location.getName().equals(IApiCoreConstants.SYSTEM_API_DESCRIPTION_XML_NAME)) { - stream = new FileInputStream(location); + return Files.readString(location.toPath()); } } - if (stream != null) { - return new String(Util.getInputStreamAsCharArray(stream, StandardCharsets.UTF_8)); - } } catch (IOException e) { ApiPlugin.log(e); - } finally { - try { - if (stream != null) { - stream.close(); - } - } catch (IOException e) { - ApiPlugin.log(e); - } - try { - if (jarFile != null) { - jarFile.close(); - } - } catch (IOException e) { - ApiPlugin.log(e); - } } } return null; diff --git a/apitools/org.eclipse.pde.api.tools/src/org/eclipse/pde/api/tools/internal/util/Util.java b/apitools/org.eclipse.pde.api.tools/src/org/eclipse/pde/api/tools/internal/util/Util.java index 9edfbbbb2c..2b3d3fdbf6 100644 --- a/apitools/org.eclipse.pde.api.tools/src/org/eclipse/pde/api/tools/internal/util/Util.java +++ b/apitools/org.eclipse.pde.api.tools/src/org/eclipse/pde/api/tools/internal/util/Util.java @@ -1761,14 +1761,12 @@ public static void saveFile(File file, String contents) throws IOException { * @return the contents of the file as a {@link String} or null */ public static String getFileContentAsString(File file) { - String contents = null; - try (FileInputStream stream = new FileInputStream(file)) { - char[] array = getInputStreamAsCharArray(stream, StandardCharsets.UTF_8); - contents = new String(array); + try { + return Files.readString(file.toPath()); } catch (IOException ioe) { ApiPlugin.log(ioe); + return null; } - return contents; } /** diff --git a/build/org.eclipse.pde.build/src_ant/org/eclipse/pde/internal/build/tasks/JNLPGenerator.java b/build/org.eclipse.pde.build/src_ant/org/eclipse/pde/internal/build/tasks/JNLPGenerator.java index 3fe1fb2a8b..6435061c87 100644 --- a/build/org.eclipse.pde.build/src_ant/org/eclipse/pde/internal/build/tasks/JNLPGenerator.java +++ b/build/org.eclipse.pde.build/src_ant/org/eclipse/pde/internal/build/tasks/JNLPGenerator.java @@ -118,41 +118,39 @@ public JNLPGenerator(String feature, String destination, String codebase, String * Parses the specified url and constructs a feature */ public void process() { - InputStream in = null; final String FEATURE_XML = "feature.xml"; //$NON-NLS-1$ - try { - ZipFile featureArchive = null; - InputStream nlsStream = null; + ZipFile featureArchive; + InputStream nlsStream; + InputStream in; if (featureRoot.isFile()) { featureArchive = new ZipFile(featureRoot); nlsStream = getNLSStream(featureArchive); ZipEntry featureXML = featureArchive.getEntry(FEATURE_XML); in = featureArchive.getInputStream(featureXML); } else { + featureArchive = null; nlsStream = getNLSStream(this.featureRoot); in = new BufferedInputStream(new FileInputStream(new File(featureRoot, FEATURE_XML))); } - try { - if (nlsStream != null) { - nlsBundle = new PropertyResourceBundle(nlsStream); - nlsStream.close(); + try (featureArchive; nlsStream; in) { + try { + if (nlsStream != null) { + nlsBundle = new PropertyResourceBundle(nlsStream); + } + } catch (IOException e) { + // do nothing + } + try { + parser.parse(new InputSource(in), this); + writeResourceEpilogue(); + writeEpilogue(); + } catch (SAXException e) { + //Ignore the exception } - } catch (IOException e) { - // do nothing - } - try { - parser.parse(new InputSource(in), this); - writeResourceEpilogue(); - writeEpilogue(); - } catch (SAXException e) { - //Ignore the exception } finally { - in.close(); if (out != null) out.close(); - if (featureArchive != null) - featureArchive.close(); } } catch (IOException e) { //Ignore the exception diff --git a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/PDERegistryStrategy.java b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/PDERegistryStrategy.java index ccde97e588..197326c018 100644 --- a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/PDERegistryStrategy.java +++ b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/PDERegistryStrategy.java @@ -215,34 +215,29 @@ private void addBundle(IExtensionRegistry registry, IPluginModelBase base) { if (input == null) { return; } - InputStream is = null; - ZipFile jfile = null; - try { if (new File(base.getInstallLocation()).isDirectory()) { // Directory bundle, access the extensions file directly - is = new FileInputStream(input); + try (InputStream is = new BufferedInputStream(new FileInputStream(input))) { + registry.addContribution(is, contributor, true, input.getPath(), null, fKey); + } } else { // Archived bundle, need to extract the file - jfile = new ZipFile(input, ZipFile.OPEN_READ); - String fileName = (base.isFragmentModel()) ? ICoreConstants.FRAGMENT_FILENAME_DESCRIPTOR - : ICoreConstants.PLUGIN_FILENAME_DESCRIPTOR; - ZipEntry entry = jfile.getEntry(fileName); - if (entry != null) { - is = jfile.getInputStream(entry); - } - } - if (is != null) { - registry.addContribution(new BufferedInputStream(is), contributor, true, input.getPath(), null, fKey); - } - } catch (IOException e) { - } finally { - if (jfile != null) { - try { - jfile.close(); - } catch (IOException e) { + try (ZipFile jfile = new ZipFile(input, ZipFile.OPEN_READ)) { + String fileName = (base.isFragmentModel()) ? ICoreConstants.FRAGMENT_FILENAME_DESCRIPTOR + : ICoreConstants.PLUGIN_FILENAME_DESCRIPTOR; + ZipEntry entry = jfile.getEntry(fileName); + if (entry != null) { + try (InputStream is = jfile.getInputStream(entry)) { + if (is != null) { + registry.addContribution(new BufferedInputStream(is), contributor, true, + input.getPath(), null, fKey); + } + } + } } } + } catch (IOException ignored) { } } diff --git a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/util/CoreUtility.java b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/util/CoreUtility.java index dc20b62ddd..89e3da79fe 100644 --- a/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/util/CoreUtility.java +++ b/ui/org.eclipse.pde.core/src/org/eclipse/pde/internal/core/util/CoreUtility.java @@ -177,36 +177,26 @@ public static void copyFile(IPath originPath, String name, File target) { } public static org.eclipse.jface.text.Document getTextDocument(File bundleLocation, String path) { - ZipFile jarFile = null; - InputStream stream = null; try { String extension = IPath.fromOSString(bundleLocation.getName()).getFileExtension(); if ("jar".equals(extension) && bundleLocation.isFile()) { //$NON-NLS-1$ - jarFile = new ZipFile(bundleLocation, ZipFile.OPEN_READ); - ZipEntry manifestEntry = jarFile.getEntry(path); - if (manifestEntry != null) { - stream = jarFile.getInputStream(manifestEntry); + try (ZipFile jarFile = new ZipFile(bundleLocation, ZipFile.OPEN_READ)) { + ZipEntry manifestEntry = jarFile.getEntry(path); + if (manifestEntry != null) { + InputStream stream = jarFile.getInputStream(manifestEntry); + return getTextDocument(stream); + } } } else { File file = new File(bundleLocation, path); if (file.exists()) { - stream = new FileInputStream(file); + try (InputStream stream = new FileInputStream(file)) { + return getTextDocument(stream); + } } } - return getTextDocument(stream); } catch (IOException e) { PDECore.logException(e); - } finally { - try { - if (jarFile != null) { - jarFile.close(); - } - if (stream != null) { - stream.close(); - } - } catch (IOException e) { - PDECore.logException(e); - } } return null; } diff --git a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/shared/target/TargetContentsGroup.java b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/shared/target/TargetContentsGroup.java index 51f7bb5081..d9255ec9e2 100644 --- a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/shared/target/TargetContentsGroup.java +++ b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/shared/target/TargetContentsGroup.java @@ -634,7 +634,7 @@ private IPath getParentPath(TargetBundle bundle) { } /** - * Parses a bunlde's manifest into a dictionary. The bundle may be in a jar + * Parses a bundle's manifest into a dictionary. The bundle may be in a jar * or in a directory at the specified location. * * @param bundleLocation root location of the bundle @@ -642,44 +642,36 @@ private IPath getParentPath(TargetBundle bundle) { * @throws IOException if unable to parse */ protected Map loadManifest(File bundleLocation) throws IOException { - ZipFile jarFile = null; - InputStream manifestStream = null; String extension = IPath.fromOSString(bundleLocation.getName()).getFileExtension(); - try { - if (extension != null && extension.equals("jar") && bundleLocation.isFile()) { //$NON-NLS-1$ - jarFile = new ZipFile(bundleLocation, ZipFile.OPEN_READ); + if (extension != null && extension.equals("jar") && bundleLocation.isFile()) { //$NON-NLS-1$ + try (ZipFile jarFile = new ZipFile(bundleLocation, ZipFile.OPEN_READ)) { ZipEntry manifestEntry = jarFile.getEntry(JarFile.MANIFEST_NAME); - if (manifestEntry != null) { - manifestStream = jarFile.getInputStream(manifestEntry); + if (manifestEntry == null) { + return null; } - } else { - File file = new File(bundleLocation, JarFile.MANIFEST_NAME); - if (file.exists()) - manifestStream = new FileInputStream(file); - } - if (manifestStream == null) { - return null; + InputStream manifestStream = jarFile.getInputStream(manifestEntry); + return loadManifest(manifestStream); } + } + File file = new File(bundleLocation, JarFile.MANIFEST_NAME); + if (!file.exists()) { + return null; + } + try (InputStream manifestStream = new FileInputStream(file)) { + return loadManifest(manifestStream); + } + } + + private Map loadManifest(InputStream manifestStream) throws IOException { + if (manifestStream == null) { + return null; + } + try { return ManifestElement.parseBundleManifest(manifestStream, new Hashtable<>(10)); } catch (BundleException e) { PDEPlugin.log(e); - } finally { - try { - if (manifestStream != null) { - manifestStream.close(); - } - } catch (IOException e) { - PDEPlugin.log(e); - } - try { - if (jarFile != null) { - jarFile.close(); - } - } catch (IOException e) { - PDEPlugin.log(e); - } + return null; } - return null; } /** diff --git a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/wizards/imports/PluginImportOperation.java b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/wizards/imports/PluginImportOperation.java index 66c6e56570..38dac850c8 100644 --- a/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/wizards/imports/PluginImportOperation.java +++ b/ui/org.eclipse.pde.ui/src/org/eclipse/pde/internal/ui/wizards/imports/PluginImportOperation.java @@ -790,36 +790,27 @@ private boolean canFindSource(IPluginModelBase model) { } // Check for source inside the binary plug-in - ZipFile zip = null; - try { - IImportStructureProvider provider; - Object root; - if (isJARd(model)) { - zip = new ZipFile(new File(model.getInstallLocation())); - provider = new ZipFileStructureProvider(zip); - root = ((ZipFileStructureProvider) provider).getRoot(); - } else { - provider = FileSystemStructureProvider.INSTANCE; - root = new File(model.getInstallLocation()); - } - List children = provider.getChildren(root); - for (Object child : children) { - String label = provider.getLabel(child); - if (label.equals(DEFAULT_SOURCE_DIR)) { - return true; - } - } - } catch (IOException e) { - // Do nothing, any other problems will be caught during binary import - } finally { - if (zip != null) { - try { - zip.close(); - } catch (IOException e) { - } + if (isJARd(model)) { + try (ZipFile zip = new ZipFile(new File(model.getInstallLocation()))) { + IImportStructureProvider provider = new ZipFileStructureProvider(zip); + Object root = ((ZipFileStructureProvider) provider).getRoot(); + return canFindSource(provider, root); + } catch (IOException e) { + return false; } } + Object root = new File(model.getInstallLocation()); + return canFindSource(FileSystemStructureProvider.INSTANCE, root); + } + private boolean canFindSource(IImportStructureProvider provider, Object root) { + List children = provider.getChildren(root); + for (Object child : children) { + String label = provider.getLabel(child); + if (label.equals(DEFAULT_SOURCE_DIR)) { + return true; + } + } return false; } @@ -1030,39 +1021,33 @@ private boolean extractSourceFolders(IProject project, IPluginModelBase model, W * @return true if source was found inside the binary plug-in, false otherwise */ private boolean handleInternalSource(IPluginModelBase model, WorkspaceBuildModel buildModel, Map packageLocations) throws CoreException, ZipException, IOException { - IImportStructureProvider provider; - Object root; - IPath prefixPath; IPath defaultSourcePath = IPath.fromOSString(DEFAULT_SOURCE_DIR); - ZipFile zip = null; - try { - if (isJARd(model)) { - zip = new ZipFile(new File(model.getInstallLocation())); - provider = new ZipFileStructureProvider(zip); - root = ((ZipFileStructureProvider) provider).getRoot(); - prefixPath = defaultSourcePath; - } else { - provider = FileSystemStructureProvider.INSTANCE; - File rootFile = new File(model.getInstallLocation()); - root = rootFile; - prefixPath = IPath.fromOSString(rootFile.getPath()).append(defaultSourcePath); + if (isJARd(model)) { + try (ZipFile zip = new ZipFile(new File(model.getInstallLocation()))) { + IImportStructureProvider provider = new ZipFileStructureProvider(zip); + Object root = ((ZipFileStructureProvider) provider).getRoot(); + return handleInternalSource(buildModel, packageLocations, provider, root, defaultSourcePath, defaultSourcePath); } + } + IImportStructureProvider provider = FileSystemStructureProvider.INSTANCE; + File rootFile = new File(model.getInstallLocation()); + IPath prefixPath = IPath.fromOSString(rootFile.getPath()).append(defaultSourcePath); + return handleInternalSource(buildModel, packageLocations, provider, rootFile, prefixPath, defaultSourcePath); + } - ArrayList collected = new ArrayList<>(); - PluginImportHelper.collectResourcesFromFolder(provider, root, defaultSourcePath, collected); - if (!collected.isEmpty()) { - Set packages = new HashSet<>(); - PluginImportHelper.collectJavaPackages(provider, collected, prefixPath, packages); - addPackageEntries(packages, defaultSourcePath, packageLocations); - addBuildEntry(buildModel, "source." + DEFAULT_LIBRARY_NAME, DEFAULT_SOURCE_DIR + "/"); //$NON-NLS-1$ //$NON-NLS-2$ - return true; - } - return false; - } finally { - if (zip != null) { - zip.close(); - } + private boolean handleInternalSource(WorkspaceBuildModel buildModel, Map packageLocations, + IImportStructureProvider provider, Object root, IPath prefixPath, IPath defaultSourcePath) + throws CoreException { + ArrayList collected = new ArrayList<>(); + PluginImportHelper.collectResourcesFromFolder(provider, root, defaultSourcePath, collected); + if (!collected.isEmpty()) { + Set packages = new HashSet<>(); + PluginImportHelper.collectJavaPackages(provider, collected, prefixPath, packages); + addPackageEntries(packages, defaultSourcePath, packageLocations); + addBuildEntry(buildModel, "source." + DEFAULT_LIBRARY_NAME, DEFAULT_SOURCE_DIR + "/"); //$NON-NLS-1$ //$NON-NLS-2$ + return true; } + return false; } /**