将函数参数列表的字符串表示形式转换为实际参数,以进行反射调用 [英] Translating a string-representation of a function's parameter list to actual parameters, for a reflective call

查看:95
本文介绍了将函数参数列表的字符串表示形式转换为实际参数,以进行反射调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:在获得一个意想不到的好的答案后,我在此问题的底部添加了一些上下文,准确说明了我将如何使用这些字符串函数调用.

我需要翻译一个字符串

my.package.ClassName#functionName(1, "a string value", true)

转换为对该函数的反射调用.获取包,类和函数名称不是问题.我已经开始使用自己的解决方案来解析参数列表,确定每个参数的类型并返回适当的对象.

into a reflective call to that function. Getting the package, class, and function name is not a problem. I have started rolling my own solution for parsing the parameter list, and determining the type of each and returning an appropriate object.

(我将类型的范围限制为八个基本类型,再加上字符串.null将被视为字符串,并且必须使用一些简单的标记(例如__DBL_QT__)严格将逗号和双引号转义,以避免因转义和在逗号上分裂而引起并发症.)

(I'm limiting the universe of types to the eight primitives, plus string. null would be considered a string, and commas and double-quotes must be strictly escaped with some simple marker, such as __DBL_QT__, to avoid complications with unescaping and splitting on the comma.)

据我了解,我不是在问如何通过字符串解析来做到这一点.这只是很多工作,我希望已经有了解决方案.不幸的是,这种通用术语使我无所适从.

I am not asking how to do this via string-parsing, as I understand how. It's just a lot of work and I'm hoping there's a solution already out there. Unfortunately it's such generic terminology, I'm getting nowhere with searching.

我知道要求外部现有库对于SO来说不是主题.我只是希望在关机之前获得一些反馈,甚至是对更好的搜索字词的建议.或许,可能会建议一种完全不同的方法.

I understand asking for an external existing library is off topic for SO. I'm just hoping to get some feedback before it's shutdown, or even a suggestion on better search terms. Or perhaps, there is a completely different approach that might be suggested...

谢谢.

上下文:

每个函数调用都在函数的JavaDoc块中找到,并代表一段示例代码(其源代码或System.out输出),该代码将显示在该位置.

Each function call is found within a function's JavaDoc block, and represents a piece of example code--either its source code or its System.out output--which will be displayed in that spot.

参数用于自定义其显示,例如

The parameters are for customizing its display, such as

  • 缩进,
  • 消除不相关的部分(例如许可证块),并且
  • 用于JavaDoc链接最重要的功能. 此自定义主要用于源代码表示,但也可以应用于其输出.
  • indentation,
  • eliminating irrelevant parts (like the license-block), and
  • for JavaDoc-linking the most important functions. This customization is mostly for the source-code presentation, but may also be applied to its output.

(第一个参数始终是Appendable,它将进行实际输出.)

(The first parameter is always an Appendable, which will do the actual outputting.)

用户需要能够调用 any 函数,在许多情况下,该函数将是位于JavaDoc-ed函数本身正下方的私有静态函数.

The user needs to be be able to call any function, which in many cases will be a private-static function located directly below the JavaDoc-ed function itself.

我正在编写的应用程序将读取源代码文件(一个包含JavaDoc块的应用程序,其中存在这些字符串函数调用),并创建* .java文件的副本,该文件随后将由javadoc处理.

The application I'm writing will read in the source-code file (the one containing the JavaDoc blocks, in which these string-function-calls exist), and create a duplicate of the *.java file, which will subsequently processed by javadoc.

因此,对于每段示例代码,这些字符串函数调用可能都会有两个,甚至可能更多.可能还有更多,因为我可能想在不同的上下文中显示同一示例的不同片段-也许将整个示例放在整个类JavaDoc块中,并在该类的相关函数中摘录其片段.

So for every piece of example code, there will be likely two, and possibly more of these string-function-calls. There may be more, because I may want to show different slices of the same example, in different contexts--perhaps the whole example in the overall class JavaDoc block, and snippets from it in the relevant functions in that class.

我已经编写了解析源代码(包含JavaDoc块的源代码,与读取示例代码的源代码分开的源代码)的过程,并使用insert example-code here盲目地重新输出其源代码.和insert example-code-output here标记.

I have already written the process that parses the source code (the source code containing the JavaDoc blocks, which is separate from the one that reads the example-code), and re-outputs its source-code blindly with insert example-code here and insert example-code-output here markers.

我现在要在字符串字段中的InsertExampleCode对象中进行此字符串函数调用.现在,我需要按照此问题顶部的说明进行操作.找出他们要调用的函数,然后执行.

I'm now at the point where I have this string-function-call in an InsertExampleCode object, in a string-field. Now I need to do as described at the top of this question. Figure out which function they want to invoke, and do so.

推荐答案

#更改为点(.),并在其周围编写类定义,以便拥有有效的Java源文件,包括在您的类路径中,并调用com.sun.tools.javac.Main.

Change the # to a dot (.), write a class definition around it so that you have a valid Java source file, include tools.jar in your classpath and invoke com.sun.tools.javac.Main.

创建您自己的ClassLoader实例以加载已编译的类,然后运行它(使其实现有用的接口,例如java.util.concurrent.Callable,以便您可以轻松获得调用结果)

Create your own instance of a ClassLoader to load the compiled class, and run it (make it implement a useful interface, such as java.util.concurrent.Callable so that you can get the result of the invocation easily)

应该可以解决问题.

这篇关于将函数参数列表的字符串表示形式转换为实际参数,以进行反射调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