-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ModuleAccess.getEVERYONE_MODULE & getALL_UNNAMED_MODULE
- Loading branch information
Showing
12 changed files
with
335 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 28 additions & 7 deletions
35
api/src/main/java/io/github/karlatemp/unsafeaccessor/UsfAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,37 @@ | ||
package io.github.karlatemp.unsafeaccessor; | ||
|
||
import java.util.function.Consumer; | ||
|
||
// internal | ||
public class UsfAccessor { | ||
static abstract class UsfAccessorSpi { | ||
abstract Object allocateUnsafe(); | ||
|
||
abstract Consumer<Object> allocateObjectInitializer(); | ||
} | ||
|
||
static UsfAccessorSpi spi; | ||
|
||
static UsfAccessorSpi spi() { | ||
if (spi != null) return spi; | ||
synchronized (UsfAccessorSpi.class) { | ||
if (spi != null) return spi; | ||
try { | ||
// in module :impl.loader | ||
Class<?> impl = Class.forName("io.github.karlatemp.unsafeaccessor.UsfAccessorImpl"); | ||
return spi = impl.asSubclass(UsfAccessorSpi.class).getDeclaredConstructor().newInstance(); | ||
} catch (Exception e) { | ||
throw new UnsupportedOperationException(e); | ||
} | ||
} | ||
} | ||
|
||
// Internal service for load Unsafe. | ||
protected static Object allocateUnsafe() { | ||
try { | ||
// in module :impl.loader | ||
Class<?> impl = Class.forName("io.github.karlatemp.unsafeaccessor.UsfAccessorImpl"); | ||
return impl.getDeclaredMethod("allocateUnsafe").invoke(null); | ||
} catch (Exception e) { | ||
throw new UnsupportedOperationException(e); | ||
} | ||
return spi().allocateUnsafe(); | ||
} | ||
|
||
protected static Consumer<Object> allocateObjectInitializer() { | ||
return spi().allocateObjectInitializer(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...odegen/src/test/java/io/github/karlatemp/unsafeaccessor/codegen/GenObjectInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package io.github.karlatemp.unsafeaccessor.codegen; | ||
|
||
import io.github.karlatemp.unsafeaccessor.CodeGenUtils; | ||
import org.objectweb.asm.MethodVisitor; | ||
import org.objectweb.asm.Opcodes; | ||
import org.objectweb.asm.tree.ClassNode; | ||
|
||
import java.util.Base64; | ||
|
||
public class GenObjectInitializer { | ||
public static void main(String[] args) throws Throwable { | ||
ClassNode newClass = new ClassNode(); | ||
newClass.visit( | ||
Opcodes.V1_8, | ||
0, | ||
"io/github/karlatemp/unsafeaccessor/ObjectInitializerHolder", | ||
null, | ||
"java/lang/Object", | ||
null | ||
); | ||
CodeGenUtils.genCons(newClass); | ||
MethodVisitor code = newClass.visitMethod(Opcodes.ACC_STATIC, "code", "()[B", null, null); | ||
code.visitMethodInsn(Opcodes.INVOKESTATIC, "java/util/Base64", "getDecoder", "()Ljava/util/Base64$Decoder;", false); | ||
{ | ||
ClassNode initializer = new ClassNode(); | ||
initializer.visit( | ||
Opcodes.V1_8, | ||
0, | ||
"io/github/karlatemp/unsafeaccessor/ObjectInitializerImpl", | ||
null, | ||
"jdk/internal/reflect/MagicAccessorImpl", | ||
new String[]{"java/util/function/Consumer"} | ||
); | ||
CodeGenUtils.genCons(initializer); | ||
MethodVisitor accept = initializer.visitMethod(Opcodes.ACC_PUBLIC, "accept", "(Ljava/lang/Object;)V", null, null); | ||
accept.visitVarInsn(Opcodes.ALOAD, 0); | ||
accept.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); | ||
accept.visitInsn(Opcodes.RETURN); | ||
accept.visitMaxs(2, 2); | ||
byte[] bytecode = CodeGenUtils.toBytecode(initializer); | ||
code.visitLdcInsn(Base64.getEncoder().encodeToString(bytecode)); | ||
} | ||
code.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/Base64$Decoder", "decode", "(Ljava/lang/String;)[B", false); | ||
code.visitInsn(Opcodes.ARETURN); | ||
code.visitMaxs(3, 3); | ||
|
||
CodeGenUtils.save(newClass); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.