Fantastic Map specializations and GC reduction

Ah well, I can only respond to the same idiocy two times before I give up

Lol, Beautiful, just beautiful. It’s plain to see you’re not interested in discussing this rationally at all. When backed into a corner by facts you can only respond with cries of “idiocy” and other such third-grade antics.

You obviously can’t accept information from anyplace except your own misconceptions,

Chris, this sentence might make sense if you could point out even a single misconception.

you are completely blinded by where you think the information is comming from… You should not be to quick to judge.

That’s hilarious. You started the ad hominem attacks (as you continue to do), at which point I tried to Google you down and suggested that people check out both of our histories. I couldn’t find any identifying information about you on the Internet at all, except the company you work for. I won’t even go into how that compares to my background, but anyone here can do the leg work in about 60 seconds.

And I find it quite funny that you would think that your personal background is somehow “blinding me”. No Chris, my observations come from many years of working with Java and C++. Personally, Chris, I don’t care who you work for, I just want to participate in cogent discussion. (Though I have to admit to finding your occupation funny in relation to all of this).

but while you might find it fun playing the fool, I find it pretty boring to talk to one

More personal attacks Chris? If you’d take the time to respond to any of the facts, this could be an interesting and fruitful discussion, but apparently you’re either incapable of or unwilling to do so.

Oh well. I encourage others to contribute to this well-scorched thread.

God bless,
-Toby Reyelts

cknoll,

It is fair and helpful to disagree with someone. However, if you disagree with someone the mature thing to do is to give some good reasons why. This can lead to discussion and people can learn. In the future, I hope you will participate by giving reasons for your opinions instead of by calling someone an idiot or thick headed. If you think about it, rreyelts posted some very good comments and backed them up with very sound concepts.

Cheers.

Just remember that ages ago the orginal C++ language was implemented as something like a preprocessor that produced straight C code… Although I guess in that sense you could call the preprocessor something that compiles to C source.

[quote]First, you aren’t manually generating any code which you have to maintain.
[/quote]

  • much like a macro

[quote]Third, the C++ template mechanism implements a Turing complete language, with features like compile time control-flow and iteration / recursion. For example, it’s possible to write a function that computes the factorial of a parameter N at compile time. This use of templates is known as template meta-programming and is commonly used to perform optimizations like compile-time loop unrolling. Try doing that with Ctrl+C and Ctrl+V.
[/quote]
Well just for the sake of argument… that ‘meta-programming’ sounds a lot like an abusive hack… I would hate to see such code. And, I unroll loops with Ctrl-C, Ctrl-V all the time, so I seem to have missed the point there. These things certianly don’t sound like the stuff that I see C++ templates used for 99.9% of the time. So the fact that the Java Generics mechanism can’t handle that specific bit doesn’t seem like a big deal.
In terms of how I see C++ templates used, they are essentially a glorified macro - a really powerful and useful one, sure.

[quote]Again, to reiterate my point, Java generics don’t have to equal C++ templates, but they should have learned something from them. Instead, they’re a dumb auto-casting mechanism, with no power and no flexibility.
[/quote]
Well I certainly agree that if Java Generics are nothing more than hiding some casting, then they perhaps aren’t as powerful as they should be. Although if you were going to use things like the generic containers and casting anyway they do make things easier. So while java Generics aren’t what you want them to be, they are still good for something.
Could you implement something that appeared to be the equivalent of STL with java generics for instance? 'cause that to me seems to cover the bulk of what C++ templates are used for.
How much am I really missing? (I know you’ll want to tell me ;), and I seriously want to know. )

Grom,
I did reply with concrete examples, and these were met with the likes of ‘LOL. Is that the best you can do?’ You should re-read the whole thread again. I never disputed fastutil’s performance, and any discussion on my part at all dealt with performance of ‘generic’ code. (Not generics specifically). I did say that copy-and-paste reuse was problematic (and I said why, but if you can’t magine going back to 20 classes to recompile against a new template, then maybe you haven’t been exposed to large projects that would require the use of collections of 20 different types).

In any case, it seems you and rreyelts are big big fastutil fans and Generic haters (Personally, I don’t care about either, I was just giving point of view) and anyone who thinks differently sucks and is a target for abuse, so I’ll leave you two to your ramblings.

