Skip to content

Commit

Permalink
fix HandleFactory.packageHandles reuse
Browse files Browse the repository at this point in the history
With resourcePath like "lib\jrt-fs.jar|java.base|com/sun/Class.class"
the packageHandles cache was never reused since the length of the
lastPkgFragmentRootPath and jarPath never matched.

With this change PackageFragment instances are reused as long as package
does not change
  • Loading branch information
EcljpseB0T authored and jukzi committed Jan 31, 2024
1 parent 4dca3bb commit 64f269a
Showing 1 changed file with 10 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
*******************************************************************************/
package org.eclipse.jdt.internal.core.util;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Objects;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
Expand Down Expand Up @@ -87,18 +89,14 @@ public Openable createOpenable(String resourcePath, IJavaSearchScope scope) {
if ((separatorIndex= resourcePath.indexOf(IJavaSearchScope.JAR_FILE_ENTRY_SEPARATOR)) > -1) {
// path to a class file inside a jar
// Optimization: cache package fragment root handle and package handles
int rootPathLength;
PackageFragmentRoot root = null;
if (this.lastPkgFragmentRootPath == null
|| (rootPathLength = this.lastPkgFragmentRootPath.length()) != resourcePath.length()
|| !resourcePath.regionMatches(0, this.lastPkgFragmentRootPath, 0, rootPathLength)) {
String jarPath= resourcePath.substring(0, separatorIndex);
root= getJarPkgFragmentRoot(resourcePath, separatorIndex, jarPath, scope);
String jarPath = resourcePath.substring(0, separatorIndex);
if (!Objects.equals(this.lastPkgFragmentRootPath, jarPath)) {
PackageFragmentRoot root = getJarPkgFragmentRoot(resourcePath, separatorIndex, jarPath, scope);
if (root == null)
return null; // match is outside classpath
this.lastPkgFragmentRootPath= jarPath;
this.lastPkgFragmentRoot= root;
this.packageHandles= new HashtableOfArrayToObject(5);
this.lastPkgFragmentRootPath = jarPath;
this.lastPkgFragmentRoot = root;
this.packageHandles = new HashtableOfArrayToObject(5);
}
// create handle
String module = null;
Expand All @@ -109,13 +107,12 @@ public Openable createOpenable(String resourcePath, IJavaSearchScope scope) {
}
String classFilePath= resourcePath.substring(separatorIndex + 1);
if (classFilePath.endsWith(TypeConstants.AUTOMATIC_MODULE_NAME))
return root;
return this.lastPkgFragmentRoot;
String[] simpleNames = new Path(classFilePath).segments();
String[] pkgName;
int length = simpleNames.length-1;
if (length > 0) {
pkgName = new String[length];
System.arraycopy(simpleNames, 0, pkgName, 0, length);
pkgName = Arrays.copyOf(simpleNames, length);
} else {
pkgName = CharOperation.NO_STRINGS;
}
Expand Down

0 comments on commit 64f269a

Please sign in to comment.