mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-03 04:54:46 +00:00
Added tests for ExtensionMethod
This commit is contained in:
1 parent
39b733ab80
commit
3d90a51163
12 files changed
+236
-3
No files matched your search
@@ -301,18 +301,23 @@ public class PatchExtensionMethod {
|
||||
// If the extension method uses varargs, the last fixed binding parameter is an array but
|
||||
// the method arguments are not. Even thought we already know that the method is fine we still
|
||||
// have to compare each parameter with the type of the array to support autoboxing/unboxing.
|
||||
boolean isVararg = false;
|
||||
boolean isVarargs = fixedBinding.isVarargs();
|
||||
for (int i = 0, iend = arguments.size(); i < iend; i++) {
|
||||
Expression arg = arguments.get(i);
|
||||
TypeBinding[] parameters = fixedBinding.parameters;
|
||||
TypeBinding param = isVararg ? parameters[parameters.length - 1].leafComponentType() : parameters[i];
|
||||
TypeBinding param;
|
||||
if (isVarargs && i >= parameters.length - 1) {
|
||||
// Extract the array element type for all vararg arguments
|
||||
param = parameters[parameters.length - 1].leafComponentType();
|
||||
} else {
|
||||
param = parameters[i];
|
||||
}
|
||||
// Resolve types for lambdas
|
||||
if (arg instanceof FunctionalExpression) {
|
||||
arg.setExpectedType(param);
|
||||
arg.resolveType(scope);
|
||||
}
|
||||
if (arg.resolvedType != null) {
|
||||
isVararg |= param.isArrayType() != arg.resolvedType.isArrayType();
|
||||
if (!param.isBaseType() && arg.resolvedType.isBaseType()) {
|
||||
int id = arg.resolvedType.id;
|
||||
arg.implicitConversion = TypeIds.BOXING | (id + (id << 4)); // magic see TypeIds
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
class ExtensionMethodAutoboxing {
|
||||
public void test() {
|
||||
Long l1 = 1L;
|
||||
long l2 = 1L;
|
||||
Integer i1 = 1;
|
||||
int i2 = 1;
|
||||
String string = "test";
|
||||
ExtensionMethodAutoboxing.Extensions.boxing(string, l1, i1);
|
||||
ExtensionMethodAutoboxing.Extensions.boxing(string, l1, i2);
|
||||
ExtensionMethodAutoboxing.Extensions.boxing(string, l2, i1);
|
||||
ExtensionMethodAutoboxing.Extensions.boxing(string, l2, i2);
|
||||
}
|
||||
|
||||
|
||||
static class Extensions {
|
||||
public static String boxing(String string, Long a, int b) {
|
||||
return string + " " + a + " " + b;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
class ExtensionMethodFunctional {
|
||||
public void test() {
|
||||
String test = "test";
|
||||
test = ExtensionMethodFunctional.Extensions.map(test, s -> ExtensionMethodFunctional.Extensions.reverse(s));
|
||||
ExtensionMethodFunctional.Extensions.consume(test, s -> System.out.println("1: " + s), s -> System.out.println("2: " + s));
|
||||
ExtensionMethodFunctional.Extensions.consume(test, System.out::println, System.out::println);
|
||||
}
|
||||
|
||||
static class Extensions {
|
||||
public static <T, R> R map(T value, Function<T, R> mapper) {
|
||||
return mapper.apply(value);
|
||||
}
|
||||
|
||||
public static String reverse(String string) {
|
||||
return new StringBuilder(string).reverse().toString();
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
public static <T> void consume(T o, Consumer<T>... consumer) {
|
||||
for (int i = 0; i < consumer.length; i++) {
|
||||
consumer[i].accept(o);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
class ExtensionMethodVarargs {
|
||||
public void test() {
|
||||
Long l1 = 1L;
|
||||
long l2 = 1L;
|
||||
Integer i1 = 1;
|
||||
int i2 = 1;
|
||||
ExtensionMethodVarargs.Extensions.format("%d %d %d %d", l1, l2, i1, i2);
|
||||
ExtensionMethodVarargs.Extensions.format("%d", l1);
|
||||
ExtensionMethodVarargs.Extensions.format("", new Integer[] {1, 2});
|
||||
ExtensionMethodVarargs.Extensions.format("", new Integer[] {1, 2}, new Integer[] {1, 2});
|
||||
}
|
||||
|
||||
|
||||
static class Extensions {
|
||||
public static String format(String string, Object... params) {
|
||||
return String.format(string, params);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import lombok.experimental.ExtensionMethod;
|
||||
@ExtensionMethod({ExtensionMethodAutoboxing.Extensions.class}) class ExtensionMethodAutoboxing {
|
||||
static class Extensions {
|
||||
Extensions() {
|
||||
super();
|
||||
}
|
||||
public static String boxing(String string, Long a, int b) {
|
||||
return ((((string + " ") + a) + " ") + b);
|
||||
}
|
||||
}
|
||||
ExtensionMethodAutoboxing() {
|
||||
super();
|
||||
}
|
||||
public void test() {
|
||||
Long l1 = 1l;
|
||||
long l2 = 1l;
|
||||
Integer i1 = 1;
|
||||
int i2 = 1;
|
||||
String string = "test";
|
||||
ExtensionMethodAutoboxing.Extensions.boxing(string, l1, i1);
|
||||
ExtensionMethodAutoboxing.Extensions.boxing(string, l1, i2);
|
||||
ExtensionMethodAutoboxing.Extensions.boxing(string, l2, i1);
|
||||
ExtensionMethodAutoboxing.Extensions.boxing(string, l2, i2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Consumer;
|
||||
import lombok.experimental.ExtensionMethod;
|
||||
@ExtensionMethod(ExtensionMethodFunctional.Extensions.class) class ExtensionMethodFunctional {
|
||||
static class Extensions {
|
||||
Extensions() {
|
||||
super();
|
||||
}
|
||||
public static <T, R>R map(T value, Function<T, R> mapper) {
|
||||
return mapper.apply(value);
|
||||
}
|
||||
public static String reverse(String string) {
|
||||
return new StringBuilder(string).reverse().toString();
|
||||
}
|
||||
public static @SafeVarargs <T>void consume(T o, Consumer<T>... consumer) {
|
||||
for (int i = 0;; (i < consumer.length); i ++)
|
||||
{
|
||||
consumer[i].accept(o);
|
||||
}
|
||||
}
|
||||
}
|
||||
ExtensionMethodFunctional() {
|
||||
super();
|
||||
}
|
||||
public void test() {
|
||||
String test = "test";
|
||||
test = ExtensionMethodFunctional.Extensions.map(test, (<no type> s) -> ExtensionMethodFunctional.Extensions.reverse(s));
|
||||
ExtensionMethodFunctional.Extensions.consume(test, (<no type> s) -> System.out.println(("1: " + s)), (<no type> s) -> System.out.println(("2: " + s)));
|
||||
ExtensionMethodFunctional.Extensions.consume(test, System.out::println, System.out::println);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import lombok.experimental.ExtensionMethod;
|
||||
@ExtensionMethod(ExtensionMethodVarargs.Extensions.class) class ExtensionMethodVarargs {
|
||||
static class Extensions {
|
||||
Extensions() {
|
||||
super();
|
||||
}
|
||||
public static String format(String string, Object... params) {
|
||||
return String.format(string, params);
|
||||
}
|
||||
}
|
||||
ExtensionMethodVarargs() {
|
||||
super();
|
||||
}
|
||||
public void test() {
|
||||
Long l1 = 1l;
|
||||
long l2 = 1l;
|
||||
Integer i1 = 1;
|
||||
int i2 = 1;
|
||||
ExtensionMethodVarargs.Extensions.format("%d %d %d %d", l1, l2, i1, i2);
|
||||
ExtensionMethodVarargs.Extensions.format("%d", l1);
|
||||
ExtensionMethodVarargs.Extensions.format("", new Integer[]{1, 2});
|
||||
ExtensionMethodVarargs.Extensions.format("", new Integer[]{1, 2}, new Integer[]{1, 2});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import lombok.experimental.ExtensionMethod;
|
||||
|
||||
@ExtensionMethod({ExtensionMethodAutoboxing.Extensions.class})
|
||||
class ExtensionMethodAutoboxing {
|
||||
public void test() {
|
||||
Long l1 = 1l;
|
||||
long l2 = 1l;
|
||||
Integer i1 = 1;
|
||||
int i2 = 1;
|
||||
|
||||
String string = "test";
|
||||
string.boxing(l1, i1);
|
||||
string.boxing(l1, i2);
|
||||
string.boxing(l2, i1);
|
||||
string.boxing(l2, i2);
|
||||
}
|
||||
|
||||
static class Extensions {
|
||||
public static String boxing(String string, Long a, int b) {
|
||||
return string + " " + a + " " + b;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// version 8:
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import lombok.experimental.ExtensionMethod;
|
||||
|
||||
@ExtensionMethod(ExtensionMethodFunctional.Extensions.class)
|
||||
class ExtensionMethodFunctional {
|
||||
public void test() {
|
||||
String test = "test";
|
||||
test = test.map(s -> s.reverse());
|
||||
|
||||
test.consume(s -> System.out.println("1: " + s), s -> System.out.println("2: " + s));
|
||||
test.consume(System.out::println, System.out::println);
|
||||
}
|
||||
|
||||
static class Extensions {
|
||||
public static <T, R> R map(T value, Function<T, R> mapper) {
|
||||
return mapper.apply(value);
|
||||
}
|
||||
|
||||
public static String reverse(String string) {
|
||||
return new StringBuilder(string).reverse().toString();
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
public static <T> void consume(T o, Consumer<T>... consumer) {
|
||||
for (int i = 0; i < consumer.length; i++) {
|
||||
consumer[i].accept(o);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// version 8:
|
||||
import lombok.experimental.ExtensionMethod;
|
||||
|
||||
@ExtensionMethod(ExtensionMethodVarargs.Extensions.class)
|
||||
class ExtensionMethodVarargs {
|
||||
public void test() {
|
||||
Long l1 = 1l;
|
||||
long l2 = 1l;
|
||||
Integer i1 = 1;
|
||||
int i2 = 1;
|
||||
|
||||
"%d %d %d %d".format(l1, l2, i1, i2);
|
||||
"%d".format(l1);
|
||||
"".format(new Integer[]{1,2});
|
||||
"".format(new Integer[]{1,2}, new Integer[]{1,2});
|
||||
}
|
||||
|
||||
static class Extensions {
|
||||
public static String format(String string, Object... params) {
|
||||
return String.format(string, params);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
14 non-varargs call of varargs method with inexact argument type for last parameter
|
||||
@@ -0,0 +1 @@
|
||||
9 non-varargs call of varargs method with inexact argument type for last parameter
|
||||
Reference in new issue
Block a user