簡體   English   中英

如何在Intellij IDEA的實時模板中獲取方法參數類型?

[英]How to get method parameters types in live templates in Intellij IDEA?

我想為默認的實時模板logm創建Timber記錄器的實時模板。 它使用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);

因此,我需要確定方法參數的類型。

嘗試使用光標在%類型創建實時模板,並在使用此模板后填充它們

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM