Search in sources :

Example 1 with PureUnresolvedIdentifierException

use of org.finos.legend.pure.m3.exception.PureUnresolvedIdentifierException in project legend-pure by finos.

the class ImportStub method resolvePackageableElement.

private static CoreInstance resolvePackageableElement(String idOrPath, org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel._import.ImportStub importStubNode, ModelRepository repository, ProcessorSupport processorSupport) {
    // Try the Special Types (not user-defined top level types!!!)
    if (_Package.SPECIAL_TYPES.contains(idOrPath)) {
        return repository.getTopLevel(idOrPath);
    }
    ImportGroup importGroup = importStubNode._importGroup();
    // Check if a package is specified
    int lastIndex = idOrPath.lastIndexOf(':');
    if (lastIndex != -1) {
        CoreInstance node = processorSupport.package_getByUserPath(idOrPath);
        if (node == null) {
            String id = idOrPath.substring(lastIndex + 1);
            throw new PureUnresolvedIdentifierException(importStubNode.getSourceInformation(), idOrPath, id, repository, processorSupport, importStubNode, importGroup);
        }
        return node;
    }
    // Look in the imported packages
    MutableSet<CoreInstance> results = Sets.mutable.with();
    Imports.getImportGroupPackages(importGroup, processorSupport).forEach(pkg -> {
        CoreInstance found = _Package.findInPackage(pkg, idOrPath);
        if (found != null) {
            results.add(found);
        }
    });
    switch(results.size()) {
        case 0:
            {
                // Try user defined top elements (important to do that last ... as doing it earlier could create conflicts...)
                CoreInstance node = processorSupport.package_getByUserPath(idOrPath);
                if (node == null) {
                    throw new PureUnresolvedIdentifierException(importStubNode.getSourceInformation(), idOrPath, idOrPath, repository, processorSupport, importStubNode, importGroup);
                }
                return node;
            }
        case 1:
            {
                return results.getAny();
            }
        default:
            {
                throw new PureCompilationException(importStubNode.getSourceInformation(), results.collect(PackageableElement::getUserPathForPackageableElement, Lists.mutable.ofInitialCapacity(results.size())).sortThis().makeString(idOrPath + " has been found more than one time in the imports: [", ", ", "]"));
            }
    }
}
Also used : PackageableElement(org.finos.legend.pure.m3.navigation.PackageableElement.PackageableElement) PureUnresolvedIdentifierException(org.finos.legend.pure.m3.exception.PureUnresolvedIdentifierException) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) ImportGroup(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel._import.ImportGroup) PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException)

Example 2 with PureUnresolvedIdentifierException

use of org.finos.legend.pure.m3.exception.PureUnresolvedIdentifierException in project legend-pure by finos.

the class ExceptionTranslation method pureExceptionToJson.

public static IDEResponse pureExceptionToJson(PureSession session, PureException e, ByteArrayOutputStream pureResponse) {
    PureException original = e.getOriginatingPureException();
    PureRuntime runtime = (null == session) ? null : session.getPureRuntime();
    IDEExceptionResponse response;
    if (e instanceof PureUnresolvedIdentifierException) {
        final PureUnresolvedIdentifierException exception = (PureUnresolvedIdentifierException) e;
        if (exception.getImportCandidates(Objects.requireNonNull(session).codeStorage.getAllRepositories()).isEmpty()) {
            response = new IDEExceptionResponse();
        } else {
            RichIterable<CoreInstance> candidates = exception.getImportCandidates(session.codeStorage.getAllRepositories());
            MutableMap<String, Pair<SourceInformation, String>> sourceInfoAndTypeByPath = UnifiedMap.newMap(candidates.size());
            for (CoreInstance candidate : candidates) {
                SourceInformation sourceInfo = candidate.getSourceInformation();
                // TODO think about what we should do with candidates with no source info
                if ((null != sourceInfo) && (null != runtime)) {
                    sourceInfoAndTypeByPath.put(PackageableElement.getUserPathForPackageableElement(candidate), Tuples.pair(sourceInfo, candidate.getClassifier().getName()));
                }
            }
            SourceInformation sourceToBeModified = exception.getImportGroup().getSourceInformation();
            if (null == sourceToBeModified) {
                // avoid null pointer exception
                sourceToBeModified = DUMMY_SOURCE_INFORMATION;
            }
            final MutableSet<String> pathsInSameSource = Sets.mutable.empty();
            final MutableSet<String> pathsNotInSameSource = Sets.mutable.empty();
            sourceInfoAndTypeByPath.forEachKeyValue(new Procedure2<String, Pair<SourceInformation, String>>() {

                @Override
                public void value(String s, Pair<SourceInformation, String> sourceInformationStringPair) {
                    if (sourceInformationStringPair.getOne().getSourceId().equals(exception.getImportGroup().getSourceInformation().getSourceId())) {
                        pathsInSameSource.add(s);
                    } else {
                        pathsNotInSameSource.add(s);
                    }
                }
            });
            IDEPureUnresolvedIdentifierExceptionResponse unresolvedResponse = new IDEPureUnresolvedIdentifierExceptionResponse();
            response = unresolvedResponse;
            unresolvedResponse.candidateName = exception.getIdOrPath();
            MutableList<Candidate> candidatesOutput = Lists.mutable.of();
            unresolvedResponse.candidates = candidatesOutput;
            for (String path : pathsInSameSource.toSortedList().withAll(pathsNotInSameSource.toSortedList())) {
                Pair<SourceInformation, String> pair = sourceInfoAndTypeByPath.get(path);
                SourceInformation sourceInfo = pair.getOne();
                String type = pair.getTwo();
                String id = exception.getIdOrPath();
                int index = id.lastIndexOf("::");
                if (-1 != index) {
                    // id contains "::"
                    id = id.substring(index + 2);
                }
                index = path.lastIndexOf(id);
                if (-1 == index) {
                    throw new RuntimeException("Unable to find identifier: " + exception.getIdOrPath());
                }
                String importToAdd = "import " + path.substring(0, index) + "*;";
                int lineToBeModified = sourceToBeModified.getStartLine();
                int columnToBeModified = sourceToBeModified.getStartColumn();
                if (0 == columnToBeModified) {
                    // special handling for importGroup without any imports, need to add import to the next line
                    lineToBeModified++;
                }
                Candidate candidate = new Candidate();
                candidate.sourceID = sourceInfo.getSourceId();
                candidate.line = sourceInfo.getLine();
                candidate.column = sourceInfo.getColumn();
                candidate.foundName = path;
                candidate.fileToBeModified = sourceToBeModified.getSourceId();
                candidate.lineToBeModified = lineToBeModified;
                candidate.columnToBeModified = columnToBeModified;
                candidate.add = true;
                candidate.messageToBeModified = importToAdd;
                candidate.type = type;
                candidatesOutput.add(candidate);
            }
        }
    } else if (e instanceof PureUnmatchedFunctionException) {
        final PureUnmatchedFunctionException exception = (PureUnmatchedFunctionException) e;
        if (0 == exception.getImportCandidatesWithPackageNotImported().size() + exception.getImportCandidatesWithPackageImported().size()) {
            response = new IDEExceptionResponse();
        } else {
            SourceInformation sourceToBeModified = exception.getImportGroup().getSourceInformation();
            if (null == sourceToBeModified) {
                // avoid null pointer exception
                sourceToBeModified = DUMMY_SOURCE_INFORMATION;
            }
            IDEPureUnmatchedFunctionExceptionResponse ideUnmatchedResponse = new IDEPureUnmatchedFunctionExceptionResponse();
            response = ideUnmatchedResponse;
            ideUnmatchedResponse.candidateName = exception.getFunctionName();
            MutableList<Candidate> candidatesOutputWithPackageNotImported = Lists.mutable.of();
            ideUnmatchedResponse.candidates = candidatesOutputWithPackageNotImported;
            MutableList<Candidate> candidatesOutputWithPackageImported = Lists.mutable.of();
            ideUnmatchedResponse.candidatesWithPackageImported = candidatesOutputWithPackageImported;
            getCandidatesOutput(session, runtime, exception, sourceToBeModified, candidatesOutputWithPackageNotImported, exception.getImportCandidatesWithPackageNotImported());
            getCandidatesOutput(session, runtime, exception, sourceToBeModified, candidatesOutputWithPackageImported, exception.getImportCandidatesWithPackageImported());
        }
    } else if ((e instanceof PureParserException || e instanceof PureCompilationException)) {
        IDEParserOrCompilerException parserOrCompilerException = new IDEParserOrCompilerException();
        parserOrCompilerException.exceptionType = e.getExceptionName();
        response = parserOrCompilerException;
    } else {
        response = new IDEExceptionResponse();
    }
    if (null != pureResponse) {
        String pureResponseStr = new String(pureResponse.toByteArray()) + '\n';
        response.appendText(pureResponseStr);
    }
    if (e.hasPureStackTrace()) {
        response.appendText(original.getMessage() + "\n" + e.getPureStackTrace("    "));
    } else {
        response.appendText(original.getMessage());
    }
    SourceInformation sourceInformation = original.getSourceInformation();
    if ((null != sourceInformation) && (null != runtime) && (null != runtime.getSourceById(sourceInformation.getSourceId()))) {
        response.RO = runtime.isSourceImmutable(sourceInformation.getSourceId());
        response.source = sourceInformation.getSourceId();
        response.line = sourceInformation.getLine();
        response.column = sourceInformation.getColumn();
    }
    if (null != session) {
    // response.compiler = session.getCompilerLogs();
    }
    return response;
}
Also used : PureUnresolvedIdentifierException(org.finos.legend.pure.m3.exception.PureUnresolvedIdentifierException) MutableList(org.eclipse.collections.api.list.MutableList) Pair(org.eclipse.collections.api.tuple.Pair) PureUnmatchedFunctionException(org.finos.legend.pure.m3.exception.PureUnmatchedFunctionException) PureCompilationException(org.finos.legend.pure.m4.exception.PureCompilationException) PureException(org.finos.legend.pure.m4.exception.PureException) PureRuntime(org.finos.legend.pure.m3.serialization.runtime.PureRuntime) SourceInformation(org.finos.legend.pure.m4.coreinstance.SourceInformation) PureParserException(org.finos.legend.pure.m4.serialization.grammar.antlr.PureParserException) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance)

Aggregations

PureUnresolvedIdentifierException (org.finos.legend.pure.m3.exception.PureUnresolvedIdentifierException)2 CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)2 PureCompilationException (org.finos.legend.pure.m4.exception.PureCompilationException)2 MutableList (org.eclipse.collections.api.list.MutableList)1 Pair (org.eclipse.collections.api.tuple.Pair)1 ImportGroup (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel._import.ImportGroup)1 PureUnmatchedFunctionException (org.finos.legend.pure.m3.exception.PureUnmatchedFunctionException)1 PackageableElement (org.finos.legend.pure.m3.navigation.PackageableElement.PackageableElement)1 PureRuntime (org.finos.legend.pure.m3.serialization.runtime.PureRuntime)1 SourceInformation (org.finos.legend.pure.m4.coreinstance.SourceInformation)1 PureException (org.finos.legend.pure.m4.exception.PureException)1 PureParserException (org.finos.legend.pure.m4.serialization.grammar.antlr.PureParserException)1