Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin Error : 'public' function exposes its 'public/*package*/' return type argument

I am new to Kotlin and trying out to write some project using the language. I am using Java library and extending a class from the library in my project and I am seeing this error message.

'public' function exposes its 'public/*package*/' return type argument FooSettings

I understand the problem is but I am not sure how to fix it in Kotlin since I am still trying get familiar with Kotlin. I can see that Kotlin is being smart and only trying to return of type that extends FooSettings. However the problem is FooSettings is package public only which means that I cannot access if in my Kotlin project.

I did some research about Kotlin generics and use of in or out but I wasn't able to fix the problem.

Is there any work around that I can do in my Kotlin project to fix the error I am seeing?

Code snippet

This is sample of Java library class: Note, I have no way to changing the implementation of the library. I must use this Library and extend it in Kotlin.

It seems odd to me that the java library is written such a way and expect it to be overridden but that is question for another day.

import java.util.Collections;
import java.util.List;

public abstract class ClassA {
    public List<FooBuilder<?>> getBuilder(Foo foo) {
        return Collections.emptyList();
    }
}

public class Foo {
}

public abstract class FooBuilder<U extends FooBuilder.FooSettings> {
    // implementation of Class
    abstract static class FooSettings {
        // implementation of Class
    }
}

Normally Java classes would override the method like such:

import java.util.List;

public class MyJavaClassA extends ClassA {
    @Override public List<FooBuilder<?>> getBuilder(final Foo foo) {
        // implementation
    }
}

But I am trying to write in Kotlin such that it looks like: Reminder that this Kotlin is depending on the Java library and does not have access to package public classes.

class MyKotlinClassA : ClassA() {
    override fun getBuilder(foo: Foo): MutableList<FooBuilder<*>> {
        // implementation
    }
}

This causes error

'public' function exposes its 'public/*package*/' return type argument FooSettings
like image 931
Zuerst Avatar asked Oct 28 '18 07:10

Zuerst


1 Answers

I presume that by "package public" you meant "package private"? In your example, FooBuilder.FooSettings has no visibility modifier so uses the Java default of package private. Assuming that's what you meant...

You will be able to access the package private class, FooSettings, in your Kotlin code, but only if you put that Kotlin code in a package matching the one where FooSettings is declared.

You'll still get the same compilation error, but that's not because you can't access the type: it's because you're trying to use it in a context which is more visible than the type's declaration. i.e. you're trying to take a package private type and use it as part of a public method's signature, which isn't allowed. To get round that problem you need to mark your Kotlin class as internal.

It's might also be worth mentioning that internal for Kotlin means it's visible in that module, not in that package. This is all explained in more detail here.

like image 61
Yoni Gibbs Avatar answered Sep 19 '22 17:09

Yoni Gibbs