Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unsupported operation exception when java method overrides a property #462

Closed
yigit opened this issue May 27, 2021 · 0 comments · Fixed by #465
Closed

Unsupported operation exception when java method overrides a property #462

yigit opened this issue May 27, 2021 · 0 comments · Fixed by #465
Assignees

Comments

@yigit
Copy link
Collaborator

yigit commented May 27, 2021

When a java method overrides a kotlin property declaration, calling javaMethod.asMemberOf(javaType) throws unsupported operation exception.

e.g.

interface MyKotlinInterface {
    val x:Int
    var y:Int
}
class JavaImpl implements MyKotlinInterface { // remove implements and it will work fine
    public int getX() {
        return 1;
    }
    public int getY() {
        return 1;
    }
    public void setY(int value) {
    }
}

This test throws the exception:

resolver.getClassDeclarationByName("JavaImpl").let { javaImplDecl ->
            checkNotNull(javaImplDecl)
            val getX = javaImplDecl.getDeclaredFunctions().first { it.simpleName.asString() == "getX" }
            results.add(getX.asMemberOf(javaImplDecl.asStarProjectedType()).toSignature())
        }

Full repro: yigit@f703801

This might be related to #248 but not sure. Hit this while updating Room to beta01.

stacktrace:

error: [ksp] java.lang.UnsupportedOperationException
	at org.jetbrains.kotlin.descriptors.impl.PropertyAccessorDescriptorImpl.substitute(PropertyAccessorDescriptorImpl.java:122)
	at org.jetbrains.kotlin.descriptors.impl.PropertyAccessorDescriptorImpl.substitute(PropertyAccessorDescriptorImpl.java:31)
	at org.jetbrains.kotlin.resolve.calls.inference.DescriptorRelatedInferenceUtilsKt.substitute(DescriptorRelatedInferenceUtils.kt:42)
	at com.google.devtools.ksp.processing.impl.ResolverImpl.computeAsMemberOf(ResolverImpl.kt:757)
	at com.google.devtools.ksp.processing.impl.ResolverImpl.asMemberOf$compiler_plugin(ResolverImpl.kt:735)
	at com.google.devtools.ksp.symbol.impl.java.KSFunctionDeclarationJavaImpl.asMemberOf(KSFunctionDeclarationJavaImpl.kt:121)
@neetopia neetopia self-assigned this Jun 1, 2021
copybara-service bot pushed a commit to androidx/androidx that referenced this issue Jun 2, 2021
* KSP beta01 introduces an api change in function overrides where it can
handle cases when a java method overrides a kotlin property. This also
means the return type of `KSFunction.findOverridee` is a KSDeclaration
instead of KSFunction. We use this when resolving types so I've updated
Room to expect a property declaration as well. It was also affecting
override variance resolution but that code only ever runs for kotlin
sources so it was not necessary to handle property overrides there.

* Adding more tests to java overriding kotlin property revelated another
bug where we were setting an invalid parameter name for property setter
values. (<set-?>). That name is valid in byte code but not in source
code. I've updated JavacVariableElement and KotlinSynthetic to use a
sanitized name instead. We could've done it while generating code but
since many other things refer to these parameters, it is safer to do it
while parsing parameters and hide the detail from the rest of the
processing.

* One of the new tests I've added hit a bug in KSP that revelated after
KSP fixed "java method overrides kotlin property" case. Added a try
catch helper for it:
google/ksp#462

* Previously, when listing methods in a kotlin class, we always listed
setters before getters which made the source code match tests slightly
more difficult. Changed it to keep setter/getter of a property together
in the methods list.

* KSP changed its processor provider interface, updated Room
accordingly. (it now receives 1 container object instead of multiple
parameters)

Bug: 160322705
Test: existing tests + more tests for overrides

Change-Id: I92ec446b59cdeccc37bd91308d1a20acf3fe959a
copybara-service bot pushed a commit to androidx/androidx that referenced this issue Aug 20, 2021
The KSP bug around java overriding kotlin properties is now
fixed hence we don't need the workaround anymore.

Issue: google/ksp#462
Bug: 160322705
Test: existing tests pass without the workaround
Change-Id: I36b0b0b83f314738d4decc5d8acccb71aef50b20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants