diff options
author | Carsten Hammer | 2021-01-01 11:50:16 +0000 |
---|---|---|
committer | Jay Arthanareeswaran | 2022-04-04 09:43:36 +0000 |
commit | 668dc1ad8dd4dcf5e69c83b9697df1b556fc20b5 (patch) | |
tree | 9012ba082dba4f2086e94a99051227525dc8eef6 | |
parent | a47425cbeecfd12f7e2a34a854245ab662311f27 (diff) | |
download | eclipse.jdt.core-668dc1ad8dd4dcf5e69c83b9697df1b556fc20b5.tar.gz eclipse.jdt.core-668dc1ad8dd4dcf5e69c83b9697df1b556fc20b5.tar.xz eclipse.jdt.core-668dc1ad8dd4dcf5e69c83b9697df1b556fc20b5.zip |
Bug 566657 - use java.net.URI instead of java.net.URL
Invocation of java.net.URL.equals(Object), which blocks to do domain
name resolution, in
org.eclipse.jdt.internal.core.search.indexing.IndexManager.computeIndexLocation(IPath,
URL)
The equals and hashCode method of URL perform domain name resolution,
this can result in a big performance hit. See
http://michaelscharf.blogspot.com/2006/11/javaneturlequals-and-hashcode-make.html
for more information. Consider using java.net.URI instead.
Rank: Of Concern (16), confidence: High
Pattern: DMI_BLOCKING_METHODS_ON_URL
Type: Dm, Category: PERFORMANCE (Performance)
Change-Id: I29936f8f7df460aeb4195df1cbdf8df72c01d6f6
Signed-off-by: Carsten Hammer <carsten.hammer@t-online.de>
Reviewed-on: https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/174169
Tested-by: JDT Bot <jdt-bot@eclipse.org>
Reviewed-by: Jeff Johnston <jjohnstn@redhat.com>
Reviewed-by: Jay Arthanareeswaran <jarthana@in.ibm.com>
4 files changed, 49 insertions, 12 deletions
diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/ClasspathChange.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/ClasspathChange.java index bca0333f88..89a39303bb 100644 --- a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/ClasspathChange.java +++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/ClasspathChange.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2016 IBM Corporation and others. + * Copyright (c) 2000, 2022 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -15,12 +15,14 @@ *******************************************************************************/ package org.eclipse.jdt.internal.core; +import java.net.URISyntaxException; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; +import java.util.Objects; import org.eclipse.core.resources.IFolder; import org.eclipse.core.resources.IResource; @@ -560,7 +562,11 @@ public class ClasspathChange { if (oldurl == null && newurl == null) { pathHasChanged = false; } else if (oldurl != null && newurl != null) { - pathHasChanged = !(newurl.equals(oldurl)); + try { + pathHasChanged = !Objects.equals(newurl.toURI(),oldurl.toURI()); + } catch (URISyntaxException e) { + // ignore + } } else if (oldurl != null) { indexManager.removeIndex(newPath); } diff --git a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/index/IndexLocation.java b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/index/IndexLocation.java index bcac151325..ca9226806b 100644 --- a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/index/IndexLocation.java +++ b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/index/IndexLocation.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011, 2013 IBM Corporation and others. + * Copyright (c) 2011, 2022 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -16,13 +16,14 @@ package org.eclipse.jdt.internal.core.index; import java.io.File; import java.io.IOException; import java.io.InputStream; - import java.net.MalformedURLException; import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; import org.eclipse.core.runtime.FileLocator; import org.eclipse.core.runtime.IPath; +import org.eclipse.jdt.internal.core.util.Util; /** * The location of the index files are represented as {@link IndexLocation} @@ -55,6 +56,7 @@ public abstract class IndexLocation { } private final URL url; // url of the given index location + private final URI uri; // uri of the given index location /** * Set to true if this index location is of an index file specified @@ -65,16 +67,28 @@ public abstract class IndexLocation { protected IndexLocation(File file) { URL tempUrl = null; + URI tempUri = null; try { - tempUrl = file.toURI().toURL(); + tempUri = file.toURI(); + tempUrl = tempUri.toURL(); } catch (MalformedURLException e) { // should not happen + Util.log(e, "Unexpected uri to url failure"); //$NON-NLS-1$ } this.url = tempUrl; + this.uri = tempUri; } public IndexLocation(URL url) { this.url = url; + URI tempUri = null; + try { + tempUri = url.toURI(); + } catch (URISyntaxException e) { + // should not happen + Util.log(e, "Unexpected url to uri failure"); //$NON-NLS-1$ + } + this.uri = tempUri; } /** @@ -110,9 +124,13 @@ public abstract class IndexLocation { return this.url; } + public URI getUri() { + return this.uri; + } + @Override public int hashCode() { - return this.url.hashCode(); + return this.uri != null ? this.uri.hashCode() : this.url.hashCode(); } public boolean isParticipantIndex() { @@ -133,6 +151,6 @@ public abstract class IndexLocation { @Override public String toString() { - return this.url.toString(); + return this.uri.toString(); } } diff --git a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/index/JarIndexLocation.java b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/index/JarIndexLocation.java index c2aa864951..3a11cf4625 100644 --- a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/index/JarIndexLocation.java +++ b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/index/JarIndexLocation.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011 IBM Corporation and others. + * Copyright (c) 2011, 2022 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -17,7 +17,9 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.JarURLConnection; +import java.net.URISyntaxException; import java.net.URL; +import java.util.Objects; import java.util.jar.JarEntry; import java.util.jar.JarFile; @@ -59,7 +61,11 @@ public class JarIndexLocation extends IndexLocation { @Override public boolean equals(Object other) { if (!(other instanceof JarIndexLocation)) return false; - return this.localUrl.equals(((JarIndexLocation) other).localUrl); + try { + return Objects.equals(this.localUrl.toURI(),((JarIndexLocation) other).localUrl.toURI()); + } catch (URISyntaxException e) { + return false; + } } @Override diff --git a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/indexing/IndexManager.java b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/indexing/IndexManager.java index 9e7a8f2bb7..c725686675 100644 --- a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/indexing/IndexManager.java +++ b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/indexing/IndexManager.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2016 IBM Corporation and others. + * Copyright (c) 2000, 2022 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -18,6 +18,7 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; +import java.net.URISyntaxException; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; @@ -28,6 +29,7 @@ import java.util.List; import java.util.ListIterator; import java.util.Locale; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; @@ -296,8 +298,13 @@ public synchronized IndexLocation computeIndexLocation(IPath containerPath, fina // an existing index location exists - make sure it has not changed (i.e. the URL has not changed) URL existingURL = indexLocation.getUrl(); if (newIndexURL != null) { - // if either URL is different then the index location has been updated so rebuild. - if(!newIndexURL.equals(existingURL)) { + boolean urisarequal = false; + try { + urisarequal = Objects.equals(newIndexURL.toURI(), existingURL.toURI()); + } catch (URISyntaxException e) { + // ignore missing RFC 2396 compliance + } + if(!urisarequal) { // URL has changed so remove the old index and create a new one this.removeIndex(containerPath); // create a new one |