通过java反射检索静态声明的功能接口中使用的静态参数值(Retrieve static argument values used within statically declared functio

系统教程 行业动态 更新时间:2024-06-14 16:59:46
通过java反射检索静态声明的功能接口中使用的静态参数值(Retrieve static argument values used within statically declared functional interfaces via java reflection)

看看我的代码:

import java.util.function.Function; public class StaticArgumentsTest { static Function<String, String> staticConsumer(String testValue) { return (st) -> testValue + " and " + st; } static final Function<String, String> aStaticConsumer = staticConsumer("Static String Example Value"); public static void main(String[] args) { System.out.println(StaticArgumentsTest.aStaticConsumer.apply("Dynamic String Example Value")); } }

我们有一些遗留代码,其中包含许多这样的功能接口实现,我宁愿将这些代码放在一个更易于管理的状态,比如数据库,而不是在普通的java代码中使用这些设置。 所以我的问题是,是否可以使用反射找到上面看到的字符串值“静态字符串示例值”? 我可能宁愿这样做而不是为这些东西编写我自己的java代码解析器,但据我所知,我坚持这样做。

Take a look at my code here:

import java.util.function.Function; public class StaticArgumentsTest { static Function<String, String> staticConsumer(String testValue) { return (st) -> testValue + " and " + st; } static final Function<String, String> aStaticConsumer = staticConsumer("Static String Example Value"); public static void main(String[] args) { System.out.println(StaticArgumentsTest.aStaticConsumer.apply("Dynamic String Example Value")); } }

We've got some legacy code with many of these sorts of functional interface implementations and I would rather have this code in a more manageable state like a database rather than have these settings in plain java code. So my question is, is it possible to find the string value "Static String Example Value" you see above using reflection? I would probably rather do this than just write my own java code parser for this stuff, but as far as I can tell I'm stuck doing that.

最满意答案

当然,这是高度依赖于实现的,不建议用于生产代码,但对于一次性转换任务,它将适用于普通的Reflection操作和HotSpot / OpenJDK的当前实现:

public class StaticArgumentsTest { static Function<String, String> staticConsumer(String testValue) { return (st) -> testValue + " and " + st; } static final Function<String, String> aStaticConsumer = staticConsumer("Static String Example Value"); public static void main(String[] args) { System.out.println(aStaticConsumer.apply("Dynamic String Example Value")); getCapturedValues(aStaticConsumer); } private static void getCapturedValues(Object instance) { Field[] f = instance.getClass().getDeclaredFields(); AccessibleObject.setAccessible(f, true); for(Field field: f) { System.out.print(field.getName()+" ("+field.getType()+"): "); try { System.out.println(field.get(instance)); } catch(ReflectiveOperationException ex) { System.out.println(ex); } } } }
Static String Example Value and Dynamic String Example Value
arg$1 (class java.lang.String): Static String Example Value
 

当然,这些合成字段没有有意义的名称,但对于只捕获一个值的lambda表达式,很明显,而对于其他字符,您可以使用一些合理的启发式方法(例如基于类型或顺序)来找出哪个字段对应哪个变量。

Of course, this is highly implementation dependent and not recommended for production code, but for a one-time conversion task, it will work with ordinary Reflection operations and the current implementation of HotSpot/OpenJDK:

public class StaticArgumentsTest { static Function<String, String> staticConsumer(String testValue) { return (st) -> testValue + " and " + st; } static final Function<String, String> aStaticConsumer = staticConsumer("Static String Example Value"); public static void main(String[] args) { System.out.println(aStaticConsumer.apply("Dynamic String Example Value")); getCapturedValues(aStaticConsumer); } private static void getCapturedValues(Object instance) { Field[] f = instance.getClass().getDeclaredFields(); AccessibleObject.setAccessible(f, true); for(Field field: f) { System.out.print(field.getName()+" ("+field.getType()+"): "); try { System.out.println(field.get(instance)); } catch(ReflectiveOperationException ex) { System.out.println(ex); } } } }
Static String Example Value and Dynamic String Example Value
arg$1 (class java.lang.String): Static String Example Value
 

Of course, these synthetic fields have no meaningful names, but for a lambda expression which captures only one value, it is obvious, whereas for others, you may use some reasonable heuristics, e.g. based on type or order, to find out which field corresponds to which variable.

更多推荐

本文发布于:2023-04-17 08:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/de2277f03e45718d18f09b3631bddc69.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:静态   反射   接口   声明   参数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!