由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 新手问题 -- dynamic variable and dynamic functions.
相关主题
ft, BSF documentation?Hibernate question
access java parent class protected variable questionJavaBean variable name standard
Java大侠们:Hashtable help please!how to solve the problem: the members change by each other .
AJAX: simple question on parameter passing问一个java的面试题 (转载)
anyone saw this on code?What's new in Java 6?
invoke a function dynamically in Java?math expression
a question about jniJava的主要就业领域有哪些?
Is there Eclipse java archive builder plug-in?最近node.js real time web 很火
相关话题的讨论汇总
话题: dynamic话题: variable话题: java话题: functions话题: do
进入Java版参与讨论
1 (共1页)
B*****g
发帖数: 34098
1
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
g*****g
发帖数: 34805
2
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

A**o
发帖数: 1550
3
How about the scripting ability added since JDK 6? It might work here, too.
But i have no experience on it.
c*****t
发帖数: 1879
4
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.

s******e
发帖数: 493
5
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.
c*****t
发帖数: 1879
6
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.

s******e
发帖数: 493
7
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.
g*****g
发帖数: 34805
8
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.

s******e
发帖数: 493
9
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.
g*****g
发帖数: 34805
10
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.

相关主题
invoke a function dynamically in Java?Hibernate question
a question about jniJavaBean variable name standard
Is there Eclipse java archive builder plug-in?how to solve the problem: the members change by each other .
进入Java版参与讨论
m******t
发帖数: 2416
11

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.
:
: .

F****n
发帖数: 3271
12
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.

c*****t
发帖数: 1879
13
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.

c*****t
发帖数: 1879
14
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.

s******e
发帖数: 493
15
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.
m***i
发帖数: 2480
16
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

m******t
发帖数: 2416
17

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.
m******t
发帖数: 2416
18

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.
c*****t
发帖数: 1879
19
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.

m******t
发帖数: 2416
20

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.

相关主题
问一个java的面试题 (转载)Java的主要就业领域有哪些?
What's new in Java 6?最近node.js real time web 很火
math expression问一个很基本很基本很基本的API问题
进入Java版参与讨论
B*****g
发帖数: 34098
21
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
进入Java版参与讨论
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的人做。

1 (共1页)
进入Java版参与讨论
相关主题
最近node.js real time web 很火anyone saw this on code?
问一个很基本很基本很基本的API问题invoke a function dynamically in Java?
怎么能学成J2EE的全能a question about jni
[合集] 这个annotation,...Is there Eclipse java archive builder plug-in?
ft, BSF documentation?Hibernate question
access java parent class protected variable questionJavaBean variable name standard
Java大侠们:Hashtable help please!how to solve the problem: the members change by each other .
AJAX: simple question on parameter passing问一个java的面试题 (转载)
相关话题的讨论汇总
话题: dynamic话题: variable话题: java话题: functions话题: do