å ç¢ãªã³ã¼ãã£ã³ã°ã«ã¼ã«ãçå®ããæ¹æ³(2)
ããã¼ããªããæã人ï¼ç¬ï¼ãè¦ã¦ãããã ãçªã£è¾¼ã¿æ¿ãããããï½
ãã¦ãååè¨ã£ã¦ãããå¤æãã¸ãã¯ãã«ã¤ãã¦ã®çãã¯åèªèãã¦ã¿ãã ãããï¼ åæ¹é¢ã®åå¿ãè¦ãã¨ã1ã4ã©ãã§ãããã*1ãã¨ããåãã«ããæ¿ãã£ã¡ãã£ã¦ãæ°ããããã©ããã¼ãã¯ã1ã4ã決ãããã¸ãã¯ãèãã¦ãã ããããªãã ãæ¸ãæ¹æªãã£ãããããï½ ã¾ãã¤ã¾ããæ¨æ¥ã®ã¨ã³ããªã®æç¹ã§ã¯1ã4ã©ãã§ããªãã®ã§ãããå¤æãã¸ãã¯ããªãã®ã§ã
ã¨ããããã§æ¬é¡ãã¢ããªã±ã¼ã·ã§ã³ã¬ãã«ã§ã®ãã°ã¨ã¯ãå®éã®æåãä»æ§ã¨éãã¨ããã§ãããæåãããã¹ã姿(=ä»æ§)ã¨éãæã«ããã¤ãã°ã¨ããã®ã妥å½ã§ããããã¦ããã¯ã³ã¼ãã¬ãã«ã§ãåãåºæºã§è¯ãããããªãã§ããããã
ããããä»æ§ã¨ã¯ä½ããJavadocã³ã¡ã³ãã§ãããJavadocããªãã¨ãã°ã®å®ç¾©ãããã§ããªããã§ããã ããå£é ¸ã£ã±ãJavadocæ¸ãã¨ï½*2
ã¾ãã¤ã¾ããJavadocã«æ¸ããã¦ããªãæåãããããã°ãã§ããã¨ããããã§Javadocãæ確ã«è¨è¿°ãã¾ãã
ã¡ãªã¿ã«ãä»åã¯ä¾ç¤ºããªãã£ãããåºåºã¯ã©ã¹ãã¤ã³ã¿ã¼ãã§ã¤ã¹ã®è¨è¿°ãéè¦ã§ããä¾ãã°ãåºåºã¯ã©ã¹ã®hoge()ã¡ã½ããã«@throws SomeRuntimeExceptionã宣è¨ããã¦ããªãéãããµãã¯ã©ã¹ã®hoge()ã§SomeRuntimeExceptionãæãã¦ã¯ãªããã§ãããã®ã¤ã³ã¹ã¿ã³ã¹ãã¤ã³ã¿ã¼ãã§ã¤ã¹è¶ãã«è¦ãæã«è¦ããJavadocã¯ãã¤ã³ã¿ã¼ãã§ã¤ã¹ã®ãã®ãªã®ã§ãããã«è¨è¿°ããã¦ããªããSomeRuntimeExceptionãæãããã¨ããæåããããã¨ã«ãªã£ã¦ãã¾ãã¾ãã
ã¾ããããªJavadocãè¨è¿°ããã¨ããã°ããæããã«ãªãã¾ãããã§ããã°ã«ã¯2種é¡èãããã¾ã*3ã
- ä»æ§ãã°
- Javadocã«æ¸ãã¹ããã¨ãæ¬ è½ãã¦ããããããã¯ééã£ããã¨ãæ¸ãã¦ããå ´åã
- å®è£
ãã°
- Javadocã«æ¸ãããä»æ§ã¯æ£ãããããã«å¯¾å¿ããå®è£ ãééã£ã¦ããå ´åã
åè¿°ã®SomeRuntimeExceptionã®ãã ãã§ãä»æ§ãã°ãã¨ããå¤æããããããã¤ã³ã¿ã¼ãã§ã¤ã¹ã«@throws SomeRuntimeExceptionã®å®£è¨ã追å ãããã¨ããfixããããã¨ã«ãªãã¾ãããå®è£ ãã°ãã¨å¤æãããããµãã¯ã©ã¹ã§SomeRuntimeExceptionãæããã«ã©ãã«ããããã¨ããfixããã¾ãã
ãã¦ãæ¨æ¥æãããµã³ãã«ã½ã¼ã¹ã«ã¤ãã¦Javadocãã¤ãã¦èãã¦ã¿ã¾ããããã¾ããããããfooã¨ãbarã¨ãhogeãªãµã³ãã«ã³ã¼ãããªã®ã§ãå ·ä½çãªä»æ§ã¯æ¸ãããããªããã§ãããå®è·µçãªã³ã¼ãã«ããã¨ãã¤ãºã«ãªã£ã¦ãã¾ãã®ã§ãå®ç¾©ãããã§ã¼ãªã®ã¯ãæå¬ã
Fooã®ãã°ã¨ãªãJavadoc
/** * (Fooã«ã¤ãã¦ã®èª¬æ) */ public class Foo { private int bar; /** * ãã®ã¤ã³ã¹ã¿ã³ã¹ã®barãè¿ãã * * @returns ãã®ã¤ã³ã¹ã¿ã³ã¹ã®bar */ public String getBar() { return String.valueOf(bar); } /** * ãã®ã¤ã³ã¹ã¿ã³ã¹ã®barãè¨å®ããã * * @param bar ãã®ã¤ã³ã¹ã¿ã³ã¹ã®bar */ public void setBar(String bar) { this.bar = Integer.parseInt(bar).intValue(); } } /** * ããã°ã©ã ã®ã¨ã³ããªãã¤ã³ãã¨ãªãmainã¡ã½ãããæã¤ã¯ã©ã¹ã */ public class Main { /** * ããã°ã©ã ã®ã¨ã³ããªãã¤ã³ãã¨ãªãmainã¡ã½ããã * * <p>ãã®ããã°ã©ã ãå®è¡ããã¨ãæ¨æºåºåã« "hoge" ã¨åºåããçµäºããã</p> * * @param args ã³ãã³ãã©ã¤ã³å¼æ°ããã®ããã°ã©ã ã§ã¯ç¡è¦ãããã */ public static void main(String[] args) { Foo foo = new Foo(); foo.setBar("hoge"); System.out.println(foo.getBar()); } }
ã¾ãFooã¯ã©ã¹ã«ã¤ãã¦ãsetBar("hoge") ããã¨NumberFormatExceptionï¼ä»¥ä¸NFEï¼ãé£ã³ã¾ããããããå®éã®æåããããã¦ãä»æ§ãã«ã¯NFEã«é¢ããè¨åã¯å ¨ããªããNFEãé£ã°ãã¨ããè¨è¿°ããªãéããã©ããªå ´é¢ã§ãNFEãé£ãã ããã°ãªãã§ãã
ãã®æç¹ã§Mainã¯ã©ã¹ãè¦ã¦ã¿ã¾ãããMainã®æåãä»æ§ã¨ç°ãªãã¾ããããåå ã¯Fooã®æ¹ã«ããã®ã§ããã¾ãèããå¿ è¦ã¯ãªããFooãç´ãã¦ããèããã¹ããã¨ã§ãã
å¾ã£ã¦ãFooã®Javadocã«NFEã«é¢ããè¨è¿°ã追å ããï¼ä»æ§ãã°ã¨å¤æããå ´åï¼ããNFEãé£ã°ãªãããã«ä½ããã®ä¿®æ£ãããï¼å®è£ ãã°ã¨å¤æããå ´åï¼å¿ è¦ãããã¾ããã©ã¡ãã«ãããsetBar()ã«ã¯ãå¼æ°ã¨ãã¦åãå ¥ããããªããã®ããå ¨ãå®ç¾©ããã¦ããªãã®ã§ãå ¨ã¦ã®å¯è½æ§ãåãå ¥ããã¹ãã§ãããã®ä»æ§ã®ã¾ã¾ã ã£ãããbarã®ãã£ã¼ã«ããStringåã«ãã¦ããµã¤ã¼ã®JavaBeansã«ããã®ãããã®ã§ããããããªãIntegerã«å¤æãã¦ããã®ããããåãããªããï¼ãµã³ãã«ã³ã¼ãã§ãªãå®æ¦çã³ã¼ãã§ã¯ããã®è¾ºããJavadocã§æããã«ããªããã°ãªããªãã¨ããã§ããï¼
Mainã®ãã°ã¨ãªãJavadoc
/** * (Fooã«ã¤ãã¦ã®èª¬æ) */ public class Foo { private int bar; /** * ãã®ã¤ã³ã¹ã¿ã³ã¹ã®barãè¿ãã * * @returns ãã®ã¤ã³ã¹ã¿ã³ã¹ã®bar */ public String getBar() { return String.valueOf(bar); } /** * ãã®ã¤ã³ã¹ã¿ã³ã¹ã®barãè¨å®ããã * * <p>barã«ã¯intã®ç¯å²ã®æ´æ°ã®æåå表ç¾ããè¨å®ã§ããªãã * ä¸è¬çã«ãæ£è¦è¡¨ç¾ "(+|-)?[0-9]+" ã§è¡¨ãããæååã®å ¥åãæ³å®ãã¦ããã * å³å¯ã«ã¯Integer#parseInt(String)ã®æåã«ããã®ã§ãåç §ã®ãã¨ã</p> * * @param bar ãã®ã¤ã³ã¹ã¿ã³ã¹ã®bar * @throws NumberFormatException å¼æ°barãInteger#parseInt(String)ã§è§£éã§ããªãæååã ã£ãå ´å */ public void setBar(String bar) { this.bar = Integer.parseInt(bar).intValue(); } } /** * ããã°ã©ã ã®ã¨ã³ããªãã¤ã³ãã¨ãªãmainã¡ã½ãããæã¤ã¯ã©ã¹ã */ public class Main { /** * ããã°ã©ã ã®ã¨ã³ããªãã¤ã³ãã¨ãªãmainã¡ã½ããã * * <p>ãã®ããã°ã©ã ãå®è¡ããã¨ãæ¨æºåºåã« "hoge" ã¨åºåããçµäºããã</p> * * @param args ã³ãã³ãã©ã¤ã³å¼æ°ããã®ããã°ã©ã ã§ã¯ç¡è¦ãããã */ public static void main(String[] args) { Foo foo = new Foo(); foo.setBar("hoge"); System.out.println(foo.getBar()); } }
ããã¯Fooã®ãã°ã¨ãããã±ã¼ã¹ãFoo#setBar(String) ã®ä»æ§ã¨å®éã®æåã¯åè´ãã¦ããã"hoge"ãä¸ãããNFEãé£ã°ãã¨ããæåãä»æ§ããæããã§ãã
ã¾ããå¥ã®åé¡ã¯ããããparseãã¨ããã¢ã¯ã·ã§ã³ã«å¯¾ãã¦ãæ°åã®æ¸å¼ãéã(NFE)ãã¨ããçµæãè¿ãã¯å¦¥å½ã ãããsetãã«å¯¾ãã¦ãNFEãã¨ããã®ã¯ããã ããªã*4ããsetãã«å¯¾ãã¦ã¯ãå¼æ°ããããã(IllegalArgumentException)ãã¨ããçµæãè¿ãã®ããã¿ã¼ã ãã¾ãä½è«ãªã®ã§ãä»åã¯ãã以ä¸æ·±ã触ããªããã¨ã«ããã
NFE extends IAEã ããã¨æãã¦ããã£ããåéãã ï½ ãããªãåé¡ãªãã
ããã¦åé¡ãããã®ã¯Mainã®æ¹ã§ãNFEãé£ã°ããããªãªãã¬ã¼ã·ã§ã³ããã¦ãå®éã®æåããããªã£ã¦ãããããMainã®ä»æ§ã¯ãæ¨æºåºåã« "hoge"ãã ããã ãé£ãéããããã®ã§ãã°ã
ãã°ãªãã¨ãªãJavadoc
/** * (Fooã«ã¤ãã¦ã®èª¬æ) */ public class Foo { private int bar; /** * ãã®ã¤ã³ã¹ã¿ã³ã¹ã®barãè¿ãã * * @returns ãã®ã¤ã³ã¹ã¿ã³ã¹ã®bar */ public String getBar() { return String.valueOf(bar); } /** * ãã®ã¤ã³ã¹ã¿ã³ã¹ã®barãè¨å®ããã * * <p>barã«ã¯intã®ç¯å²ã®æ´æ°ã®æåå表ç¾ããè¨å®ã§ããªãã * ä¸è¬çã«ãæ£è¦è¡¨ç¾ "(+|-)?[0-9]+" ã§è¡¨ãããæååã®å ¥åãæ³å®ãã¦ããã * å³å¯ã«ã¯Integer#parseInt(String)ã®æåã«ããã®ã§ãåç §ã®ãã¨ã</p> * * @param bar ãã®ã¤ã³ã¹ã¿ã³ã¹ã®bar * @throws NumberFormatException å¼æ°barãInteger#parseInt(String)ã§è§£éã§ããªãæååã ã£ãå ´å */ public void setBar(String bar) { this.bar = Integer.parseInt(bar).intValue(); } } /** * ããã°ã©ã ã®ã¨ã³ããªãã¤ã³ãã¨ãªãmainã¡ã½ãããæã¤ã¯ã©ã¹ã */ public class Main { /** * ããã°ã©ã ã®ã¨ã³ããªãã¤ã³ãã¨ãªãmainã¡ã½ããã * * <p>ãã®ããã°ã©ã ãå®è¡ããã¨ãNumberFormatExceptionãçºçããå¾ãçµäºããã * Fooã®ä»æ§ãèæ ®ããªãã¨ãç´æã§ãæ¨æºåºåã« "hoge" ãåºåããããã¨ããæåã«ãªãããã ãã * å®éã¯NumberFormatExceptionãçºçãã¦ãã¾ãç¶æ³ã表ç¾ãã¦ããã</p> * * @param args ã³ãã³ãã©ã¤ã³å¼æ°ããã®ããã°ã©ã ã§ã¯ç¡è¦ãããã */ public static void main(String[] args) { Foo foo = new Foo(); foo.setBar("hoge"); System.out.println(foo.getBar()); } }
ããã¯ãFooãMainããä»æ§ã¨å®éã®æåãå®å ¨ã«åè´ãã¦ãããMainã«è²ã æ¸ãã¦ããããSystem.out.println(...)ã¨ããã絶対ã«å®è¡ãããªããã©ããããä½ãæ¸ãã¦ããããã¨ãæ£å½åããä»æ§ã«ãªã£ã¦ããã®ãåããã¨æãããã åã«ãNFEãé£ã¶ãã¨ããä»æ§ã§ããã°ããªããthrow new NumberFormatException()ãã¨æ¸ããªãã®ã ãã¨ããè°è«ã«ãªããããããªãã®ã§ã
ä¸è¨ã®Javadocã§ãã¾ã çãã¨ãããããããããã¾ãããããããã§ã¯ãFooãMainã®ä»æ§ãå®ç¾©ãããã¨ãã§ã¯ãªãããããªãã¨ãä»æ§ã«çãè¾¼ãã¹ããªãã ããã¨ããæ¹åä»ãã趣æ¨ãªã®ã§ããã®è¾ºã«ãã¦ããã¾ãï½ï¼éãï¼
Javadocã«æ¸ãã¹ããã¨
ã¨ããããã§ãã³ã¼ãé¨ã¯å ¨ãä¸ç·ã§ããä¸è¨ã®ããããã®éãã¯Javadocã ããJavadocã«ãã£ã¦ãä½ããã°ãªã®ãã決ã¾ãæ§åãåãããã¨ã
ãJavadocã«æ¸ããã¦ããªãæåãããããã°ãã§ããããã¦Javadocã«ã¯ããã®æç« ãèªãã ãã§ããã®ã¯ã©ã¹ãã¡ã½ããã®å®è£ ãæ¼ãç¡ã復å ã§ãããã ãã®æ å ±ãè¨è¿°ãã¹ãã§ãã
ä¾ãã°ãããããsetterã§ãç¶æ ãå¤åããã¤ãã³ãããé£ã°ãããããä¾ã
/** * (ç¥) */ public class Hoge { private Collection<PropertyChangeListener> listeners; private String fuga; /** * Fugaãè¨å®ããã * * @param fuga æ°ãã«è¨å®ããFuga */ public void setFuga(String fuga) { String old = this.fuga; this.fuga = fuga; PropertyChangeEvent e = new PropertyChangeEvent(this, "fuga", old, fuga); for (PropertyChangeListener l : listeners) { l.propertyChange(e); } } /** * ãã®ã¤ã³ã¹ã¿ã³ã¹ã®ãã£ã¼ã«ãå¤æ´ãæ¤ç¥ãããªã¹ãã追å ããã * * @param listener æ°ãã«è¿½å ãããªã¹ã */ public void addListener(PropertyChangeListener listener) { listeners.add(listener); } // ãã®ä»è²ã ãlistenersãåæåãããããªãããããã§ããçç¥ã }
ããããäºæ *5ã§ãããªã£ã¦ãã¾ã£ãå ´åâ¦ã
/** * (ç¥) */ public class Hoge { /** * Hogeãè¨å®ããã * * @param hoge æ°ãã«è¨å®ããHoge */ public void setHoge(String hoge) { // æ¶ãã¡ã¾ã£ãã¼ } /** * ãã®ã¤ã³ã¹ã¿ã³ã¹ã®ãã£ã¼ã«ãå¤æ´ãæ¤ç¥ãããªã¹ãã追å ããã * * @param listener æ°ãã«è¿½å ãããªã¹ã */ public void addListener(PropertyChangeListener listener) { // æ¶ãã¡ã¾ã£ãã¼ } // çç¥ã }
ãã®ããã¥ã¡ã³ãããå éãã®å®è£ 復活ã§ãã¾ããï¼ ã¾ããã®ç¨åº¦ãªãåºæ¥ãªããã¨ããªããããããªããã©ãå®æ¦çãªã³ã¼ããªãã¾ãç¡çã
/** * Fugaãè¨å®ããã * * <p>Hogeãè¨å®ããå¾ããã®ã¯ã©ã¹ã«ç»é²æ¸ã¿ã®å ¨ã¦ã®PropertyChangeListener * ã«å¯¾ãã¦fugaãã£ã¼ã«ããå¤æ´ããããã¨ã表ãPropertyChangeEventãéåºããã</p> * * @param fuga æ°ãã«è¨å®ããFuga */
ã¨ãæ¸ããã¦ããã°ããªãã¨ãå ã«æ»ãããã§ããã¾ããã®ã³ã¼ããé©å½ã«æ¸ããä»æ§ãªã®ã§ãçãããããã¾ãããï½ï¼åã³éãï¼
ãã¦ãã¡ãªã¿ã«setFuga()ã®Javadocãâã®ããã«ä¿®æ£ãã¦ããã¾ã ãã°ãæ®ã£ã¦ã¾ããæ¯éè¦ã¤ãã¦ã¿ã¦ãã ãããï¼ã³ã³ãã¤ã©ã«æãããããã¹ãã¨ãã£ã¿ã§ã³ã¼ãæ¸ãã¦ãã®ã§ã俺ãæå³ãã¦ããªããã°å ¥ã£ã¦ããããããªããã©ï½ï½ï¼
ã¾ã¨ã
ãã°ã®å®ç¾©ãããããã«ã¯ãJavadocï¼ä»æ§ï¼ãå¿ è¦ã§ããã³ã¼ãã£ã³ã°ã«ã¼ã«ã¨ãã¦ã¯ãã¾ããä»æ§ã¨æåã®é£ãéããããã°ã¨ãããã¨æ示ãããããããã¨èªåçã«ãJavadocã«ä½ãæ¸ãã¹ãããã決ã¾ãã
ã³ã¼ãã£ã³ã°ã«ã¼ã«ã«ãäºåæ¡ä»¶ãæ¸ããªãããã¨ããRuntimeExceptionç³»ã§ã@throwsã«æ¸ããªãããã¨ããç´°ããç²åº¦ãåæãã¦ç¶²ç¾ ãããã¨ãããã¾ã大綱ã¨ãã¦ä¸è¨ãå®ç¾©ããã¨è¯ãã¨æãããã ãããã ãã§ã¯ãããªãåãããªã人ãã«ä¼ãããªãå¯è½æ§ãããã®ã§ãå ·ä½ä¾ã¨ãã¦ãäºåæ¡ä»¶ãæ¸ããã¦ãªãã£ãããäºåæ¡ä»¶ãªãã¨å¤æãããã¨ããRTEç³»ã¯@throwsã«æ¸ãã¦ãªããã°ãæããªãã¨å¤æãããã¨ãããããªä¾ç¤ºããã¦è£è¶³ããã
*1:ãã®æåã¯Fooã®ãã°ã§ããããï¼Mainã®ãã°ã§ããããï¼...
*2:åè¿°ã®éããä»æ§ãä¸æ確ã ããããããã¼çãªåå¿ãå¤ãã£ããããã®éããååã§Javadocãæ¸ãã¦ãã¾ãã¨ã1ã4ãåæã§ããããã®å ´ã§1ã¤ã«æ±ºã¾ã£ã¦ãã¾ãã®ã§æ¢ãã¦æ¸ããªãã£ãã
*3:ã©ã¡ããfixã®ããã«ã¯ã½ã¼ã¹ãã¡ã¤ã«ãç´ããã¨ã«ãªãã¾ãã
*4:ä¼è©±ãæç«ãã¦ããªã
*5:é½åã®ããäºæ ã â¦ï½