Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Java Pretty Printer #145

consiliens opened this Issue Sep 4, 2011 · 1 comment


None yet
1 participant

consiliens commented Sep 4, 2011

From the mailing list.

headius posted:

I'd love to see someone incorporate a small Java pretty-printer into
the -j output. It wouldn't be hard, and you'd get code out that almost
looks hand-written.

  • Charlie

woodie posted:

Extra credit if you can collapse the fully-qualified class names, and insert
the appropriate import statements at the top of the file.


consiliens commented Sep 8, 2011

# tmp.mirah
puts 'hi'
// Generated from tmp.mirah
public class Tmp extends java.lang.Object {
  public static void main(java.lang.String[] argv) {
    java.io.PrintStream temp$1 = java.lang.System.out;

Compared to human Java:

public class Tmp {
  public static void main(String[] argv) {

There's a bit of work to get readable Java out of mirah. I created a small patch that attempts to address the extends java.lang.Object and java.lang.* in arguments issue. With the below patch, this is what the Tmp class looks like.

// Patched.
// Generated from tmp.mirah
public class Tmp {
  public static void main(String[] argv) {
    java.io.PrintStream temp$1 = java.lang.System.out;
diff --git a/lib/mirah/jvm/source_generator/builder.rb b/lib/mirah/jvm/source_generator/builder.rb
index 60c3e12..5ad2ac7 100644
--- a/lib/mirah/jvm/source_generator/builder.rb
+++ b/lib/mirah/jvm/source_generator/builder.rb
@@ -232,7 +232,14 @@ module Mirah

         @declaration_finished = true
         modifiers = "public#{' static' if @static}#{' abstract' if @abstract}"
-        print "#{modifiers} class #{class_name} extends #{superclass.name}"
+        superclass_name = superclass.name
+        if superclass_name == 'java.lang.Object'
+          print "#{modifiers} class #{class_name}"  
+        else
+          print "#{modifiers} class #{class_name} extends #{superclass_name}"
+        end
         unless @interfaces.empty?
           print " implements "
           @interfaces.each_with_index do |interface, index|
@@ -370,7 +377,9 @@ module Mirah
         print "#{@visibility}#{@static}#{@abstract} #{@typename} #{@name}("
         @args.each_with_index do |(type, name), i|
           print ', ' unless i == 0
-          print "#{type.to_source} #{name}"
+          arg_type = type.to_source
+          arg_type = arg_type[10..-1] if arg_type[0,10] == 'java.lang.' && arg_type[10,1].upcase! == nil
+          print "#{arg_type} #{name}"
         print ')'
         unless @exceptions.empty?

It'd be nice if this was done in the typer automatically so that messing with strings isn't required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment