コード置き場

/*
 * 作成日: 2005/03/22
 */
package com.wikiroom.tpircs.checkstyle.check;

import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.FileContents;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;

/**
 * @author tpircs メソッドをポイント換算
 */
public class MethodPointCheck extends Check {
    /** whether to ignore empty lines and single line comments */
    private boolean mCountEmpty = false;

    /** デフォルトポイント */
    private static final int DEFAULT_MAX_POINT = 15;

    /** ポイント */
    private int maxPoint = DEFAULT_MAX_POINT;

    /** @see com.puppycrawl.tools.checkstyle.api.Check */
    public final int[] getDefaultTokens() {
        return new int[] { TokenTypes.METHOD_DEF, TokenTypes.CTOR_DEF };
    }

    /**
     * @see com.puppycrawl.tools.checkstyle.api.Check#visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST)
     */
    public final void visitToken(final DetailAST aAST) {
        final DetailAST openingBrace = aAST.findFirstToken(TokenTypes.SLIST);
        if (openingBrace != null) {
            final DetailAST closingBrace = openingBrace
                    .findFirstToken(TokenTypes.RCURLY);
            int point = closingBrace.getLineNo() - openingBrace.getLineNo() + 1;

            final FileContents contents = getFileContents();
            if (!mCountEmpty) {
                final int lastLine = closingBrace.getLineNo();
                for (int i = openingBrace.getLineNo() - 1; i < lastLine; i++) {
                    //ここと
                    if (contents.lineIsBlank(i) || contents.lineIsComment(i)
                            || (contents.getLines())[i].trim().equals("{")
                            || (contents.getLines())[i].trim().equals("}")) {
                        point--;
                    }
                }
            }

            //ここだけ
            point += count(aAST, 0);

            if (point > maxPoint) {
                log(aAST.getLineNo(), aAST.getColumnNo(), "maxLen.method",
                        new Integer(point), new Integer(maxPoint));
            }
        }
    }

    /**
     * ポイント増加
     */
    private int count(DetailAST aAST, int point) {
        if (aAST == null) {
            return point;
        }

        switch (aAST.getType()) {
        case TokenTypes.LITERAL_IF:
            //point += 1;
              //変更
              point += countCondition(aAST, 1);
            break;
        case TokenTypes.LITERAL_FOR:
        case TokenTypes.LITERAL_WHILE:
            point += 2;
        default:
        }

        final DetailAST child = (DetailAST) aAST.getFirstChild();
        if (child != null) {
            point = count(child, point);
        }

        final DetailAST sibling = (DetailAST) aAST.getNextSibling();
        if (sibling != null) {
            point = count(sibling, point);
        }

        return point;
    }

    //めちゃめちゃコピペ。リファクタリング前、ってやつです。
    //テストコードはあるんでコピペも許して。
    private int countCondition(DetailAST aAST, int point) {
        if (aAST == null) {
            return point;
        }

        switch (aAST.getType()) {
        case TokenTypes.LOR:
        case TokenTypes.LAND:
            point += 1;
        default:
        }

        final DetailAST child = (DetailAST) aAST.getFirstChild();
        if (child != null) {
            point = countCondition(child, point);
        }

        final DetailAST sibling = (DetailAST) aAST.getNextSibling();
        if (sibling != null) {
            point = countCondition(sibling, point);
        }

        return point;
    }

    /**
     * @return point を戻します。
     */
    public final int getMaxPoint() {
        return maxPoint;
    }

    /**
     * @param point
     *            point を設定。
     */
    public final void setMaxPoint(final int point) {
        this.maxPoint = point;
    }

    /**
     * @param aCountEmpty
     *            whether to count empty and single line comments of the form
     *            //.
     */
    public final void setCountEmpty(final boolean aCountEmpty) {
        mCountEmpty = aCountEmpty;
    }
}


このメソッドがポイントオーバー。かつ、コピペプログラムなのはどういうことでしょう。心よりはじる。

#23日追記
#if文の条件によってポイント加算ロジック追加(コピペ)