Skip to content

Commit

Permalink
Access Javac API from a standalone Javac jar insteadof the JDK
Browse files Browse the repository at this point in the history
  • Loading branch information
testforstephen committed Nov 19, 2024
1 parent 96196bf commit ea7bd25
Show file tree
Hide file tree
Showing 14 changed files with 230 additions and 172 deletions.
6 changes: 4 additions & 2 deletions org.eclipse.jdt.core.javac/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
<classpath>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-23">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-21">
<attributes>
<attribute name="module" value="true"/>
<attribute name="add-exports" value="jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED:jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED:jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED:jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED:jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED:jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED:jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED:jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED:jdk.javadoc/jdk.javadoc.internal.doclets.formats.html.taglets.snippet=ALL-UNNAMED:jdk.javadoc/jdk.javadoc.internal.doclets.formats.html.taglets=ALL-UNNAMED:jdk.compiler/com.sun.tools.javac.platform=ALL-UNNAMED:jdk.compiler/com.sun.tools.javac.resources=ALL-UNNAMED"/>
<attribute name="limit-modules" value="java.base"/>
</attributes>
</classpathentry>
<classpathentry exported="true" kind="lib" path="lib/nb-javac-jdk-23+35.jar"/>
<classpathentry exported="true" kind="lib" path="lib/nb-javac-jdk-23+35-api.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=23
org.eclipse.jdt.core.compiler.codegen.targetPlatform=21
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=23
org.eclipse.jdt.core.compiler.compliance=21
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
Expand All @@ -12,7 +12,7 @@ org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=23
org.eclipse.jdt.core.compiler.source=21
org.eclipse.jdt.core.formatter.align_arrows_in_switch_on_columns=false
org.eclipse.jdt.core.formatter.align_assignment_statements_on_columns=false
org.eclipse.jdt.core.formatter.align_fields_grouping_blank_lines=2147483647
Expand Down
9 changes: 7 additions & 2 deletions org.eclipse.jdt.core.javac/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ Bundle-SymbolicName: org.eclipse.jdt.core.javac;singleton:=true
Bundle-Version: 1.0.0.qualifier
Fragment-Host: org.eclipse.jdt.core
Automatic-Module-Name: org.eclipse.jdt.core.javac
Require-Capability: osgi.ee; filter:="(&(osgi.ee=JavaSE)(version=23))"
Import-Package: org.eclipse.jdt.core.dom
Bundle-RequiredExecutionEnvironment: JavaSE-21
Import-Package: org.eclipse.core.resources,
org.eclipse.core.runtime,
org.eclipse.jdt.core.dom
Export-Package: org.eclipse.jdt.internal.javac;x-friends:="org.eclipse.jdt.core.tests.javac",
org.eclipse.jdt.internal.javac.dom;x-friends:="org.eclipse.jdt.core.tests.javac"
Bundle-ClassPath: lib/nb-javac-jdk-23+35-api.jar,
lib/nb-javac-jdk-23+35.jar,
.
61 changes: 1 addition & 60 deletions org.eclipse.jdt.core.javac/META-INF/p2.inf
Original file line number Diff line number Diff line change
@@ -1,72 +1,13 @@
instructions.configure=\
org.eclipse.equinox.p2.touchpoint.eclipse.addJvmArg(jvmArg:\
--add-opens\n\
jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED\n\
--add-opens\n\
jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED\n\
--add-opens\n\
jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED\n\
--add-opens\n\
jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED\n\
--add-opens\n\
jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED\n\
--add-opens\n\
jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED\n\
--add-opens\n\
jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED\n\
--add-opens\n\
jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED\n\
--add-opens\n\
jdk.javadoc/jdk.javadoc.internal.doclets.formats.html.taglets.snippet=ALL-UNNAMED\n\
--add-opens\n\
jdk.javadoc/jdk.javadoc.internal.doclets.formats.html.taglets=ALL-UNNAMED\n\
--add-opens\n\
jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED\n\
--add-opens\n\
jdk.compiler/com.sun.tools.javac.jvm=ALL-UNNAMED\n\
--add-opens\n\
jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED\n\
--add-opens\n\
jdk.compiler/com.sun.tools.javac.platform=ALL-UNNAMED\n\
--add-opens\n\
jdk.compiler/com.sun.tools.javac.resources=ALL-UNNAMED\n\
-DICompilationUnitResolver=org.eclipse.jdt.core.dom.JavacCompilationUnitResolver\n\
-DAbstractImageBuilder.compilerFactory=org.eclipse.jdt.internal.javac.JavacCompilerFactory\n\
-DCompilationUnit.DOM_BASED_OPERATIONS=true\n\
-DCompilationUnit.codeComplete.DOM_BASED_OPERATIONS=true_\n\
-DSourceIndexer.DOM_BASED_INDEXER=true);\

