Java vs dynamic language
Hi, I am a student and here is my question: (no idea if i post it at the right section)
Is there anything that dynamic programming languages (Python, Ruby)can do but static ones(Java, C++) can't?
In term of method interception, dynamic class loading, runtime reflection.....we can do them in Java
but there is one thing that i wonder....where it involes class manipulation in runtime. We have an assignment writing a DSL, instead of writing it from scratch, we build it up upon Ruby. So there is specific class and specific methods for this DSL. So I wonder if Java can do the same thing.....many thanks!!
best regard
but I am not familiar with class loading:
[697 byte] By [
weihanga] at [2007-11-27 3:26:24]

# 1
and one more thing is that....DSL class name and methods name is unknown until runtime, so it does not work with library extension
# 2
This is not a forum for general questions about Java:
http://forum.java.sun.com/ann.jspa?annID=14
To answer you question, yes there are a number of things you can do in Python or Ruby that you cannot do in Java.
For example, in python you can override __getattr__ to capture method calls for methods that do not exist. There is no such feature in Java reflection that I am aware of (it could have been added recently. I haven't been paying close attention.)
# 3
java.lang.reflect.Proxy can do something like thatbut with the restriction to use only interface.Else, you can intrument the bytecode, i.e add/remove any instructionsat runtime by controling the class loader or installinga java.lang.instrument agent.R閙i
# 4
> java.lang.reflect.Proxy can do something like that
> but with the restriction to use only interface.
But you would have to be calling the method via reflection, right? There is no such limitation in Python. You can call the method like you would call any other 'real' method. It's really an apples to oranges comparison though. I think Python and Java are complimentary tools, personally. Thankfully there's been some movement in Jython recently.
# 5
No, you don't have to.
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.regex.Pattern;
public class ProxyExample {
public static void main(String[] args) {
final String test="hello world";
CharSequence proxy = (CharSequence)Proxy.newProxyInstance(
CharSequence.class.getClassLoader(),
new Class<?>[]{CharSequence.class},
new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("call "+method+" "+Arrays.toString(args));
return method.invoke(test, args);
}
});
Pattern.compile(" ").matcher(proxy).replaceFirst("--");
}
}
R閙i
# 6
Huh. Never knew that was around. It's a nice thing to have in the old toolbox. Thanks.
# 7
> No, you don't have to.
> [code]
> import java.lang.reflect.InvocationHandler;
> import java.lang.reflect.Method;
> import java.lang.reflect.Proxy;
> import java.util.Arrays;
> import java.util.regex.Pattern;
>
> public class ProxyExample {
>public static void main(String[] args) {
>final String test="hello world";
> CharSequence proxy =
> (CharSequence)Proxy.newProxyInstance(
>CharSequence.class.getClassLoader(),
> new Class<?>[]{CharSequence.class},
>new InvocationHandler() {
>
> public Object invoke(Object proxy, Method
> method, Object[] args)
> throws Throwable {
> System.out.println("call "+method+"
> "+Arrays.toString(args));
>return method.invoke(test, args);
> });
> Pattern.compile("
> ").matcher(proxy).replaceFirst("--");
>}
> ode]
>
> R閙i
And tools like CGLIB can give you similar functionality for classes, too