|
3 | 3 | *
|
4 | 4 | * The MIT License (MIT)
|
5 | 5 | *
|
6 |
| - * Copyright (c) 2018 TheRealBuggy/JonathanxD (https://github.com/JonathanxD/) <jonathan.scripter@programmer.net> |
| 6 | + * Copyright (c) 2021 TheRealBuggy/JonathanxD (https://github.com/JonathanxD/) <jonathan.scripter@programmer.net> |
7 | 7 | * Copyright (c) contributors
|
8 | 8 | *
|
9 | 9 | *
|
@@ -35,6 +35,9 @@ import com.github.jonathanxd.kores.bytecode.post.GotoOptimizer
|
35 | 35 | import com.github.jonathanxd.kores.bytecode.post.MethodProcessor
|
36 | 36 | import com.github.jonathanxd.kores.bytecode.pre.GenLineVisitor
|
37 | 37 | import com.github.jonathanxd.iutils.option.Option
|
| 38 | +import com.github.jonathanxd.iutils.option.Options |
| 39 | +import com.github.jonathanxd.kores.bytecode.doc.NestLogic |
| 40 | +import com.github.jonathanxd.kores.bytecode.doc.IndyConcatLogic |
38 | 41 |
|
39 | 42 | /**
|
40 | 43 | * Calls [org.objectweb.asm.util.CheckClassAdapter] to check generated class.
|
@@ -115,6 +118,194 @@ val GENERATE_BRIDGE_METHODS = Option(false)
|
115 | 118 | @JvmField
|
116 | 119 | val GENERATE_SYNTHETIC_ACCESS = Option(true)
|
117 | 120 |
|
| 121 | +/** |
| 122 | + * Automatically generate synthetic accessors for private members in private inner classes. |
| 123 | + * Synthetic accessors will be only generated for private members which are accessed. |
| 124 | + * |
| 125 | + * An anonymous synthetic class may be generated for private constructors. |
| 126 | + * |
| 127 | + * @since 4.0.5.bytecode.5 |
| 128 | + */ |
| 129 | +@JvmField |
| 130 | +val FORCE_GENERATE_SYNTHETIC_ACCESS = Option(false) |
| 131 | + |
| 132 | +/** |
| 133 | + * When enabled, generate nests declarations as specified in [JEP 181](https://openjdk.java.net/jeps/181), |
| 134 | + * enabling this automatically disables [GENERATE_SYNTHETIC_ACCESS]. |
| 135 | + * |
| 136 | + * To have both enabled, use [FORCE_GENERATE_SYNTHETIC_ACCESS]. This is not recommended since |
| 137 | + * [Nest-Based Access Control](https://openjdk.java.net/jeps/181) introduces JVM-Level access control |
| 138 | + * and removes the need of having bridge methods to access inner classes private members. |
| 139 | + * |
| 140 | + * @since 4.0.5.bytecode.5 |
| 141 | + */ |
| 142 | +@JvmField |
| 143 | +val GENERATE_NESTS = Option(true) |
| 144 | + |
| 145 | +/** |
| 146 | + * Force generate [Nests](https://openjdk.java.net/jeps/181) even when emitting bytecode for Java 10 (54) or earlier. |
| 147 | + * |
| 148 | + * Note that forcing to generate Nests may result in two scenarios: |
| 149 | + * |
| 150 | + * - Generate Nests info but invoke with **synthetic accessors** when [FORCE_GENERATE_SYNTHETIC_ACCESS] is set. |
| 151 | + * - Generate Nests info and access members directly. JVM may not accept this kind of access when running under Java 10 or earlier. |
| 152 | + * |
| 153 | + * Read more at [GENERATE_NESTS]. |
| 154 | + */ |
| 155 | +@JvmField |
| 156 | +val FORCE_GENERATE_NESTS = Option(true) |
| 157 | + |
| 158 | +/** |
| 159 | + * Uses `invokedynamic` instruction for string concatenation instead of [StringBuilder]. |
| 160 | + * |
| 161 | + * This option is automatically disabled when emitting bytecode for Java 8 or earlier, to force use [FORCE_INDIFY_STRING_CONCAT]. |
| 162 | + * |
| 163 | + * Read more at [IndyConcatLogic] |
| 164 | + */ |
| 165 | +@JvmField |
| 166 | +val INDIFY_STRING_CONCAT = Option(true) |
| 167 | + |
| 168 | +/** |
| 169 | + * Force the use of `invokedynamic` instruction for string concatenation instead of [StringBuilder]. |
| 170 | + * |
| 171 | + * This applies even if the target bytecode version is lower or equal to 52 (Java 8), so the concatenation will |
| 172 | + * only work when running under Java 9 or newer. |
| 173 | + * |
| 174 | + * Read [INDIFY_STRING_CONCAT] for more information. |
| 175 | + */ |
| 176 | +@JvmField |
| 177 | +val FORCE_INDIFY_STRING_CONCAT = Option(false) |
| 178 | + |
| 179 | +/** |
| 180 | + * Specifies the strategy to use to generate `invokedynamic` string concatenation, as specified in |
| 181 | + * [java.lang.invoke.StringConcatFactory.makeConcatWithConstants]. |
| 182 | + * |
| 183 | + * Read more at [IndyConcatLogic]. |
| 184 | + */ |
| 185 | +@JvmField |
| 186 | +val INDY_CONCAT_STRATEGY = Option(IndyConcatStrategy.INTERPOLATE) |
| 187 | + |
| 188 | +enum class IndyConcatStrategy { |
| 189 | + /** |
| 190 | + * Indify using interpolation, the produced `invokedynamic` instruction interpolates the constants in the |
| 191 | + * [recipe][java.lang.invoke.StringConcatFactory.makeConcatWithConstants] argument. |
| 192 | + * |
| 193 | + * For example, the following concatenation scenario: |
| 194 | + * |
| 195 | + * ```kotlin |
| 196 | + * fun concat(a: String, b: String) = |
| 197 | + * a + "<:>" + b |
| 198 | + * ``` |
| 199 | + * |
| 200 | + * Is translated into the following recipe: |
| 201 | + * ``` |
| 202 | + * aload 0 // Load a |
| 203 | + * aload 1 // Load b |
| 204 | + * makeConcatWithConstants<invokedynamic>("\u0001<:>\u0001") |
| 205 | + * ``` |
| 206 | + * |
| 207 | + */ |
| 208 | + INTERPOLATE, |
| 209 | + |
| 210 | + /** |
| 211 | + * Indify providing constants to the [bootstrap method constants parameter][java.lang.invoke.StringConcatFactory.makeConcatWithConstants]. |
| 212 | + * This results in an interpolation using `\u0001` for arguments in the stack and `\u0002` for constants in the **ConstantPool**. |
| 213 | + * |
| 214 | + * For example, the following concatenation scenario: |
| 215 | + * |
| 216 | + * ```kotlin |
| 217 | + * fun concat(a: String, b: String) = |
| 218 | + * a + "<:>" + b |
| 219 | + * ``` |
| 220 | + * |
| 221 | + * Is translated into the following recipe: |
| 222 | + * ``` |
| 223 | + * aload 0 // Load a |
| 224 | + * aload 1 // Load b |
| 225 | + * |
| 226 | + * makeConcatWithConstants<invokedynamic>("\u0001\u0002\u0001", "<:>") |
| 227 | + * ``` |
| 228 | + * |
| 229 | + * With `<:>` provided as an argument to the [bootstrap method constants parameter][java.lang.invoke.StringConcatFactory.makeConcatWithConstants]. |
| 230 | + */ |
| 231 | + CONSTANT, |
| 232 | + |
| 233 | + /** |
| 234 | + * Indify using only arguments in the stack. Constant values are pushed to the stack using `ldc` instruction. |
| 235 | + * |
| 236 | + * For example, the following concatenation scenario: |
| 237 | + * |
| 238 | + * ```kotlin |
| 239 | + * fun concat(a: String, b: String) = |
| 240 | + * a + "<:>" + b |
| 241 | + * ``` |
| 242 | + * |
| 243 | + * Is translated into the following recipe: |
| 244 | + * ``` |
| 245 | + * aload 0 // Load a |
| 246 | + * ldc "<:>" |
| 247 | + * aload 1 // Load b |
| 248 | + * makeConcatWithConstants<invokedynamic>("\u0001\u0001\u0001") |
| 249 | + * ``` |
| 250 | + */ |
| 251 | + LDC |
| 252 | +} |
| 253 | + |
| 254 | +/** |
| 255 | + * Read more in [NestLogic] |
| 256 | + */ |
| 257 | +fun Options.nestAccessGenerationMode(version: Int) = |
| 258 | + when { |
| 259 | + this[FORCE_GENERATE_SYNTHETIC_ACCESS] == true && this[GENERATE_NESTS] == true && version >= 55 -> NestAccessGenerationMode.MIXED |
| 260 | + this[FORCE_GENERATE_SYNTHETIC_ACCESS] == true && this[FORCE_GENERATE_NESTS] == true -> NestAccessGenerationMode.MIXED |
| 261 | + this[FORCE_GENERATE_NESTS] == true -> NestAccessGenerationMode.NEST_BASED |
| 262 | + this[GENERATE_NESTS] == true && version >= 55 -> NestAccessGenerationMode.NEST_BASED |
| 263 | + this[GENERATE_SYNTHETIC_ACCESS] == true -> NestAccessGenerationMode.SYNTHETIC_ONLY |
| 264 | + else -> NestAccessGenerationMode.DISABLED |
| 265 | + } |
| 266 | + |
| 267 | +/** |
| 268 | + * Read more in [NestLogic] |
| 269 | + */ |
| 270 | +enum class NestAccessGenerationMode { |
| 271 | + /** |
| 272 | + * Synthetic and Bridge based inner class private member access. |
| 273 | + */ |
| 274 | + SYNTHETIC_ONLY, |
| 275 | + |
| 276 | + /** |
| 277 | + * Nest-based inner class private member access, as specified in [JEP 181](https://openjdk.java.net/jeps/181). |
| 278 | + */ |
| 279 | + NEST_BASED, |
| 280 | + |
| 281 | + /** |
| 282 | + * Mixed, generate NestHost and NestMember declarations, as specified in [JEP 181](https://openjdk.java.net/jeps/181), |
| 283 | + * but use **Synthetic Bridge Methods** for access instead of direct access. |
| 284 | + * |
| 285 | + * Not recommended since [Nest-Based Access Control](https://openjdk.java.net/jeps/181) allows direct access to private |
| 286 | + * members without **bridge methods**. This should only be used in **very very very** specific cases. |
| 287 | + */ |
| 288 | + MIXED, |
| 289 | + |
| 290 | + /** |
| 291 | + * Totally disabled. This may cause **class load-time** and/or **runtime exceptions** in most JVM Implementations |
| 292 | + * since classes are not allowed to access private members. |
| 293 | + */ |
| 294 | + DISABLED |
| 295 | +} |
| 296 | + |
| 297 | +/** |
| 298 | + * Read more in [NestLogic] |
| 299 | + */ |
| 300 | +fun NestAccessGenerationMode.isSyntheticAccess() = |
| 301 | + this == NestAccessGenerationMode.SYNTHETIC_ONLY || this == NestAccessGenerationMode.MIXED |
| 302 | + |
| 303 | +/** |
| 304 | + * Read more in [NestLogic] |
| 305 | + */ |
| 306 | +fun NestAccessGenerationMode.isToGenerateNests() = |
| 307 | + this == NestAccessGenerationMode.MIXED || this == NestAccessGenerationMode.NEST_BASED |
| 308 | + |
118 | 309 | enum class VisitLineType {
|
119 | 310 | /**
|
120 | 311 | * Disable line visit
|
|
0 commit comments