question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Refaster support for varargs methods

See original GitHub issue

I tried to write a Refaster rule for callers of some internal library method M which has a signature that matches String#format(String, Object...). The idea is to identify callers which explicitly call String.format before invoking M and to rewrite those expressions to defer the formatting to M. But nothing happened.

This appears to be because of the use of varargs. As a test I tried to apply the following dummy Refaster template to our code base, but that didn’t do anything either:

final class VarargsRefasterTemplate {
    @BeforeTemplate
    String before(final String format, final Object... args) {
        return String.format(format, args);
    }

    @AfterTemplate
    String after(final String format, final Object... args) {
        return "This is a test";
    }
}

Would it be possible to support varargs?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
lowassercommented, Mar 11, 2017

It is supported, just not in a super well documented way, I think?

Write @BeforeTemplate String before(String format, @Repeated Object arg).

@Repeated is slightly more general, though; it can be interspersed with normal arguments, put in an array declaration, and generally stand in for any comma repeated expression.

0reactions
knutwannhedencommented, Jan 29, 2020

When writing a Refaster template for a method with a varargs parameter, I currently always need to specify at least two templates: one for call sites using varargs and one for call sites using explicit arrays. Could Refaster simplify this use case?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Refaster (Error Prone parent POM HEAD-SNAPSHOT API)
Indicates that Refaster should treat this @Repeated argument specifically as a varargs argument. For example, you might write
Read more >
How to refactor code with varargs - put to map - Stack Overflow
Simple answer. You're doing it wrong. This stuff already exists; use e.g. guava's ImmutableMap.builder , or use the existing Map.of() .
Read more >
Variable argument and Varargs methods in Java with Example
Variable argument or varargs in Java allows you to write more flexible methods which can accept as many arguments as you need. variable...
Read more >
Log - HEAD - external/guava-libraries - Git at Google
EMPTY_ENTRY_ARRAY in other ImmutableMap types by Chris Povirk · 8 years ago; 8aa58da Eliminate varargs-accepting create() method, which was never called ...
Read more >
The jOOQ Release Note History
Names unquoted. #9825, Document new jOOQ-refaster module. #9833, Add missing @Support annotation to DSLContext#fetch*() methods.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found