|
| 1 | +/* |
| 2 | + * Minecraft Development for IntelliJ |
| 3 | + * |
| 4 | + * https://mcdev.io/ |
| 5 | + * |
| 6 | + * Copyright (C) 2025 minecraft-dev |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU Lesser General Public License as published |
| 10 | + * by the Free Software Foundation, version 3.0 only. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Lesser General Public License |
| 18 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +package com.demonwav.mcdev.platform.mixin.handlers.desugar |
| 22 | + |
| 23 | +import com.demonwav.mcdev.util.constantValue |
| 24 | +import com.intellij.openapi.project.Project |
| 25 | +import com.intellij.psi.JavaPsiFacade |
| 26 | +import com.intellij.psi.PsiAssignmentExpression |
| 27 | +import com.intellij.psi.PsiClass |
| 28 | +import com.intellij.psi.PsiClassInitializer |
| 29 | +import com.intellij.psi.PsiExpressionStatement |
| 30 | +import com.intellij.psi.PsiField |
| 31 | +import com.intellij.psi.PsiMethod |
| 32 | +import com.intellij.psi.PsiModifier |
| 33 | +import com.intellij.psi.PsiStatement |
| 34 | +import com.intellij.psi.PsiType |
| 35 | +import com.intellij.util.JavaPsiConstructorUtil |
| 36 | + |
| 37 | +object FieldAssignmentDesugarer : Desugarer { |
| 38 | + override fun desugar(project: Project, clazz: PsiClass) { |
| 39 | + val staticStatementsToInsertPre = mutableListOf<PsiStatement>() |
| 40 | + val staticStatementsToInsertPost = mutableListOf<PsiStatement>() |
| 41 | + val nonStaticStatementsToInsert = mutableListOf<PsiStatement>() |
| 42 | + var seenStaticInitializer = false |
| 43 | + |
| 44 | + for (child in clazz.children) { |
| 45 | + when (child) { |
| 46 | + is PsiField -> { |
| 47 | + val initializer = child.initializer ?: continue |
| 48 | + |
| 49 | + if (child.hasModifierProperty(PsiModifier.STATIC)) { |
| 50 | + // check if the field is a ConstantValue with no initializer in the bytecode |
| 51 | + val constantValue = initializer.constantValue |
| 52 | + if (constantValue != null && constantValue !is PsiType) { |
| 53 | + continue |
| 54 | + } |
| 55 | + |
| 56 | + val fieldInitializer = JavaPsiFacade.getElementFactory(project) |
| 57 | + .createStatementFromText("${child.name} = null;", child) as PsiExpressionStatement |
| 58 | + (fieldInitializer.expression as PsiAssignmentExpression).rExpression!!.replace(initializer) |
| 59 | + DesugarUtil.setOriginalElement(fieldInitializer, DesugarUtil.getOriginalElement(child)) |
| 60 | + |
| 61 | + if (seenStaticInitializer) { |
| 62 | + staticStatementsToInsertPost += fieldInitializer |
| 63 | + } else { |
| 64 | + staticStatementsToInsertPre += fieldInitializer |
| 65 | + } |
| 66 | + } else { |
| 67 | + val fieldInitializer = JavaPsiFacade.getElementFactory(project) |
| 68 | + .createStatementFromText("this.${child.name} = null;", child) as PsiExpressionStatement |
| 69 | + (fieldInitializer.expression as PsiAssignmentExpression).rExpression!!.replace(initializer) |
| 70 | + DesugarUtil.setOriginalElement(fieldInitializer, DesugarUtil.getOriginalElement(child)) |
| 71 | + |
| 72 | + nonStaticStatementsToInsert += fieldInitializer |
| 73 | + } |
| 74 | + |
| 75 | + initializer.delete() |
| 76 | + } |
| 77 | + is PsiClassInitializer -> { |
| 78 | + if (child.hasModifierProperty(PsiModifier.STATIC)) { |
| 79 | + seenStaticInitializer = true |
| 80 | + } else { |
| 81 | + nonStaticStatementsToInsert += child.body.statements |
| 82 | + child.delete() |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if (staticStatementsToInsertPre.isNotEmpty() || staticStatementsToInsertPost.isNotEmpty()) { |
| 89 | + val staticBlock = findStaticBlock(project, clazz) |
| 90 | + for (statement in staticStatementsToInsertPre) { |
| 91 | + staticBlock.body.addAfter(statement, staticBlock.body.lBrace) |
| 92 | + } |
| 93 | + for (statement in staticStatementsToInsertPost) { |
| 94 | + staticBlock.body.addBefore(statement, staticBlock.body.rBrace) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if (nonStaticStatementsToInsert.isNotEmpty()) { |
| 99 | + for (constructor in findConstructorsCallingSuper(project, clazz)) { |
| 100 | + val body = constructor.body ?: continue |
| 101 | + val delegateCtorCall = JavaPsiConstructorUtil.findThisOrSuperCallInConstructor(constructor) |
| 102 | + for (statement in nonStaticStatementsToInsert.asReversed()) { |
| 103 | + body.addAfter(statement, delegateCtorCall?.parent ?: body.lBrace) |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private fun findStaticBlock(project: Project, clazz: PsiClass): PsiClassInitializer { |
| 110 | + for (initializer in clazz.initializers) { |
| 111 | + if (initializer.hasModifierProperty(PsiModifier.STATIC)) { |
| 112 | + return initializer |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + val initializer = JavaPsiFacade.getElementFactory(project) |
| 117 | + .createClass("class _Dummy_ { static {} }") |
| 118 | + .initializers |
| 119 | + .first() |
| 120 | + DesugarUtil.setOriginalElement(initializer, DesugarUtil.getOriginalElement(clazz)) |
| 121 | + return clazz.add(initializer) as PsiClassInitializer |
| 122 | + } |
| 123 | + |
| 124 | + private fun findConstructorsCallingSuper(project: Project, clazz: PsiClass): List<PsiMethod> { |
| 125 | + val className = clazz.name ?: return emptyList() |
| 126 | + |
| 127 | + val constructors = clazz.constructors.filter { |
| 128 | + !JavaPsiConstructorUtil.isChainedConstructorCall( |
| 129 | + JavaPsiConstructorUtil.findThisOrSuperCallInConstructor(it) |
| 130 | + ) |
| 131 | + } |
| 132 | + |
| 133 | + if (constructors.isNotEmpty()) { |
| 134 | + return constructors |
| 135 | + } |
| 136 | + |
| 137 | + val constructor = JavaPsiFacade.getElementFactory(project).createConstructor(className) |
| 138 | + DesugarUtil.setOriginalElement(constructor, DesugarUtil.getOriginalElement(clazz)) |
| 139 | + return listOf(clazz.add(constructor) as PsiMethod) |
| 140 | + } |
| 141 | +} |
0 commit comments