JVM languages for J2ME platform - java-me

I'm currently writing an embedded application for J2ME environment (CLDC 1.1 configuration and IMP-NG profile). Being spoiled by all those new features in JVM-based languages (Groovy, Scala, Clojure, you name it), I was considering using one of them for my code.
However, most of the mentioned languages require pretty decent JVM environment. Most so-called "dynamic" languages require the VM to have reflection. Many ask for annotations support. None of the above features are available under J2ME.
From what I found, Xtend looks like a viable options, as its compiler spits out plain Java, not bytecode, and doesn't require any library for runtime needs. Of course, the generated Java code also must meet some requirements, but Xtend webpage looks promising in this regard:
Xtend just does classes and nothing else
Interface definitions in Java are already nice and concise. They have a decent default visibility and also in other areas there is very little to improve. Given all the knowledge and the great tools being able to handle these files there is no reason to define them in a different way. The same applies for enums and annotation types.
That's why Xtend can do classes only and relies on interfaces, annotations and enums being defined in Java. Xtend is really not meant to replace Java but to modernize it.
Am I right and it is possible to compile Xtend-generated code for J2ME platform, or there are some constructs that will not work there?
Alternatively, can you recommend any other "rich" Java modification language that can be run on J2ME?
Update: Knowing that the "compiler" producing results as another source code is called transcompiler, one can also find Mirah, a tool which requires no runtime library and specific Java features.

Xtend's generated code uses google guava heavily. If that is compatible to the J2ME, Xtend could be the language of your choice. I'm not aware of anything that prevents from using it on other platforms that provide a dedicated development kit (e.g. Android).

In addition to being able to generate Java source, Mirah recently added support for javac's --bootclasspath option, which allows you to generate your bytecode against a non-standard version of the java core classes, e.g. LeJOS.
It's still a little fresh, but it'd be nice to have more people using it on different javas.

Related

What is the difference between AspectJ And ASM?

As I understand, the 2 frameworks are both static that injects monitor codes into class codes. So, what is the difference?
ASM is a framework/library which provides you an API to manipulate existing bytecode and/or generate new bytecode easily.
AspectJ, on the other hand is a language extension on top of the Java language with it's own syntax, specifically designed to extend the capabilities of the Java runtime with aspect oriented programming concepts. It includes a compiler/weaver, which can either be run at compile time or run-time.
They're similar in the sense that both achieve their goals by bytecode manipulation of existing bytecode and/or generating new bytecode.
ASM is more general in the sense that it doesn't have an opinion about how you would want to modify existing bytecode, it just gives you an API and you can do whatever you want with it. AspectJ, on the other hand, is more specific, more narrow scoped, it only supports a few predefined AOP constructs, but it gives you an interface (the aspectj language) which is much easier to work with if you can fit within those constructs it provides you with.
For most use-cases I've seen, AspectJ is more than enough, but in those rare cases where it wouldn't, ASM can be a good alternative, but you'll need more programming work to achieve similar results.

Is groovy a must to learn OfBiz?

Is it a must to know Groovy to start learning Ofbiz? The one book i can find is for version 4 of ofbiz and it uses Beanshell. I don't know if later versions of ofbiz have added support for groovy.
Objective: to create workflows as necessary.
Thanks.
EDIT: Found this. From what i understand, one can use any compliant Java scripting language.However, Groovy will be supported OOTB. (Groovy is not just a scripting language, but it is one of the roles)
Later versions have added support for Groovy.
AFAIK, you can still use Beanshell if you want
No, you shouldn't, unless you want use not java but groovy to develop something - e.g, event, service - for OFBiz.
If you familiar with java, that's enough for you to read and understand the source code written in groovy, and it's enough in most case to write some simple in-line groovy script used in OFBiz.
"Know groovy", it's nice-to-have but not must to "start learning OFBiz".
Groovy is used a lot in screen actions, which is the data preparation code that is part of generating UI output. There is a lot of code in the project like this.
It can also be used to implement services and request events (used for processing input), and is a popular tool for custom extensions to OFBiz even though not used a lot in OFBiz itself.
As stated in other answers if you know Java it's easy to read most Groovy code (some closure syntax can be confusing at first), but it's worth learning about more to reduce code size and effort, and make your code cleaner and easier to maintain. In other words, Groovy has a lot of extensions beyond plain Java that are very useful, especially for business logic in applications like those built with Apache OFBiz.
As a case in point, the next generation framework based on the ideas in OFBiz (Moqui Framework, www.moqui.org) is written largely in Groovy and supports Groovy for everything whereas OFBiz also uses JUEL for expressions (and even Beanshell still in a couple places). I should note that both frameworks support a number of other scripting languages for business logic if you have other strong preferences, but it is nice to standardize on one so that developers have less to learn and can more easily work with existing business logic and (as applicable) framework code.

Are there real world applications that use metaprogramming?

We all know that MetaProgramming is a Concept of Code == Data (or programs that write programs).
But are there any applications that use it & what are the advantages of using it?
This Question can be closed but i didnt see any related questions.
IDEs are full with metaprogramming:
code completion
code generation
automated refactoring
Metaprogramming is often used to work around the limitations of Java:
code generation to work around the verbosity (e.g. getter/setter)
code generation to work around the complexity (e.g. generating Swing code from a WYSIWIG editor)
compile time/load time/runtime bytecode rewriting to work around missing features (AOP, Kilim)
generating code based on annotations (Hibernate)
Frameworks are another example:
generating Models, Views, Controllers, Helpers, Testsuites in Ruby on Rails
generating Generators in Ruby on Rails (metacircular metaprogramming FTW!)
In Ruby, you pretty much cannot do anything without metaprogramming. Even simply defining a method is actually running code that generates code.
Even if you just have a simple shell script that sets up your basic project structure, that is metaprogramming.
Since code as data is one of key concepts of Lisp, the best thing would be to see the real applications of projects written in these.
On this link you can see an article about a real world application written partly in Clojure, a dialect of Lisp.
The thing is not to write programs that write programs, just because you can, but to add new functionality to your language when you really need it. Just think if you could simply add new keyword to Java or C#...
If you implement metaprogramming in a language-independent way, you get a program analysis and transformation system. This is precisely a tool that treats (arbitrary) programs as data. These can be used to carry out arbitrary transformations on arbitrary programs.
It also means you aren't limited by the specific metaprogramming features that the compiler guys happened to put into your language. For instance, while C++ has templates, it has no "reflection". But a program transformation system can provide reflection even if the base langauge doesn't have it. In particular, having a program transformation engine means never having to say "I'm sorry, your language doesn't support metaprogramming (well enough) so I can't do much except write code manually".
See our DMS Software Reengineering Toolkit for such a program transformation system. It has been used to build test coverage and profiling tools, code generation tools, tools to reshape the architecture of large scale C++ applications, tools to migrate applications from one langauge to another, ... This is all extremely practical. Most of the tasks done with DMS would completely impractical to do by hand.
Not a real world application, but a talk about metaprogramming in ruby:
http://video.google.com/videoplay?docid=1541014406319673545
Google TechTalks August 3, 2006 Jack Herrington, the author of Code Generation in Action (Manning, July 2003) , will talk about code generation techniques using Ruby. He will cover both do-it-yourself and off-the-shelf solutions in a conversation about where Ruby is as a tool, and where it's going.
A real world example would be Django's model metaclass. It is the class of the class, from which models inherit from and responsible for the outfit of the model instances with all their attributes and methods.
Any ORM in a dynamic language is an instant example of practical metaprogramming. E.g. see how SQLAlchemy or Django's ORM creates classes for tables it discovers in the database, dynamically, in runtime.
ORMs and other tools in Java world that use #annotations to modify class behavior do a bit of metaprogramming, too.
Metaprogramming in C++ allows you to write code that will get transformed at compilation.
There are a few great examples I know about (google for them):
Blitz++, a library to write efficient code for manipulating arrays
Intel Array Building Blocks
CGAL
Boost::spirit, Boost::graph
Many compilers and interpreters are implemented with metaprogramming techniques internally - as a chain of code rewriting passes.
ORMs, project templates, GUI code generation in IDEs had been mentioned already.
Domain Specific Languages are widely used, and the best way to implement them is to use metaprogramming.
Things like Autoconf are obviously cases of metaprogramming.
Actually, it's unlikely one can find an area of software development which won't benefit from one or another form of metaprogramming.

What is the best way to create multiple language versions of a domain?

I would like to create a set of domain objects in multiple languages, so that I can target different platforms. I have been looking at external DSLs as a way to define a language for my domain, and then potentially writing adapters that generate code for the languages I'm interested in targeting. Is this the best way to solve this problem? Or is it just simpler to maintain multiple versions of the project?
I think that Apache Thrift delivers what you are asking for.
Sorry for late answer, but as you mention C# being your main language, this practically fully supported Visual Studio based technology is exactly what you are looking for.
You have to understand what you want to abstract with your DSLs, but the multiple-platform support is trivial on top of that.
Disclaimer: This is our technology, but it's publicly open and it solves exactly the problem presented in the question.
http://abstraction.codeplex.com/
Note! Mind the very "alpha" stage of the current download, I suggest you skip the zipped download and grab the latest source. I am updating better construct in relatively near future. Check out the "Context" implementation in "Production/Dev/AbstractionTemplate" solution.
It is difficult to be helpful without understanding what you are planning to use your DSL for.
Is portability your main problem here?
To succesfully target these different platforms, you will probably have to maintain plaftorm-specific layers anyway (generated or not).
If you plan to write your whole application in your DSL, then use your own compiler to transform it into runnable code for each platform, well it is most probably a bad idea, too complex and overengineered.
However, if you have a well-defined chunk of platform-independent logic, then a DSL is a good choice. Just write an interpreter for it on each target platform (provided that performance is not critical, this is also simpler and easier than generating code).
What is the best way to create multiple language versions of a domain?
This is (was?) somehow the idea of Model Driven Architecture (MDA). Quoting Model-driven architecture from Wikipedia:
The Model-Driven Architecture approach
defines system functionality using a
platform-independent model (PIM) using
an appropriate domain-specific
language (DSL).
Then, given a platform definition
model (PDM) corresponding to CORBA,
.NET, the Web, etc., the PIM is
translated to one or more
platform-specific models (PSMs) that
computers can run. This requires
mappings and transformations and
should be modeled too.
The PSM may use different Domain
Specific Languages (DSLs), or a
General Purpose Language (GPL) like
Java, C#, PHP, Python, etc. Automated tools generally
perform this translation.
Depending on the complexity of your domain and the availability of a MDA Tool, this might be an option (with a lower implementation cost).
See also
MDA: Nice idea, shame about the ...
Language Workbenches and Model Driven Architecture
UML vs. Domain-Specific Languages
DSL in the context of UML and GPL
UML or DSL: Which Bear Is Best? (be sure to read this one)

