Refactor/better kotlin metadata (#1061)

* Kotlin: Don't depend on metadata internals.

* Kotlin: Remap type parameter annotations.

* Kotlin: Bump metadata to 0.9.0
This commit is contained in:
LlamaLad7
2024-03-04 09:40:09 +00:00
committed by GitHub
parent ae1ba0ab86
commit 04ca22c225
4 changed files with 20 additions and 462 deletions

View File

@@ -1,271 +0,0 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 2023 FabricMC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.fabricmc.loom.kotlin.remapping;
import java.util.List;
import kotlinx.metadata.KmAnnotation;
import kotlinx.metadata.KmProperty;
import kotlinx.metadata.internal.extensions.KmClassExtension;
import kotlinx.metadata.internal.extensions.KmConstructorExtension;
import kotlinx.metadata.internal.extensions.KmFunctionExtension;
import kotlinx.metadata.internal.extensions.KmPackageExtension;
import kotlinx.metadata.internal.extensions.KmPropertyExtension;
import kotlinx.metadata.internal.extensions.KmTypeExtension;
import kotlinx.metadata.internal.extensions.KmTypeParameterExtension;
import kotlinx.metadata.jvm.JvmFieldSignature;
import kotlinx.metadata.jvm.JvmMethodSignature;
import kotlinx.metadata.jvm.internal.JvmClassExtension;
import kotlinx.metadata.jvm.internal.JvmConstructorExtension;
import kotlinx.metadata.jvm.internal.JvmFunctionExtension;
import kotlinx.metadata.jvm.internal.JvmPackageExtension;
import kotlinx.metadata.jvm.internal.JvmPropertyExtension;
import kotlinx.metadata.jvm.internal.JvmTypeExtension;
import kotlinx.metadata.jvm.internal.JvmTypeParameterExtension;
import org.jetbrains.annotations.Nullable;
/*
* This is a fun meme. All of these kotlin classes are marked as "internal" so Kotlin code cannot compile against them.
* However, luckily for us the Java compiler has no idea about this, so they can compile against it :D
*
* This file contains Java wrappers around Kotlin classes, to used by Kotlin.
*/
public interface JvmExtensionWrapper {
record Class(JvmClassExtension extension) implements JvmExtensionWrapper {
@Nullable
public static Class get(KmClassExtension classExtension) {
if (classExtension instanceof JvmClassExtension jvmClassExtension) {
return new Class(jvmClassExtension);
}
return null;
}
public List<KmProperty> getLocalDelegatedProperties() {
return extension.getLocalDelegatedProperties();
}
@Nullable
public String getModuleName() {
return extension.getModuleName();
}
public void setModuleName(@Nullable String name) {
extension.setModuleName(name);
}
@Nullable
public String getAnonymousObjectOriginName() {
return extension.getAnonymousObjectOriginName();
}
public void setAnonymousObjectOriginName(@Nullable String name) {
extension.setAnonymousObjectOriginName(name);
}
public int getJvmFlags() {
return extension.getJvmFlags();
}
public void setJvmFlags(int flags) {
extension.setJvmFlags(flags);
}
}
record Package(JvmPackageExtension extension) {
@Nullable
public static Package get(KmPackageExtension packageExtension) {
if (packageExtension instanceof JvmPackageExtension jvmPackageExtension) {
return new Package(jvmPackageExtension);
}
return null;
}
public List<KmProperty> getLocalDelegatedProperties() {
return extension.getLocalDelegatedProperties();
}
@Nullable
public String getModuleName() {
return extension.getModuleName();
}
public void setModuleName(@Nullable String name) {
extension.setModuleName(name);
}
}
record Function(JvmFunctionExtension extension) {
@Nullable
public static Function get(KmFunctionExtension functionExtension) {
if (functionExtension instanceof JvmFunctionExtension jvmFunctionExtension) {
return new Function(jvmFunctionExtension);
}
return null;
}
@Nullable
public JvmMethodSignature getSignature() {
return extension.getSignature();
}
public void setSignature(@Nullable JvmMethodSignature signature) {
extension.setSignature(signature);
}
@Nullable
public String getLambdaClassOriginName() {
return extension.getLambdaClassOriginName();
}
public void setLambdaClassOriginName(@Nullable String name) {
extension.setLambdaClassOriginName(name);
}
}
record Property(JvmPropertyExtension extension) {
@Nullable
public static Property get(KmPropertyExtension propertyExtension) {
if (propertyExtension instanceof JvmPropertyExtension jvmPropertyExtension) {
return new Property(jvmPropertyExtension);
}
return null;
}
public int getJvmFlags() {
return extension.getJvmFlags();
}
public void setJvmFlags(int flags) {
extension.setJvmFlags(flags);
}
@Nullable
public JvmFieldSignature getFieldSignature() {
return extension.getFieldSignature();
}
public void setFieldSignature(@Nullable JvmFieldSignature signature) {
extension.setFieldSignature(signature);
}
@Nullable
public JvmMethodSignature getGetterSignature() {
return extension.getGetterSignature();
}
public void setGetterSignature(@Nullable JvmMethodSignature signature) {
extension.setGetterSignature(signature);
}
@Nullable
public JvmMethodSignature getSetterSignature() {
return extension.getSetterSignature();
}
public void setSetterSignature(@Nullable JvmMethodSignature signature) {
extension.setSetterSignature(signature);
}
@Nullable
public JvmMethodSignature getSyntheticMethodForAnnotations() {
return extension.getSyntheticMethodForAnnotations();
}
public void setSyntheticMethodForAnnotations(@Nullable JvmMethodSignature signature) {
extension.setSyntheticMethodForAnnotations(signature);
}
@Nullable
public JvmMethodSignature getSyntheticMethodForDelegate() {
return extension.getSyntheticMethodForDelegate();
}
public void setSyntheticMethodForDelegate(@Nullable JvmMethodSignature signature) {
extension.setSyntheticMethodForDelegate(signature);
}
}
record Constructor(JvmConstructorExtension extension) {
@Nullable
public static Constructor get(KmConstructorExtension constructorExtension) {
if (constructorExtension instanceof JvmConstructorExtension jvmConstructorExtension) {
return new Constructor(jvmConstructorExtension);
}
return null;
}
@Nullable
public JvmMethodSignature getSignature() {
return extension.getSignature();
}
public void setSignature(@Nullable JvmMethodSignature signature) {
extension.setSignature(signature);
}
}
record TypeParameter(JvmTypeParameterExtension extension) {
@Nullable
public static TypeParameter get(KmTypeParameterExtension typeParameterExtension) {
if (typeParameterExtension instanceof JvmTypeParameterExtension jvmTypeParameterExtension) {
return new TypeParameter(jvmTypeParameterExtension);
}
return null;
}
public List<KmAnnotation> getAnnotations() {
return extension.getAnnotations();
}
}
record Type(JvmTypeExtension extension) {
@Nullable
public static Type get(KmTypeExtension typeExtension) {
if (typeExtension instanceof JvmTypeExtension jvmTypeExtension) {
return new Type(jvmTypeExtension);
}
return null;
}
public boolean isRaw() {
return extension.isRaw();
}
public void setRaw(boolean raw) {
extension.setRaw(raw);
}
public List<KmAnnotation> getAnnotations() {
return extension.getAnnotations();
}
}
}

View File

@@ -31,7 +31,6 @@ import java.util.List;
import java.util.stream.Stream;
import net.fabricmc.loom.LoomGradlePlugin;
import net.fabricmc.loom.kotlin.remapping.JvmExtensionWrapper;
import net.fabricmc.loom.kotlin.remapping.KotlinMetadataTinyRemapperExtensionImpl;
/**
@@ -62,8 +61,7 @@ public class KotlinRemapperClassloader extends URLClassLoader {
public static KotlinRemapperClassloader create(KotlinClasspath classpathProvider) {
// Include the libraries that are not on the kotlin classpath.
final Stream<URL> loomUrls = getClassUrls(
KotlinMetadataTinyRemapperExtensionImpl.class, // Loom (Kotlin)
JvmExtensionWrapper.class // Loom (Java)
KotlinMetadataTinyRemapperExtensionImpl.class // Loom (Kotlin)
);
final URL[] urls = Stream.concat(

View File

@@ -40,18 +40,17 @@ import kotlinx.metadata.KmTypeAlias
import kotlinx.metadata.KmTypeParameter
import kotlinx.metadata.KmTypeProjection
import kotlinx.metadata.KmValueParameter
import kotlinx.metadata.internal.extensions.KmClassExtension
import kotlinx.metadata.internal.extensions.KmConstructorExtension
import kotlinx.metadata.internal.extensions.KmFunctionExtension
import kotlinx.metadata.internal.extensions.KmPackageExtension
import kotlinx.metadata.internal.extensions.KmPropertyExtension
import kotlinx.metadata.internal.extensions.KmTypeAliasExtension
import kotlinx.metadata.internal.extensions.KmTypeExtension
import kotlinx.metadata.internal.extensions.KmTypeParameterExtension
import kotlinx.metadata.internal.extensions.KmValueParameterExtension
import kotlinx.metadata.isLocalClassName
import kotlinx.metadata.jvm.JvmFieldSignature
import kotlinx.metadata.jvm.JvmMethodSignature
import kotlinx.metadata.jvm.annotations
import kotlinx.metadata.jvm.fieldSignature
import kotlinx.metadata.jvm.getterSignature
import kotlinx.metadata.jvm.localDelegatedProperties
import kotlinx.metadata.jvm.setterSignature
import kotlinx.metadata.jvm.signature
import kotlinx.metadata.jvm.syntheticMethodForAnnotations
import kotlinx.metadata.jvm.syntheticMethodForDelegate
import kotlinx.metadata.jvm.toJvmInternalName
import org.objectweb.asm.commons.Remapper
@@ -68,7 +67,7 @@ class KotlinClassRemapper(private val remapper: Remapper) {
clazz.nestedClasses.replaceAll(this::remap)
clazz.sealedSubclasses.replaceAll(this::remap)
clazz.contextReceiverTypes.replaceAll(this::remap)
clazz.getExtensions().replaceAll(this::remap)
clazz.localDelegatedProperties.replaceAll(this::remap)
return clazz
}
@@ -81,7 +80,7 @@ class KotlinClassRemapper(private val remapper: Remapper) {
pkg.functions.replaceAll(this::remap)
pkg.properties.replaceAll(this::remap)
pkg.typeAliases.replaceAll(this::remap)
pkg.getExtensions().replaceAll(this::remap)
pkg.localDelegatedProperties.replaceAll(this::remap)
return pkg
}
@@ -107,7 +106,7 @@ class KotlinClassRemapper(private val remapper: Remapper) {
type.abbreviatedType = type.abbreviatedType?.let { remap(it) }
type.outerType = type.outerType?.let { remap(it) }
type.flexibleTypeUpperBound = type.flexibleTypeUpperBound?.let { remap(it) }
type.getExtensions().replaceAll(this::remap)
type.annotations.replaceAll(this::remap)
return type
}
@@ -117,7 +116,7 @@ class KotlinClassRemapper(private val remapper: Remapper) {
function.contextReceiverTypes.replaceAll(this::remap)
function.valueParameters.replaceAll(this::remap)
function.returnType = remap(function.returnType)
function.getExtensions().replaceAll(this::remap)
function.signature = function.signature?.let { remap(it) }
return function
}
@@ -127,7 +126,11 @@ class KotlinClassRemapper(private val remapper: Remapper) {
property.contextReceiverTypes.replaceAll(this::remap)
property.setterParameter = property.setterParameter?.let { remap(it) }
property.returnType = remap(property.returnType)
property.getExtensions().replaceAll(this::remap)
property.fieldSignature = property.fieldSignature?.let { remap(it) }
property.getterSignature = property.getterSignature?.let { remap(it) }
property.setterSignature = property.setterSignature?.let { remap(it) }
property.syntheticMethodForAnnotations = property.syntheticMethodForAnnotations?.let { remap(it) }
property.syntheticMethodForDelegate = property.syntheticMethodForDelegate?.let { remap(it) }
return property
}
@@ -136,19 +139,18 @@ class KotlinClassRemapper(private val remapper: Remapper) {
typeAlias.underlyingType = remap(typeAlias.underlyingType)
typeAlias.expandedType = remap(typeAlias.expandedType)
typeAlias.annotations.replaceAll(this::remap)
typeAlias.getExtensions().replaceAll(this::remap)
return typeAlias
}
private fun remap(constructor: KmConstructor): KmConstructor {
constructor.valueParameters.replaceAll(this::remap)
constructor.getExtensions().replaceAll(this::remap)
constructor.signature = constructor.signature?.let { remap(it) }
return constructor
}
private fun remap(typeParameter: KmTypeParameter): KmTypeParameter {
typeParameter.upperBounds.replaceAll(this::remap)
typeParameter.getExtensions().replaceAll(this::remap)
typeParameter.annotations.replaceAll(this::remap)
return typeParameter
}
@@ -163,7 +165,6 @@ class KotlinClassRemapper(private val remapper: Remapper) {
private fun remap(valueParameter: KmValueParameter): KmValueParameter {
valueParameter.type = remap(valueParameter.type)
valueParameter.varargElementType = valueParameter.varargElementType?.let { remap(it) }
valueParameter.getExtensions().replaceAll(this::remap)
return valueParameter
}
@@ -171,76 +172,6 @@ class KotlinClassRemapper(private val remapper: Remapper) {
return KmAnnotation(remap(annotation.className), annotation.arguments)
}
private fun remap(classExtension: KmClassExtension): KmClassExtension {
JvmExtensionWrapper.Class.get(classExtension)?.let {
it.localDelegatedProperties.replaceAll(this::remap)
return it.extension
}
return classExtension
}
private fun remap(packageExtension: KmPackageExtension): KmPackageExtension {
JvmExtensionWrapper.Package.get(packageExtension)?.let {
it.localDelegatedProperties.replaceAll(this::remap)
return it.extension
}
return packageExtension
}
private fun remap(typeExtension: KmTypeExtension): KmTypeExtension {
JvmExtensionWrapper.Type.get(typeExtension)?.let {
it.annotations.replaceAll(this::remap)
return it.extension
}
return typeExtension
}
private fun remap(functionExtension: KmFunctionExtension): KmFunctionExtension {
JvmExtensionWrapper.Function.get(functionExtension)?.let {
it.signature = it.signature?.let { sig -> remap(sig) }
return it.extension
}
return functionExtension
}
private fun remap(propertyExtension: KmPropertyExtension): KmPropertyExtension {
JvmExtensionWrapper.Property.get(propertyExtension)?.let {
it.fieldSignature = it.fieldSignature?.let { sig -> remap(sig) }
it.getterSignature = it.getterSignature?.let { sig -> remap(sig) }
it.setterSignature = it.setterSignature?.let { sig -> remap(sig) }
it.syntheticMethodForAnnotations = it.syntheticMethodForAnnotations?.let { sig -> remap(sig) }
it.syntheticMethodForDelegate = it.syntheticMethodForDelegate?.let { sig -> remap(sig) }
return it.extension
}
return propertyExtension
}
private fun remap(typeAliasExtension: KmTypeAliasExtension): KmTypeAliasExtension {
return typeAliasExtension
}
private fun remap(typeParameterExtension: KmTypeParameterExtension): KmTypeParameterExtension {
return typeParameterExtension
}
private fun remap(valueParameterExtension: KmValueParameterExtension): KmValueParameterExtension {
return valueParameterExtension
}
private fun remap(constructorExtension: KmConstructorExtension): KmConstructorExtension {
JvmExtensionWrapper.Constructor.get(constructorExtension)?.let {
it.signature = it.signature?.let { sig -> remap(sig) }
return it.extension
}
return constructorExtension
}
private fun remap(signature: JvmMethodSignature): JvmMethodSignature {
return JvmMethodSignature(signature.name, remapper.mapMethodDesc(signature.descriptor))
}

View File

@@ -1,100 +0,0 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 2023 FabricMC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
@file:Suppress("UNCHECKED_CAST")
package net.fabricmc.loom.kotlin.remapping
import kotlinx.metadata.KmClass
import kotlinx.metadata.KmConstructor
import kotlinx.metadata.KmFunction
import kotlinx.metadata.KmPackage
import kotlinx.metadata.KmProperty
import kotlinx.metadata.KmType
import kotlinx.metadata.KmTypeAlias
import kotlinx.metadata.KmTypeParameter
import kotlinx.metadata.KmValueParameter
import kotlinx.metadata.internal.extensions.KmClassExtension
import kotlinx.metadata.internal.extensions.KmConstructorExtension
import kotlinx.metadata.internal.extensions.KmFunctionExtension
import kotlinx.metadata.internal.extensions.KmPackageExtension
import kotlinx.metadata.internal.extensions.KmPropertyExtension
import kotlinx.metadata.internal.extensions.KmTypeAliasExtension
import kotlinx.metadata.internal.extensions.KmTypeExtension
import kotlinx.metadata.internal.extensions.KmTypeParameterExtension
import kotlinx.metadata.internal.extensions.KmValueParameterExtension
import java.lang.reflect.Field
import kotlin.reflect.KClass
val KM_CLASS_EXTENSIONS = getField(KmClass::class)
val KM_PACKAGE_EXTENSIONS = getField(KmPackage::class)
val KM_TYPE_EXTENSIONS = getField(KmType::class)
val KM_FUNCTION_EXTENSIONS = getField(KmFunction::class)
val KM_PROPERTY_EXTENSIONS = getField(KmProperty::class)
val KM_TYPE_ALIAS_EXTENSIONS = getField(KmTypeAlias::class)
val KM_TYPE_PARAMETER_EXTENSIONS = getField(KmTypeParameter::class)
val KM_VALUE_PARAMETER_EXTENSIONS = getField(KmValueParameter::class)
val KM_CONSTRUCTOR_EXTENSIONS = getField(KmConstructor::class)
fun KmClass.getExtensions(): MutableList<KmClassExtension> {
return KM_CLASS_EXTENSIONS.get(this) as MutableList<KmClassExtension>
}
fun KmPackage.getExtensions(): MutableList<KmPackageExtension> {
return KM_PACKAGE_EXTENSIONS.get(this) as MutableList<KmPackageExtension>
}
fun KmType.getExtensions(): MutableList<KmTypeExtension> {
return KM_TYPE_EXTENSIONS.get(this) as MutableList<KmTypeExtension>
}
fun KmFunction.getExtensions(): MutableList<KmFunctionExtension> {
return KM_FUNCTION_EXTENSIONS.get(this) as MutableList<KmFunctionExtension>
}
fun KmProperty.getExtensions(): MutableList<KmPropertyExtension> {
return KM_PROPERTY_EXTENSIONS.get(this) as MutableList<KmPropertyExtension>
}
fun KmTypeAlias.getExtensions(): MutableList<KmTypeAliasExtension> {
return KM_TYPE_ALIAS_EXTENSIONS.get(this) as MutableList<KmTypeAliasExtension>
}
fun KmTypeParameter.getExtensions(): MutableList<KmTypeParameterExtension> {
return KM_TYPE_PARAMETER_EXTENSIONS.get(this) as MutableList<KmTypeParameterExtension>
}
fun KmValueParameter.getExtensions(): MutableList<KmValueParameterExtension> {
return KM_VALUE_PARAMETER_EXTENSIONS.get(this) as MutableList<KmValueParameterExtension>
}
fun KmConstructor.getExtensions(): MutableList<KmConstructorExtension> {
return KM_CONSTRUCTOR_EXTENSIONS.get(this) as MutableList<KmConstructorExtension>
}
private fun getField(clazz: KClass<*>): Field {
val field = clazz.java.getDeclaredField("extensions")
field.isAccessible = true
return field
}