Example usage for org.eclipse.jdt.core.compiler ReconcileContext getAST4

List of usage examples for org.eclipse.jdt.core.compiler ReconcileContext getAST4

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.compiler ReconcileContext getAST4.

Prototype

public org.eclipse.jdt.core.dom.CompilationUnit getAST4() throws JavaModelException 

Source Link

Document

Returns a resolved AST with AST#JLS4 JLS4 level.

Usage

From source file:com.google.gwt.eclipse.core.validators.java.JavaCompilationParticipant.java

License:Open Source License

@Override
public void reconcile(ReconcileContext context) {
    ICompilationUnit cu = context.getWorkingCopy();

    try {//from  w  ww  .  ja  v a  2 s .c om
        CompilationUnit ast = null;

        try {
            ast = context.getAST4();
        } catch (JavaModelException e) {
            // Fall through to null check below
        }
        if (ast == null) {
            GWTPluginLog.logError("Could not get AST for " + cu.getPath());
            return;
        }

        // TODO: Merge this code with that of buildStarting

        CategorizedProblem[] currentProblems = context.getProblems(GWTJavaProblem.MARKER_ID);
        // GWT-validation and error reporting
        JavaValidationResult result = validateCompilationUnit(ast);
        List<GWTJavaProblem> gwtCoreProblems = result.getProblems();
        ArrayList<CategorizedProblem> finalProblemSet = new ArrayList<CategorizedProblem>();
        if (currentProblems != null) {
            finalProblemSet.addAll(Arrays.asList(currentProblems));
        }
        finalProblemSet.addAll(gwtCoreProblems);
        context.putProblems(GWTJavaProblem.MARKER_ID,
                (finalProblemSet.size() > 0 ? finalProblemSet.toArray(EMPTY_PROBLEMS) : null));

        // GWT RPC validation and error reporting
        RemoteServiceValidator rsv = new RemoteServiceValidator();
        ValidationResult validationResult = rsv.validate(ast);
        List<CategorizedProblem> rpcProblems = validationResult.getProblems();
        context.putProblems(RemoteServiceProblem.MARKER_ID,
                (rpcProblems.size() > 0 ? rpcProblems.toArray(EMPTY_PROBLEMS) : null));

        // ClientBundle validation
        ClientBundleValidator cbv = new ClientBundleValidator();
        ValidationResult cbvResult = cbv.validate(ast);
        List<CategorizedProblem> cbProblems = cbvResult.getProblems();
        context.putProblems(ClientBundleProblem.MARKER_ID,
                (cbProblems.size() > 0 ? cbProblems.toArray(EMPTY_PROBLEMS) : null));

        if (UiBinderConstants.UI_BINDER_ENABLED) {
            /*
             * Set up the UiBinder validator. Note that we're passing in copies of
             * the subtype-to-owner and subtype-to-xml indices instead of using the
             * "real" indices (i.e. the one that we update during builds). This
             * ensures that any updates the validator makes during a reconcile are
             * only used by that reconcile pass, and are not persisted.
             */
            UiBinderJavaValidator uiv = new UiBinderJavaValidator(ast,
                    new UiBinderSubtypeToOwnerIndex(UiBinderReferenceManager.INSTANCE.getSubtypeToOwnerIndex()),
                    new UiBinderSubtypeToUiXmlIndex(UiBinderReferenceManager.INSTANCE.getSubtypeToUiXmlIndex()),
                    UiBinderReferenceManager.INSTANCE.getUiXmlReferencedFieldIndex(), null);
            ValidationResult uivResult = uiv.validate();
            List<CategorizedProblem> uivProblems = uivResult.getProblems();
            context.putProblems(UiBinderJavaProblem.MARKER_ID,
                    (uivProblems.size() > 0 ? uivProblems.toArray(EMPTY_PROBLEMS) : null));
        }
    } catch (OperationCanceledException e) {
        // Thrown by Eclipse to abort long-running processes
        throw e;
    } catch (Exception e) {
        // Don't want to allow any unexpected exceptions to escape
        GWTPluginLog.logError(e, "Unexpected error while validating {0}", cu.getElementName());
    }
}

From source file:net.atos.jdt.ast.validation.engine.internal.participant.ASTValidationParticipant.java

License:Open Source License

@Override
public void reconcile(final ReconcileContext context) {

    // We check if we can execute...
    if (!ASTRulesPreferences.isValidationParticipantEnabled()) {
        return;/*  w w  w.ja  v  a  2 s . com*/
    }

    // At first, let's do some validation.
    CompilationUnit domCU = null;
    try {
        domCU = context.getAST4();
    } catch (final JavaModelException e) {
        Activator.logException(e);
        return;
    }
    if (domCU == null) {
        return;
    }

    final IJavaElement javaElement = domCU.getJavaElement();
    if (!(javaElement instanceof ICompilationUnit)) {
        return;
    }

    // Now we perform the process
    ICompilationUnit iCompilationUnit = (ICompilationUnit) javaElement;

    if (iCompilationUnit.getResource() != null && iCompilationUnit.getResource().exists()) {
        for (final ASTRulesRepository repository : this.dataSource.getRepositories()) {
            try {
                iCompilationUnit.getResource().deleteMarkers(repository.getMarkerId(), true,
                        IResource.DEPTH_ZERO);
            } catch (final CoreException e) {
                Activator.logException(e);
            }
            if (repository.isEnabled(iCompilationUnit)) {
                for (final ASTRuleDescriptor ruleDescriptor : repository.getRules(iCompilationUnit)) {
                    final AbstractASTRule rule = ruleDescriptor.getRule();
                    domCU.accept(rule);
                    for (final ASTValidationProblem problem : rule.getProblems()) {
                        problem.toMarker(iCompilationUnit.getResource());
                    }
                }
            }
        }
    }
}