Skip to content

Commit

Permalink
Access Javac API from a standalone javac jar instead of the JDK
Browse files Browse the repository at this point in the history
  • Loading branch information
testforstephen committed Nov 5, 2024
1 parent 7491ef5 commit 2a3c437
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 109 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-17">
<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"/>
<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>
5 changes: 4 additions & 1 deletion org.eclipse.jdt.core.javac/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ 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))"
Require-Capability: osgi.ee; filter:="(&(osgi.ee=JavaSE)(version=17))"
Import-Package: 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.jar,
lib/nb-javac-jdk-23+35-api.jar,
.
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
81 changes: 53 additions & 28 deletions org.eclipse.jdt.core.javac/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +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.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 @@ -1346,7 +1346,7 @@ private IMethodBinding resolveConstructorImpl(ClassInstanceCreation expression)
boolean hasTrailingNull;
boolean matchExactParamCount = false;
do {
hasTrailingNull = !givenTypes.isEmpty() && givenTypes.getLast() == null;
hasTrailingNull = !givenTypes.isEmpty() && JavacConverter.getLast(givenTypes) == null;
// try just checking by known args
// first filter by args count
var matchExactParamCountFinal = matchExactParamCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
import java.util.OptionalInt;
Expand Down Expand Up @@ -1681,7 +1682,7 @@ private Expression convertExpressionImpl(JCExpression javac) {
if (arrayType.dimensions().isEmpty()) {
arrayType.dimensions().addAll(extraDimensions);
} else {
var lastDimension = arrayType.dimensions().removeFirst();
var lastDimension = removeFirst(arrayType.dimensions());
arrayType.dimensions().addAll(extraDimensions);
arrayType.dimensions().add(lastDimension);
}
Expand Down Expand Up @@ -1724,7 +1725,7 @@ private Expression convertExpressionImpl(JCExpression javac) {
for( int i = 0; i < dims - 1; i++ ) {
if( this.ast.apiLevel >= AST.JLS8_INTERNAL) {
// TODO, this dimension needs source range
arrayType.dimensions().addFirst(this.ast.newDimension());
addFirst(arrayType.dimensions(), this.ast.newDimension());
} else {
// JLS < 8, wrap underlying
arrayType = this.ast.newArrayType(arrayType);
Expand Down Expand Up @@ -2000,11 +2001,11 @@ private ArrayInitializer createArrayInitializerFromJCNewArray(JCNewArray jcNewAr
if (!jcNewArray.getInitializers().isEmpty()) {
jcNewArray.getInitializers().stream().map(this::convertExpression).filter(Objects::nonNull).forEach(initializer.expressions()::add);
this.rawText.charAt(0);
int start = ((Expression)initializer.expressions().getFirst()).getStartPosition() - 1;
int start = ((Expression) getFirst(initializer.expressions())).getStartPosition() - 1;
while (start >= 0 && this.rawText.charAt(start) != '{') {
start--;
}
Expression lastExpr = (Expression)initializer.expressions().getLast();
Expression lastExpr = (Expression) getLast(initializer.expressions());
int end = lastExpr.getStartPosition() + lastExpr.getLength() + 1;
while (end < this.rawText.length() && this.rawText.charAt(end) != '}') {
end++;
Expand Down Expand Up @@ -2956,7 +2957,7 @@ Type convertToType(JCTree javac) {
ArrayType res;
if (t instanceof ArrayType childArrayType && this.ast.apiLevel > AST.JLS4_INTERNAL) {
res = childArrayType;
res.dimensions().addFirst(this.ast.newDimension());
addFirst(res.dimensions(), this.ast.newDimension());
commonSettings(res, jcArrayType.getType());
} else {
int dims = countDimensions(jcArrayType);
Expand Down Expand Up @@ -3359,7 +3360,7 @@ private int modifierToFlagVal(javax.lang.model.element.Modifier javac) {
private Modifier convert(javax.lang.model.element.Modifier javac, int startPos, int endPos) {
Modifier res = modifierToDom(javac);
if (startPos >= 0 && endPos >= startPos && endPos <= this.rawText.length()) {
int indOf = this.rawText.indexOf(res.getKeyword().toString(), startPos, endPos);
int indOf = indexOf(this.rawText, res.getKeyword().toString(), startPos, endPos);
if( indOf != -1 ) {
res.setSourceRange(indOf, res.getKeyword().toString().length());
}
Expand Down Expand Up @@ -3531,7 +3532,7 @@ private int findPositionOfText(String text, ASTNode in, List<ASTNode> excluding)
return -1;
}
if (excluded.isEmpty()) {
int position = this.contents.indexOf(text, current, current + in.getLength());
int position = indexOf(this.contents, text, current, current + in.getLength());
if (position >= 0) {
return position;
}
Expand All @@ -3540,7 +3541,7 @@ private int findPositionOfText(String text, ASTNode in, List<ASTNode> excluding)
while ((currentExclusion = excluded.poll()) != null) {
if (currentExclusion.getStartPosition() >= current) {
int rangeEnd = currentExclusion.getStartPosition();
int position = this.contents.indexOf(text, current, rangeEnd);
int position = indexOf(this.contents, text, current, rangeEnd);
if (position >= 0) {
return position;
}
Expand Down Expand Up @@ -3654,5 +3655,44 @@ public DocTreePath[] searchRelatedDocTreePath(MethodRef ref) {
return r;
}

public static int indexOf(String original, String str, int fromIndex, int endIndex) {
int idx = original.indexOf(str, fromIndex);
if (idx < 0) {
return idx;
}

if (idx + str.length() > endIndex) {
return -1;
}

return idx;
}

public static <T> void addFirst(List<T> original, T element) {
original.add(0, element);
}

public static <T> T removeFirst(List<T> list) {
if (list.isEmpty()) {
throw new NoSuchElementException();
} else {
return list.remove(0);
}
}

public static <T> T getFirst(List<T> original) {
if (original.isEmpty()) {
throw new NoSuchElementException();
} else {
return original.get(0);
}
}

public static <T> T getLast(List<T> original) {
if (original.isEmpty()) {
throw new NoSuchElementException();
} else {
return original.get(original.size() - 1);
}
}
}
Loading

0 comments on commit 2a3c437

Please sign in to comment.