Toby Inkster avatar Toby Inkster committed 34279cc

Proof-of-concept rewriting from Java interfaces to Moose roles.

Comments (0)

Files changed (1)

examples/inferface-parsing.pl

 use Data::Printer;
 use Data::Dumper;
 
-my $jt = Java::Parser->new(tokens => <<'JAVA');
+my $doc = Java::Parser->new(tokens => <<'JAVA')->parse;
 /**
  * interface Predator
  */
 }
 JAVA
 
-print Dumper($jt->parse);
-say "Done";
+print "use 5.010;\n";
+print "use MooseX::Declare;\n\n";
+
+foreach my $decl (@{ $doc->declarations })
+{
+	next unless $decl->isa('Java::Ast::InterfaceDeclaration');
+	my @code;
+	
+	push @code,
+		sprintf("our \@JAVA_MODIFIERS = qw/%s/;", join q{ }, @{$decl->modifiers}),
+		"use boolean;";
+	
+	# This is hackish. Need to handle the value for the constant better.
+	push @code, map
+		{ sprintf("use constant %s => %s;", $_->name, $_->value->token->spelling); }
+		@{ $decl->members };
+
+	push @code, map
+		{ sprintf("requires '%s'; # returns %s", $_->name, $_->type); }
+		@{ $decl->methods };
+	
+	my $code = sprintf("role %s\n", $decl->name)
+		. (join q{}, map {"\twith $_\n"} @{ $decl->base })
+		. "{\n"
+		. (join q{}, map {"\t$_\n"} @code)
+		. "}\n\n";
+	
+	print $code;
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.