Skip to content

Commit a96014b

Browse files
HannesWellfedejeanne
authored andcommitted
[PDE-Build] Use File instead of URL to represent local files
This avoids unnecessary conversations that also use deprecated methods like File.toURL()
1 parent 324cc4d commit a96014b

File tree

5 files changed

+23
-120
lines changed

5 files changed

+23
-120
lines changed

build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/Utils.java

Lines changed: 6 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
import java.io.IOException;
2121
import java.io.InputStream;
2222
import java.io.OutputStream;
23-
import java.net.MalformedURLException;
24-
import java.net.URL;
2523
import java.nio.file.Files;
2624
import java.nio.file.Path;
2725
import java.nio.file.StandardCopyOption;
@@ -257,72 +255,6 @@ public static String[] getArrayFromString(String list) {
257255
return getArrayFromString(list, ","); //$NON-NLS-1$
258256
}
259257

260-
/**
261-
* Converts an array of strings into an array of URLs.
262-
*
263-
* @return URL[]
264-
*/
265-
public static URL[] asURL(String[] target) throws CoreException {
266-
if (target == null)
267-
return null;
268-
try {
269-
URL[] result = new URL[target.length];
270-
for (int i = 0; i < target.length; i++)
271-
result[i] = new URL(target[i]);
272-
return result;
273-
} catch (MalformedURLException e) {
274-
throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_MALFORMED_URL, e.getMessage(), e));
275-
}
276-
}
277-
278-
public static URL[] asURL(Collection<File> target) throws CoreException {
279-
if (target == null)
280-
return null;
281-
try {
282-
URL[] result = new URL[target.size()];
283-
int i = 0;
284-
for (File file : target) {
285-
result[i++] = file.toURL();
286-
}
287-
return result;
288-
} catch (MalformedURLException e) {
289-
throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_MALFORMED_URL, e.getMessage(), e));
290-
}
291-
}
292-
293-
public static File[] asFile(String[] target) {
294-
if (target == null)
295-
return new File[0];
296-
File[] result = new File[target.length];
297-
for (int i = 0; i < result.length; i++) {
298-
result[i] = new File(target[i]);
299-
}
300-
return result;
301-
}
302-
303-
public static File[] asFile(URL[] target) {
304-
if (target == null)
305-
return new File[0];
306-
File[] result = new File[target.length];
307-
for (int i = 0; i < result.length; i++) {
308-
result[i] = new File(target[i].getFile());
309-
}
310-
return result;
311-
}
312-
313-
public static File[] asFile(Collection<?> collection) {
314-
if (collection.size() == 0)
315-
return new File[0];
316-
Object first = collection.iterator().next();
317-
if (first instanceof String)
318-
return asFile(collection.toArray(new String[collection.size()]));
319-
else if (first instanceof URL)
320-
return asFile(collection.toArray(new URL[collection.size()]));
321-
else if (first instanceof File)
322-
return collection.toArray(new File[collection.size()]);
323-
throw new IllegalArgumentException();
324-
}
325-
326258
/**
327259
* Return a string which is a concatination of each member of the given
328260
* collection, separated by the given separator.
@@ -396,19 +328,13 @@ public static IPath makeRelative(IPath location, IPath base) {
396328
}
397329

398330
static public void copyFile(String src, String dest) throws IOException {
399-
File source = new File(src);
400-
if (!source.exists())
331+
Path source = Path.of(src);
332+
if (!Files.isRegularFile(source)) {
401333
return;
402-
File destination = new File(dest);
403-
File destDir = destination.getParentFile();
404-
if ((!destDir.exists() && !destDir.mkdirs()) || destDir.isFile())
405-
return; //we will fail trying to create the file, TODO log warning/error
406-
407-
copy(source, destination);
408-
}
409-
410-
public static void copy(File source, File destination) throws IOException {
411-
org.eclipse.pde.internal.publishing.Utils.copy(source, destination);
334+
}
335+
Path destination = Path.of(dest);
336+
Files.createDirectories(destination.getParent());
337+
Files.copy(source, destination);
412338
}
413339

414340
public static void writeProperties(Properties properites, File outputFile, String comment) throws IOException {

build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/site/BuildTimeSiteContentProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public String getInstalledBaseURL() {
5454
public Collection<File> getPluginPaths() {
5555
Collection<File> pluginsToCompile = findPluginXML(files);
5656
if (installedBaseURL != null) {
57-
pluginsToCompile.addAll(Arrays.asList(PluginPathFinder.getPluginPaths(installedBaseURL, filterP2Base)));
57+
pluginsToCompile.addAll(PluginPathFinder.getPluginPaths(installedBaseURL, filterP2Base));
5858
}
5959
return pluginsToCompile;
6060
}

build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/site/BuildTimeSiteFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public BuildTimeSite createSite() throws CoreException {
8787
featureXMLs.addAll(installedFeatures);
8888

8989
// extract features from platform.xml
90-
File[] featureDirectories = PluginPathFinder.getFeaturePaths(installedBaseURL);
90+
List<File> featureDirectories = PluginPathFinder.getFeaturePaths(installedBaseURL);
9191
for (File element : featureDirectories) {
9292
File featureXML = new File(element, Constants.FEATURE_FILENAME_DESCRIPTOR);
9393
if (featureXML.exists())

build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/site/P2Utils.java

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616
import java.io.File;
1717
import java.io.FileInputStream;
1818
import java.io.IOException;
19-
import java.net.MalformedURLException;
2019
import java.net.URI;
2120
import java.net.URISyntaxException;
22-
import java.net.URL;
2321
import java.util.ArrayList;
2422
import java.util.Arrays;
2523
import java.util.Collection;
@@ -45,7 +43,7 @@ public class P2Utils {
4543

4644
/**
4745
* Returns bundles defined by the 'bundles.info' file in the
48-
* specified location, or <code>null</code> if none. The "bundles.info" file
46+
* specified location, or an empty list if none. The "bundles.info" file
4947
* is assumed to be at a fixed relative location to the specified file. This
5048
* method will also look for a "source.info". If available, any source
5149
* bundles found will also be added to the returned list.
@@ -54,7 +52,7 @@ public class P2Utils {
5452
* @return URLs of all bundles in the installation or <code>null</code> if not able
5553
* to locate a bundles.info
5654
*/
57-
public static URL[] readBundlesTxt(String platformHome) {
55+
public static List<File> readBundlesTxt(String platformHome) {
5856
SimpleConfiguratorManipulator manipulator = BundleHelper.getDefault().acquireService(SimpleConfiguratorManipulator.class);
5957

6058
File root = new File(platformHome);
@@ -72,22 +70,7 @@ public static URL[] readBundlesTxt(String platformHome) {
7270
// TODO Auto-generated catch block
7371
e.printStackTrace();
7472
}
75-
76-
URL[] bundles = null;
77-
if (infos.size() > 0) {
78-
bundles = new URL[infos.size()];
79-
for (int i = 0; i < bundles.length; i++) {
80-
try {
81-
bundles[i] = new File(infos.get(i).getLocation()).toURL();
82-
} catch (MalformedURLException e) {
83-
// TODO Auto-generated catch block
84-
e.printStackTrace();
85-
}
86-
}
87-
} else {
88-
bundles = new URL[0];
89-
}
90-
return bundles;
73+
return infos.stream().map(BundleInfo::getLocation).map(File::new).toList();
9174
}
9275

9376
/**

build/org.eclipse.pde.build/src/org/eclipse/pde/internal/build/site/PluginPathFinder.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import java.io.File;
1717
import java.io.FileInputStream;
1818
import java.io.IOException;
19-
import java.net.URL;
2019
import java.util.ArrayList;
2120
import java.util.Arrays;
2221
import java.util.Collections;
@@ -25,7 +24,6 @@
2524

2625
import org.eclipse.core.runtime.IPath;
2726
import org.eclipse.pde.internal.build.IPDEBuildConstants;
28-
import org.eclipse.pde.internal.build.Utils;
2927

3028
public class PluginPathFinder {
3129
private static final String DROPINS = "dropins"; //$NON-NLS-1$
@@ -66,8 +64,8 @@ private static String getSitePath(String platformHome, File linkFile, boolean fe
6664
* @param features false for plugin sites, true for feature sites
6765
* @return array of ".../plugins" or ".../features" Files
6866
*/
69-
private static File[] getSites(String platformHome, boolean features) {
70-
ArrayList<File> sites = new ArrayList<>();
67+
private static List<File> getSites(String platformHome, boolean features) {
68+
List<File> sites = new ArrayList<>();
7169

7270
File file = new File(platformHome, features ? IPDEBuildConstants.DEFAULT_FEATURE_LOCATION : IPDEBuildConstants.DEFAULT_PLUGIN_LOCATION);
7371
if (!features && !file.exists())
@@ -84,7 +82,7 @@ private static File[] getSites(String platformHome, boolean features) {
8482
}
8583
}
8684
}
87-
return sites.toArray(new File[sites.size()]);
85+
return sites;
8886
}
8987

9088
private static List<File> getDropins(String platformHome, boolean features) {
@@ -132,41 +130,37 @@ private static List<File> getDropins(String platformHome, boolean features) {
132130
}
133131
}
134132

135-
results.addAll(scanLocations(sites.toArray(new File[sites.size()])));
133+
results.addAll(scanLocations(sites));
136134
return results;
137135
}
138136

139-
public static File[] getFeaturePaths(String platformHome) {
137+
public static List<File> getFeaturePaths(String platformHome) {
140138
return getPaths(platformHome, true, false);
141139
}
142140

143-
public static File[] getPluginPaths(String platformHome, boolean filterP2Base) {
141+
public static List<File> getPluginPaths(String platformHome, boolean filterP2Base) {
144142
return getPaths(platformHome, false, filterP2Base);
145143
}
146144

147-
public static File[] getPluginPaths(String platformHome) {
148-
return getPaths(platformHome, false, false);
149-
}
150-
151-
public static File[] getPaths(String platformHome, boolean features, boolean filterP2Base) {
145+
public static List<File> getPaths(String platformHome, boolean features, boolean filterP2Base) {
152146

153147
if (filterP2Base) {
154-
URL[] urls = P2Utils.readBundlesTxt(platformHome);
155-
if (urls != null && urls.length > 0) {
156-
return Utils.asFile(urls);
148+
List<File> files = P2Utils.readBundlesTxt(platformHome);
149+
if (files != null && !files.isEmpty()) {
150+
return files;
157151
}
158152
}
159153

160154
List<File> list = scanLocations(getSites(platformHome, features));
161155
list.addAll(getDropins(platformHome, features));
162-
return Utils.asFile(list);
156+
return list;
163157
}
164158

165159
/**
166160
* Scan given plugin/feature directories or jars for existence
167161
* @return URLs to plugins/features
168162
*/
169-
private static List<File> scanLocations(File[] sites) {
163+
private static List<File> scanLocations(List<File> sites) {
170164
ArrayList<File> result = new ArrayList<>();
171165
for (File site : sites) {
172166
if (site == null || !site.exists())

0 commit comments

Comments
 (0)