Groovy advantages over Jython or Jruby?

Why would I choose to use Groovy when I could use Jython or Jruby? Does the language provide any inherent advantages to make up for the fact that Jython and Jruby skills are applicable to their parent languages outside of the JVM?
Keep in mind that I purposely keeping this question generic, but if there are any advantages that exist in a particular domain, please don't hesitate to describe them.
EDIT
To clarify, If I write some code in Jruby, I can now, in some cases, move that code outside of the JVM if need be, or at the very least I have gained a better understanding of Ruby. Whereas Groovy skills are applicable only when using a language that just exists inside the JVM. Jython and Jruby have this built in advantage, what does Groovy have to make up for this disadvantage?
If Groovy doesn't have any advantages that you've found, and you would suggest just using Jython or Jruby, let me know.
Edit 2
Thanks everyone for all the answers, most of them make the same point, Groovy integrates slightly better with Java then Jython or Jruby.
Follow up
Using Netbeans 6.5 as my IDE I have found that Groovy to integrates better with Java projects then Jruby. I am not sure if lack of integration is a failing of Jruby or Netbeans. But after using it for alittle Groovy definitely seems to have a leg up.
I've done pretty extensive development in Ruby and Groovy (as well as a little Jython using Grinder as a load testing tool).
Of the 3, I prefer Groovy the most. I like the closure syntax the best and I think that it has the tightest integration in how it works with other java classes on the JVM. It's been a little while since I last used JRuby, but importing Java classes and working with the classloader in JRuby didn't feel as clean to me.
The fact that Groovy is also essentially a superset of Java means that the huge population of Java programmers out there will have a quicker uptake time in picking Groovy up over Ruby/JRuby. They can start programming it like it's Java and slowly start inserting idomatic groovy as they pick it up.
More to the point of what you're asking, I think that another advantage of Groovy is that the language that you go to when you want to optimize something is almost the exact same syntax, it's Java. If you're working in the Ruby or Python worlds, you're going to have to go to either C which is a big shift or Java, which is also quite different than those languages. Programming in Groovy tends to help keep your Java skills somewhat sharp as well.
If you have particular access to a Ruby or Python infrastructure, or a team that has familiarity with those kind of environments, then I could see choosing one of those other languages.
Really, all 3 of them are very nice languages and what you pick should depend more on the problem that you're trying to fix and the resources that you have available to you. Once you've become proficient in one dynamic language, picking up a second or a third is much easier.
I would say if you need to mix Java with Jruby/Groovy, go with Groovy. As everybody said, Groovy has tighter Java integration.
But as far as the language implementation goes, I prefer the Ruby language over Groovy, the language revolves around itself, in Groovy there are some hacks that are inherent to the implementation itself (just watch a Grails stacktrace vs. a Rails stacktrace and you'll see what I mean).
I highly recommend seeing Neal Ford's comparison of Groovy and JRuby
I think Dick Wall gave a very good summary of the differences between these three on the Java Posse podcast (#213, about 34:20 in) ...
"JRuby was designed to make programmers happy ... it's a programming language developer's choice; Python has very strong roots in simplicity and education; Groovy is aimed squarely at being the choice for Java developers ... it's a very familiar environment for Java ... with support for annotations".
In terms of moving the language outside of the JVM, I don't think the Java runtime imposes much of an overhead -- it's a simple install, and you need to set some environment variables -- but it does provide a number of benefits including a mature runtime which has been highly optimised, and a large set of libraries. The JRuby team are now reporting better performance than the native MRI. http://blog.headius.com/2008/08/twas-brillig.html
I've only had experience with Jython and Groovy. The biggest disadvantage with Jython, at the moment, is that the latest release recommended for production (2.2.1) has a feature set that "roughly corresponds to that of Python-2.2" (Jython FAQ). There is a beta implementing what I assume is Python 2.5, which is now a version behind. Don't know if the same can be said for JRuby.
I don't know why you should choose Groovy because I don't know your background. If you are a Java developer Groovy feels more similar to your current language then JRuby or Jython. Groovy combines the best of Java, the language, Java, the platform, and Ruby the language.

Resources