From 1387bb17bd72c07a604df3b17a3d61d003e1aabc Mon Sep 17 00:00:00 2001 From: Hannes Wellmann Date: Fri, 26 Jul 2024 19:13:12 +0200 Subject: [PATCH] [PDE-Build] Use File instead of URL to represent local files This avoids unnecessary conversations that also use deprecated methods like File.toURL() --- .../org/eclipse/pde/internal/build/Utils.java | 86 ++----------------- .../site/BuildTimeSiteContentProvider.java | 2 +- .../build/site/BuildTimeSiteFactory.java | 2 +- .../pde/internal/build/site/P2Utils.java | 23 +---- .../internal/build/site/PluginPathFinder.java | 30 +++---- 5 files changed, 23 insertions(+), 120 deletions(-) diff --git a/build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/Utils.java b/build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/Utils.java index a64fbb9fe4..8d18fc50d3 100644 --- a/build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/Utils.java +++ b/build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/Utils.java @@ -20,8 +20,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.net.MalformedURLException; -import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; @@ -257,72 +255,6 @@ public static String[] getArrayFromString(String list) { return getArrayFromString(list, ","); //$NON-NLS-1$ } - /** - * Converts an array of strings into an array of URLs. - * - * @return URL[] - */ - public static URL[] asURL(String[] target) throws CoreException { - if (target == null) - return null; - try { - URL[] result = new URL[target.length]; - for (int i = 0; i < target.length; i++) - result[i] = new URL(target[i]); - return result; - } catch (MalformedURLException e) { - throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_MALFORMED_URL, e.getMessage(), e)); - } - } - - public static URL[] asURL(Collection target) throws CoreException { - if (target == null) - return null; - try { - URL[] result = new URL[target.size()]; - int i = 0; - for (File file : target) { - result[i++] = file.toURL(); - } - return result; - } catch (MalformedURLException e) { - throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_MALFORMED_URL, e.getMessage(), e)); - } - } - - public static File[] asFile(String[] target) { - if (target == null) - return new File[0]; - File[] result = new File[target.length]; - for (int i = 0; i < result.length; i++) { - result[i] = new File(target[i]); - } - return result; - } - - public static File[] asFile(URL[] target) { - if (target == null) - return new File[0]; - File[] result = new File[target.length]; - for (int i = 0; i < result.length; i++) { - result[i] = new File(target[i].getFile()); - } - return result; - } - - public static File[] asFile(Collection collection) { - if (collection.size() == 0) - return new File[0]; - Object first = collection.iterator().next(); - if (first instanceof String) - return asFile(collection.toArray(new String[collection.size()])); - else if (first instanceof URL) - return asFile(collection.toArray(new URL[collection.size()])); - else if (first instanceof File) - return collection.toArray(new File[collection.size()]); - throw new IllegalArgumentException(); - } - /** * Return a string which is a concatination of each member of the given * collection, separated by the given separator. @@ -396,19 +328,13 @@ public static IPath makeRelative(IPath location, IPath base) { } static public void copyFile(String src, String dest) throws IOException { - File source = new File(src); - if (!source.exists()) + Path source = Path.of(src); + if (!Files.isRegularFile(source)) { return; - File destination = new File(dest); - File destDir = destination.getParentFile(); - if ((!destDir.exists() && !destDir.mkdirs()) || destDir.isFile()) - return; //we will fail trying to create the file, TODO log warning/error - - copy(source, destination); - } - - public static void copy(File source, File destination) throws IOException { - org.eclipse.pde.internal.publishing.Utils.copy(source, destination); + } + Path destination = Path.of(dest); + Files.createDirectories(destination.getParent()); + Files.copy(source, destination); } public static void writeProperties(Properties properites, File outputFile, String comment) throws IOException { diff --git a/build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/site/BuildTimeSiteContentProvider.java b/build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/site/BuildTimeSiteContentProvider.java index 5d39c7c11e..a5e14b9225 100644 --- a/build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/site/BuildTimeSiteContentProvider.java +++ b/build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/site/BuildTimeSiteContentProvider.java @@ -54,7 +54,7 @@ public String getInstalledBaseURL() { public Collection getPluginPaths() { Collection pluginsToCompile = findPluginXML(files); if (installedBaseURL != null) { - pluginsToCompile.addAll(Arrays.asList(PluginPathFinder.getPluginPaths(installedBaseURL, filterP2Base))); + pluginsToCompile.addAll(PluginPathFinder.getPluginPaths(installedBaseURL, filterP2Base)); } return pluginsToCompile; } diff --git a/build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/site/BuildTimeSiteFactory.java b/build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/site/BuildTimeSiteFactory.java index 9ec80fbe5e..a12a6f0c53 100644 --- a/build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/site/BuildTimeSiteFactory.java +++ b/build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/site/BuildTimeSiteFactory.java @@ -87,7 +87,7 @@ public BuildTimeSite createSite() throws CoreException { featureXMLs.addAll(installedFeatures); // extract features from platform.xml - File[] featureDirectories = PluginPathFinder.getFeaturePaths(installedBaseURL); + List featureDirectories = PluginPathFinder.getFeaturePaths(installedBaseURL); for (File element : featureDirectories) { File featureXML = new File(element, Constants.FEATURE_FILENAME_DESCRIPTOR); if (featureXML.exists()) diff --git a/build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/site/P2Utils.java b/build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/site/P2Utils.java index 99591e28ef..5cba1529e7 100644 --- a/build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/site/P2Utils.java +++ b/build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/site/P2Utils.java @@ -16,10 +16,8 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; -import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; -import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -45,7 +43,7 @@ public class P2Utils { /** * Returns bundles defined by the 'bundles.info' file in the - * specified location, or null if none. The "bundles.info" file + * specified location, or an empty list if none. The "bundles.info" file * is assumed to be at a fixed relative location to the specified file. This * method will also look for a "source.info". If available, any source * bundles found will also be added to the returned list. @@ -54,7 +52,7 @@ public class P2Utils { * @return URLs of all bundles in the installation or null if not able * to locate a bundles.info */ - public static URL[] readBundlesTxt(String platformHome) { + public static List readBundlesTxt(String platformHome) { SimpleConfiguratorManipulator manipulator = BundleHelper.getDefault().acquireService(SimpleConfiguratorManipulator.class); File root = new File(platformHome); @@ -72,22 +70,7 @@ public static URL[] readBundlesTxt(String platformHome) { // TODO Auto-generated catch block e.printStackTrace(); } - - URL[] bundles = null; - if (infos.size() > 0) { - bundles = new URL[infos.size()]; - for (int i = 0; i < bundles.length; i++) { - try { - bundles[i] = new File(infos.get(i).getLocation()).toURL(); - } catch (MalformedURLException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - } else { - bundles = new URL[0]; - } - return bundles; + return infos.stream().map(BundleInfo::getLocation).map(File::new).toList(); } /** diff --git a/build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/site/PluginPathFinder.java b/build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/site/PluginPathFinder.java index 5d98d6c438..429931bc1c 100644 --- a/build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/site/PluginPathFinder.java +++ b/build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/site/PluginPathFinder.java @@ -16,7 +16,6 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; -import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -25,7 +24,6 @@ import org.eclipse.core.runtime.IPath; import org.eclipse.pde.internal.build.IPDEBuildConstants; -import org.eclipse.pde.internal.build.Utils; public class PluginPathFinder { private static final String DROPINS = "dropins"; //$NON-NLS-1$ @@ -66,8 +64,8 @@ private static String getSitePath(String platformHome, File linkFile, boolean fe * @param features false for plugin sites, true for feature sites * @return array of ".../plugins" or ".../features" Files */ - private static File[] getSites(String platformHome, boolean features) { - ArrayList sites = new ArrayList<>(); + private static List getSites(String platformHome, boolean features) { + List sites = new ArrayList<>(); File file = new File(platformHome, features ? IPDEBuildConstants.DEFAULT_FEATURE_LOCATION : IPDEBuildConstants.DEFAULT_PLUGIN_LOCATION); if (!features && !file.exists()) @@ -84,7 +82,7 @@ private static File[] getSites(String platformHome, boolean features) { } } } - return sites.toArray(new File[sites.size()]); + return sites; } private static List getDropins(String platformHome, boolean features) { @@ -132,41 +130,37 @@ private static List getDropins(String platformHome, boolean features) { } } - results.addAll(scanLocations(sites.toArray(new File[sites.size()]))); + results.addAll(scanLocations(sites)); return results; } - public static File[] getFeaturePaths(String platformHome) { + public static List getFeaturePaths(String platformHome) { return getPaths(platformHome, true, false); } - public static File[] getPluginPaths(String platformHome, boolean filterP2Base) { + public static List getPluginPaths(String platformHome, boolean filterP2Base) { return getPaths(platformHome, false, filterP2Base); } - public static File[] getPluginPaths(String platformHome) { - return getPaths(platformHome, false, false); - } - - public static File[] getPaths(String platformHome, boolean features, boolean filterP2Base) { + public static List getPaths(String platformHome, boolean features, boolean filterP2Base) { if (filterP2Base) { - URL[] urls = P2Utils.readBundlesTxt(platformHome); - if (urls != null && urls.length > 0) { - return Utils.asFile(urls); + List files = P2Utils.readBundlesTxt(platformHome); + if (files != null && !files.isEmpty()) { + return files; } } List list = scanLocations(getSites(platformHome, features)); list.addAll(getDropins(platformHome, features)); - return Utils.asFile(list); + return list; } /** * Scan given plugin/feature directories or jars for existence * @return URLs to plugins/features */ - private static List scanLocations(File[] sites) { + private static List scanLocations(List sites) { ArrayList result = new ArrayList<>(); for (File site : sites) { if (site == null || !site.exists())