Skip to content

Commit

Permalink
Added workarounds for removed Compileroptions: originalSourceLevel and
Browse files Browse the repository at this point in the history
originalComplianceLevel
  • Loading branch information
mehmet-karaman committed Sep 26, 2024
1 parent 8ede3f2 commit ffc3b15
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,21 @@ protected CompilerOptions getCompilerOptions(JavaConfig javaConfig) {
compilerOptions.sourceLevel = sourceLevel;
compilerOptions.produceMethodParameters = true;
compilerOptions.produceReferenceInfo = true;
compilerOptions.originalSourceLevel = targetLevel;
compilerOptions.complianceLevel = sourceLevel;
compilerOptions.originalComplianceLevel = targetLevel;
if (ORIGINAL_SOURCE_LEVEL != null) {
try {
ORIGINAL_SOURCE_LEVEL.invoke(compilerOptions, targetLevel);
} catch (Throwable e) {
// ignore
}
}
if (ORIGINAL_COMPLIANCE_LEVEL != null) {
try {
ORIGINAL_COMPLIANCE_LEVEL.invoke(compilerOptions, targetLevel);
} catch (Throwable e) {
// ignore
}
}
if (INLINE_JSR_BYTECODE != null) {
try {
INLINE_JSR_BYTECODE.invoke(compilerOptions, true);
Expand All @@ -308,6 +320,24 @@ private static MethodHandle findInlineJsrBytecode() {
}
}

private final static MethodHandle ORIGINAL_SOURCE_LEVEL = findOriginalSourceLevel();
private static MethodHandle findOriginalSourceLevel() {
try {
return MethodHandles.lookup().findSetter(CompilerOptions.class, "originalSourceLevel", long.class);
} catch (Exception e) {
return null;
}
}

private final static MethodHandle ORIGINAL_COMPLIANCE_LEVEL = findOriginalComplianceLevel();
private static MethodHandle findOriginalComplianceLevel() {
try {
return MethodHandles.lookup().findSetter(CompilerOptions.class, "originalComplianceLevel", long.class);
} catch (Exception e) {
return null;
}
}

protected long toJdtVersion(JavaVersion version) {
return version.toJdtClassFileConstant();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,24 @@ private static MethodHandle findInlineJsrBytecode() {
}
}

private final static MethodHandle ORIGINAL_SOURCE_LEVEL = findOriginalSourceLevel();
private static MethodHandle findOriginalSourceLevel() {
try {
return MethodHandles.lookup().findSetter(CompilerOptions.class, "originalSourceLevel", long.class);
} catch (Exception e) {
return null;
}
}

private final static MethodHandle ORIGINAL_COMPLIANCE_LEVEL = findOriginalComplianceLevel();
private static MethodHandle findOriginalComplianceLevel() {
try {
return MethodHandles.lookup().findSetter(CompilerOptions.class, "originalComplianceLevel", long.class);
} catch (Exception e) {
return null;
}
}

public InMemoryJavaCompiler(ClassLoader parent, CompilerOptions compilerOptions) {
this.nameEnv = new ClassLoaderBasedNameEnvironment(parent);
this.parentClassLoader = parent;
Expand Down Expand Up @@ -257,7 +275,13 @@ private long toClassFmt(JavaVersion version) {
*/
private void setSourceLevel(long jdkVersion) {
compilerOptions.sourceLevel = jdkVersion;
compilerOptions.originalSourceLevel = jdkVersion;
if (ORIGINAL_SOURCE_LEVEL != null) {
try {
ORIGINAL_SOURCE_LEVEL.invoke(compilerOptions, jdkVersion);
} catch (Throwable e) {
// ignore
}
}
}

/**
Expand All @@ -266,7 +290,13 @@ private void setSourceLevel(long jdkVersion) {
*/
private void setComplianceLevel(long jdkVersion) {
compilerOptions.complianceLevel = jdkVersion;
compilerOptions.originalComplianceLevel = jdkVersion;
if (ORIGINAL_COMPLIANCE_LEVEL != null) {
try {
ORIGINAL_COMPLIANCE_LEVEL.invoke(compilerOptions, jdkVersion);
} catch (Throwable e) {
// ignore
}
}
}

public Result compile(JavaSource... sources) {
Expand Down

0 comments on commit ffc3b15

Please sign in to comment.