https://kotlinlang.org logo
#compiler
Title
# compiler
a

aramaki

11/23/2023, 12:11 PM
Given an
IrClassSymbol
that I got using
pluginContext.referenceClass(FqName("package.MyClass"))
, how can I add a new function
myFun
I've just created to that class?
j

Javier

11/23/2023, 12:14 PM
you can get the
IrClass
from the symbol, which contains
declarations: MutableList<IrDeclaration>
so you can add there a
IrSimpleFunction
for example
a

aramaki

11/23/2023, 2:04 PM
Could you please give me an example on how can I get the
IrClass
from the symbol?
j

Javier

11/23/2023, 2:07 PM
I an mobile so I can’t check it directly but maybe
owner
?
a

aramaki

11/23/2023, 2:10 PM
Hm, I've tried using
owner
but if I add the function there I got an exception, which is weird
Copy code
The root cause kotlin.UninitializedPropertyAccessException was thrown at: org.jetbrains.kotlin.ir.declarations.impl.IrFunctionCommonImpl.getParent(IrFunctionImpl.kt:41)
	at org.jetbrains.kotlin.backend.common.CodegenUtil.reportBackendException(CodegenUtil.kt:253)
	at org.jetbrains.kotlin.backend.common.CodegenUtil.reportBackendException$default(CodegenUtil.kt:237)
	at org.jetbrains.kotlin.backend.common.phaser.PerformByIrFilePhase.invokeSequential(performByIrFile.kt:68)
	at org.jetbrains.kotlin.backend.common.phaser.PerformByIrFilePhase.invoke(performByIrFile.kt:55)
	at org.jetbrains.kotlin.backend.common.phaser.PerformByIrFilePhase.invoke(performByIrFile.kt:41)
	at org.jetbrains.kotlin.backend.common.phaser.NamedCompilerPhase.phaseBody(CompilerPhase.kt:147)
	at org.jetbrains.kotlin.backend.common.phaser.AbstractNamedCompilerPhase.invoke(CompilerPhase.kt:94)
	at org.jetbrains.kotlin.backend.common.phaser.CompositePhase.invoke(PhaseBuilders.kt:29)
	at org.jetbrains.kotlin.backend.common.phaser.CompositePhase.invoke(PhaseBuilders.kt:16)
	at org.jetbrains.kotlin.backend.common.phaser.NamedCompilerPhase.phaseBody(CompilerPhase.kt:147)
	at org.jetbrains.kotlin.backend.common.phaser.AbstractNamedCompilerPhase.invoke(CompilerPhase.kt:94)
	at org.jetbrains.kotlin.backend.common.phaser.CompilerPhaseKt.invokeToplevel(CompilerPhase.kt:43)
	at org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory.invokeLowerings(JvmIrCodegenFactory.kt:308)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.runLowerings(KotlinToJVMBytecodeCompiler.kt:331)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli(KotlinToJVMBytecodeCompiler.kt:115)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli$default(KotlinToJVMBytecodeCompiler.kt:47)
	at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:168)
	at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:53)
	at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:100)
	at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:46)
	at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:101)
	at org.jetbrains.kotlin.incremental.IncrementalJvmCompilerRunner.runCompiler(IncrementalJvmCompilerRunner.kt:495)
	at org.jetbrains.kotlin.incremental.IncrementalJvmCompilerRunner.runCompiler(IncrementalJvmCompilerRunner.kt:133)
	at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.doCompile(IncrementalCompilerRunner.kt:486)
	at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compileImpl(IncrementalCompilerRunner.kt:409)
	at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compileNonIncrementally(IncrementalCompilerRunner.kt:290)
	at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compile(IncrementalCompilerRunner.kt:112)
	at org.jetbrains.kotlin.daemon.CompileServiceImplBase.execIncrementalCompiler(CompileServiceImpl.kt:627)
	at org.jetbrains.kotlin.daemon.CompileServiceImplBase.access$execIncrementalCompiler(CompileServiceImpl.kt:101)
	at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:1587)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
	at java.base/java.lang.reflect.Method.invoke(Method.java:577)
	at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:360)
	at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:200)
	at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:197)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:712)
	at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196)
	at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:598)
	at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:844)
	at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:721)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
	at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:720)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property parent has not been initialized
	at org.jetbrains.kotlin.ir.declarations.impl.IrFunctionCommonImpl.getParent(IrFunctionImpl.kt:41)
	at org.jetbrains.kotlin.backend.jvm.MemoizedMultiFieldValueClassReplacements.quickCheckIfFunctionIsNotApplicable(MemoizedMultiFieldValueClassReplacements.kt:266)
	at org.jetbrains.kotlin.backend.jvm.MemoizedValueClassAbstractReplacements.getReplacementFunction(MemoizedValueClassAbstractReplacements.kt:33)
	at org.jetbrains.kotlin.backend.jvm.lower.JvmValueClassAbstractLowering.transformFunctionFlat(JvmValueClassAbstractLowering.kt:42)
	at org.jetbrains.kotlin.backend.jvm.lower.JvmMultiFieldValueClassLowering$visitClassNew$1.invoke(JvmMultiFieldValueClassLowering.kt:290)
	at org.jetbrains.kotlin.backend.jvm.lower.JvmMultiFieldValueClassLowering$visitClassNew$1.invoke(JvmMultiFieldValueClassLowering.kt:288)
	at org.jetbrains.kotlin.ir.util.TransformKt.transformDeclarationsFlat(transform.kt:94)
	at org.jetbrains.kotlin.backend.jvm.lower.JvmMultiFieldValueClassLowering.visitClassNew(JvmMultiFieldValueClassLowering.kt:288)
	at org.jetbrains.kotlin.backend.jvm.lower.JvmValueClassLoweringDispatcher.visitClassNew(JvmValueClassLoweringDispatcher.kt:51)
	at org.jetbrains.kotlin.backend.jvm.lower.JvmValueClassLoweringDispatcher.visitClassNew(JvmValueClassLoweringDispatcher.kt:35)
	at org.jetbrains.kotlin.backend.common.IrElementTransformerVoidWithContext.visitClass(IrElementTransformerVoidWithContext.kt:62)
	at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitClass(IrElementTransformerVoid.kt:67)
	at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.visitClass(IrElementTransformerVoid.kt:24)
	at org.jetbrains.kotlin.ir.declarations.IrClass.accept(IrClass.kt:64)
	at org.jetbrains.kotlin.ir.IrElementBase.transform(IrElementBase.kt:24)
	at org.jetbrains.kotlin.ir.util.TransformKt.transformInPlace(transform.kt:35)
	at org.jetbrains.kotlin.ir.declarations.IrFile.transformChildren(IrFile.kt:40)
	at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoidKt.transformChildrenVoid(IrElementTransformerVoid.kt:346)
	at org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid.transformChildrenVoid(IrElementTransformerVoid.kt:341)
	at org.jetbrains.kotlin.backend.jvm.lower.JvmValueClassLoweringDispatcher.lower(JvmValueClassLoweringDispatcher.kt:42)
	at org.jetbrains.kotlin.backend.common.phaser.FileLoweringPhaseAdapter.invoke(PhaseBuilders.kt:120)
	at org.jetbrains.kotlin.backend.common.phaser.FileLoweringPhaseAdapter.invoke(PhaseBuilders.kt:116)
	at org.jetbrains.kotlin.backend.common.phaser.NamedCompilerPhase.phaseBody(CompilerPhase.kt:147)
	at org.jetbrains.kotlin.backend.common.phaser.AbstractNamedCompilerPhase.invoke(CompilerPhase.kt:94)
	at org.jetbrains.kotlin.backend.common.phaser.PerformByIrFilePhase.invokeSequential(performByIrFile.kt:65)
j

Javier

11/23/2023, 2:12 PM
probably the function you are building is missing setting some property which is
lateinit var
All of those are mandatory
a

aramaki

11/23/2023, 2:29 PM
setting
myFunc.parent = classSymbol.owner
did the trick, thank you
🙂 1
3 Views