Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent random exceptions when Emfatic is unable to provide a textual model view for EMF #1175

Merged
merged 1 commit into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/src/main/java/de/jplag/Submission.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ private static File createErrorDirectory(String... subdirectoryNames) {
try {
tokenList = language.parse(new HashSet<>(files));
} catch (ParsingException e) {
logger.warn("Failed to parse submission {} with error {}", this, e);
logger.warn("Failed to parse submission {} with error {}", this, e.getMessage(), e);
tokenList = null;
hasErrors = true;
if (debugParser) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ protected String getCorrespondingViewFileSuffix() {
* @param file is the path for the view file to be created.
* @param modelResource is the resource containing the metamodel.
* @return the view implementation.
* @throws ParsingException if view could not be created due to an invalid model.
*/
protected AbstractModelView createView(File file, Resource modelResource) {
protected AbstractModelView createView(File file, Resource modelResource) throws ParsingException {
return new EmfaticModelView(file, modelResource);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.eclipse.emf.ecore.util.EcoreUtil.Copier;
import org.eclipse.emf.emfatic.core.generator.emfatic.Writer;

import de.jplag.ParsingException;
import de.jplag.Token;
import de.jplag.TokenTrace;
import de.jplag.emf.MetamodelToken;
Expand Down Expand Up @@ -44,8 +45,9 @@ public final class EmfaticModelView extends AbstractModelView {
* Creates an Emfatic view for a metamodel.
* @param file is the path for the view file to be created.
* @param modelResource is the resource containing the metamodel.
* @throws ParsingException if Emfatic crashes.
*/
public EmfaticModelView(File file, Resource modelResource) {
public EmfaticModelView(File file, Resource modelResource) throws ParsingException {
super(file);
elementToLine = new HashMap<>();
lines = generateEmfaticCode(viewBuilder, modelResource);
Expand Down Expand Up @@ -93,11 +95,16 @@ protected void visitENamedElement(ENamedElement eNamedElement) {

/**
* Generates Emfatic code from a model resource and splits it into lines with a string builder.
* @throws ParsingException if the Emfatic writer fails.
*/
private final List<String> generateEmfaticCode(StringBuilder builder, Resource modelResource) {
private final List<String> generateEmfaticCode(StringBuilder builder, Resource modelResource) throws ParsingException {
Writer writer = new Writer();
String code = writer.write(modelResource, null, null);
builder.append(code);
try {
String code = writer.write(modelResource, null, null);
builder.append(code);
} catch (Exception exception) { // Emfatic does not properly handle errors, thus throws random exceptions.
throw new ParsingException(file, "Emfatic view could not be generated!", exception);
}
return builder.toString().lines().toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import de.jplag.ParsingException;
import de.jplag.emf.AbstractEmfTest;
import de.jplag.emf.Language;

Expand All @@ -23,7 +24,7 @@ private static List<String> provideModelNames() {
@ParameterizedTest
@DisplayName("Test content of emfatic view files of example metamodels")
@MethodSource("provideModelNames")
void testEmfaticViewFiles(String modelName) {
void testEmfaticViewFiles(String modelName) throws ParsingException {
// Load model:
File modelFile = new File(baseDirectory, modelName);
Resource modelResource = loadAndVerifyModel(modelFile);
Expand Down