Redian新闻
>
新手问题 -- dynamic variable and dynamic functions.
avatar
新手问题 -- dynamic variable and dynamic functions.# Java - 爪哇娇娃
m*r
1
【 以下文字转载自 Working 讨论区 】
发信人: moar (阿穆斯特朗), 信区: Working
标 题: 码农在家上班的机会多吗
发信站: BBS 未名空间站 (Sun Aug 16 00:04:25 2015, 美东)
想找一个可以remote的工作。以前主要做ASP.net方面的网络应用,Web api和MVC的项
目也都有。前端javascript framework像angularjs, jquery, 数据库sql server,
mysql也都还可以,算是full stack,呵呵。
大家有机会请知会一下,感激不尽!
avatar
B*g
2
dynamic variable:
比如说我想用variable "varA". The word "varA" retrieved from database, how
can I use this word to get the variable value? 我现在想的是建一个lookup.
dynamic functions:
比如说我想trim一个variable. The word ".trim()" retrieved from
database, "" will be replaced by real variable and ".trim()" will be
excuted, how can I do that?
Thanks
avatar
g*g
3
Java Reflection

be

【在 B*****g 的大作中提到】
: dynamic variable:
: 比如说我想用variable "varA". The word "varA" retrieved from database, how
: can I use this word to get the variable value? 我现在想的是建一个lookup.
: dynamic functions:
: 比如说我想trim一个variable. The word ".trim()" retrieved from
: database, "" will be replaced by real variable and ".trim()" will be
: excuted, how can I do that?
: Thanks

avatar
A*o
4
How about the scripting ability added since JDK 6? It might work here, too.
But i have no experience on it.
avatar
c*t
5
It is nothing more than an equivalent of BSF framework.

.

【在 A**o 的大作中提到】
: How about the scripting ability added since JDK 6? It might work here, too.
: But i have no experience on it.

avatar
s*e
6
How can you do it in java reflection?
I am not sure if you can do it in a pure java way with java 1.6 scripting
engine either.
For either way, it will be interesting to see a working example.
avatar
c*t
7
LZ is not concrete enough. So there won't be concrete example either :P

【在 s******e 的大作中提到】
: How can you do it in java reflection?
: I am not sure if you can do it in a pure java way with java 1.6 scripting
: engine either.
: For either way, it will be interesting to see a working example.

avatar
s*e
8
not concrete enough?
I believe that even the poster title is good enough for you to come with a
sample.
The dynamic variable and dynamic functions are common features in emacscript
based language such as .js and .as but it is extemely hard to any strong
typed object oriented language. personally i do not see any way you can do
it in java reflection. even with jsr223,
you might still have a trouble to do it.
avatar
g*g
9
Think of Spring IoC declared in XML.
You declare a class name, a method name, and you can invoke the method.
Something like
Class c = Class.forName(yourClassName);
Object o = c.newInstance();
Method method = c.getDeclaredMethod(yourMethod);
method.invoke(o);
You probably want to have a constructor passing in a String parameter,
something we've seen in spring IoC too.
I believe OP's problem can be resolved in a similar way, although he
may need to format his string in DB with more type details

【在 s******e 的大作中提到】
: not concrete enough?
: I believe that even the poster title is good enough for you to come with a
: sample.
: The dynamic variable and dynamic functions are common features in emacscript
: based language such as .js and .as but it is extemely hard to any strong
: typed object oriented language. personally i do not see any way you can do
: it in java reflection. even with jsr223,
: you might still have a trouble to do it.

avatar
s*e
10
You misunderstood what dynamic variable and dynamic functions mean. they are
not someting that preexist at the compiling time.
both js ans as allow you to define a new memeber of class on the fly.
.for example, adding a new variable/function to a class at run time.
avatar
g*g
11
How you want to coin the names is not that important. The OP wants
to call some function by parsing a string from DB. And Spring IoC
is a perfect example for that. If you want to add new variable/function,
look at AspectJ.

are

【在 s******e 的大作中提到】
: You misunderstood what dynamic variable and dynamic functions mean. they are
: not someting that preexist at the compiling time.
: both js ans as allow you to define a new memeber of class on the fly.
: .for example, adding a new variable/function to a class at run time.

avatar
m*t
12

I would go with an embedded scripting language too.
Groovy or jruby are both good choices here, as they closely
resemble java syntax.

【在 c*****t 的大作中提到】
: It is nothing more than an equivalent of BSF framework.
:
: .

avatar
F*n
13
Mozilla Rhino Javascript. You can hack into it quite deep.

【在 m******t 的大作中提到】
:
: I would go with an embedded scripting language too.
: Groovy or jruby are both good choices here, as they closely
: resemble java syntax.

avatar
c*t
14
BeanShell is actually even better :P Zero learning curves.

【在 m******t 的大作中提到】
:
: I would go with an embedded scripting language too.
: Groovy or jruby are both good choices here, as they closely
: resemble java syntax.

avatar
c*t
15
Well, he wasn't specific enough in what he was trying to do. When he
mentions "dynamic variable", it makes me wonder why and where he would
use it.

emacscript

【在 s******e 的大作中提到】
: not concrete enough?
: I believe that even the poster title is good enough for you to come with a
: sample.
: The dynamic variable and dynamic functions are common features in emacscript
: based language such as .js and .as but it is extemely hard to any strong
: typed object oriented language. personally i do not see any way you can do
: it in java reflection. even with jsr223,
: you might still have a trouble to do it.

avatar
s*e
16
Even many terms are vague in cs.
But these two are not vague at all when we talk about language. actually
they are one of the criteria to distinguish a type strong langauge and a
dynamic language.
aspectj by nature is just another layer of compilation. actually no aop has
ever brought dynamic variable and dynamic functions to the table.
dynamic variable and fuctions are very useful when you do not know what and
how many things you want your object to carry at compilation time.
avatar
m*i
17
It should be something like eval() in python. did a little search, https://
eval.dev.java.net/ might be helpful.

be

【在 B*****g 的大作中提到】
: dynamic variable:
: 比如说我想用variable "varA". The word "varA" retrieved from database, how
: can I use this word to get the variable value? 我现在想的是建一个lookup.
: dynamic functions:
: 比如说我想trim一个variable. The word ".trim()" retrieved from
: database, "" will be replaced by real variable and ".trim()" will be
: excuted, how can I do that?
: Thanks

avatar
m*t
18

Very true. I used to use a lot of beanshell, even
embedding it in a web app as an admin console.
The active development on it seems to have stopped though.

【在 c*****t 的大作中提到】
: BeanShell is actually even better :P Zero learning curves.
avatar
m*t
19

There is this design approach to implement validation
logic in javascript, and run it once on client side, then again
on server side with rhino. Very interesting idea, I thought.

【在 F****n 的大作中提到】
: Mozilla Rhino Javascript. You can hack into it quite deep.
avatar
c*t
20
Why do you need "active" development anyways? It has already fullfilled
its purposes.

【在 m******t 的大作中提到】
:
: There is this design approach to implement validation
: logic in javascript, and run it once on client side, then again
: on server side with rhino. Very interesting idea, I thought.

avatar
m*t
21

I personally don't have problems with that. I'm just saying that
it might be a concern for some projects, especially when the latest
version released is nominally a beta.

【在 c*****t 的大作中提到】
: Why do you need "active" development anyways? It has already fullfilled
: its purposes.

avatar
B*g
22
sorry,太忙没时间搞。这个是我前一段问的dynamic ETL map要用的。
parse source file没啥问题,map to target(oracle db)有问题。
假设source是

FixedLength



firstname
text
10


lastname
text
10



target map是



testschema
c*****t
发帖数: 1879
22
ft... 下次你问问题一定要贴全。。。你这问的让所有人都误解了。。。
你自己弄个 hashmap,把 firstname 等放进去。然后自己 parse 下那个
expression 就是了。又不是多难的 expression 。
不过我估计你 java 不是太熟。

【在 B*****g 的大作中提到】
: sorry,太忙没时间搞。这个是我前一段问的dynamic ETL map要用的。
: parse source file没啥问题,map to target(oracle db)有问题。
: 假设source是
:
: FixedLength
:
:
:
: firstname
: text

