question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Kotlin. error: Event fields must be declared as public non-final.

See original GitHub issue

Version

    implementation "com.facebook.soloader:soloader:0.6.0"

    // ext.litho_version = '0.32.0'
    implementation "com.facebook.litho:litho-core:$litho_version"
    implementation "com.facebook.litho:litho-widget:$litho_version"
    kapt "com.facebook.litho:litho-processor:$litho_version"
    implementation "com.facebook.litho:litho-fresco:$litho_version"
    implementation "com.facebook.litho:litho-sections-core:$litho_version"
    implementation "com.facebook.litho:litho-sections-widget:$litho_version"
    compileOnly "com.facebook.litho:litho-sections-annotations:$litho_version"
    kapt "com.facebook.litho:litho-sections-processor:$litho_version"

Issues and Steps to Reproduce

Create a Kotlin Custom Event

import com.facebook.litho.annotations.Event

@Event
class StorefrontRowMinimizedEvent(
    var storefrontRowId: String = ""
)

Use this event in a Spec

@LayoutSpec(events = [StorefrontRowMinimizedEvent::class])
object StorefrontItemComponentSpec {
    private const val TAG : String = "StorefrontItem"

You will see error

Event fields must be declared as public non-final.
    private java.lang.String storefrontRowId;
                                         ^

This is how it was compiled in Java

import java.lang.System;

@com.facebook.litho.annotations.Event()
@kotlin.Metadata(...)
public final class StorefrontRowMinimizedEvent {
    @org.jetbrains.annotations.NotNull()
    private java.lang.String storefrontRowId;
    
    @org.jetbrains.annotations.NotNull()
    public final java.lang.String getStorefrontRowId() {
        return null;
    }
    
    public final void setStorefrontRowId(@org.jetbrains.annotations.NotNull()
    java.lang.String p0) {
    }
    
    public StorefrontRowMinimizedEvent(@org.jetbrains.annotations.NotNull()
    java.lang.String storefrontRowId) {
        super();
    }
    
    public StorefrontRowMinimizedEvent() {
        super();
    }
}

Special emphasis on

    @org.jetbrains.annotations.NotNull()
    private java.lang.String storefrontRowId;

Expected Behavior

This should have been public because, in Kotlin, everything is public by default. Also, even if you mark it public it’s still private 😢. The output should be something like this:

    @org.jetbrains.annotations.NotNull()
    public java.lang.String storefrontRowId;

Workaround for now. Use Java

import com.facebook.litho.annotations.Event;

@Event
public class StorefrontItemSelectedEvent {
    public String StorefrontItemId;
}

Link to Code

Intentionally not added because this is fairly easy to reproduce.

Please show some code we can use to reproduce this issue. Consider using PlaygroundComponentSpec to provide a small example of the issue

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
pavlosptcommented, Nov 25, 2019

It can not be public because Kotlin adds setters/getters for the fields and keeps it private. The real fix here should be that the AP will check if this is a Kotlin class in order to use the appropriate getter, when accessing that field. @passy @pasqualeanatriello I could probably have a look at it if needed!

1reaction
colriotcommented, Jan 18, 2021

Hi @NishcheyBhutani . Issue is still open, but the open-source work has already been done by Pavlos. The blocker is in the internal integration at FB.

Read more comments on GitHub >

github_iconTop Results From Across the Web

error: Event fields must be declared as public non-final. · Issue ...
I'm trying to write a custom Event in my application, that includes @IntDef annotation, and contains public final static int fields. ... Error:(19 ......
Read more >
Kotlin calling non final function in constructor works
Another option is to require code to be passed to the base class constructor: abstract class Base(var code: Int) { abstract fun calculate(): ......
Read more >
Contexts and Dependency Injection - Quarkus
This behavior is defined by CDI. But producer methods and fields and observer methods are discovered even if the declaring class is not...
Read more >
FindBugs Bug Descriptions - SourceForge
ME: Public enum method unconditionally sets its field ... Se: The readResolve method must be declared with a return type of Object.
Read more >
JUnit 5 User Guide
Used to declare tags for filtering tests, either at the class or method ... that a test should be aborted instead of marked...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found