(这很难搜索,因为结果都是关于"方法参考")
我想获得一个Methodlambda表达式的实例,以便与基于遗留反射的API一起使用.应该包含clousure,因此调用thatMethod.invoke(null, ...)应该与调用lambda具有相同的效果.
我看过MethodHandles.Lookup,但它似乎只与逆向变换有关.但我想这种bind方法可能有助于包括clousure?
编辑:
说我有lambda experssion:
Function<String, String> sayHello = name -> "Hello, " + name;
Run Code Online (Sandbox Code Playgroud)
我有一个具有API 的遗留框架(SpEL)
registerFunction(String name, Method method)
Run Code Online (Sandbox Code Playgroud)
这将调用Method没有this参数的给定(即假定方法是静态的).所以我需要得到一个Method包含lambda逻辑+ clousure数据的特殊实例.
我完全知道,有时可能会问这个问题,但我找不到答案。:/
我有一个参数化的类:
public class MessageType<T> {
private final Class<T> clazz;
public MessageType(final Class<T> clazz) {
this.clazz = clazz;
}
public Class<T> getClazz() {
return clazz;
}
}
Run Code Online (Sandbox Code Playgroud)
以及此类的几个静态对象:
static final MessageType<String> TYPE_A = new MessageType<>(String.class);
static final MessageType<Double> TYPE_B = new MessageType<>(Double.class);
static final MessageType<List<String>> PROBLEM_TYPE = new MessageType(List.class);
Run Code Online (Sandbox Code Playgroud)
问题是,我必须忽略钻石运营商和坚守的投选中MessageType以MessageType<List<String>>在最后一行。
我想写点东西
static final MessageType<List<String>> PROBLEM_TYPE = new MessageType<>(List<String>.class);
Run Code Online (Sandbox Code Playgroud)
但List<String>.class无法在运行时中进行计算,因为您知道键入擦除。:D
?有什么方法可以符合编译器的要求,并避免未经检查的强制转换?(由于我的疏忽和缺乏关注,已经花了一个小时的时间)
假设我有这个application.java
@SpringBootApplication
public class Application {
public static void main(String[] args){
SpringApplication.run(Application.class, args);
}
@Bean
public Executor asyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(50);
executor.setMaxPoolSize(100);
executor.setQueueCapacity(5000);
executor.setThreadNamePrefix("sm-async-");
executor.setWaitForTasksToCompleteOnShutdown(true);
executor.initialize();
return executor;
}
}
Run Code Online (Sandbox Code Playgroud)
我的目标是,如果异步执行器的当前实时队列大小达到 80% 或接近限制,则创建警报。我认为我们可以从中获得价值executor.getThreadPoolExecutor().getQueue().size();。我目前陷入了如何实现这一目标的困境
在javax.imageio.ImageIO有一个方法#write(RenderedImage im, String formatName, OutputStream output),接受一个“stringly类型的”格式开发者想要的图像被写入。
CanEncodeImageAndFormatFilter在该文件内部有一个名为deep的内部类,用于检查是否存在支持上述格式的 SPI(无论它是什么)。例如,BMPImageWriterSpi声明
private static String[] formatNames = {"bmp", "BMP"};
Run Code Online (Sandbox Code Playgroud)
但是在所有这些 SPI 类中,这些格式名称被声明为私有或默认包,因此我无法在我的应用程序中访问它们。但是我想在我现在正在处理的 API 中公开该格式参数,但我不想重新发明轮子。
? 是否有枚举或我可以安全地用作格式名称参数的东西#write方法?
是的,这更像是编码风格的基于意见的问题标签的。:D
ImageIO.write(image, "jpg", outStream);
Run Code Online (Sandbox Code Playgroud)
我想要的是:
ImageIO.write(image, ImageTypes.JPEG, outStream);
Run Code Online (Sandbox Code Playgroud) 很难找到有关该主题的任何线索。我能找到的只是关于将一个函数式接口转换为另一个函数式接口的问题,以及一些关于 Java 中类型转换的文章。不是我要找的。
这个问题是关于转换的lambda ? Method,我想要相反的,转换Method为任何功能接口,例如,转换为Consumer.
我发现的方法是围绕该Method#invoke方法创建一个 lambda 适配器:
public void registerCallbacks(final Object annotated) {
Class clazz = annotated.getClass();
for (Method method : clazz.getDeclaredMethods()) {
if (method.isAnnotationPresent(Callback.class)) {
Callback registration = method.getAnnotation(Callback.class);
List<String> warnings = new ArrayList<>(3);
if (!Modifier.isPublic(method.getModifiers()))
warnings.add(String.format("Method %s must be public", method));
if (method.getParameterCount() != 1)
warnings.add(String.format("Method %s must consume only one argument", method));
if (method.getParameterCount() == 1 && !method.getParameterTypes()[0].equals(Integer.class))
warnings.add(String.format("Method %s must consume %s", method, Integer.class));
if …Run Code Online (Sandbox Code Playgroud) 我尝试为一些合成负载测试生成数千个随机测试数据,但我遇到了一个我不明白的奇怪错误。
\n\n这是我设法缩小范围的基本最小可重现代码。让我们创建一个包含一些唯一行的表:
\n\nCREATE TABLE vals (\n value INT PRIMARY KEY\n);\nINSERT INTO vals SELECT generate_series(1, 10);\nRun Code Online (Sandbox Code Playgroud)\n\n让我们检查一下是否有唯一值:
\n\nSELECT array(SELECT * FROM vals);\n\n>> {1,2,3,4,5,6,7,8,9,10}\nRun Code Online (Sandbox Code Playgroud)\n\n是的,那很好。现在让我们创建一个包含大量引用该vals表的用户数据的表:
CREATE TABLE tmp (\n a INT REFERENCES vals,\n b INT[]\n);\nRun Code Online (Sandbox Code Playgroud)\n\n并用大量随机数据填充它:
\n\n WITH test_count AS (SELECT generate_series(1, 10000))\n -- some more CTEs, so I cannot give up on them\nINSERT\n INTO tmp\nSELECT\n (SELECT value FROM vals ORDER BY random() LIMIT 1),\n array(SELECT value FROM vals WHERE random() > 0.85)\n …Run Code Online (Sandbox Code Playgroud) 在Java 11中,有一组静态方法java.util.Map可以实例化AbstractImmutableMap:
static <K, V> Map<K, V> of(K k1, V v1) { return new Map1(k1, v1); }
static <K, V> Map<K, V> of(K k1, V v1, K k2, V v2) { return new MapN(new Object[]{k1, v1, k2, v2}); }
// ... some more "vararg" static methods until 10 pairs (inclusive).
Run Code Online (Sandbox Code Playgroud)
还有另一种方法,除了true-vararg之外,几乎相同:
static <K, V> Map<K, V> ofEntries(Map.Entry<? extends K, ? extends V>... entries) { /* impl here */ }
Run Code Online (Sandbox Code Playgroud)
我想使用后一种方法,因为它允许将条目数扩展到远远超过十个。问题是,我不知道如何创建Map.Entry。它在不同的Maps中有很多实现,但是没有new运算符或静态构造方法,但是 …
java ×5
reflection ×2
asynchronous ×1
generics ×1
java-11 ×1
java-8 ×1
lambda ×1
postgresql ×1
spring ×1
spring-boot ×1
sql ×1