Skip to content

Commit

Permalink
Fix[compat]: Java 21 compatibility
Browse files Browse the repository at this point in the history
Requires new commandline arguments, check pom.xml
  • Loading branch information
artdeell committed May 4, 2024
1 parent 2c3f705 commit bc7eb4e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 48 deletions.
1 change: 1 addition & 0 deletions cacio-tta/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<target>17</target>
<compilerArgs>
<arg>-XDignore.symbol.file=true</arg>
<arg>--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED</arg>
<arg>--add-exports=java.desktop/java.awt.peer=ALL-UNNAMED</arg>
<arg>--add-exports=java.desktop/sun.awt.image=ALL-UNNAMED</arg>
<arg>--add-exports=java.desktop/sun.java2d=ALL-UNNAMED</arg>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,10 @@
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.net.*;
import jdk.internal.misc.Unsafe;

public class CTCPreloadClassLoader extends URLClassLoader {
// https://stackoverflow.com/a/56043252/1050369
private static final VarHandle MODIFIERS;

static {
try {
var lookup = MethodHandles.privateLookupIn(Field.class, MethodHandles.lookup());
MODIFIERS = lookup.findVarHandle(Field.class, "modifiers", int.class);
} catch (IllegalAccessException | NoSuchFieldException ex) {
throw new RuntimeException(ex);
}
}

static {
try {
Field toolkit = Toolkit.class.getDeclaredField("toolkit");
Expand All @@ -67,12 +53,12 @@ public class CTCPreloadClassLoader extends URLClassLoader {
defaultHeadlessField.set(null, Boolean.FALSE);
headlessField.set(null,Boolean.FALSE);

makeNonFinal(ge);

Class<?> smfCls = Class.forName("sun.java2d.SurfaceManagerFactory");
Field smf = smfCls.getDeclaredField("instance");
smf.setAccessible(true);
smf.set(null, null);

setWithUnsafe(ge, new CTCGraphicsEnvironment());

ge.set(null, new CTCGraphicsEnvironment());

Expand Down Expand Up @@ -107,7 +93,7 @@ static URL getFileURL(File file) {
} catch (IOException e) {}

try {
return file.toURL();
return file.toURI().toURL();
} catch (MalformedURLException e) {
// Should never happen since we specify the protocol...
throw new InternalError(e);
Expand All @@ -119,10 +105,16 @@ private void appendToClassPathForInstrumentation(String path) {
super.addURL(getFileURL(new File(path)));
}

public static void makeNonFinal(Field field) {
int mods = field.getModifiers();
if (Modifier.isFinal(mods)) {
MODIFIERS.set(field, mods & ~Modifier.FINAL);
}
public static void setWithUnsafe(Field field, Object value) {
/*
* We can do this because we are running under *very* specific conditions:
* - This class is initialized way before anything AWT-related gets JIT-ed
* - The field is an object reference.
* Otherwise this won't work at all.
*/
Unsafe unsafe = Unsafe.getUnsafe();
Object base = unsafe.staticFieldBase(field);
long offset = unsafe.staticFieldOffset(field);
unsafe.putReference(base, offset, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,16 @@
import javax.swing.plaf.metal.MetalLookAndFeel;

import com.github.caciocavallosilano.cacio.ctc.CTCGraphicsEnvironment;
import com.github.caciocavallosilano.cacio.ctc.CTCPreloadClassLoader;
import com.github.caciocavallosilano.cacio.ctc.CTCToolkit;

import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.InitializationError;

import java.awt.*;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;

public class CacioTestRunner extends BlockJUnit4ClassRunner {
// https://stackoverflow.com/a/56043252/1050369
private static final VarHandle MODIFIERS;

static {
try {
var lookup = MethodHandles.privateLookupIn(Field.class, MethodHandles.lookup());
MODIFIERS = lookup.findVarHandle(Field.class, "modifiers", int.class);
} catch (IllegalAccessException | NoSuchFieldException ex) {
throw new RuntimeException(ex);
}
}

static {
try {
Field toolkit = Toolkit.class.getDeclaredField("toolkit");
Expand All @@ -59,7 +45,7 @@ public class CacioTestRunner extends BlockJUnit4ClassRunner {

Field defaultHeadlessField = java.awt.GraphicsEnvironment.class.getDeclaredField("defaultHeadless");
defaultHeadlessField.setAccessible(true);
defaultHeadlessField.set(null,Boolean.TRUE);
defaultHeadlessField.set(null,Boolean.TRUE);
Field headlessField = java.awt.GraphicsEnvironment.class.getDeclaredField("headless");
headlessField.setAccessible(true);
headlessField.set(null,Boolean.TRUE);
Expand All @@ -70,14 +56,13 @@ public class CacioTestRunner extends BlockJUnit4ClassRunner {
defaultHeadlessField.set(null, Boolean.FALSE);
headlessField.set(null,Boolean.FALSE);

makeNonFinal(ge);

Class<?> smfCls = Class.forName("sun.java2d.SurfaceManagerFactory");
Field smf = smfCls.getDeclaredField("instance");
smf.setAccessible(true);
smf.set(null, null);

ge.set(null, new CTCGraphicsEnvironment());
CTCPreloadClassLoader.setWithUnsafe(ge, new CTCGraphicsEnvironment());
} catch (Exception e) {
e.printStackTrace();
}
Expand All @@ -88,11 +73,4 @@ public class CacioTestRunner extends BlockJUnit4ClassRunner {
public CacioTestRunner(Class<?> klass) throws InitializationError {
super(klass);
}

public static void makeNonFinal(Field field) {
int mods = field.getModifiers();
if (Modifier.isFinal(mods)) {
MODIFIERS.set(field, mods & ~Modifier.FINAL);
}
}
}
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<configuration>
<additionalOptions>
<additionalOption>-XDignore.symbol.file=true</additionalOption>
<additionalOption>--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED</additionalOption>
<additionalOption>--add-exports=java.desktop/java.awt.peer=ALL-UNNAMED</additionalOption>
<additionalOption>--add-exports=java.desktop/sun.awt=ALL-UNNAMED</additionalOption>
<additionalOption>--add-exports=java.desktop/sun.awt.event=ALL-UNNAMED</additionalOption>
Expand Down Expand Up @@ -130,6 +131,7 @@
<java.awt.headless>false</java.awt.headless>
</systemPropertyVariables>
<argLine>
--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED
--add-exports=java.desktop/java.awt=ALL-UNNAMED
--add-exports=java.desktop/java.awt.peer=ALL-UNNAMED
--add-exports=java.desktop/sun.awt.image=ALL-UNNAMED
Expand Down

0 comments on commit bc7eb4e

Please sign in to comment.