B*****g
发帖数: 34098
23
这个map的xml是存在DB里的,这个client可能要求f + l, 另一个可能要 l + f, 还有
的是
substring(l, 20) + f.
原则就是只能update DB,不能change java code

【在 c*****t 的大作中提到】
: ft... 下次你问问题一定要贴全。。。你这问的让所有人都误解了。。。
: 你自己弄个 hashmap,把 firstname 等放进去。然后自己 parse 下那个
: expression 就是了。又不是多难的 expression 。
: 不过我估计你 java 不是太熟。

c*****t
发帖数: 1879
24
有什么问题么?不难啊。不能 change java code 什么意思?
你的表达太不清楚了。

【在 B*****g 的大作中提到】
: 这个map的xml是存在DB里的,这个client可能要求f + l, 另一个可能要 l + f, 还有
: 的是
: substring(l, 20) + f.
: 原则就是只能update DB,不能change java code

B*****g
发帖数: 34098
25
问题就是怎么parse那个+, substring之类的。

【在 c*****t 的大作中提到】
: 有什么问题么?不难啊。不能 change java code 什么意思?
: 你的表达太不清楚了。

c*****t
发帖数: 1879
26
你要是一点基础都没有,这没谁有能力帮你。
你可以考虑用 StreamTokenizer 来找出 identifier, + operator 等。
然后自己搞定。因为你的例子里面的 identifier 前面都有 : ,可能
稍微要改动些。
另外,你可以用 lex / yacc (再看我的 cookcc 例子)。不过这个没兴趣
的话跟天书一样,其实非常简单。
http://epaperpress.com/lexandyacc/ lex yacc tutorial for C
http://code.google.com/p/cookcc/ cookcc for java
即使你这次不想用,学会 lex / parse 以后受益无穷。

【在 B*****g 的大作中提到】
: 问题就是怎么parse那个+, substring之类的。
B*****g
发帖数: 34098
27
你还是没明白,parse出来 +等东西怎么用呀。总部不能 if + 然后 +吧。
要是几个function套在一块怎么办。

【在 c*****t 的大作中提到】
: 你要是一点基础都没有,这没谁有能力帮你。
: 你可以考虑用 StreamTokenizer 来找出 identifier, + operator 等。
: 然后自己搞定。因为你的例子里面的 identifier 前面都有 : ,可能
: 稍微要改动些。
: 另外,你可以用 lex / yacc (再看我的 cookcc 例子)。不过这个没兴趣
: 的话跟天书一样,其实非常简单。
: http://epaperpress.com/lexandyacc/ lex yacc tutorial for C
: http://code.google.com/p/cookcc/ cookcc for java
: 即使你这次不想用,学会 lex / parse 以后受益无穷。

c*****t
发帖数: 1879
28
... 你去看看那个 lex yacc tutorial 。有简单的 +-*/ 计算。。。

【在 B*****g 的大作中提到】
: 你还是没明白,parse出来 +等东西怎么用呀。总部不能 if + 然后 +吧。
: 要是几个function套在一块怎么办。

c*****t
发帖数: 1879
29

^^^^^^^^^^^^^^^^^^^^^^^^^^
这又是怎么回事?你不要说一半。。。如果是什么语言的话,你先说清楚。
说不定有现成的 parser 。

【在 B*****g 的大作中提到】
: 你还是没明白,parse出来 +等东西怎么用呀。总部不能 if + 然后 +吧。
: 要是几个function套在一块怎么办。

B*****g
发帖数: 34098
30
f + l is just an example.
Real world can be complicate.
Like first remove special characters from f and l, then trim them, then get
first 20 characters, then trim again, then uppercase.
More complicate example may involve if/else

【在 c*****t 的大作中提到】
:
: ^^^^^^^^^^^^^^^^^^^^^^^^^^
: 这又是怎么回事?你不要说一半。。。如果是什么语言的话,你先说清楚。
: 说不定有现成的 parser 。

相关主题
怎么能学成J2EE的全能access java parent class protected variable question
[合集] 这个annotation,...Java大侠们:Hashtable help please!
ft, BSF documentation?AJAX: simple question on parameter passing
F****n
发帖数: 3271
31
You must first know what the expression language is. If you don't know, then
there's no solution.

