Class UnsafeMemoryAccessDeprecationExamples

java.lang.Object
net.jrodolfo.java_evolution.java23.unsafe_memory_access_deprecation.UnsafeMemoryAccessDeprecationExamples

public class UnsafeMemoryAccessDeprecationExamples extends Object
Demonstrates Java 23's migration pressure away from sun.misc.Unsafe memory-access methods.

The main project source deliberately does not import sun.misc.Unsafe. Instead, this example writes a tiny child source file, compiles it with javac -Xlint:removal, and captures the deprecation-for-removal diagnostics. It also runs the child class with --sun-misc-unsafe-memory-access=deny to show the runtime boundary.

  • Constructor Details

    • UnsafeMemoryAccessDeprecationExamples

      public UnsafeMemoryAccessDeprecationExamples()
  • Method Details

    • createUnsafeMemoryAccessProbe

      public Path createUnsafeMemoryAccessProbe(Path sourceDirectory) throws IOException
      Writes the generated child source used only for migration-boundary testing.
      Parameters:
      sourceDirectory - directory where the probe source should be written
      Returns:
      path to the generated source file
      Throws:
      IOException - when the source file cannot be written
    • compileUnsafeMemoryAccessProbe

      public UnsafeMemoryAccessDeprecationExamples.ProcessResult compileUnsafeMemoryAccessProbe(Path workDirectory) throws IOException, InterruptedException
      Compiles the generated child source and captures compiler diagnostics.
      Parameters:
      workDirectory - directory where source and class files should be created
      Returns:
      the child process result
      Throws:
      IOException - when files cannot be written or javac cannot be started
      InterruptedException - when interrupted while waiting for javac
    • runUnsafeProbeWithDeny

      public UnsafeMemoryAccessDeprecationExamples.ProcessResult runUnsafeProbeWithDeny(Path workDirectory) throws IOException, InterruptedException
      Runs the generated child class with unsafe memory access denied.
      Parameters:
      workDirectory - directory where source and class files should be created
      Returns:
      the child process result
      Throws:
      IOException - when files cannot be written or the child JVM cannot be started
      InterruptedException - when interrupted while waiting for the child JVM
    • updateWithVarHandle

      public int updateWithVarHandle(int replacementValue) throws ReflectiveOperationException
      Demonstrates the supported replacement direction for ordinary variable access.
      Parameters:
      replacementValue - value to store through a VarHandle
      Returns:
      the value read back through the same supported API
      Throws:
      ReflectiveOperationException - when the field handle cannot be created
    • exampleBoundary

      public String exampleBoundary()
      Explains why sun.misc.Unsafe appears only in generated child source.
      Returns:
      a short boundary explanation
    • migrationLesson

      public String migrationLesson()
      Explains the migration lesson.
      Returns:
      a short migration lesson