|
| 1 | +/* |
| 2 | + * Kores-BytecodeWriter - Translates Kores Structure to JVM Bytecode <https://github.com/JonathanxD/CodeAPI-BytecodeWriter> |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2021 TheRealBuggy/JonathanxD (https://github.com/JonathanxD/) <jonathan.scripter@programmer.net> |
| 7 | + * Copyright (c) contributors |
| 8 | + * |
| 9 | + * |
| 10 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | + * of this software and associated documentation files (the "Software"), to deal |
| 12 | + * in the Software without restriction, including without limitation the rights |
| 13 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | + * copies of the Software, and to permit persons to whom the Software is |
| 15 | + * furnished to do so, subject to the following conditions: |
| 16 | + * |
| 17 | + * The above copyright notice and this permission notice shall be included in |
| 18 | + * all copies or substantial portions of the Software. |
| 19 | + * |
| 20 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 26 | + * THE SOFTWARE. |
| 27 | + */ |
| 28 | +package com.koresframework.kores.bytecode.classloader |
| 29 | + |
| 30 | +import com.koresframework.kores.base.TypeDeclaration |
| 31 | +import com.koresframework.kores.bytecode.BytecodeClass |
| 32 | +import com.koresframework.kores.type.`is` |
| 33 | +import java.lang.reflect.Type |
| 34 | + |
| 35 | +open class BytecodeCodeClassLoader : ClassLoader { |
| 36 | + |
| 37 | + constructor() : super() |
| 38 | + constructor(parent: ClassLoader) : super(parent) |
| 39 | + |
| 40 | + /** |
| 41 | + * Define type declaration class. |
| 42 | + * |
| 43 | + * @param typeDeclaration Type declaration. |
| 44 | + * @param bytes Bytes. |
| 45 | + * @return Defined Class. |
| 46 | + */ |
| 47 | + open fun define(typeDeclaration: TypeDeclaration, bytes: ByteArray): Class<*> { |
| 48 | + return super.defineClass(typeDeclaration.type, bytes, 0, bytes.size) |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Define [BytecodeClass] class. |
| 53 | + * |
| 54 | + * @param bytecodeClass Bytecode class. |
| 55 | + * @return Defined Class. |
| 56 | + */ |
| 57 | + open fun define(bytecodeClass: BytecodeClass): Class<*> { |
| 58 | + val type = (bytecodeClass.declaration as? TypeDeclaration) |
| 59 | + ?: throw IllegalArgumentException("Non-TypeDeclaration loading is not supported yet. BytecodeClass: $bytecodeClass") |
| 60 | + |
| 61 | + return this.define(type, bytecodeClass.bytecode) |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Define [classes][BytecodeClass] and inner classes. |
| 66 | + * |
| 67 | + * Make sure that all elements in the `bytecodeClasses` is a inner type of first element. |
| 68 | + * |
| 69 | + * @param bytecodeClasses Bytecode class (first element) and inner classes (remaining). |
| 70 | + * @return First Defined Class. |
| 71 | + */ |
| 72 | + open fun define(bytecodeClasses: Array<out BytecodeClass>): Class<*> { |
| 73 | + return this.define(bytecodeClasses.iterator()) |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Define [classes][BytecodeClass] and inner classes. |
| 78 | + * |
| 79 | + * Make sure that all elements in the `bytecodeClasses` is a inner type of first element. |
| 80 | + * |
| 81 | + * @param bytecodeClasses Bytecode class (first element) and inner classes (remaining). |
| 82 | + * @return First Defined Class. |
| 83 | + */ |
| 84 | + open fun define(bytecodeClasses: Collection<BytecodeClass>): Class<*> { |
| 85 | + return this.define(bytecodeClasses.iterator()) |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Define [classes][BytecodeClass] and inner classes. |
| 90 | + * |
| 91 | + * Make sure that all elements in the `bytecodeClasses` is a inner type of first element. |
| 92 | + * |
| 93 | + * @param bytecodeClasses Bytecode class (first element) and inner classes (remaining). |
| 94 | + * @return First Defined Class. |
| 95 | + */ |
| 96 | + open fun define(bytecodeClasses: Iterator<BytecodeClass>): Class<*> { |
| 97 | + if (!bytecodeClasses.hasNext()) { |
| 98 | + throw IllegalArgumentException("Empty 'bytecodeClasses' array") |
| 99 | + } |
| 100 | + |
| 101 | + val bytecodeClass = bytecodeClasses.next() |
| 102 | + |
| 103 | + val type = (bytecodeClass.declaration as? TypeDeclaration) |
| 104 | + ?: throw IllegalArgumentException("Non-TypeDeclaration loading is not supported yet. BytecodeClass: $bytecodeClass") |
| 105 | + |
| 106 | + val define = this.define(type, bytecodeClass.bytecode) |
| 107 | + |
| 108 | + bytecodeClasses.forEach { |
| 109 | + this.define(it) |
| 110 | + } |
| 111 | + |
| 112 | + return define |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Defines very [class][BytecodeClass] in [bytecodeClasses] iterator. All defined |
| 117 | + * classes are added to provided [collection]. |
| 118 | + * |
| 119 | + * This function may be used to define inner classes as well as multiple individual classes. |
| 120 | + * |
| 121 | + * @param bytecodeClasses Bytecode class (first element) and inner classes (remaining). |
| 122 | + * @return First Defined Class. |
| 123 | + */ |
| 124 | + open fun <C: MutableCollection<in LoadedBytecodeClass>> defineEveryTo( |
| 125 | + collection: C, |
| 126 | + bytecodeClasses: Iterator<BytecodeClass> |
| 127 | + ): C { |
| 128 | + if (!bytecodeClasses.hasNext()) { |
| 129 | + throw IllegalArgumentException("Empty 'bytecodeClasses' array") |
| 130 | + } |
| 131 | + |
| 132 | + val known = mutableListOf<LoadedBytecodeClass>() |
| 133 | + |
| 134 | + while (bytecodeClasses.hasNext()) { |
| 135 | + val bytecodeClass = bytecodeClasses.next() |
| 136 | + |
| 137 | + val type = (bytecodeClass.declaration as? TypeDeclaration) |
| 138 | + ?: throw IllegalArgumentException("Non-TypeDeclaration loading is not supported yet. BytecodeClass: $bytecodeClass") |
| 139 | + |
| 140 | + val define = this.define(type, bytecodeClass.bytecode) |
| 141 | + val outerType = bytecodeClass.declaration.outerType |
| 142 | + val outerLazy by lazy { |
| 143 | + known.firstOrNull { outerType != null && (it.bytecodeClass.declaration as Type).`is`(outerType) } |
| 144 | + } |
| 145 | + |
| 146 | + val loaded = LoadedBytecodeClass( |
| 147 | + bytecodeClass, |
| 148 | + outerType = { outerLazy }, |
| 149 | + loadedClass = define |
| 150 | + ) |
| 151 | + |
| 152 | + collection.add(loaded) |
| 153 | + known.add(loaded) |
| 154 | + } |
| 155 | + |
| 156 | + return collection |
| 157 | + } |
| 158 | +} |
0 commit comments