Search in sources :

Example 21 with Diagnostic

use of org.eclipse.emf.ecore.resource.Resource.Diagnostic in project n4js by eclipse.

the class ErrorAwareLinkingService method addError.

/**
 * Add the error to the resource of the given {@code context} if it does support validation.
 *
 * @param context
 *            the context object that caused the error.
 * @param node
 *            the error location.
 * @param error
 *            the actual error description.
 */
protected void addError(EObject context, INode node, IEObjectDescriptionWithError error) {
    N4JSResource resource = (N4JSResource) context.eResource();
    if (resource.isValidationDisabled())
        return;
    List<Diagnostic> list = resource.getErrors();
    // Convert key value user data to String array
    String[] userData = null;
    if (error.getUserDataKeys() != null) {
        ArrayList<String> userDataList = new ArrayList<>(error.getUserDataKeys().length * 2);
        for (String userDataKey : error.getUserDataKeys()) {
            final String userDataValue = error.getUserData(userDataKey);
            if (userDataValue != null) {
                userDataList.add(userDataKey);
                userDataList.add(userDataValue);
            }
        }
        userData = userDataList.toArray(new String[userDataList.size()]);
    }
    Diagnostic diagnostic = new XtextLinkingDiagnostic(node, error.getMessage(), error.getIssueCode(), userData);
    if (!list.contains(diagnostic))
        list.add(diagnostic);
}
Also used : XtextLinkingDiagnostic(org.eclipse.xtext.linking.impl.XtextLinkingDiagnostic) ArrayList(java.util.ArrayList) XtextLinkingDiagnostic(org.eclipse.xtext.linking.impl.XtextLinkingDiagnostic) Diagnostic(org.eclipse.emf.ecore.resource.Resource.Diagnostic)

Example 22 with Diagnostic

use of org.eclipse.emf.ecore.resource.Resource.Diagnostic in project joynr by bmwcarit.

the class Executor method generate.

public void generate() {
    ModelLoader modelLoader = prepareGeneratorEnvironment();
    for (Resource resource : modelLoader.getResources()) {
        if (resource.getErrors().size() > 0) {
            StringBuilder errorMsg = new StringBuilder();
            errorMsg.append("Error loading model " + resource.getURI().toString() + ". The following errors occured: \n");
            for (Diagnostic error : resource.getErrors()) {
                errorMsg.append(error.getMessage());
            }
            logger.log(Level.SEVERE, errorMsg.toString());
            System.exit(-1);
        } else {
            generator.doGenerate(resource, outputFileSystem);
        }
    }
}
Also used : ModelLoader(io.joynr.generator.loading.ModelLoader) Resource(org.eclipse.emf.ecore.resource.Resource) Diagnostic(org.eclipse.emf.ecore.resource.Resource.Diagnostic)

Example 23 with Diagnostic

use of org.eclipse.emf.ecore.resource.Resource.Diagnostic in project smarthome by eclipse.

the class ScriptParsingException method addDiagnosticErrors.

public ScriptParsingException addDiagnosticErrors(List<Diagnostic> errors) {
    for (Diagnostic emfDiagnosticError : errors) {
        if (emfDiagnosticError instanceof AbstractDiagnostic) {
            AbstractDiagnostic e = (AbstractDiagnostic) emfDiagnosticError;
            this.getErrors().add(new ScriptError(e.getMessage(), e.getLine(), e.getOffset(), e.getLength()));
        } else if (emfDiagnosticError instanceof ExceptionDiagnostic) {
            ExceptionDiagnostic e = (ExceptionDiagnostic) emfDiagnosticError;
            this.getErrors().add(new ScriptError(e.getMessage(), e.getLine(), e.getOffset(), e.getLength()));
        } else {
            this.getErrors().add(new ScriptError(emfDiagnosticError.getMessage(), -1, -1, -1));
        }
    }
    return this;
}
Also used : ExceptionDiagnostic(org.eclipse.xtext.diagnostics.ExceptionDiagnostic) Diagnostic(org.eclipse.emf.ecore.resource.Resource.Diagnostic) AbstractDiagnostic(org.eclipse.xtext.diagnostics.AbstractDiagnostic) ExceptionDiagnostic(org.eclipse.xtext.diagnostics.ExceptionDiagnostic) AbstractDiagnostic(org.eclipse.xtext.diagnostics.AbstractDiagnostic)

Example 24 with Diagnostic

use of org.eclipse.emf.ecore.resource.Resource.Diagnostic in project n4js by eclipse.

the class PositiveAnalyser method analyse.

/*
	 * (non-Javadoc)
	 *
	 * @see org.eclipse.n4js.tests.libraryparsing.analysis.DiagnosticAnalyser #analyse(java.util.List, java.lang.String,
	 * java.lang.String)
	 */
@Override
public void analyse(Script script, String codeName, String code) {
    List<Diagnostic> errors = null;
    try {
        errors = getScriptErrors(script);
    // if (serializerTester != null) {
    // serializerTester.assertSerializeWithNodeModel(script);
    // }
    } catch (Throwable t) {
        StringWriter writer = new StringWriter();
        t.printStackTrace(new PrintWriter(writer));
        assertEquals(code, writer.toString());
        return;
    }
    if (!errors.isEmpty()) {
        String msg = "expected no errors in " + codeName + " but I got : " + errors.size();
        if (this.logger.isDebugEnabled()) {
            StringBuilder sb = new StringBuilder(msg).append("\n");
            sb.append("========== " + codeName + " ==========\n");
            if (this.logCode) {
                sb.append(code).append("\n");
            }
            sb.append(">>>> errors: ").append("\n");
            sb.append(this.aggregateDiagnosticsToStringBuilder(codeName, errors));
            sb.append(">>>> warnings: ").append("\n");
            sb.append("<<<<").append("\n");
            sb.append("========================================").append("\n");
            this.logger.debug(sb);
        }
        assertEquals(msg, withLineNumbers(code), this.aggregateDiagnosticsToStringBuilder(codeName, errors).toString());
    }
}
Also used : StringWriter(java.io.StringWriter) Diagnostic(org.eclipse.emf.ecore.resource.Resource.Diagnostic) PrintWriter(java.io.PrintWriter)

Example 25 with Diagnostic

use of org.eclipse.emf.ecore.resource.Resource.Diagnostic in project coffeescript-eclipse by adamschmideg.

the class ParserTestBase method expect.

/**
 * Parse and check for errors and warnings
 * @param errorCount 1 means expect error(s)
 * @param warningCount -1 means don't care about warnings
 */
protected void expect(CharSequence input, int errorCount, int warningCount) {
    List<String> tokens = null;
    // Assert::assertEquals('warnings ' + resource.warnings, 0, resource.warnings.size)
    // Assert::assertEquals('errors ' + resource.errors, 0, resource.errors.size)
    EObject parseResult = null;
    EList<Diagnostic> errors = null;
    EList<Diagnostic> warnings = null;
    try {
        Lexer lexer = new Lexer(input);
        tokens = lexer.tokenizeToStrings();
        InputStream in = getAsStream("" + input);
        URI uri = URI.createURI("mytestmodel." + getCurrentFileExtension());
        XtextResource resource = doGetResource(in, uri);
        parseResult = getModel(resource);
        errors = resource.getErrors();
        warnings = resource.getWarnings();
        if (errorCount == 0)
            assertEquals("Errors: " + errors, 0, errors.size());
        else if (errorCount > 0)
            assertTrue("No errors", errors.size() > 0);
        if (warningCount >= 0) {
            assertEquals("Warnings: " + warnings, warningCount, warnings.size());
        }
    } catch (AssertionFailedError afe) {
        if (logger.isDebugEnabled()) {
            String debug = "" + this.getClass().getSimpleName() + " input: \n" + input + "\n";
            debug += "Tokens: " + tokens + "\n" + Helper.stringify(parseResult);
            for (Diagnostic d : errors) {
                debug += "\tError: " + d + "\n";
            }
            for (Diagnostic d : warnings) {
                debug += "\tWarning: " + d + "\n";
            }
            logger.debug(debug);
        }
        throw new AssertionError(afe);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : InputStream(java.io.InputStream) Diagnostic(org.eclipse.emf.ecore.resource.Resource.Diagnostic) XtextResource(org.eclipse.xtext.resource.XtextResource) URI(org.eclipse.emf.common.util.URI) Lexer(csep.parser.Lexer) EObject(org.eclipse.emf.ecore.EObject) AssertionFailedError(junit.framework.AssertionFailedError)

Aggregations

Diagnostic (org.eclipse.emf.ecore.resource.Resource.Diagnostic)32 Test (org.junit.Test)12 ExceptionDiagnostic (org.eclipse.xtext.diagnostics.ExceptionDiagnostic)10 Resource (org.eclipse.emf.ecore.resource.Resource)9 XtextResource (org.eclipse.xtext.resource.XtextResource)8 XtextLinkingDiagnostic (org.eclipse.xtext.linking.impl.XtextLinkingDiagnostic)7 BasicDiagnostic (org.eclipse.emf.common.util.BasicDiagnostic)6 Issue (org.eclipse.xtext.validation.Issue)6 List (java.util.List)5 EObject (org.eclipse.emf.ecore.EObject)5 Severity (org.eclipse.xtext.diagnostics.Severity)5 IDiagnosticConverter (org.eclipse.xtext.validation.IDiagnosticConverter)5 ResourceValidatorImpl (org.eclipse.xtext.validation.ResourceValidatorImpl)5 IOException (java.io.IOException)4 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)3 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)3 XBlockExpression (org.eclipse.xtext.xbase.XBlockExpression)3 ComparisonFailure (org.junit.ComparisonFailure)3 ArrayList (java.util.ArrayList)2 URI (org.eclipse.emf.common.util.URI)2