instructions.unconfigure=\
org.eclipse.equinox.p2.touchpoint.eclipse.removeJvmArg(jvmArg:--add-opens\n\
jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED\n\
--add-opens\n\
jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED\n\
--add-opens\n\
jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED\n\
--add-opens\n\
jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED\n\
--add-opens\n\
jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED\n\
--add-opens\n\
jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED\n\
--add-opens\n\
jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED\n\
--add-opens\n\
jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED\n\
--add-opens\n\
jdk.javadoc/jdk.javadoc.internal.doclets.formats.html.taglets.snippet=ALL-UNNAMED\n\
--add-opens\n\
jdk.javadoc/jdk.javadoc.internal.doclets.formats.html.taglets=ALL-UNNAMED\n\
--add-opens\n\
jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED\n\
--add-opens\n\
jdk.compiler/com.sun.tools.javac.jvm=ALL-UNNAMED\n\
--add-opens\n\
jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED\n\
--add-opens\n\
jdk.compiler/com.sun.tools.javac.platform=ALL-UNNAMED\n\
--add-opens\n\
jdk.compiler/com.sun.tools.javac.resources=ALL-UNNAMED\n\
org.eclipse.equinox.p2.touchpoint.eclipse.removeJvmArg(jvmArg:\
-DICompilationUnitResolver=org.eclipse.jdt.core.dom.JavacCompilationUnitResolver\n\
-DAbstractImageBuilder.compilerFactory=org.eclipse.jdt.internal.javac.JavacCompilerFactory\n\
-DCompilationUnit.DOM_BASED_OPERATIONS=true\n\
Expand Down
4 changes: 3 additions & 1 deletion org.eclipse.jdt.core.javac/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.,\
fragment.xml
fragment.xml,\
lib/nb-javac-jdk-23+35.jar,\
lib/nb-javac-jdk-23+35-api.jar
85 changes: 53 additions & 32 deletions org.eclipse.jdt.core.javac/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,62 @@
<build>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-compiler-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<configuration>
<deriveReleaseCompilerArgumentFromTargetLevel>false</deriveReleaseCompilerArgumentFromTargetLevel>
<compilerId>javac</compilerId>
<compilerArgs combine.self="override">
<arg>--add-exports</arg>
<arg>java.base/java.lang=ALL-UNNAMED</arg>
<arg>--add-exports</arg>
<arg>jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED</arg>
<arg>--add-exports</arg>
<arg>jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED</arg>
<arg>--add-exports</arg>
<arg>jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED</arg>
<arg>--add-exports</arg>
<arg>jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED</arg>
<arg>--add-exports</arg>
<arg>jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED</arg>
<arg>--add-exports</arg>
<arg>jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED</arg>
<arg>--add-exports</arg>
<arg>jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED</arg>
<arg>--add-exports</arg>
<arg>jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED</arg>
<arg>--add-exports</arg>
<arg>jdk.compiler/com.sun.tools.javac.platform=ALL-UNNAMED</arg>
<arg>--add-exports</arg>
<arg>jdk.compiler/com.sun.tools.javac.resources=ALL-UNNAMED</arg>
<arg>--add-exports</arg>
<arg>jdk.javadoc/jdk.javadoc.internal.doclets.formats.html.taglets.snippet=ALL-UNNAMED</arg>
<arg>--add-exports</arg>
<arg>jdk.javadoc/jdk.javadoc.internal.doclets.formats.html.taglets=ALL-UNNAMED</arg>
</compilerArgs>
<artifactItems>
<artifactItem>
<groupId>com.dukescript.nbjavac</groupId>
<artifactId>nb-javac</artifactId>
<version>jdk-23+35</version>
</artifactItem>
<artifactItem>
<groupId>com.dukescript.nbjavac</groupId>
<artifactId>nb-javac</artifactId>
<version>jdk-23+35</version>
<classifier>api</classifier>
</artifactItem>
</artifactItems>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.8.1</version>
<executions>
<execution>
<?m2e execute onConfiguration onIncremental?>
<id>get-libs</id>
<goals>
<goal>copy</goal>
</goals>
<phase>validate</phase>
</execution>
</executions>
<configuration>
<skip>false</skip>
<outputDirectory>${basedir}/lib/</outputDirectory>
<!-- baseVersion is to avoid SNAPSHOT dependencies being copied with
ever daily changing timestamp -->
</configuration>
</plugin>
<plugin>
<!-- make sure lib dir is removed after clean to avoid "dirty" build -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>3.4.0</version>
<configuration>
<filesets>
<fileset>
<directory>${basedir}/lib</directory>
</fileset>
</filesets>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,8 @@ private Map<org.eclipse.jdt.internal.compiler.env.ICompilationUnit, CompilationU
if (sourceUnits.length == 0) {
return Collections.emptyMap();
}

JavacPatch.loadVMWrapperPatch();
var compiler = ToolProvider.getSystemJavaCompiler();
Context context = new Context();
CachingJarsJavaFileManager.preRegister(context);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*******************************************************************************
* 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 {
private static Reference<Path> cachedCtSym = new SoftReference<>(null);

public static void loadVMWrapperPatch() {
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 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);
}
}
}
Loading

0 comments on commit ea7bd25

Please sign in to comment.