-Chris

Just remember that ages ago the orginal C++ language was implemented as something like a preprocessor that produced straight C code…

Apples and oranges Scott. There are many languages which use C as their IL (intermediate language). Heck, there are static Java compilers which use C as their IL. Are you suggesting that Java is just a macro? The key here is that the process is actually compilation - which all of us CS geeks know is just a synonym for translation.

much like a macro

Fine. You’ve taken what my point was (Templates, like java.lang.reflect.Proxy, are a form of generative-generic programming), and turned it on its head. Let me ask you this, then: Do you believe java.lang.reflect.Proxy is basically a macro? If not, then how is it different? Then how does Proxy differ from templates? Hopefully, these questions will be rhetorical for you.

that ‘meta-programming’ sounds a lot like an abusive hack…

It’s definitely not a hack (templates were designed to have those capabilities), but it’s not exactly pretty either. It’s the preferred way, now, because it is currently the most elegant solution. I would love to see Java implement the same functionality, but in a much more elegant fashion.

And, I unroll loops with Ctrl-C, Ctrl-V all the time, so I seem to have missed the point there.

Yes, you strayed far from the point. You can use templates to create a parameterized algorithm which the compiler manually unrolls for you. For example, you can create a matrix algorithm that will automatically unroll itself perfectly for any size matrix. With manual unrolling, you would have to create a new version of the algorithm for each differently sized matrix. If you needed to change your algorithm, you’d have to do it for each and every unrolled loop for each and every differently sized matrix. Hope your wife won’t miss you while your gone.

These things certianly don’t sound like the stuff that I see C++ templates used for 99.9% of the time.

It is common. Check out OONumerics and the Loki library for example.

So the fact that the Java Generics mechanism can’t handle that specific bit doesn’t seem like a big deal.

Humm… what you’ve done here is falsely trivialized what templates can do, and then said that you don’t see why they are good.

As another example of what templates can do, consider specialization. I can take any algorithm and specialize it for any data type, and it’s totally transparent to the users of that algorithm. Jace (my open-source C++ JNI library) uses template specialization extensively throughout its library to create seamless access to Java arrays, fields, and methods of any type.

In terms of how I see C++ templates used, they are essentially a glorified macro - a really powerful and useful one, sure.

Actually, if anything is like a macro, it’s Java generics. Think about it some. One is a Turing complete language - the other drops in type casts.

Well I certainly agree that if Java Generics are nothing more than hiding some casting, then they perhaps aren’t as powerful as they should be. Although if you were going to use things like the generic containers and casting anyway they do make things easier. So while java Generics aren’t what you want them to be, they are still good for something.

Right - they’re mostly useless. Somewhat more powerful than comments.

Could you implement something that appeared to be the equivalent of STL with java generics for instance?

Somebody has already written a poor Java imitation of the standard C++ container library - It’s called JGL. It, for example, doesn’t support specialization. It also doesn’t implement anything akin to traits classes. It also has no concept of policy classes. All of these things come from templates.

'cause that to me seems to cover the bulk of what C++ templates are used for.

So, I’ve already covered what you missed in the standard C++ library alone. Again, I suggest you take a look at some powerful template libraries. Check out Loki, Blitz++, the C++ Lambda library, even Jace, for real world examples of the many powerful things you can do with templates.

God bless,
-Toby Reyelts

I did reply with concrete examples, and these were met with the likes of ‘LOL. Is that the best you can do?’

The responses were equal to the merits of the questions. At least I was considerate enough to refrain from calling you an “idiotic fool who’s too stupid to understand the ongoing discussion”.

I did say that copy-and-paste reuse was problematic (and I said why, but if you can’t magine going back to 20 classes to recompile against a new template, then maybe you haven’t been exposed to large projects that would require the use of collections of 20 different types).

You don’t have to “go back” to any classes. You change your template class as needed and type “make” at the command prompt. This and your other comments: C++ templates is nothing more than a code generator: It actually produces new source code based on the type you specify as the parameter. and Negative impact of copy-paste: If you find a problem with your ‘boilerplate’ template, you need to go back and recompile (or re-generate) all the code that was built off that template. seem to indicate that you believe that templates end up generating some sort of source code, which is patently false. Templates have the same advantages as Java Generics, in that you only maintain a single source file. Templates vary from Java generics, in that additional object code is produced - either by the compiler or the linker.

