Generated files: Implementation code

The New Plug-in Project wizard generates a Java™ package and class for implementing EGL user-written rules.

You must add your own Java code to this class to do the analysis that is required by your custom rule.

Implementation code for EGL

Figure 1 shows an example of the generated code for a EGL rule class. The example assumes that the following element was selected on the EGL Rule Template page:
  • AddStatement
The name of the rule class, com.example.EglRule, is taken from the example value column of Table 1 in the topic Generated files: Rule and category extensions.
Notice the following elements in the generated code:
  • The package name is set to the package-name portion of the rule class, com.example.
  • The class name is set to the class-name portion of the rule class, EglRule.
  • A visit() method is generated for both an AST visit and an IR visit, for each of the elements that are selected on the EGL Rule Template wizard page.
Figure 1. Class for implementing a EGL rule
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;
	}
}

Feedback