diff options
author | Julian Honnen | 2021-02-01 12:10:56 +0000 |
---|---|---|
committer | Julian Honnen | 2021-02-03 15:17:54 +0000 |
commit | 77dbd9dac200ce426c5da4aea960129a04353bea (patch) | |
tree | b255d04483c70c5555b9d41b6bf70540687f3338 | |
parent | babeca48101828a5e0f0b65ec684699efb4bc8e5 (diff) | |
download | eclipse.jdt.core-77dbd9dac200ce426c5da4aea960129a04353bea.tar.gz eclipse.jdt.core-77dbd9dac200ce426c5da4aea960129a04353bea.tar.xz eclipse.jdt.core-77dbd9dac200ce426c5da4aea960129a04353bea.zip |
Bug 567661 - constructor should not leave ZipFile open
Lazily initialize supportedVersions to
a) avoid leaving a file handle open by simply invoking the constructor.
Since ClasspathJars are held in the
JavaModelManager.PerProjectInfo.savedState, that blocks writes to
those jars even when no build is currently running. This breaks VCS
updates trying to updated jars in the workspace.
b) reduce the initialization cost. If an instance doesn't ever access
the supportedVersions, the initialization is unnecessary.
Change-Id: I38a4e7d2bfde5ce49dc7ba520a759b883f5c13c1
Signed-off-by: Julian Honnen <julian.honnen@vector.com>
-rw-r--r-- | org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/builder/ClasspathMultiReleaseJar.java | 38 |
1 files changed, 17 insertions, 21 deletions
diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/builder/ClasspathMultiReleaseJar.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/builder/ClasspathMultiReleaseJar.java index b86abbd3e8..b118dec72a 100644 --- a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/builder/ClasspathMultiReleaseJar.java +++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/builder/ClasspathMultiReleaseJar.java @@ -27,20 +27,18 @@ import org.eclipse.jdt.internal.core.util.Util; public class ClasspathMultiReleaseJar extends ClasspathJar { private static final String META_INF_VERSIONS = "META-INF/versions/"; //$NON-NLS-1$ private static final int META_INF_LENGTH = META_INF_VERSIONS.length(); - String[] supportedVersions; + private volatile String[] supportedVersions; ClasspathMultiReleaseJar(IFile resource, AccessRuleSet accessRuleSet, IPath externalAnnotationPath, boolean isOnModulePath, String compliance) { super(resource, accessRuleSet, externalAnnotationPath, isOnModulePath); this.compliance = compliance; - initializeVersions(this); } ClasspathMultiReleaseJar(String zipFilename, long lastModified, AccessRuleSet accessRuleSet, IPath externalAnnotationPath, boolean isOnModulePath, String compliance) { super(zipFilename, lastModified, accessRuleSet, externalAnnotationPath, isOnModulePath); this.compliance = compliance; - initializeVersions(this); } public ClasspathMultiReleaseJar(ZipFile zipFile, AccessRuleSet accessRuleSet, IPath externalAnnotationPath, @@ -64,7 +62,7 @@ public class ClasspathMultiReleaseJar extends ClasspathJar { try (ZipFile file = new ZipFile(this.zipFilename)){ ClassFileReader classfile = null; try { - for (String path : this.supportedVersions) { + for (String path : supportedVersions(file)) { classfile = ClassFileReader.read(file, path.toString() + '/' + IModule.MODULE_INFO_CLASS); if (classfile != null) { break; @@ -87,30 +85,28 @@ public class ClasspathMultiReleaseJar extends ClasspathJar { return mod; } - private static synchronized void initializeVersions(ClasspathMultiReleaseJar jar) { - if (jar.zipFile == null) { - if (org.eclipse.jdt.internal.core.JavaModelManager.ZIP_ACCESS_VERBOSE) { - System.out.println("(" + Thread.currentThread() + ") [ClasspathMultiReleaseJar.initializeVersions(String)] Creating ZipFile on " + jar.zipFilename); //$NON-NLS-1$ //$NON-NLS-2$ - } - try { - jar.zipFile = new ZipFile(jar.zipFilename); - } catch (IOException e) { - return; - } - jar.closeZipFileAtEnd = true; - } + private static String[] initializeVersions(ZipFile zipFile, String compliance) { int earliestJavaVersion = ClassFileConstants.MAJOR_VERSION_9; - long latestJDK = CompilerOptions.versionToJdkLevel(jar.compliance); + long latestJDK = CompilerOptions.versionToJdkLevel(compliance); int latestJavaVer = (int) (latestJDK >> 16); List<String> versions = new ArrayList<>(); for (int i = latestJavaVer; i >= earliestJavaVersion; i--) { - String name = META_INF_VERSIONS+ (i - 44); - ZipEntry entry = jar.zipFile.getEntry(name); + String name = META_INF_VERSIONS + (i - 44); + ZipEntry entry = zipFile.getEntry(name); if (entry != null) { versions.add(name); } } - jar.supportedVersions = versions.toArray(new String[versions.size()]); + return versions.toArray(new String[versions.size()]); + } + + private String[] supportedVersions(ZipFile file) { + String[] versions = this.supportedVersions; + if (versions == null) { + versions = initializeVersions(file, this.compliance); + this.supportedVersions = versions; + } + return versions; } @Override @@ -142,7 +138,7 @@ public class ClasspathMultiReleaseJar extends ClasspathJar { if (!isPackage(qualifiedPackageName, moduleName)) { return null; // most common case } - for (String path : this.supportedVersions) { + for (String path : supportedVersions(this.zipFile)) { String s = null; try { s = META_INF_VERSIONS + path + "/" + binaryFileName; //$NON-NLS-1$ |