简体   繁体   中英

Need help to convert Kotlin to Java

I have zero knowledge on Kotlin, and I knew that Kotlin might be the future, but I have project need to be done with Java, and I've seen this nice post: here but it's done in Kotlin, and I have difficulties change it to Java, esp the (T.() -> Unit)? syntax...

so, anyone, could kind enough to help me convert 2 below to Java?

class LiveMessageEvent<T> : SingleLiveEvent<(T.() -> Unit)?>() {
    fun setEventReceiver(owner: LifecycleOwner, receiver: T) {
        observe(owner, Observer { event ->
            if ( event != null ) {
                receiver.event()
            }
        })
    }

    fun sendEvent(event: (T.() -> Unit)?) {
        value = event
    }
}

and this code:

const val GOOGLE_SIGN_IN : Int = 9001

class LoginViewModel @Inject constructor(
    private val loginRepository: LoginRepository,
    private val googleSignInClient: GoogleSignInClient
): ViewModel() {

    val startActivityForResultEvent = LiveMessageEvent<ActivityNavigation>()
    ..

    //Called on google login button click
    fun googleSignUp() {
        val signInIntent = googleSignInClient.signInIntent
        startActivityForResultEvent.sendEvent { 
startActivityForResult(signInIntent, GOOGLE_SIGN_IN) }
    }

//Called from Activity receving result
fun onResultFromActivity(requestCode: Int, resultCode: Int, data: Intent?) {
    when(requestCode) {
        GOOGLE_SIGN_IN -> {
            val task = GoogleSignIn.getSignedInAccountFromIntent(data)
            googleSignInComplete(task)
        }
        ..
    }
}

private fun googleSignInComplete(completedTask: Task<GoogleSignInAccount>) {
    try {
        val account = completedTask.getResult(ApiException::class.java)
        account?.apply {
            // .. Store user details
            emitUiState(
                showSuccess = Event(R.string.login_successful)
            )
        }
    }catch (e: ApiException) {
        emitUiState(
            showError = Event(R.string.login_failed)
        )
    }
}

Thank you and appreciate the help...

EDIT:

i did try to decompile from Android studio, and here is what i get:

public final class LiveMessageEvent extends SingleLiveEvent {
   public final void setEventReceiver(@NotNull LifecycleOwner owner, final Object receiver) {
      Intrinsics.checkParameterIsNotNull(owner, "owner");
      this.observe(owner, (Observer)(new Observer() {
         // $FF: synthetic method
         // $FF: bridge method
         public void onChanged(Object var1) {
            this.onChanged((Function1)var1);
         }

         public final void onChanged(@Nullable Function1 event) {
            if (event != null) {
               event.invoke(receiver);
            }

         }
      }));
   }

   public final void sendEvent(@Nullable Function1 event) {
      this.setValue(event);
   }
}

I'm not quite sure what is Intrinsics , function1 , invoke and object from the result above...

You just have to do the following to get java code from kotlin:

  1. Menu > Tools > Kotlin > Show Kotlin Bytecode
  2. Click on the Decompile button
  3. Copy the java code

With a recent version (1.2+) of the Kotlin plugin you also can directly do Menu > Tools > Kotlin -> Decompile Kotlin to Java.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM