在Eclipse插件中创建标记和问题

时间:2015-05-01 05:29:17

标签: java eclipse compiler-construction compilation eclipse-plugin

我一直在进行大量的研究,但我很难理解IProblem和IMarker之间的关系。我已经能够创建问题并添加标记,但这两个似乎没有连接。问题和标记是否独立?起初我以为如果我向ReconcileContext添加问题,标记会自动出现但看起来并非如此。有谁知道我做错了什么?

SecureCompilationParticipant.java:

@IBAction func unwindToViewControllerA(segue:UIStoryboardSegue){
    //do your stuff here.
}

SecureCodingProblem.java:

    public class SecureCompilationParticipant extends CompilationParticipant {

    public boolean isActive(IJavaProject project) {
        return true;
    }

    @Override
    public void reconcile(ReconcileContext context) {
        // Call Parent
        super.reconcile(context);

        // Used in expression loop
        int start, end, line;
        String fileName;

        // Check to see if content has changed
        IJavaElementDelta elementDelta = context.getDelta();
        IJavaElement element = elementDelta.getElement();
        if((elementDelta.getFlags() & IJavaElementDelta.F_CONTENT) != 0) {
            System.out.printf("The content of %s has changed%n",
                    element.getElementName());
            try {
                CompilationUnit compilation = context.getAST8();
                NodeVisitor visitor = new NodeVisitor();
                compilation.accept(visitor);

                // Iterate through expressions
                for (ExpressionStatement expressionStatement : visitor.getExpressionStatements()) {
                    start = expressionStatement.getStartPosition();
                    end = start + expressionStatement.getLength();
                    line = compilation.getLineNumber(start - 1);
                    fileName = element.getElementName();
                    System.out.printf("Expression: %s%n", expressionStatement.getExpression().toString());

                    CategorizedProblem[] problems = new CategorizedProblem[0];
                    ArrayList<CategorizedProblem> problemList = new ArrayList<CategorizedProblem>();

                    // Put problems
                    SecureCodingProblem problem = new SecureCodingProblem(fileName);
                    problem.setSourceStart(start);
                    problem.setSourceEnd(end);
                    problem.setSourceLineNumber(line);
                    problemList.add(problem);
                    context.putProblems(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER, problemList.toArray(problems));

                    // Create marker
                    IResource resource = element.getUnderlyingResource();
                    IMarker marker = resource.createMarker(IMarker.PROBLEM);
                    marker.setAttribute(IMarker.MESSAGE, "This is a test marker");
                    marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_WARNING);
                    marker.setAttribute(IMarker.LINE_NUMBER, line);
                    marker.setAttribute(IMarker.LOCATION, String.format("Line %d", line));
                }
            } catch (JavaModelException e) {
                // From CompilationUnit compilation = context.getAST8();
                e.printStackTrace();
            } catch (CoreException e) {
                // From IMarker marker = resource.createMarker(IMarker.PROBLEM);
                e.printStackTrace();
            }
        }

    }
}

1 个答案:

答案 0 :(得分:0)

IProblem特定于Java开发工具,而IMarker可用于工作区中的任何文件。

IProblem的JavaDoc说:

  

注意:编译器在内部生成IProblems,它们被转换   由JavaBuilder创建标记以便持久化问题描述。   这解释了为什么没有API允许检测到IProblem   编译时但是,Java问题标记具有相同的功能   信息到IProblem,特别是它们的ID(属性“id”)是   设置为此接口上定义的ID之一。