private MethodSpec createOnPermissionRequestResultMethod() {
        MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder("onRequestPermissionsResult")
                .addParameter(TypeName.INT, "requestCode")
                .addParameter(String[].class, "permissions")
                .addParameter(int[].class, "grantResults")
                .addStatement("if (!ON_GRANT_METHODS_MAP.containsKey(requestCode)) return")
                .addStatement("$T<String> permissionsNotGrantList = new $T<String>(permissions.length)",
                        List.class, ArrayList.class)
                .beginControlFlow(" for (int i = 0; i < grantResults.length; i++)")
                .beginControlFlow("if (grantResults[i] != android.content.pm.PackageManager.PERMISSION_GRANTED)")
                .addStatement("permissionsNotGrantList.add(permissions[i])")
                .endControlFlow()
                .endControlFlow()
                .beginControlFlow(" if (permissionsNotGrantList.size() == 0)")
                .addCode("// Now call his method.\n")
                .addStatement("Runnable r = ON_GRANT_METHODS_MAP.remove(requestCode)")
                .addStatement(" if (r != null) r.run()")
                .addStatement("return")
                .endControlFlow()
                .addStatement("Runnable r2 = ON_DENY_METHODS_MAP.remove(requestCode)")
                .addStatement(" if (r2 != null) r2.run()")
                .addModifiers(Modifier.STATIC)
                .addModifiers(Modifier.FINAL);
        return methodSpecBuilder.build();
    }
Created by guohao4 on 2017/9/6.
Email: Tornaco@163.com
android/annotation_processors/permission-requester-compiler/src/main/java/github/tornaco/permission/compiler/RuntimePermissionsCompiler.java