mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-11 03:13:38 +00:00
Merge pull request #2658 from Rawi01/extension-method-generic
Reset inference context for extension methods
This commit is contained in:
7 files changed
+154
-13
No files matched your search
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2012-2014 The Project Lombok Authors.
|
||||
* Copyright (C) 2012-2020 The Project Lombok Authors.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -58,7 +58,6 @@ import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
|
||||
import org.eclipse.jdt.internal.compiler.lookup.Binding;
|
||||
import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
|
||||
import org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
|
||||
import org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding;
|
||||
import org.eclipse.jdt.internal.compiler.lookup.ProblemMethodBinding;
|
||||
import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
|
||||
import org.eclipse.jdt.internal.compiler.lookup.Scope;
|
||||
@@ -304,12 +303,13 @@ public class PatchExtensionMethod {
|
||||
argumentTypes.add(argumentType);
|
||||
}
|
||||
|
||||
// Copy generic information. This one covers a few simple cases, more complex cases are still broken
|
||||
int typeVariables = extensionMethod.typeVariables.length;
|
||||
if (typeVariables > 0 && methodCall.receiver.resolvedType instanceof ParameterizedTypeBinding) {
|
||||
ParameterizedTypeBinding parameterizedTypeBinding = (ParameterizedTypeBinding) methodCall.receiver.resolvedType;
|
||||
if (parameterizedTypeBinding.arguments != null && parameterizedTypeBinding.arguments.length == typeVariables) {
|
||||
methodCall.genericTypeArguments = parameterizedTypeBinding.arguments;
|
||||
if (methodCall.receiver instanceof MessageSend) {
|
||||
if (Reflection.inferenceContexts != null) {
|
||||
try {
|
||||
Permit.set(Reflection.inferenceContexts, methodCall.receiver, null);
|
||||
} catch (IllegalAccessException ignore) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -402,6 +402,7 @@ public class PatchExtensionMethod {
|
||||
private static final class Reflection {
|
||||
public static final Field argumentTypes = Permit.permissiveGetField(MessageSend.class, "argumentTypes");
|
||||
public static final Field argumentsHaveErrors = Permit.permissiveGetField(MessageSend.class, "argumentsHaveErrors");
|
||||
public static final Field inferenceContexts = Permit.permissiveGetField(MessageSend.class, "inferenceContexts");
|
||||
private static final Class<?> functionalExpression;
|
||||
private static final Constructor<?> polyTypeBindingConstructor;
|
||||
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
class ExtensionMethodFunctional {
|
||||
public void test() {
|
||||
@@ -7,6 +10,8 @@ class ExtensionMethodFunctional {
|
||||
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);
|
||||
ExtensionMethodFunctional.Extensions.toList(Stream.of("a", "b", "c").map(String::toUpperCase));
|
||||
List<Integer> i2 = ExtensionMethodFunctional.Extensions.toList2(Stream.of("a", "b", "c").map(String::toUpperCase));
|
||||
}
|
||||
|
||||
static class Extensions {
|
||||
@@ -24,5 +29,13 @@ class ExtensionMethodFunctional {
|
||||
consumer[i].accept(o);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> List<T> toList(Stream<T> stream) {
|
||||
return (List<T>) stream.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static <T, U> List<U> toList2(Stream<T> stream) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
class ExtensionMethodGeneric {
|
||||
public void test() {
|
||||
List<String> stringList = null;
|
||||
List<Number> numberList = null;
|
||||
ExtensionMethodGeneric.Extensions.test(stringList);
|
||||
ExtensionMethodGeneric.Extensions.test(stringList, numberList);
|
||||
ExtensionMethodGeneric.Extensions.test(ExtensionMethodGeneric.Extensions.test(stringList, stringList), numberList);
|
||||
Integer i = ExtensionMethodGeneric.Extensions.test2(stringList);
|
||||
Map<String, Integer> map = null;
|
||||
List<String> l = ExtensionMethodGeneric.Extensions.test(map, stringList, numberList);
|
||||
}
|
||||
|
||||
static class Extensions {
|
||||
public static <T> List<T> test(List<String> obj, List<T> list) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static <K, V> K test(Map<String, Integer> obj, K k, V v) {
|
||||
return k;
|
||||
}
|
||||
|
||||
public static <T> T test(List<T> list) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static <T, U> U test2(List<T> list) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,10 @@
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import lombok.experimental.ExtensionMethod;
|
||||
@ExtensionMethod(ExtensionMethodFunctional.Extensions.class) class ExtensionMethodFunctional {
|
||||
@ExtensionMethod(value = ExtensionMethodFunctional.Extensions.class,suppressBaseMethods = false) class ExtensionMethodFunctional {
|
||||
static class Extensions {
|
||||
Extensions() {
|
||||
super();
|
||||
@@ -18,6 +21,12 @@ import lombok.experimental.ExtensionMethod;
|
||||
consumer[i].accept(o);
|
||||
}
|
||||
}
|
||||
public static <T>List<T> toList(Stream<T> stream) {
|
||||
return (List<T>) stream.collect(Collectors.toList());
|
||||
}
|
||||
public static <T, U>List<U> toList2(Stream<T> stream) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
ExtensionMethodFunctional() {
|
||||
super();
|
||||
@@ -27,5 +36,7 @@ import lombok.experimental.ExtensionMethod;
|
||||
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);
|
||||
ExtensionMethodFunctional.Extensions.toList(Stream.of("a", "b", "c").map(String::toUpperCase));
|
||||
List<Integer> i2 = ExtensionMethodFunctional.Extensions.toList2(Stream.of("a", "b", "c").map(String::toUpperCase));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import lombok.experimental.ExtensionMethod;
|
||||
@ExtensionMethod(ExtensionMethodGeneric.Extensions.class) class ExtensionMethodGeneric {
|
||||
static class Extensions {
|
||||
Extensions() {
|
||||
super();
|
||||
}
|
||||
public static <T>List<T> test(List<String> obj, List<T> list) {
|
||||
return null;
|
||||
}
|
||||
public static <K, V>K test(Map<String, Integer> obj, K k, V v) {
|
||||
return k;
|
||||
}
|
||||
public static <T>T test(List<T> list) {
|
||||
return null;
|
||||
}
|
||||
public static <T, U>U test2(List<T> list) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
ExtensionMethodGeneric() {
|
||||
super();
|
||||
}
|
||||
public void test() {
|
||||
List<String> stringList = null;
|
||||
List<Number> numberList = null;
|
||||
ExtensionMethodGeneric.Extensions.test(stringList);
|
||||
ExtensionMethodGeneric.Extensions.test(stringList, numberList);
|
||||
ExtensionMethodGeneric.Extensions.test(ExtensionMethodGeneric.Extensions.test(stringList, stringList), numberList);
|
||||
Integer i = ExtensionMethodGeneric.Extensions.test2(stringList);
|
||||
Map<String, Integer> map = null;
|
||||
List<String> l = ExtensionMethodGeneric.Extensions.test(map, stringList, numberList);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,13 @@
|
||||
// version 8:
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import lombok.experimental.ExtensionMethod;
|
||||
|
||||
@ExtensionMethod(ExtensionMethodFunctional.Extensions.class)
|
||||
@ExtensionMethod(value = ExtensionMethodFunctional.Extensions.class, suppressBaseMethods = false)
|
||||
class ExtensionMethodFunctional {
|
||||
public void test() {
|
||||
String test = "test";
|
||||
@@ -12,6 +15,9 @@ class ExtensionMethodFunctional {
|
||||
|
||||
test.consume(s -> System.out.println("1: " + s), s -> System.out.println("2: " + s));
|
||||
test.consume(System.out::println, System.out::println);
|
||||
|
||||
Stream.of("a", "b", "c").map(String::toUpperCase).toList();
|
||||
List<Integer> i2 = Stream.of("a", "b", "c").map(String::toUpperCase).toList2();
|
||||
}
|
||||
|
||||
static class Extensions {
|
||||
@@ -29,5 +35,13 @@ class ExtensionMethodFunctional {
|
||||
consumer[i].accept(o);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> List<T> toList(Stream<T> stream) {
|
||||
return (List<T>) stream.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static <T, U> List<U> toList2(Stream<T> stream) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import lombok.experimental.ExtensionMethod;
|
||||
|
||||
@ExtensionMethod(ExtensionMethodGeneric.Extensions.class)
|
||||
class ExtensionMethodGeneric {
|
||||
public void test() {
|
||||
List<String> stringList = null;
|
||||
List<Number> numberList = null;
|
||||
stringList.test();
|
||||
stringList.test(numberList);
|
||||
stringList.test(stringList).test(numberList);
|
||||
Integer i = stringList.test2();
|
||||
|
||||
Map<String, Integer> map = null;
|
||||
List<String> l = map.test(stringList, numberList);
|
||||
}
|
||||
|
||||
static class Extensions {
|
||||
public static <T> List<T> test(List<String> obj, List<T> list) {
|
||||
return null;
|
||||
}
|
||||
public static <K,V> K test(Map<String, Integer> obj, K k, V v) {
|
||||
return k;
|
||||
}
|
||||
public static <T> T test(List<T> list) {
|
||||
return null;
|
||||
}
|
||||
public static <T,U> U test2(List<T> list) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user