get

【在 B*****g 的大作中提到】
: f + l is just an example.
: Real world can be complicate.
: Like first remove special characters from f and l, then trim them, then get
: first 20 characters, then trim again, then uppercase.
: More complicate example may involve if/else

s******e
发帖数: 493
32
if so, even dynamic functions are not enough. Dynamic functions are not real
dynamic, you still need to code them in your code base.
what you need is either using db or xml file or any format file to act as a
configuration file, treating your first name as operand one, and last name
as operand two (if you are sure that you will only have two operands, and
they will be stored in columns that you know ahead. Otherwise if even the
operands are unknown at compilation time, it will be way too compli
s******e
发帖数: 493
33
just have thought, you may be able to take advantage of jdk 1.6 with mozilla
rhino(the only built-in Jscript implemntation support in hotspot)
for example you have a formula (op1 * 13 - op2)/13 *op2, you store in
formula in db or xml, because java new script engine allows you to define
your js function in java, you can just simply replace op1 and op2 at run
time, and send the defined function to js engine. this way you just take
advantage of exsiting js engine parser and interpreter. You do not
B*****g
发帖数: 34098
34
this one I decide. I want to do it in java

then

【在 F****n 的大作中提到】
: You must first know what the expression language is. If you don't know, then
: there's no solution.
:
: get

B*****g
发帖数: 34098
35
Solution storing data in columns (no matter is physical or logical) has been
turned down by my manager.
Solution use pl/sql as script language for all operations is also turned
down.
That is why I ask here.
My manager asked " why can not you do it in java?"
I am new to java, his is pre^10-new

real
a
name
used

【在 s******e 的大作中提到】
: if so, even dynamic functions are not enough. Dynamic functions are not real
: dynamic, you still need to code them in your code base.
: what you need is either using db or xml file or any format file to act as a
: configuration file, treating your first name as operand one, and last name
: as operand two (if you are sure that you will only have two operands, and
: they will be stored in columns that you know ahead. Otherwise if even the
: operands are unknown at compilation time, it will be way too compli

c*****t
发帖数: 1879
36
你没这方面的知识。没戏。

【在 B*****g 的大作中提到】
: this one I decide. I want to do it in java
:
: then

g*****g
发帖数: 34805
37
我觉得你的问题没有那么难。什么file type, operator,你都是有限的。
既然你说你控制XML数据的格式,那就不会太难。
就是扔一堆if else进去,最容易解决。

been

【在 B*****g 的大作中提到】
: Solution storing data in columns (no matter is physical or logical) has been
: turned down by my manager.
: Solution use pl/sql as script language for all operations is also turned
: down.
: That is why I ask here.
: My manager asked " why can not you do it in java?"
: I am new to java, his is pre^10-new
:
: real
: a

B*****g
发帖数: 34098
38
operator有限,combination就太多了。逻辑搞得太复杂可能会影响performance。
而且难不难看人,你觉得不难,对我可就难了。其实我的任务是搞明白design后交给一
个不回java的人做。

【在 g*****g 的大作中提到】
: 我觉得你的问题没有那么难。什么file type, operator,你都是有限的。
: 既然你说你控制XML数据的格式,那就不会太难。
: 就是扔一堆if else进去,最容易解决。
:
: been

F****n
发帖数: 3271
39
Use script with customized context. People do that kind of things all the
time. You can either use Rhino (then the expression is officially javascript
, not java), or use BeanShell.

【在 B*****g 的大作中提到】
: operator有限,combination就太多了。逻辑搞得太复杂可能会影响performance。
: 而且难不难看人,你觉得不难,对我可就难了。其实我的任务是搞明白design后交给一
: 个不回java的人做。

s******n
发帖数: 876
40
generate a java file containing the computation code.
invoke javac on it; load the class, run it.

【在 B*****g 的大作中提到】
: operator有限,combination就太多了。逻辑搞得太复杂可能会影响performance。
: 而且难不难看人,你觉得不难,对我可就难了。其实我的任务是搞明白design后交给一
: 个不回java的人做。

avatar
c*t
23
ft... 下次你问问题一定要贴全。。。你这问的让所有人都误解了。。。
你自己弄个 hashmap,把 firstname 等放进去。然后自己 parse 下那个
expression 就是了。又不是多难的 expression 。
不过我估计你 java 不是太熟。

【在 B*****g 的大作中提到】
: sorry,太忙没时间搞。这个是我前一段问的dynamic ETL map要用的。
: parse source file没啥问题,map to target(oracle db)有问题。
: 假设source是
:
: FixedLength
:
:
:
: firstname
: text

avatar
B*g
24
这个map的xml是存在DB里的,这个client可能要求f + l, 另一个可能要 l + f, 还有
的是
substring(l, 20) + f.
原则就是只能update DB,不能change java code

【在 c*****t 的大作中提到】
: ft... 下次你问问题一定要贴全。。。你这问的让所有人都误解了。。。
: 你自己弄个 hashmap,把 firstname 等放进去。然后自己 parse 下那个
: expression 就是了。又不是多难的 expression 。
: 不过我估计你 java 不是太熟。

avatar
c*t
25
有什么问题么?不难啊。不能 change java code 什么意思?
你的表达太不清楚了。

【在 B*****g 的大作中提到】
: 这个map的xml是存在DB里的,这个client可能要求f + l, 另一个可能要 l + f, 还有
: 的是
: substring(l, 20) + f.
: 原则就是只能update DB,不能change java code

avatar
B*g
26
问题就是怎么parse那个+, substring之类的。

【在 c*****t 的大作中提到】
: 有什么问题么?不难啊。不能 change java code 什么意思?
: 你的表达太不清楚了。

avatar
c*t
27
你要是一点基础都没有,这没谁有能力帮你。
你可以考虑用 StreamTokenizer 来找出 identifier, + operator 等。
然后自己搞定。因为你的例子里面的 identifier 前面都有 : ,可能
稍微要改动些。
另外,你可以用 lex / yacc (再看我的 cookcc 例子)。不过这个没兴趣
的话跟天书一样,其实非常简单。
http://epaperpress.com/lexandyacc/ lex yacc tutorial for C
http://code.google.com/p/cookcc/ cookcc for java
即使你这次不想用,学会 lex / parse 以后受益无穷。

【在 B*****g 的大作中提到】
: 问题就是怎么parse那个+, substring之类的。
avatar
B*g
28
你还是没明白,parse出来 +等东西怎么用呀。总部不能 if + 然后 +吧。
要是几个function套在一块怎么办。

【在 c*****t 的大作中提到】
: 你要是一点基础都没有,这没谁有能力帮你。
: 你可以考虑用 StreamTokenizer 来找出 identifier, + operator 等。
: 然后自己搞定。因为你的例子里面的 identifier 前面都有 : ,可能
: 稍微要改动些。
: 另外,你可以用 lex / yacc (再看我的 cookcc 例子)。不过这个没兴趣
: 的话跟天书一样,其实非常简单。
: http://epaperpress.com/lexandyacc/ lex yacc tutorial for C
: http://code.google.com/p/cookcc/ cookcc for java
: 即使你这次不想用,学会 lex / parse 以后受益无穷。

avatar
c*t
29
... 你去看看那个 lex yacc tutorial 。有简单的 +-*/ 计算。。。

【在 B*****g 的大作中提到】
: 你还是没明白,parse出来 +等东西怎么用呀。总部不能 if + 然后 +吧。
: 要是几个function套在一块怎么办。

avatar
c*t
30

^^^^^^^^^^^^^^^^^^^^^^^^^^
这又是怎么回事?你不要说一半。。。如果是什么语言的话,你先说清楚。
说不定有现成的 parser 。

【在 B*****g 的大作中提到】
: 你还是没明白,parse出来 +等东西怎么用呀。总部不能 if + 然后 +吧。
: 要是几个function套在一块怎么办。

avatar
B*g
31
f + l is just an example.
Real world can be complicate.
Like first remove special characters from f and l, then trim them, then get
first 20 characters, then trim again, then uppercase.
More complicate example may involve if/else

【在 c*****t 的大作中提到】
:
: ^^^^^^^^^^^^^^^^^^^^^^^^^^
: 这又是怎么回事?你不要说一半。。。如果是什么语言的话,你先说清楚。
: 说不定有现成的 parser 。

avatar
F*n
32
You must first know what the expression language is. If you don't know, then
there's no solution.

get

【在 B*****g 的大作中提到】
: f + l is just an example.
: Real world can be complicate.
: Like first remove special characters from f and l, then trim them, then get
: first 20 characters, then trim again, then uppercase.
: More complicate example may involve if/else

avatar
s*e
33
if so, even dynamic functions are not enough. Dynamic functions are not real
dynamic, you still need to code them in your code base.
what you need is either using db or xml file or any format file to act as a
configuration file, treating your first name as operand one, and last name
as operand two (if you are sure that you will only have two operands, and
they will be stored in columns that you know ahead. Otherwise if even the
operands are unknown at compilation time, it will be way too compli
avatar
s*e
34
just have thought, you may be able to take advantage of jdk 1.6 with mozilla
rhino(the only built-in Jscript implemntation support in hotspot)
for example you have a formula (op1 * 13 - op2)/13 *op2, you store in
formula in db or xml, because java new script engine allows you to define
your js function in java, you can just simply replace op1 and op2 at run
time, and send the defined function to js engine. this way you just take
advantage of exsiting js engine parser and interpreter. You do not
avatar
B*g
35
this one I decide. I want to do it in java

then

【在 F****n 的大作中提到】
: You must first know what the expression language is. If you don't know, then
: there's no solution.
:
: get

avatar
B*g
36
Solution storing data in columns (no matter is physical or logical) has been
turned down by my manager.
Solution use pl/sql as script language for all operations is also turned
down.
That is why I ask here.
My manager asked " why can not you do it in java?"
I am new to java, his is pre^10-new

real
a
name
used

【在 s******e 的大作中提到】
: if so, even dynamic functions are not enough. Dynamic functions are not real
: dynamic, you still need to code them in your code base.
: what you need is either using db or xml file or any format file to act as a
: configuration file, treating your first name as operand one, and last name
: as operand two (if you are sure that you will only have two operands, and
: they will be stored in columns that you know ahead. Otherwise if even the
: operands are unknown at compilation time, it will be way too compli

avatar
c*t
37
你没这方面的知识。没戏。

【在 B*****g 的大作中提到】
: this one I decide. I want to do it in java
:
: then

avatar
g*g
38
我觉得你的问题没有那么难。什么file type, operator,你都是有限的。
既然你说你控制XML数据的格式,那就不会太难。
就是扔一堆if else进去,最容易解决。

been

【在 B*****g 的大作中提到】
: Solution storing data in columns (no matter is physical or logical) has been
: turned down by my manager.
: Solution use pl/sql as script language for all operations is also turned
: down.
: That is why I ask here.
: My manager asked " why can not you do it in java?"
: I am new to java, his is pre^10-new
:
: real
: a

avatar
B*g
39
operator有限,combination就太多了。逻辑搞得太复杂可能会影响performance。
而且难不难看人,你觉得不难,对我可就难了。其实我的任务是搞明白design后交给一
个不回java的人做。

【在 g*****g 的大作中提到】
: 我觉得你的问题没有那么难。什么file type, operator,你都是有限的。
: 既然你说你控制XML数据的格式,那就不会太难。
: 就是扔一堆if else进去,最容易解决。
:
: been

avatar
F*n
40
Use script with customized context. People do that kind of things all the
time. You can either use Rhino (then the expression is officially javascript
, not java), or use BeanShell.

【在 B*****g 的大作中提到】
: operator有限,combination就太多了。逻辑搞得太复杂可能会影响performance。
: 而且难不难看人,你觉得不难,对我可就难了。其实我的任务是搞明白design后交给一
: 个不回java的人做。

avatar
s*n
41
generate a java file containing the computation code.
invoke javac on it; load the class, run it.

【在 B*****g 的大作中提到】
: operator有限,combination就太多了。逻辑搞得太复杂可能会影响performance。
: 而且难不难看人,你觉得不难,对我可就难了。其实我的任务是搞明白design后交给一
: 个不回java的人做。

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。