Btw, I use templates extensively in Jace. In Jace, at least one template gets instantiated for each Java type you build against. In the case of the Java class library, that’s, at a bare minimum, over 5000 different template instantiations. Off the cuff, I would guess it’s somewhere around 5 times that much in reality. I’ve built against the entire Java class library. I think I have a pretty good idea of what’s involved in extensive instantiation of templates.

In any case, it seems you and rreyelts are big big fastutil fans and Generic haters

I’ve never used fastutil and reserve the right to pass any judgement on it until I do. It may be the greatest thing since sliced bread, or it may be crap.

and anyone who thinks differently sucks and is a target for abuse

I don’t think you suck and have never said such a thing. At this point, I do think you’re confused about Java generics and C++ templates in general.

so I’ll leave you two to your ramblings.

As you wish.

God bless,
-Toby Reyelts

[quote]Are you suggesting that Java is just a macro? The key here is that the process is actually compilation…
[/quote]
I just meant that a preprocesor could be something of a compiler… to rationalize why people would think of templates in that way. I acknowledge that they are more integrated into the language than a simple macro facility.

[quote]Do you believe java.lang.reflect.Proxy is basically a macro? If not, then how is it different? Then how does Proxy differ from templates?
[/quote]
I’ve never looked at java.lang.reflect.Proxy … so I’ll have to go away and read a bit before I can appreciate your point…

[quote]Yes, you strayed far from the point. You can use templates to create a parameterized algorithm which the compiler manually unrolls for you.
[/quote]
I wasn’t trying to stray, but I see your point now… templates can be designed to adapt much better than some sort of macro would… (is that the point?)… I have to admit though taht my knowledge of C++ templates (which may be severely lacking) makes it seem that what you are talking about is akin to a fancy recursive macro. Something that really does need (as you say) a more elegant implementation. Not that it would be easy to do… maybe generics in Java are not all that because the Java language tries to keep simple and elegant … and a simple elegant way to do this meta-programming was too far off? I don’t know, I’m just hypothesizing.

[quote]So the fact that the Java Generics mechanism can’t handle that specific bit doesn’t seem like a big deal.

Humm… what you’ve done here is falsely trivialized what templates can do, and then said that you don’t see why they are good.
[/quote]
That wasn’t my intention at all. I can only speak from my personal experience, in which I have never seen serious use of templates to unroll loops and the like. I had heard it coudl be done… but it seemed more like the kind of stuff that you would do in an obfuscated C++ contest, than stuff that people did for real. I don’t mean to suggest that it is an unreasonable use of templates… but it adds a significantly different dimension to the language… I’m not sure if I’m ready to go there… it feels like it would be similar to coding half of your routines in prolog and the other half in C :slight_smile:

[quote]As another example of what templates can do, consider specialization. I can take any algorithm and specialize it for any data type, and it’s totally transparent to the users of that algorithm.
[/quote]
Can’t you get a lot of that with Java Generics? I mean, there are existing ways to deal with that… e…g. you can sort any objects that implement comparable. The generic algorithms and containers of STL are where I see the most use of C+= templates… and I thought that java generics would allow you to work like that, even if the implementation really just amounted to doing the casting for you.
Which is why this:[quote]Right - they’re mostly useless. Somewhat more powerful than comments.
[/quote]
seems to be a strrange conclusion… I would have concluded “Right - so there isn’t much to them, but they give you a lot.”

[quote]So, I’ve already covered what you missed in the standard C++ library alone. Again, I suggest you take a look at some powerful template libraries. Check out Loki, Blitz++, the C++ Lambda library, even Jace, for real world examples of the many powerful things you can do with templates.[\quote]

I would like to, if I can find some time I probably will… but to be honest, the fact that I worked with C++ for so many years and only recently hearing of these things is why I suggested that templates generally aren’t used that way in most programs, and that what most programs actually use templates for in typical applications may be coverd by java generics.

The whole Lambda-calculus thing for instance… it seems mostly academic now… how much programming is really done that way… in the grand scheme of things none (at he moment), regardless of it’s power. So while I accept your point that generics could be vastly more powerful, and that C++ templates ARE more powerful, it still seems that the power is ultimately lost on the chumps like me that haven’t caught on to it yet… but since we chumps make up the bulk of the users (I’m making assumptions, I know) it isn’t really worth it…
Just an opinion, it could easily change by the time I read your reply :slight_smile:

Generally rreyelts gave those types of responses when you used immature and offensive language.

It is wrong of you to second-guess what I “magine”.

It is funny that you instantly degrade to offensive 13-year-old “anyone who thinks differently sucks” communications. If you read back, you will see I did not make you a target at all. I only complained about your language.

I am positive about fastUtils because I wrote a microbenchmark and liked the results. I can see it being useful. I call it “fantastic” because of its “potential” and to get people interested and join in discussion so I can learn more about it and related technologies like Generics. In doing so, I received a pretty good idea about Generics. I need to do a lot of reading on Generics, and I need to use fastUtil a lot before I really understand it and trust it enough to recommend.

Well, grom, you certainly spent a lot of time sifting through my responses, how about you sift through rreyelts and critique those?

It’s funny that you claim i use immature and offensive language, but you don’t quote any of my passages on that. Hmm…

-Chris

Well, there’s been claims that there’s no code-generation wrt STL, here’s an interesting quote from an article i found at http://www.yrl.co.uk/~phil/stl/stl.htmlx

The next question is: is there a way to view the c++ source that a STL generates? I didn’t find much, but I did find this quote:

So, exactly how is STL not the code generator that I’ve been describing all this time? I understand that it’s ‘smart’ and it handles recursion, etc etc, but I think it still amounts to copy-paste coding which can be a nightmare to maintain. R. claims that you just need to re-run your make and all the code will be re-generated, but that’s one thing I like about java: you don’t need to do an entire rebuild if you just change a single class. Generics allow you to define a full-fledged java object that is parameterized (aka “Parameterized Types”) that if it changes, you only need to rebuild the PT and re-deploy. With the copy-paste methodology, you need to redeploy every class that was generated from the template. I think this is a negative.

-Chris

I’ve used proxies in an object-relational tool, and they were used in that context to defer loading objects from the datastore until they are accessed for the first time. Querries return proxies to the data objects, and not the actual data objects themselves. How does this relate to the code-generation mechanism you were talking about? From what I’ve read, proxies act ‘on-behalf’ of the actual target of an invocation such that you can give the fascade that you are talking to an actual object, but you are talking to an object that can do some work before calling the actual class. Can you please explain where the concept of ‘code-generation’ comes into play here?

-Chris

Well, there’s been claims that there’s no code-generation wrt STL, here’s an interesting quote from an article i found at http://www.yrl.co.uk/~phil/stl/stl.htmlx

Templates are an intimate part of the language Chris. The compiler reads them and then translates them to IL - just like any other C++ code you write. If you don’t believe me, simply confirm it with your favorite compiler / library implementer. You might consider Steve Clamage who works on the Forte C++ compiler for Sun, or perhaps Pete Becker who works on the Dinkumware Standard C++ library that ships with VC++, or even Greg Comeau who has written possibly the world’s most conforming C++ compiler. They hang out at comp.lang.c++.moderated, so you might be able to catch them there.

The next question is: is there a way to view the c++ source that a STL generates? I didn’t find much, but I did find this quote:

Not unless you enjoy looking at the object code. For example, where do you think all those template instantiations go in VC++? They go into the object files. Where do you think they go in Forte C++? Object files. The answer is the same for all of the C++ compilers you use. I challenge you to find one which does otherwise.

So, exactly how is STL not the code generator that I’ve been describing all this time? I understand that it’s ‘smart’ and it handles recursion, etc etc, but I think it still amounts to copy-paste coding which can be a nightmare to maintain.

Here we go again. Exactly what disadvantages can you associate between copy-paste and templates?

R. claims that you just need to re-run your make and all the code will be re-generated, but that’s one thing I like about java: you don’t need to do an entire rebuild if you just change a single class.

You’re not doing an entire rebuild if you change a template class in C++ either. Yes, the compiler / linker will automatically rebuild any template instantiations that are affected by a template change.

Generics allow you to define a full-fledged java object that is parameterized (aka “Parameterized Types”) that if it changes, you only need to rebuild the PT and re-deploy.

Apples and oranges, Chris. In the situation that you want your templates to act like Java generics, your template code will delegate to functions outside of the template. You simply redeploy those functions.

God bless,
-Toby Reyelts

How does this relate to the code-generation mechanism you were talking about?.. Can you please explain where the concept of ‘code-generation’ comes into play here?

Umm… because Proxy generates code? I suggest you sit down with a good article on Proxy and spend some time looking at the source code for both Proxy and sun.misc.ProxyGenerator. In particular, I hope you’ll find Proxy.getProxyClass() and ProxyGenerator.generateProxyClass() enlightening.

God bless,
-Toby Reyelts

That’s not really answering my question, Toby. For example, wrt STL, i showed examples of how code is generated from templates (reguardless of whether or not there was a actual c++ source artifact produced, code was generated base don the type). I’m asking you to produce an example of the use of a proxy that generates code. The getProxy() and generateProxyClass() are all methods executed at run-time, not at compile time like STL.

Just to be clear, we’re not talking about STL anymore, we’re talking about ‘Proxies being code generators.’ I look forward to seeing your example.

-Chris

That’s not really answering my question, Toby. For example, wrt STL, i showed examples of how code is generated from templates

Chris, you didn’t show examples, you just twisted the text in an article to fit your needs. I did answer your question. The compiler generates code for templates just like it does for any other language construct. How can I make that more clear to you? Like I said, if you don’t believe me, go ask the experts - even Stroustrup. They are nice guys, and they don’t bite, I promise. I’ve already held conversations with each one of them. It wouldn’t be nice, though, if I didn’t warn you that they may laugh at you (that or killfile you) if you keep insisting that templates are in the same class as macros or copy and paste.

If you’re unwilling to believe anybody else, no matter how clear the facts, why don’t you do yourself a couple of favors. Purchase the Ansi C++ Standard for $18 and read it. Seriously, I’ll front you the dough if you can’t afford it. Then go download and examine the source code for gcc 3.1+.

I’m asking you to produce an example of the use of a proxy that generates code. The getProxy() and generateProxyClass() are all methods executed at run-time, not at compile time like STL.

Huh? All “code generators” generate code at “runtime”. The code doesn’t just drop out of mid-air. In the case of C++ templates, the code generator is the compiler. In the case of Proxy instances, the code generator is java.lang.reflect.Proxy.

I look forward to seeing your example.

Umm… how about this:


Proxy.newProxyInstance( 
  getClass().getClassLoader(), 
  new Class[] { java.awt.ActionListener.class },  
  new InvocationHandler() { 
    public Object invoke( Object proxy, Method method, Object[] args) {
          System.out.println( "Hello" );
    }
  }
);

God bless,
-Toby Reyelts

Don’t accuse me of twisting text of an article, that was a direct cut-and-paste of relavent sections of the article, and I gave a reference to the full article for anyone who wants more background.

As far as your response, you chose to ignore my statement that “We are not talking about STL, we are talking about Proxies as code generators” and decided to go on an off-topic rant. In addition, you say:

and then go on to say

So, compilers compile at runtime…[sarcasm]right[/sarcasm].

Anyways, getting back to the topic, your example doesn’t demonstrate any code generation from proxies, you just created an object and passed in a few parameters. This is code generation? does that mean something like:

vector.add(new Object()) // this generates code??

actually generates some code based on the logic of your example? Of course not. Proxies are more akin to dynamic method invocation than code generations, and certainly have no resembelance to macros as you sugest here:

Perhaps you should explain (without saying ‘read this book’) how Proxies do not differ from Templates.

-Chris

I wasn’t trying to stray, but I see your point now… templates can be designed to adapt much better than some sort of macro would… (is that the point?)…

My point is that templates are a powerful first class language feature. Preprocessors (and hence macros) by their very nature are not. That is why macros suck and should be avoided at all costs and why templates rock.

I have to admit though taht my knowledge of C++ templates (which may be severely lacking) makes it seem that what you are talking about is akin to a fancy recursive macro.

I’m beginning to believe that you think that anything that operates on a meta level is a macro. Maybe you should state what you think a macro is?

Maybe generics in Java are not all that because the Java language tries to keep simple and elegant … and a simple elegant way to do this meta-programming was too far off?

This is pretty close to the answer I got from Gilad Bracha (the spec lead for Java Generics). He basically said that they felt that a more powerful version of Java generics would have been too difficult for Java users. I personally prefer not to have the language dumbed-down to the level of VB. Reflection is a difficult (well I don’t think so, but others do) part of the Java language too, but it’s incredibly powerful and Java would be a much, much less useful language without it. But it’s presence, doesn’t mean that every Java programmer has to be an expert at it.

Can’t you get a lot of that with Java Generics? I mean, there are existing ways to deal with that… e…g. you can sort any objects that implement comparable.

No. Have you heard of concepts before? They are the C++ template equivalent of a Java interface. For example, the sort function defines the values of the iterator parameters to implement the LessThanComparable concept, which, roughly speaking, means that they must be able to compare to each other using the “<” operator. Concepts are actually more powerful than interfaces, in that they allow conformance through structure and not just name. On the other hand, specialization allows you to replace an entire function, method, or class altogether for a particular combination of types. It’s quite powerful.

Which is why this seems to be a strrange conclusion… I would have concluded “Right - so there isn’t much to them, but they give you a lot.”

Java generics don’t enable anything whatsoever besides casting. That doesn’t help anybody implement generic algorithms or containers in any fashion. Hopefully you’re starting to see some of the differences here.

the fact that I worked with C++ for so many years and only recently hearing of these things is why I suggested that templates generally aren’t used that way in most programs

Programs that use templates this way have been around for at least over five years. That’s over half of Java’s entire lifetime. Perhaps you don’t keep up with C++ as much as you used to? For example, how many C++ journals do you read compared to Java journals? How many C++ newsgroups do you read as compared to Java newsgroups? It’s easy to fall behind.

The whole Lambda-calculus thing for instance… it seems mostly academic now… how much programming is really done that way… in the grand scheme of things none (at he moment), regardless of it’s power.

Come now, nobody’s programming in functional languages? Lisp, Haskell, etc…? I personally know people who do. Besides all of that, the Standard C++ library is based on a functional approach. What do you think algorithms are? In fact, most C++ users have been complaining their heads off that they can’t create lambda functions to use with their algorithms. This is one potential advantage that Java has over C++. Last time I checked, Gilad Bracha was working on a JSR to add lambda functions to Java.

So while I accept your point that generics could be vastly more powerful, and that C++ templates ARE more powerful, it still seems that the power is ultimately lost on the chumps like me that haven’t caught on to it yet… but since we chumps make up the bulk of the users (I’m making assumptions, I know) it isn’t really worth it…

I’m not suggesting that Java be made into a vastly complex language. I’m suggesting that we get a more powerful generics mechanism. If it takes you a while to use the mechanism for more than shallow casts, then that’s fine. Just like for those who use reflection at its very shallowest level - System.out.println( getClass().getName() ).

God bless,
-Toby Reyelts

Don’t accuse me of twisting text of an article, that was a direct cut-and-paste of relavent sections of the article, and I gave a reference to the full article for anyone who wants more background.

You derived a different meaning from the text of the article than the author intended. That’s twisting the meaning.

As far as your response, you chose to ignore my statement that “We are not talking about STL, we are talking about Proxies as code generators” and decided to go on an off-topic rant.

What? Off-topic? Rant? I simply responded to your statements.

So, compilers compile at runtime…[sarcasm]right[/sarcasm].

Umm, they sure do Chris. What do you think a compiler is? Code that runs on your machine. When do template instantiations get created? As the compiler is running.

What do you think Proxy is? Code that runs on your machine. When do Proxy instances get created? As the Proxy is running.

Anyways, getting back to the topic, your example doesn’t demonstrate any code generation from proxies, you just created an object and passed in a few parameters. This is code generation?

Ok Chris. If you can’t see that the function Proxy.newProxyInstance actually generates (through delegation to sun.misc.ProxyGenerator) the byte code for an entirely new class and then loads that the class into the Java Virtual Machine, then nothing else I say can make any sense to you.

does that mean something like:
vector.add(new Object()) // this generates code??
actually generates some code based on the logic of your example?

You’ve managed to draw an analogy that makes no sense. I’m not impressed.

Proxies are more akin to dynamic method invocation than code generations

No they aren’t. A Proxy doesn’t dynamic dispatch to anything. Proxy generates the bytecode for a new class that implements the interfaces that you specify. That new class is hardcoded to call the InvocationHandler that you pass it. That’s it. No dynamic method invocation whatsoever. Lots of code generation.

and certainly have no resembelance to macros as you sugest here:

Congratulations. You managed to answer the rhetorical question correctly. You win a $1000. (Well, maybe not). Proxy does not have a relationship to macros, and neither do templates. If you read that converation closely, you will see that I was saying that templates are like Proxy in that they are not like macros. They both have total respect for all of the features of their language.

God bless,
-Toby Reyelts

Ok, I did some reading on proxies, and I’m not sure where you got the information that bytecode was being generated, but this is my take on it (bearing in mind I didn’t write the JVM nor the proxy classes API, I’m only relaying my understanding)…

First, I’m going to take the code you provided, and re-work it slightly to remove the annonymous class so that it’s clearer what bytecode is being generated.

there’s 2 classes here, the class that is the inner class, and the class that is using the Proxy class to make the Proxy instance based on the parameters passed:


public class ActionListenerInvocationHandler implements InvocationHandler
{
  public void invoke(Object proxy, Method method, Object[] args)
  {
    System.out.println("Hello");
  }
}

Public class ProxyBuilder
{
  public ActionListener buildActionListenerProxy()
  {
    Class[] implementedInterfaces = {java.awt.ActionListener.class };
    return Proxy.newProxyInstance(getClass.getClassLoader(), implementedInterfaces, new ActionListenerInvocationHandler());
   }
}


After we compile, we have 2 .class files with bytecode, the ActionListenerInvocationHandler and the ProxyBuilder. When this code runs, all the code is doing is dynamically building a class definition (indicating what class loader should be the ‘owner’ of the class, defining the interfaces that the class (the proxy) should implement and who should handle method invocations. All that’s happening is a object is being built, but no new bytecodes are being produced, it’s all using the standard reflection api to define the type at runtime. Also, I can dynamically decide how to handle the invocation at runtime by putting the required logic into the invocation handler’s invoke() method.

When I wrote this:

the analogy I was trying to show that building a list isn’t code generation, and building a proxy is not code generation either. I hope that this is clearer now.

As an aside, do you consider yourself more of a C++ programmer than a Java programmer?

-Chris

Ok, I did some reading on proxies, and I’m not sure where you got the information that bytecode was being generated, but this is my take on it

Where did I get my information from? Where did you get your (mis)information from? You don’t seem interested in reading anything, but I’ll suggest you try, http://www.sys-con.com/java/articleprint.cfm?id=1308, anyway. It’s detailed enough to talk about the bytecode generation, and it’s short, so hopefully it’s right down your alley.

Personally, I read the Javadoc for Proxy, I read a couple of articles on Proxy, I used Proxy myself when I developed a JDO based O/R engine from scratch (got to love JavaCC for that JDOQL parsing), I wrote an article for CUJ (The Tattletale Technique) based on code I wrote for generic resource management that uses Proxy, and I actually read the source code for java.lang.reflect.Proxy and sun.misc.ProxyGenerator. It only takes about 1/10th of that to make it painfully obvious that Proxy does actually generate bytecode.

Btw, I don’t appreciate the condescending rewrite and explanation of my code. It really doesn’t reflect well on you as it demonstrates that you’re missing the entire point of this discussion.

the analogy I was trying to show that building a list isn’t code generation, and building a proxy is not code generation either. I hope that this is clearer now.

As I said before, you have managed to draw a meaningless analogy. Nothing has changed.

As an aside, do you consider yourself more of a C++ programmer than a Java programmer?

Actually I consider myself an excellent software engineer who happens to be particularly well-versed in both C++ and Java. The work I’ve done on Jace, http://jace.reyelts.com/jace, should demonstrate that. As an example, Jace has a tool that parses the bytecode in class files to generate C++ proxy classes, and it has another tool that modifies the bytecode (significantly more difficult than generation of bytecode) in .class files to add transparent C++ peer management (similar in ways to JDO enhancement).

God bless,
-Toby Reyelts