0

I'd like to rewrite some of my code to Kotlin. I pasted the working code first to show the origin.
I tried

The following is a working excerpt written in Java:

Function1.class (part of a framework):

public interface Function1<T1, R> {
    R call(T1 var1);
}

Authmanager.class:

public boolean isAuthed() {
    Boolean isAuthed = getWithAuthPolicyManager(
            authPolicyManager -> authPolicyManager.getBoolean(IS_AUTHED)
    );

    if (isAuthed != null) {
        return isAuthed;
    } else {
        return false;
    }
}

public <T> T getWithAuthPolicyManager(@NonNull Function1<AuthManager, T> function) {
    Objects.requireNonNull(function);
    synchronized (AUTH_POLICY_LOCK) {
        try {
            openAuthPolicyManager();
            return function.call(authPolicyManager);
        } catch (OpenFailureException | EncryptionError e) {
            LOGGER.error("Error:", e);
            return null;
        } finally {
            authPolicyManager.close();
        }
    }
}

I would like to rewrite it to Kotlin but I cannot:
Authmanager.kt

fun isAuthed(): Boolean {
    val isAuthed =
        getWithAuthPolicyManager<Boolean>({
            authPolicyManager -> authPolicyManager.getBoolean(IS_AUTHED) 
        })
    return isAuthed ?: false
}

fun <T> getWithAuthPolicyManager(function: Function1<AuthManager, T>) : T? {
    synchronized (AUTH_POLICY_LOCK) {
        return try {
            openAuthPolicyManager();
            return function.call(authPolicyManager);
        } catch (OpenFailureException | EncryptionError e) {
            LOGGER.error("Error", e);
            null;
        } finally {
            authPolicyManager.close();
            null
        }
    }
}

Actually, I just copy-pasted the Java code to the Kotlin class to automatically convert to Kotlin. However, the linter says:

Type mismatch.  
Required: Function1<AuthManager, Boolean>  
Found:    (AuthManager) -> Boolean!

to this line getWithAuthPolicyManager<Boolean>({authPolicyManager -> authPolicyManager.getBoolean(IS_AUTHED)}) in fun isAuthed().

Do you have any idea how this could be fixed?

1 Answer 1

1

Your second function is not converted at all, it still Java code. I used converter from try.kotlinlang.org and got the following code:

fun <T> getWithAuthPolicyManager(function : Function1<AuthManager, T>) : T? {
  synchronized (AUTH_POLICY_LOCK) {
    try
    {
      openAuthPolicyManager()
      return function.call(authPolicyManager)
    }
    catch (e : OpenFailureException) {
      LOGGER.error("Error", e)
      return null
    }
    catch (e : EncryptionError) {
      LOGGER.error("Error", e)
      return null
    }
    finally
    {
      authPolicyManager.close()
    }
  }
}

After light cleanup it should look like this:

inline fun <T> getWithAuthPolicyManager(function : (AuthManager) -> T?) : T? {
  synchronized (AUTH_POLICY_LOCK) {
    return try
    {
      openAuthPolicyManager()
      function(authPolicyManager)
    }
    catch (e : OpenFailureException) {
      LOGGER.error("Error", e)
      null
    }
    catch (e : EncryptionError) {
      LOGGER.error("Error", e)
      null
    }
    finally
    {
      authPolicyManager.close()
    }
  }
}

Please look into doing Kotlin tutorial if you can't tell Kotlin and Java function declarations apart.

Sign up to request clarification or add additional context in comments.

2 Comments

Mea culpa, I copy-pasted the second Kotlin function wrongly to here. Thanks for spending your time on it, anyway! However, the problem still stands. Do you have any idea ? (function calls in getWithAuthPolicyManager() contains non-public-API calls, so I cannot make it an inline function)
Also changing function : Function1<AuthManager, T> to function : (AuthManager) -> T? solved the problem! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.