kotlin在线转java_Kotlin转Java记录

kotlin在线转java_Kotlin转Java记录昨天写了一篇PermissionsDispatcher初探,文章最后说要用java重写processor模块,今天花了半天时间完成,现在把代码放在github上:https://github.com/ileelay/PermissionsDispatcher/tree/2.2.0j,欢迎大家纠正!同时感慨到Kotlin太强大,Java太繁琐;虽然Kotlin语法很简练,终究还是基于JVM,同时这篇…

昨天写了一篇PermissionsDispatcher初探,文章最后说要用java重写processor模块,今天花了半天时间完成,现在把代码放在github上:https://github.com/ileelay/PermissionsDispatcher/tree/2.2.0j,欢迎大家纠正!同时感慨到Kotlin太强大,Java太繁琐;

虽然Kotlin语法很简练,终究还是基于JVM,同时这篇文章不是循序渐进的Kotlin语法讲解,只是为了记录在转PermissionsDispatcher时的粗略见解;

普通class

kotlin是这样子写的

class NoParametersAllowedException(e: ExecutableElement) : RuntimeException(“Method ‘${e.simpleString()}()’ must not have any parameters”)

为了保持参数一致,我用java是这样写的:

public class NoParametersAllowedException extends RuntimeException {

public static RuntimeException getInstance(ExecutableElement e) {

return new NoParametersAllowedException(“Method “+e.getSimpleName().toString()+” must not have any parameters”);

}

private NoParametersAllowedException(String s) {

super(s);

}

}

常量的定义

kotlin这样

//Constants.kt

val FILE_COMMENT = “This file was generated by PermissionsDispatcher. Do not modify!”

val GEN_CLASS_SUFFIX = “PermissionsDispatcher”

val GEN_REQUESTCODE_PREFIX = “REQUEST_”

val GEN_PERMISSION_PREFIX = “PERMISSION_”

val GEN_PENDING_PREFIX = “PENDING_”

val GEN_WITHCHECK_SUFFIX = “WithCheck”

val GEN_PERMISSIONREQUEST_SUFFIX = “PermissionRequest”

说实话,kotlin这样写好变态,连class关键字都不用声明,我用java也尽量保持简洁,可还是比不过它;

public interface Constants {

String FILE_COMMENT = “This file was generated by PermissionsDispatcher. Do not modify!”;

String GEN_CLASS_SUFFIX = “PermissionsDispatcher”;

String GEN_REQUESTCODE_PREFIX = “REQUEST_”;

String GEN_PERMISSION_PREFIX = “PERMISSION_”;

String GEN_PENDING_PREFIX = “PENDING_”;

String GEN_WITHCHECK_SUFFIX = “WithCheck”;

String GEN_PERMISSIONREQUEST_SUFFIX = “PermissionRequest”;

}

Extensions

//Extensions.kt

fun TypeMirror.simpleString(): String {

val toString: String = this.toString()

val indexOfDot: Int = toString.lastIndexOf(‘.’)

return if (indexOfDot == -1) toString else toString.substring(indexOfDot + 1)

}

//调用

typeMirror.simpleString()

这个用java写就比较尴尬了,我是用工具类,功能可以实现,但还是没法保持统一的调用方式;

//Extensions.java

public static String TypeMirrorSimpleString(TypeMirror typeMirror) {

String toString = typeMirror.toString();

int indexOfDot = toString.lastIndexOf(‘.’);

if (indexOfDot == -1)

return toString;

else

return toString.substring(indexOfDot + 1);

}

//调用

Extensions.TypeMirrorSimpleString(typeMirror);

初始化一个List

processorUnits = listOf(ActivityProcessorUnit(),SupportFragmentProcessorUnit(),NativeFragmentProcessorUnit())

用java咋写,尴尬了

processorUnits = new ArrayList();

processorUnits.add(new ActivityProcessorUnit());

processorUnits.add(new SupportFragmentProcessorUnit());

processorUnits.add(new NativeFragmentProcessorUnit());

Safe Call (?. ?:)

先看这一段懵逼的代码

//ADD_WITH_CHECK_BODY_MAP是一个HashMap

ADD_WITH_CHECK_BODY_MAP[needsPermissionParameter]?.addHasSelfPermissionsCondition(builder, activityVar, permissionField) ?: builder.beginControlFlow(“if (\$T.hasSelfPermissions(\$N, \$N))”, PERMISSION_UTILS, activityVar, permissionField)

用Java理解是啥样子

SensitivePermissionInterface anInterface = ADD_WITH_CHECK_BODY_MAP.get(needsPermissionParameter);

if (anInterface != null) {

anInterface.addHasSelfPermissionsCondition(builder, activityVar, permissionField);

} else {

builder.beginControlFlow(“if ($T.hasSelfPermissions($N, $N))”, PERMISSION_UTILS, activityVar, permissionField);

}

未完待续

今天的文章kotlin在线转java_Kotlin转Java记录分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/25419.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注