forked from eclipse-jdt/eclipse.jdt.core
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Patch the ctsym loading error in nbjavac.VMWrapper
- Loading branch information
1 parent
e273f67
commit 86f68f9
Showing
1 changed file
with
80 additions
and
0 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
org.eclipse.jdt.core.javac/src/org/eclipse/jdt/core/dom/JavacPatch.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Microsoft Corporation and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Microsoft Corporation - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
package org.eclipse.jdt.core.dom; | ||
|
||
import java.io.IOException; | ||
import java.lang.ref.Reference; | ||
import java.lang.ref.SoftReference; | ||
import java.lang.reflect.Field; | ||
import java.net.URISyntaxException; | ||
import java.net.URL; | ||
import java.nio.file.FileSystem; | ||
import java.nio.file.FileSystems; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Enumeration; | ||
|
||
import org.eclipse.core.runtime.FileLocator; | ||
import org.eclipse.core.runtime.URIUtil; | ||
|
||
import nbjavac.VMWrapper; | ||
|
||
public class JavacPatch { | ||
|
||
public static void patchVMWrapper() { | ||
try { | ||
Field cachedCtSymField = VMWrapper.class.getDeclaredField("cachedCtSym"); | ||
cachedCtSymField.setAccessible(true); | ||
if (cachedCtSym.get() == null) { | ||
cachedCtSym = new SoftReference<>(findCtSym()); | ||
} | ||
cachedCtSymField.set(null, cachedCtSym); | ||
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
private static Reference<Path> cachedCtSym = new SoftReference<>(null); | ||
|
||
public static Path findCtSym() { | ||
Path obj = cachedCtSym.get(); | ||
if (obj instanceof Path) { | ||
return obj; | ||
} | ||
try { | ||
ClassLoader loader = VMWrapper.class.getClassLoader(); | ||
if (loader == null) { | ||
loader = ClassLoader.getSystemClassLoader(); | ||
} | ||
Enumeration<URL> en = loader.getResources("META-INF/services/com.sun.tools.javac.platform.PlatformProvider"); | ||
URL res = en.hasMoreElements() ? en.nextElement() : null; | ||
if (res == null) { | ||
throw new IllegalStateException("Cannot find ct.sym"); | ||
} | ||
URL jar = FileLocator.resolve(res); | ||
String jarFile = jar.getFile(); | ||
int idx = jarFile.indexOf('!'); | ||
if (idx >= 0) { | ||
jarFile = jarFile.substring(0, idx); | ||
} | ||
Path path = Paths.get(URIUtil.fromString(jarFile)); | ||
FileSystem fs = FileSystems.newFileSystem(path, (ClassLoader) null); | ||
Path ctSym = fs.getPath("META-INF", "ct.sym"); | ||
cachedCtSym = new SoftReference<>(ctSym); | ||
return ctSym; | ||
} catch (IOException | URISyntaxException ex) { | ||
throw new IllegalStateException(ex); | ||
} | ||
} | ||
} |