Package protected alternative in Kotlin

后端 未结 3 2076
离开以前
离开以前 2020-12-01 02:31

In Java, we have package protected (default) modifier for classes, which allows us to have many classes in a single package but exposes only a few and keep the logic encapsu

相关标签:
3条回答
  • 2020-12-01 03:04

    Kotlin, compared to Java, seems to rely on packages model to a lesser degree (e.g. directories structure is not bound to packages). Instead, Kotlin offers internal visibility, which is designed for modular project architecture. Using it, you can encapsulate a part of your code inside a separate module.

    So, on top level declarations you can use

    • private to restrict visibility to the file
    • internal to restrict visibility to the module

    At this point, there is no other option for visibility restriction.

    0 讨论(0)
  • 2020-12-01 03:05

    As a workaround for me on android I've created @PackagePrivate annotation and lint checks to control access. Here you can find the project.

    Lint checks are obviously not that strict as compiler checks and some setup needed to fail the build on errors. But android studio picks up lint checks automatically and shows error immediately while typing. Unfortunately I don't know a way to exclude annotated members from autocomplete.

    Also, as lint is a purely compile-time tool, no checks at runtime performed.

    0 讨论(0)
  • 2020-12-01 03:18

    As @hotkeys points out, you can use the internal keyword in a module or you can put all classes that would otherwise belong in a package inside a single file, but sticking several classes in a file may be a questionable design decision.

    For me, the package visibility is helpful for its documenting value. I want to know what public interface some package is presenting to the rest of the project, hide factory implementation classes and so on.

    So even if it's possible to access package-private classes and methods in Java, I still choose to use the package modifier.

    For this I created a project with a single annotation:

    package com.mycompany.libraries.kotlinannotations;
    
    import static java.lang.annotation.ElementType.CONSTRUCTOR;
    import static java.lang.annotation.ElementType.METHOD;
    import static java.lang.annotation.ElementType.TYPE;
    import static java.lang.annotation.RetentionPolicy.SOURCE;
    
    import java.lang.annotation.Documented;
    import java.lang.annotation.Retention;
    import java.lang.annotation.Target;
    
    @Documented
    @Retention(SOURCE)
    @Target({ TYPE, METHOD, CONSTRUCTOR })
    /**
     * Use in Kotlin code for documentation purposes. 
     * 
     * Whenever a Kotlin class or method is intended to be accesible at package level only.
     *
     */
    public @interface PackagePrivate {
    
    }
    

    Then I can use this annotation in any Kotlin project.

    The second step, which I haven't done yet, is creating a PMD rule to enforce this with maven (or any other build tool for that matter) and also be able to see violations of the rule in my IDE with the pmd plugin.

    There no is full Kotlin support in pmd at this moment but it seems to be expected at some point.

    0 讨论(0)
提交回复
热议问题