はじめに

Eclipse の [ソース]-[SetterおよびGetterの生成] で setter/getter を作ると、Checkstyle に怒られるコードができるのが嫌だ*1

sg1.png
 
sg2.png

変更箇所

sg3.png

実際に使ってみる

  1. まずはフィールド変数を作る
    • Bean
      package com.snail.example;
      
      /**
       * Example Bean.
       *
       * @author $author$
       * @version $Revision$
        */
      public class ExampleBean {
        /** ABC. */
        private int pAbc;
      
        /** DEF. */
        private int pDef;
      }
      
      
    • エラー
      sg4.png
  2. [右クリック]-[ソース]-[SetterおよびGetterの生成] を選ぶ
    フィールド変数名は "pAbc" でも作られるsetter/getter は、setAbc() / getAbc() になる
    • Bean
      package com.snail.example;
      
      /**
       * Example Bean.
       *
       * @author $author$
       * @version $Revision$
        */
      public class ExampleBean {
        /** ABC. */
        private int pAbc;
      
        /** DEF. */
        private int pDef;
      
        /**
         * @return the abc
         */
        public final int getAbc() {
          return pAbc;
        }
      
        /**
         * @param abc the abc to set
         */
        public final void setAbc(int abc) {
          pAbc = abc;
        }
      
        /**
         * @return the def
         */
        public final int getDef() {
          return pDef;
        }
      
        /**
         * @param def the def to set
         */
        public final void setDef(int def) {
          pDef = def;
        }
      
      }
      
      
    • エラー
      sg5.png
  3. [右クリック]-[Apply Checkstyle Fixes]
    Checkstyle は、メソッド引数を final にするべきだと言っているので、そうしてもらいましょう。
    • Bean
      package com.snail.example;
      
      /**
       * Example Bean.
       *
       * @author $author$
       * @version $Revision$
        */
      public class ExampleBean {
        /** ABC. */
        private int pAbc;
      
        /** DEF. */
        private int pDef;
      
        /**
         * @return the abc
         */
        public final int getAbc() {
          return pAbc;
        }
      
        /**
         * @param abc the abc to set
         */
        public final void setAbc(final int abc) {
          pAbc = abc;
        }
      
        /**
         * @return the def
         */
        public final int getDef() {
          return pDef;
        }
      
        /**
         * @param def the def to set
         */
        public final void setDef(final int def) {
          pDef = def;
        }
      
      }
      
      
    • エラー
      sg6.png
      とりあえずマウス操作だけで、Checkstyle に怒られない setter/getter ができた

Java#Eclipse


*1 Checkstyleの規則を曲げるのは余り良くない...一つ曲げると際限なく曲げるように圧力がかかり...gdgdに

添付ファイル: filesg6.png 2309件 [詳細] filesg3.png 2387件 [詳細] filesg2.png 2410件 [詳細] filesg4.png 2297件 [詳細] filesg1.png 2722件 [詳細] filesg5.png 2361件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS   sitemap
Last-modified: 2008-03-30 (日) 00:43:53 (5870d)
Short-URL: https://at-sushi.com:443/pukiwiki/index.php?cmd=s&k=981d93f93b
ISBN10
ISBN13
9784061426061