Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

Can ArchUnit check for certain string patterns in method calls?

In our code we again and again have the issue that somebody forgot to adapt the usage of placeholders when switching between the use of the logger and String.format(...) methods.

For log statements one has to use '{}' as placeholders, like so:

logger.info("File {} successfully opened: {} bytes read, {} objects created", file, nrBytes, nrObjects); 

But when using String.format(...) to compose a message one has to use '%s' as placeholders for strings and the statement has to read:

logger.info(String.format("File %s successfully opened: %s bytes read, %s objects created", file, nrBytes, nrObjects)); 

The second form is often used, when logging an error where the second argument is the Throwable that one wants to log.

Too often people forget about this details and then we end up with wrong log statements that output nothing reasonable.

I know and agree that this is absolutely not an architecture issue but rather a simple programming error, but it would be great if one could (ab-)use ArchUnit to check for the use of '%s' (or the absence of '{}') in the first String argument of the String.format()-method. Is something like that possible?

question from:https://stackoverflow.com/questions/65876645/can-archunit-check-for-certain-string-patterns-in-method-calls

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The ArchUnit, currently in version 0.16.0, does not analyze parameter values for method calls.

The sonar rule "Printf-style format strings should be used correctly" might however catch these bugs.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...