ITKeyword,专注技术干货聚合推荐

注册 | 登录

解决visibility - Ways to obfuscate access to prefereably private, but actually public class members for the sake of using them in inline methods

itPublisher 分享于

2020腾讯云10周年活动,优惠非常大!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1040

2020阿里云最低价产品入口,含代金券(新老用户有优惠),
地址https://www.aliyun.com/minisite/goods

What are some ways to obfuscate or limit access to public members of a class?

The motivation behind doing this is that I would like to be able to inline a method, but it needs access to other members in the class, which requires them to be public, however I would prefer these members to be anything but public, but since they must be public, I am looking for ways to obfuscate or limit access to them.

Here is an example of such a case where I would prefer preferablyPrivateLock to be private:

class SomeClass
{
    val preferablyPrivateLock = ReentrantLock()

    inline fun <R> fancyInlineFunction(function:()->R):R
    {
        preferablyPrivateLock.lock()
        try
        {
            return function()
        }
        finally
        {
            preferablyPrivateLock.unlock()
        }
    }
}

P.S. I know this seems like bad OO design; hope this question doesn't cost me any reputation...

inline visibility kotlin
|
  this question
asked Feb 23 '16 at 9:15 Eric 1,456 2 13 24

 | 

2 Answers
2

解决方法

The same can be done without Unit by having the method be an extension on its own class, in this case SomeClass. This simplifies the usage:

class SomeClass
{
    private val preferablyPrivateLock = ReentrantLock()
    val SomeClass.hiddenLock: ReentrantLock
        get() = preferablyPrivateLock

    inline fun <R> fancyInlineFunction(function:()->R):R
    {
        with(this) { hiddenLock }.lock()
        try
        {
            return function()
        }
        finally
        {
            with(this) { hiddenLock }.unlock()
        }
    }
}

class OtherClass {
    fun foo() {
        val x = SomeClass().hiddenLock // ERROR: Unresolved reference hiddenLock
    }
}

This solution was mentioned by @Kirill in Kotlin Slack. It is similar technique to this: In Kotlin, how do I add extension methods to another class, but only visible in a certain context?


|
  this answer
answered Feb 23 '16 at 12:48 Jayson Minard 14.7k 2 46 106

 | 

Here's a way to obfuscate access to preferablyPrivateLock with the help of extensions:

class SomeClass
{
    private val preferablyPrivateLock = ReentrantLock()
    val Unit.obfuscatedLock:ReentrantLock
        get() = preferablyPrivateLock

    inline fun <R> fancyInlineFunction(function:()->R):R
    {
        with(this) {Unit.obfuscatedLock}.lock()
        try
        {
            return function()
        }
        finally
        {
            with(this) {Unit.obfuscatedLock}.unlock()
        }
    }
}

|
  this answer
answered Feb 23 '16 at 9:18 Eric 1,456 2 13 24

 | 


相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。