æ°è¦åºåéçºé¨ã®æ¾æ¬ã§ãã ã¯ãã¯ãããiOS/Androidã¢ããªã®åºåã®éçºã«æºãã£ã¦ãã¾ãã
Androidã¢ããªéçºã®éãçããã¯nullãã©ã®ããã«æ±ã£ã¦ãã¾ããï¼ã¾ããnullãã§ãã¯ãè¡ãã®ã§ããã°ãã©ã®ãããªåºæºã§è¡ã£ã¦ãã¾ããï¼ç§èªèº«ã¾ã ã¾ã Androidéçºæ´ãæµ
ããããç¹ã«ä½ãåºæºããã訳ã§ããªãè³ãæã§if (foo != null)
ã¨ãã£ãnullãã§ãã¯ãè¡ããã¨ãã¦ãã¾ããã
ããã«å¯¾ããå æ¥ã®ç¤¾å ã³ã¼ãã¬ãã¥ã¼ã§ã¨ã¦ãããã«ãªãã¢ããã¤ã¹ããããã¾ãããç§ã®ãããªAndroidåå¿è ã«ã¨ã£ã¦nullã«å¯¾ããèãæ¹ã®åºç¤ãä½ã£ã¦ãããã¬ãã¥ã¼ã ã£ãã®ã§ãæ¬ç¨¿ã§å ±æãããã¨æãã¾ãã
ã¾ããAndroidãJavaéçºã«æ £ããæ¹ã«ã¨ã£ã¦ã¯ãä»æ´ãããªè©±ããã¨ãã£ãå 容ãã¨æãã¾ãããã¯ãã¯ãããã§ã®ã¬ãã¥ã¼ã®ä¸ä¾ã¨ãã¦ã覧ããã ããã°å¹¸ãã§ãã
ãããã¡ãªnullãã§ãã¯
ããã¨ãããAndroidã§ã¯ã¯ã©ãã·ã¥ããªããã¨ã大äºã§ããããã®ããã«nullãã§ãã¯ã¯åºæ¬ï¼ãã¨ããèãã®ãã¨ã以ä¸ã®ãããªã³ã¼ããè³ãã¨ããã«æ¸ãã¾ããã
if (foo != null) { foo.bar(); }
ããã¨ã³ã¼ãã¬ãã¥ã¼ã§ãããªã³ã¡ã³ãããããã¾ããã
ä¸è¬ã«ãéå°ãªã¨ã©ã¼ãã§ãã¯ã¯æ··ä¹±ãæãã®ã§ãããªãããå é¨ç¶æ ãå£ãã¦ããã®ã«ãªãã¨ãªãåãã¦ãã¾ãã¨ããã®ã¯çµæãäºæ¸¬ã§ãããã¯ã©ãã·ã¥ããæ·±å»ãªãã°ã«ãªãå¯è½æ§ãããã®ã§ãããªãã§ãã
ä»ã¾ã§èãã¦ããäºã¨ã¯éã§ããã確ãã«ã¬ãã¥ã¢ã¼ã®è¨ãã¨ããã§ããæªãªä¿®æ£ã¯æ ¹æ¬çãªåå ãé ã*1ãã¨ã«ç¹ããã®ã§ããã
ã§ã¯ãã©ããªåºæºã§nullãã§ãã¯ãè¡ãã°è¯ãã®ã§ããããï¼ ãããªçåãæµ®ããã é ããããªã¢ããã¤ã¹ããããã¾ããã
- nullã¯ãµã¤ãã«ããããç¡è¦ã§ãã
- -> ç¾ç¶ãã®æ¯ãèãã§OK
- nullã¯æ³å®å ã®ä¾å¤ãªã®ã§ãã³ãã«ããã
- -> error listenerãç»é²ã§ããããã«ãã
- nullã¯å®å ¨ã«æ³å®å¤ãªã®ã§ãAPIã®ãã°ã§ããèµ·ããããªã
- -> IllegalStateExceptionã§ã¯ã©ãã·ã¥ããããã¾ããã®çµæCrashlyticsã«ãã°ãè¨é²ããã
nullãã§ãã¯ã ãã§ãªããtry catchæãCrashlyticsã使ãåºæºã¾ã§ã¾ã¨ãã¦ãããã¾ããããæ¢ãã¦ã¯ã©ãã·ã¥ããå¯è½æ§ãæ®ãããã¨ãæã«ã¯å¿ è¦ãªãã§ããã
@Nullableã¨@NonNull
ã¾ãããã¨ãããããªã³ã¼ããæ¸ãã¾ããã
public class Car { private Engine engine; public Car(Engine engine) { this.engine = engine; } }
ããã«å¯¾ãããããªã¬ãã¥ã¼ããããã¾ããã
engineã«ã¤ã㦠@Nullable or @NonNull annotationãã»ãããããnullãããã®ãä»æ§ãªã®ãããã§ãªããç¥ãããã
ããããä»æ§ã¨ãã¦nullã¯ããªãï¼ããã¹ãã§ãªãï¼ãªããå¼æ°ã @NonNull ã§æ³¨éãã¤ã¤nullããããIllegalArgumentExceptionæããã¿ãããªã®ã§ããããã§ããã
ãããªã¢ããã¼ã·ã§ã³ã使ãããã§ããï¼ã©ãããAndroid Support library version 19.1ãããµãã¼ããã¦ããããã§ã*2ã
æ´ã«ãããªã¢ããã¤ã¹ãã
å¼æ°ã«ã @NonNull Engine engine ã¨æå®ãããã¨ãã§ãã¦ããããã¦ããã¨ãã®ã¡ã½ãããå¼ã³åºãã¨ãã« @Nullable ãªå¤ã渡ããã¨ããã¨IDEãè¦ååºãã¦ãããããããã§ããï¼ã¾ããè¦åã¯ããªããåºãã¦ãããããã§ã¯ãªãã®ã§ãdocumentã®æå³åãã®ã»ããå¼·ãã§ããã©ã
@Nullable ãªã©ã¯ã©ã®å¤æ°ããã£ã¼ã«ãã«ã¤ãã¦ããããªãã«å¹æã¯ããã¾ãããä¸çªå¹æãããï¼ããã«åªå 度ãé«ãï¼ã®ã¯å ¬éã¤ã³ã¿ã¼ãã§ã¤ã¹é¨åã§ããã¤ã¾ãå¼æ°ã¨æ»ãå¤ããã
å¼æ°ã«æå®ãã¦ããã¨ããã¼null渡ãã¨ã ããªãã ãã£ã¦ãããããããæ»ãå¤ãnullableã 㨠someMethod().chainedMethod() ã¿ããã«ããã¨è¦åãåºãã®ã§ãããããããã¨ã¯ããã¨ããããã¯å¼æ°ã ãã¤ããã®ãããããã§ãã
Android Studioã¯version 0.5.5ãã@Nullable/@NonNull対å¿ãããªãã¡è¦åãåºãã¦ãããããã§ãã*3ã å¼æ°ã«@NonNullãæå®ããã®ã¯ã¨ã¦ã便å©ã§ãããã©ã¤ãã©ãªãSDKãä½ãéã«ãããã®å¼æ°ã¯nullç¦æ¢ï¼ãã¨ãããã¨ãæ示çã«ç¤ºããã¨ã§æ³å®å¤ã®ä½¿ããæ¹ãæ¸ãããã§ãã
æ£ããnullãæ±ãä¾
以ä¸ã®ã¢ããã¤ã¹ãå ã«ä»¥ä¸ã®ã³ã¼ããä¿®æ£ãã¦ã¿ã¾ãããã
public class CarMaker { public Car make(Engine engine) { Car car = new Car(engine); car.checkEngine(); return car; } private class Car { private Engine engine; public Car(Engine engine) { this.engine = engine; } public void checkEngine() { if (engine != null) { engine.check(); } } } }
ã³ã¼ãä¸ã®engineå¤æ°ã¯nullã許容ã§ããªãã®ã§ãCarMakerã¯ã©ã¹ã®å©ç¨è ã«ãã®æ¨ãéç¥ãããã¨ãã¾ãã
ããã§ãengineå¤æ°ã¯@NonNullã§ãããã¨ãã¡ã½ããã®å¼æ°ã§å®£è¨ãã¾ãã
public class CarMaker { public Car make(@NonNull Engine engine) { Car car = new Car(engine); car.checkEngine(); return car; } private class Car { private Engine engine; public Car(@NonNull Engine engine) { this.engine = engine; } public void checkEngine() { engine.check(); } } }
checkEngine()ã¡ã½ããå ã®nullãã§ãã¯ããªããªãã¹ãããªããå¯èªæ§ãä¸ããã¾ããã
ã¾ããengineã¯nullãåãå ¥ããªããã¨ãæ示çã«å®£è¨ãããã®ã§ãã¢ããã¼ã·ã§ã³å¯¾å¿ã®IDE*4ã使ç¨ããéã«è¦åã表示ãããããã«ãªãã¾ããã
ä½è«
å®ã¯ãã®nullã®æ±ããããåç´ã«å²ãåããªãããã§ãã ããã¤ãèæ ®ãã¹ãç¹ãããã¾ãã
ã©ãã¾ã§@NonNullãä»ããã
ä¸ã®ä¾ã§ã¯åã¡ã½ããã®å¼æ°ã«ã®ã¿@NonNullãä»ãã¾ããã ããããCarã¯ã©ã¹ã®engineã¡ã³ããªã©ã«ã@NonNullãä»ãããã¨ã¯å¯è½ã§ãã ã©ãã¾ã§@NonNullãä»ãã¦ãã©ãããã¯ä»ããªãã¦ããã®ã§ããããï¼
å³æ ¼ã«èããå ´å
å³æ ¼ã«èããã°Carã®ã¡ã³ãã§ããengineã«ã@NonNullãä»å ãã¹ãã§ãããã çç±ã¯ä»¥ä¸ã®ã¨ããã§ãã
- public methodã®å¼æ°ã¯ããã¥ã¡ã³ãã¨ãã¦ããIDEã«å¯¾ãã注éã¨ãã¦ãæå¹ãªã®ã§ä»ãã
- ããã«ã@Nullable/@NonNullã¯ä»ããã°ä»ããã ãæ©æµãåºããã®ã§ãªãã¹ãä»ãã
public class CarMaker { public Car make(@NonNull Engine engine) { Car car = new Car(engine); car.checkEngine(); return car; } private class Car { @NonNull private Engine engine; public Car(@NonNull Engine engine) { this.engine = engine; } public void checkEngine() { engine.check(); } } }
ç·©ãèããå ´å
ç·©ãèããã°ã@NonNullãä»å ããã®ã¯make()ã¡ã½ããã ãã§ååã§ãããã çç±ã¯ä»¥ä¸ã®éãã§ãã
- Carã¯CarMakerã®å é¨ã¯ã©ã¹ãªã®ã§ãCarMaker#make() ã«NonNullãä»ããã°åå
public class CarMaker { public Car make(@NonNull Engine engine) { Car car = new Car(engine); car.checkEngine(); return car; } private class Car { private Engine engine; public Car(Engine engine) { this.engine = engine; } public void checkEngine() { engine.check(); } } }
ä½ã®ä¾å¤ãæããã
ããã¾ã§ã®ä¾ã§ã¯æ示çã«ä¾å¤ãæãã¦ããªãã®ã§ãNullPointerExceptionãçºçãã¾ãã ãããããå¼æ°ãä¸æ£ãªã®ã ããIllegalArgumentExceptionãæããã¹ãã ãã¨èããäºãã§ãã¾ãã
ãã®å ´åãã³ã¼ãã¯ä»¥ä¸ã®ããã«ãªãã§ãããã @NonNullã¯ä¸åä»å ãããengineã«å¯¾ãã¦nullãã§ãã¯ããã¦ãã¾ãã
public class CarMaker { public Car make(Engine engine) { if (engine == null) { throw new IllegalArgumentException("Engine must not be null."); } Car car = new Car(engine); car.checkEngine(); return car; } private class Car { private Engine engine; public Car(Engine engine) { this.engine = engine; } public void checkEngine() { engine.check(); } } }
ã©ãã¾ã§ä½¿ç¨è ãä¿¡é ¼ããã
ãã©ãã¾ã§@NonNullãä»ããããã¨ãä½ã®ä¾å¤ãæããããã以ä¸ã®2ã¤ã¯ãã©ãã¾ã§ã¯ã©ã¹ä½¿ç¨è ï¼ä»åã¯CarMaker使ç¨è ï¼ãä¿¡é ¼ããããã«ä¾ãã¨ãããããããã§ãã
ä¾ãã°CarMakerã使ãã®ã¯èªåã ãã§ããã°ãmake()ã¡ã½ããã®å¼æ°ã«@NonNullãä»å ãã¦nullãä¸ããªãããæ°ãã¤ããã ãã§ããã§ãããã
ä¸æ¹ãCarMakerãã©ã¤ãã©ãªã¨ãã¦å ¬éãä¸ç¹å®å¤æ°ã®äººã使ç¨ããã®ã§ããã°ãmake()ã¡ã½ããã®å¼æ°ã§@NonNullãä»å ã注æãä¿ããä¸ã§ãCarã¯ã©ã¹å ã§IllegalArgumentExceptionãæãããã¨ãããã§ãããã
public class CarMaker { public Car make(@NonNull Engine engine) { Car car = new Car(engine); car.checkEngine(); return car; } private class Car { private Engine engine; public Car(Engine engine) { if (engine == null) { throw new IllegalArgumentException("Engine must not be null."); } this.engine = engine; } public void checkEngine() { engine.check(); } } }
ã¾ã¨ã
æ¬ç¨¿ã§ã¯ãAndroidéçºã«ããã¦ç§èªèº«ãåããã¬ãã¥ã¼ãå ã«ãnullã®æ±ãã«ã¤ãã¦ã話ãã¾ããã ä¸è¦ç°¡åãªãããç¥ãã°ç¥ãã»ã©å¥¥ãæ·±ããç§ãå¼ãç¶ãåå¼·ãããã¨æãã¾ã*5ã
ç§ã¨åããæè¿Androidéçºãå§ããæ¹ã®ãå½¹ã«å°ãã§ãç«ã¦ã°å¹¸ãã§ãã