生成されたファイル: 実装コード

新規プラグイン・プロジェクト」ウィザードを使用して、EGL のユーザー作成規則を実装するための Java™ パッケージとクラスを生成します。

このクラスに、カスタム規則で必要とされる分析を実行する独自の Java コードを追加する必要があります。

EGL のコード実装

図 1 は、 EGL 規則クラス用に生成されるコードの例を示しています。この例は、「EGL 規則テンプレート」ページで次のエレメントが選択されていることが前提になっています。
  • AddStatement
規則クラスの名前 com.example.EglRule は、 トピック 生成されたファイル: 規則およびカテゴリーの拡張表 1 のサンプル値の列から取られています。
生成されたコードの 以下のエレメントに注目してください。
  • パッケージ名は、規則クラス com.example のパッケージ名部分に設定されます。
  • クラス名は、規則クラス EglRule のクラス名部分に設定されます。
  • 「EGL 規則テンプレート」ウィザード・ページで選択されたエレメントごとに、AST アクセスおよび IR アクセスの 両方について visit() メソッドが生成されます。
図 1. EGL 規則を実装するためのクラス
package com.example;

import java.util.ArrayList;
import java.util.List;

public class EglRule extends com.ibm.etools.egl.codereview.rules.AbstractEglAnalysisRule {

	@Override
	public List<com.ibm.etools.edt.core.ast.Node> performRule(com.ibm.etools.edt.core.ast.Node baseNode) {
		final List<com.ibm.etools.edt.core.ast.Node> tokens = new ArrayList<com.ibm.etools.edt.core.ast.Node>();
		// This method will always be invoked and passed the lightweight AST model instead of the bound IR model.
		// Use the AST model when less information is needed than what is provided by the IR model.
		// If the IR model is wanted, simply return either null or an empty tokens list for this return and
		// place the code to analyze the IR model in the IR version of the performRule (see below).
		// If you wish to add additional text to the flagged Node's displayed UI message, then you may use a
		// List<com.ibm.etools.egl.codereview.NodeStringResult> for token instead. The NodeStringResult class allows
		// you to set the Node and set the String that is appended to the flagged location in the displayed message.
		baseNode.accept(new com.ibm.etools.egl.codereview.rules.AbstractASTRuleVisitor() {
			@Override
			public boolean visit(com.ibm.etools.edt.core.ast.AddStatement clause) {
				// TODO examine node for rule violations and add violating nodes to tokens list, like this:
				tokens.add(clause);
				// Return true if you wish to process visits for objects within this one, otherwise return false
				return false;
			}
		});
		return tokens;
	}

	@Override
	public List<com.ibm.etools.edt.core.ir.api.Element> performRule(com.ibm.etools.edt.core.ir.api.Element element) {
		final List<com.ibm.etools.edt.core.ir.api.Element> tokens = new ArrayList<com.ibm.etools.edt.core.ir.api.Element>();
		// In order for this method to be invoked, the analysisParameter "RUN_AS_IR" in the manifest must be set to "true".
		// Once set, this class will be called passing the bound IR model in addition to the lightweight AST model.
		// Use the IR model when more information is needed than what is provided by the AST model, but be aware
		// that IR model usage is not as fast as the AST model.
		// If you wish to add additional text to the flagged Element's displayed UI message, then you may use a
		// List<com.ibm.etools.egl.codereview.ElementStringResult> for token instead. The ElementStringResult class allows
		// you to set the Element and set the String that is appended to the flagged location in the displayed message.
		element.accept(new com.ibm.etools.egl.codereview.rules.AbstractIRRuleVisitor() {
			@Override
			public boolean visit(com.ibm.etools.edt.core.ir.api.AddStatement clause) {
				// TODO examine node for rule violations and add violating nodes to tokens list, like this:
				tokens.add(clause);
				// Return true if you wish to process visits for objects within this one, otherwise return false
				return false;
			}
		});
		return tokens;
	}
}

フィードバック
(C) Copyright IBM Corporation 2000, 2015. All Rights Reserved.
(C) Copyright IBM Japan 2005.