6

我想为Timber logger创建一个与默认实时模板logm类似的实时模板。它使用 Groovy 脚本来收集方法参数并用逗号分隔它们。例如:

public int func(int a, float b, Object c, String d) {
    logm
}

生成以下代码:

public int func(int a, float b, Object c, String d) {
    Log.d(TAG, "func() called with: a = [" + a + "], b = [" + b + "], c = [" + c + "], d = [" + d + "]");
}

参数由以下代码收集:

def params = _2.collect {it + ' = [" + ' + it + ' + "]'}.join(', ');
return '"' + _1 + '() called' + (params.empty  ? '' : ' with: ' + params) + '"'

//Where _1 and _2 - default IDEA methods, in this case 
//_1 - methodName(), which eturns the name of the embracing method (where the template is expanded).
//_2 - methodParameters(), which returns the list of parameters of the embracing method (where the template is expanded).

问题是 Timber 方法需要参数的类型格式,例如:

int a = 5;
String s = new String("test");
boolean b = false;

Timber.d("%d, %s, %b", a, s, b);

因此,我需要确定方法参数的类型。

4

1 回答 1

3

尝试使用 % 类型的光标创建实时模板,并在使用此模板后填充它们

于 2016-11-28T16:52:27.607 回答