JRuby+ERB+ãªãã¬ã¯ã·ã§ã³ã§Javaã½ã¼ã¹ã³ã¼ãã®èªåçæ
Javaã®ã½ã¼ã¹ã³ã¼ãèªåçæãå¯ãã«?ãã¼ã ã§ããã¢ãã«ã¯ã©ã¹ã¨ããµã¼ãã¹ã®ã¯ã©ã¤ã¢ã³ãã¹ã¿ãã¯ã©ã¹ã¨ããç´æãªã³ã¼ãã¯ãããããèªåçæãã¦æ¥½ããã!
ã¨ãããã¨ã§ãRuby+ERBでJavaBeanを作るプログラムã¯åã«ä½ã£ãã®ã§ãããããã¯ãyamlã®å®ç¾©ãã¡ã¤ã«ããã½ã¼ã¹ãä½ããä»çµã¿ãªã®ã§ã¾ãYAMLãæ¸ããªãã¨ä½¿ããªããã¢ãã«ã¯ã©ã¹ã¯ã¾ãããã§ããããã¨æã£ããã©ãã¹ã¿ãã¯ã©ã¹ãªããã¯
- Javaã§ãµã¼ãã¹ã¤ã³ã¿ã¼ãã§ã¤ã¹ãæ¸ã!
- ãã¼ã«ãå®è¡
- âã¤ã³ã¿ã¼ãã§ã¤ã¹ãèªãã§ã¯ã©ã¤ã¢ã³ãã¹ã¿ãã¯ã©ã¹ãã§ãã!
ã¿ããã«ããããã¨ããããããããªãã¨Java使ãããã¯ã¬ã¼ã ããããã ã
ã¨ã¯ãããRubyã§Javaã¤ã³ã¿ã¼ãã§ã¤ã¹ã®ã³ã¼ããæ§æ解æãã¦ã©ãããããã®ã¯ãªããªãã«æ·å± ãé«ããããã§ã²ããããã®ãã
- JRuby+ãªãã¬ã¯ã·ã§ã³ã§Javaã¯ã©ã¹ã«ã¢ã¯ã»ã¹
- èªã¿è¾¼ãã æ å ±ãå ã«ERBã§ã½ã¼ã¹ã³ã¼ãçæ
ã¨ããä½æ¦ãã¨ãããã¨ã§ã¤ã³ã¿ã¼ãã§ã¤ã¹ãèªã¿è¾¼ãã§ãå®è£ ã¯ã©ã¹ãä½ããµã³ãã«ã試ãã«ä½ã£ã¦ã¿ã¾ããã
ã¤ã³ã¿ã¼ãã§ã¤ã¹ãèªã¿è¾¼ãã§å®è£ ã¯ã©ã¹ãä½ããµã³ãã«
ã¾ãã¯ãã¤ã³ã¿ã¼ãã§ã¤ã¹ãèªã¿è¾¼ãã§å®è£ ãçæããJRubyã³ã¼ã(read-interface.rb)ãjava.util.Mapã®å®è£ ãèªåçæãã¾ãã
require 'erb' require 'java' # ERB erb = ERB.new(IO.read("./impl.erb"), nil, "%" ) # ã¤ã³ã¿ã¼ãã§ã¤ã¹ãã¤ã³ãã¼ã import 'java.util.Map' clazz = Map.java_class # java_classã§Javaã®ã¯ã©ã¹ãã¨ãããããã # å®è£ ã¹ã¿ãã®çæ puts erb.result(binding)
ERB(impl.erb)ã¯æ¬¡ã®éããé©å½ã§ãã
/** * {@link <%= clazz.name %>} * * @version $Revision:$ * @author $Author:$ */ public class <%= clazz.simple_name %>Impl implements <%= clazz.name %> { % clazz.java_instance_methods.each{|m| public <%= m.return_type != nil ? m.return_type : "void" %> <%= m.name %>( <%= i=0;m.argument_types.map!(){|arg| i+=1; arg.to_s + " arg" + i.to_s }.join(", ") %> ){ // auto generate % if m.return_type != nil % if m.return_type.name == "int" || m.return_type.name == "long" return 0; % elsif m.return_type.name == "boolean" return false; % else return null; % end % end } % } }
å®è¡!
jruby read-interface.rb > MapImpl.java
å®è¡çµæã§ãã
/** * {@link java.util.Map} * * @version $Revision:$ * @author $Author:$ */ public class MapImpl implements java.util.Map { public int hashCode( ){ // auto generate return 0; } public java.lang.Object put( java.lang.Object arg1, java.lang.Object arg2 ){ // auto generate return null; } public void clear( ){ // auto generate } public boolean equals( java.lang.Object arg1 ){ // auto generate return false; } public java.util.Set entrySet( ){ // auto generate return null; } public java.lang.Object get( java.lang.Object arg1 ){ // auto generate return null; } public void putAll( java.util.Map arg1 ){ // auto generate } public int size( ){ // auto generate return 0; } public java.util.Collection values( ){ // auto generate return null; } public java.lang.Object remove( java.lang.Object arg1 ){ // auto generate return null; } public boolean containsKey( java.lang.Object arg1 ){ // auto generate return false; } public boolean containsValue( java.lang.Object arg1 ){ // auto generate return false; } public boolean isEmpty( ){ // auto generate return false; } public java.util.Set keySet( ){ // auto generate return null; } }
java_classã¨java_instance_methodsãããããªãã¦ãã°ããã¯ã¾ã£ããã©ãã¯ã©ã¹æ å ±ã«ã¢ã¯ã»ã¹ã§ããã®ã¯ã¨ãããã確èªãããã§ã¢ããã¼ã·ã§ã³ã¨ãå¦çã§ããã°ä½¿ãããã ãã©ããã ãã¡ãã£ã¨è¦ãéãã§ã¯ã¢ã¯ã»ãµãè¦ã¤ãããªãã£ãã®ãæ°ã«ãªãã¨ãããè¦èª¿æ»ã§ãã