생성된 파일: 구현 코드
새 플러그인 프로젝트 마법사는 EGL 사용자 작성 규칙을 구현하는 Java™ 패키지 및 클래스를 생성합니다.
사용자 정의 규칙에 필요한 분석을 수행하려면 사용자 고유의 Java 코드를 이 클래스에 추가해야 합니다.
EGL의 구현 코드
그림 1에서는
EGL 규칙 클래스에 대해 생성된 코드의 예제를 보여줍니다. 이 예제는
EGL 규칙 템플리트 페이지에서 다음 요소가
선택되었다고 가정합니다.
- AddStatement
생성된 코드에서 다음 요소를
확인하십시오.
- 패키지 이름이 규칙 클래스의 패키지-이름 부분으로 설정되었습니다(com.example).
- 클래스 이름은 규칙 클래스 EglRule의 클래스 이름 부분으로 설정됩니다.
- visit() 메소드는 AST 방문 및 IR 방문 둘 다에 대해 생성되고 EGL 규칙 템플리트 마법사 페이지에서 선택되는 각 요소에 대해 생성됩니다.
그림 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;
}
}
피드백