Robert's AST-GREP is better for this. Forbiddenapis can't do this easily:
: Arrays.<Entry<String,String>>asList(....) Uwe: does forbidden-apis have enough type information to recognize: 1) the length of a varargs list 2) when the source code it's checking is missing explicit generic type declarations in the method call
forbiddenapis does not scan thorugh source code, it only analyzes the bytecode. So both can't be detected:
1) In bytecode varargs are just an array passed to a single-arg method. This could be detected with some heuristics, but this would require to get the newArray opcode and the length the array. This needs to be based on analzing how stuff is pushed to stack. Not easy!
2) the generic type annotation is not part of bytecode
I'm wondering if a new forbidden-api feature could restrict usage based on generics and/or varargs length, such that we could write a rule saying "Arrays.asList w/ varargs.length > 3 must specify explicit type" So this would be forbidden... Arrays.asList("x", 2, "y", 3); ...but these would be legal... Arrays.<Object>asList("x", 2, "y", 3); Arrays.asList("x", 2, foo)
Roberts solution works on source code. Uwe -- Uwe Schindler Achterdiek 19, D-28357 Bremen https://www.thetaphi.de eMail:u...@thetaphi.de