简体   繁体   中英

Lombok Getter on array index

Is there a way to use lombok to generate a getters for variables that can be indexed? For example, if I want:

public int getValue(int index) 
{
    return value[i];
}

Is there a way to do this with lombok?

With all due respect to you Roel as a dev of Lombok which is great tool btw!

There can be a work-around solution to Mozbi question:

import static org.junit.Assert.assertEquals;
import lombok.experimental.ExtensionMethod;

import org.junit.Test;

@ExtensionMethod({ Extensions.class })
public class LombokTest {

    @Test
    public void test() {
        int[] intArray = { 5, 3, 8, 2 };

        int actual = intArray.getValue(1);
        assertEquals(3, actual);
    }

}

class Extensions {
    public static int getValue(int[] array, int index) {
        return array[index];
    }

}

I propose to write extension and use Lombok's @ExtensionMethod annotation instead of @Getter (which as Roel noted won't work here). You would need to write a method manually, but you can use it in a very nice way in your code.

Lombok doc https://projectlombok.org/features/experimental/ExtensionMethod.html

No, sorry.

Disclosure: I am a Lombok developer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM