Search in sources :

Example 1 with SourceRelativeURI

use of org.eclipse.xtext.generator.trace.SourceRelativeURI in project xtext-eclipse by eclipse.

the class DebugSourceInstallingCompilationParticipant method buildFinished.

@Override
public void buildFinished(IJavaProject project) {
    StoppedTask task = Stopwatches.forTask("DebugSourceInstallingCompilationParticipant.install");
    try {
        task.start();
        super.buildFinished(project);
        if (files == null)
            return;
        for (BuildContext ctx : files) {
            try {
                IFile generatedJavaFile = ctx.getFile();
                // This may fail if there is no trace file.
                IEclipseTrace traceToSource = traceInformation.getTraceToSource(generatedJavaFile);
                if (traceToSource == null) {
                    continue;
                }
                AbstractTraceRegion rootTraceRegion = findRootTraceRegion(traceToSource);
                if (rootTraceRegion == null)
                    continue;
                SourceRelativeURI dslSourceFile = rootTraceRegion.getAssociatedSrcRelativePath();
                // OutputConfigurations are only available for folders targeted by Xtext's code generation.
                OutputConfiguration outputConfiguration = findOutputConfiguration(dslSourceFile, generatedJavaFile);
                if (outputConfiguration == null)
                    continue;
                IJavaElement element = JavaCore.create(generatedJavaFile);
                if (element == null)
                    continue;
                deleteTaskMarkers(generatedJavaFile);
                markerReflector.reflectErrorMarkerInSource(generatedJavaFile, traceToSource);
                ITraceToBytecodeInstaller installer = getInstaller(outputConfiguration);
                installer.setTrace(generatedJavaFile.getName(), rootTraceRegion);
                for (IFile javaClassFile : findGeneratedJavaClassFiles(element)) {
                    InputStream contents = javaClassFile.getContents();
                    try {
                        byte[] byteCode = installer.installTrace(ByteStreams.toByteArray(contents));
                        if (byteCode != null) {
                            javaClassFile.setContents(new ByteArrayInputStream(byteCode), 0, null);
                        } else {
                            // we need to touch the class file to do a respin of the build
                            // otherwise a needsRebuild request is ignored since no IResourceDelta is available
                            javaClassFile.touch(null);
                        }
                    } finally {
                        contents.close();
                    }
                }
            } catch (Exception e) {
                String msg = "Could not process %s to install source information: %s";
                log.error(String.format(msg, ctx.getFile().getFullPath().toString(), e.getMessage()), e);
            }
        }
    } finally {
        files = null;
        task.stop();
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IFile(org.eclipse.core.resources.IFile) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StoppedTask(org.eclipse.xtext.util.internal.Stopwatches.StoppedTask) CoreException(org.eclipse.core.runtime.CoreException) ITraceToBytecodeInstaller(org.eclipse.xtext.generator.trace.ITraceToBytecodeInstaller) BuildContext(org.eclipse.jdt.core.compiler.BuildContext) ByteArrayInputStream(java.io.ByteArrayInputStream) OutputConfiguration(org.eclipse.xtext.generator.OutputConfiguration) AbstractTraceRegion(org.eclipse.xtext.generator.trace.AbstractTraceRegion) IEclipseTrace(org.eclipse.xtext.ui.generator.trace.IEclipseTrace) SourceRelativeURI(org.eclipse.xtext.generator.trace.SourceRelativeURI)

Example 2 with SourceRelativeURI

use of org.eclipse.xtext.generator.trace.SourceRelativeURI in project xtext-eclipse by eclipse.

the class XbaseBreakpointUtil method getBreakpointURI.

// this URI is only used for breakpoints on JARed files
public SourceRelativeURI getBreakpointURI(IEditorInput input) {
    Object adapter = input.getAdapter(IResource.class);
    if (adapter != null)
        return null;
    if (input instanceof IStorageEditorInput) {
        IStorage storage;
        try {
            storage = ((IStorageEditorInput) input).getStorage();
            if (storage instanceof IResource)
                return null;
            if (storage instanceof IJarEntryResource) {
                IJarEntryResource jarEntryResource = (IJarEntryResource) storage;
                if (!jarEntryResource.getPackageFragmentRoot().isArchive())
                    return null;
                Object parent = jarEntryResource.getParent();
                if (parent instanceof IPackageFragment) {
                    String path = ((IPackageFragment) parent).getElementName().replace('.', '/');
                    return new SourceRelativeURI(path + "/" + storage.getName());
                } else if (parent instanceof IPackageFragmentRoot) {
                    return new SourceRelativeURI(storage.getName());
                }
            }
        } catch (CoreException e) {
            logger.error("Error finding breakpoint URI", e);
            return null;
        }
    } else if (input.getAdapter(IClassFile.class) != null) {
        IClassFile classFile = (IClassFile) input.getAdapter(IClassFile.class);
        ITrace traceToSource = traceForTypeRootProvider.getTraceToSource(classFile);
        if (traceToSource == null)
            return null;
        for (ILocationInResource loc : traceToSource.getAllAssociatedLocations()) return loc.getSrcRelativeResourceURI();
        return null;
    }
    return null;
}
Also used : IStorageEditorInput(org.eclipse.ui.IStorageEditorInput) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IClassFile(org.eclipse.jdt.core.IClassFile) IStorage(org.eclipse.core.resources.IStorage) ILocationInResource(org.eclipse.xtext.generator.trace.ILocationInResource) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) CoreException(org.eclipse.core.runtime.CoreException) IJarEntryResource(org.eclipse.jdt.core.IJarEntryResource) ITrace(org.eclipse.xtext.generator.trace.ITrace) SourceRelativeURI(org.eclipse.xtext.generator.trace.SourceRelativeURI) IResource(org.eclipse.core.resources.IResource)

Example 3 with SourceRelativeURI

use of org.eclipse.xtext.generator.trace.SourceRelativeURI in project xtext-eclipse by eclipse.

the class TraceEditor method updateText.

protected void updateText(EObject obj) {
    DebugTraceRegion region = EcoreUtil2.getContainerOfType(obj, DebugTraceRegion.class);
    if (region == null) {
        text.setText("No " + DebugTraceRegion.class.getSimpleName() + " found for " + obj.eClass().getName());
        return;
    }
    final DebugTraceBasedRegion trace = new DebugTraceBasedRegion(null, region);
    SourceRelativeURI sourceURI = trace.getAssociatedSrcRelativePath();
    if (sourceURI == null) {
        text.setText("Could not find associated URI");
        return;
    }
    Registry registry = IResourceServiceProvider.Registry.INSTANCE;
    IResourceServiceProvider serviceProvider = registry.getResourceServiceProvider(sourceURI.getURI());
    URI traceURI = obj.eResource().getURI();
    IStorage localStorage = getLocalStorage(serviceProvider, traceURI);
    StorageAwareTrace traceProvider = serviceProvider.get(StorageAwareTrace.class);
    traceProvider.setLocalStorage(localStorage);
    traceProvider.setTraceToSource(true);
    traceProvider.setTraceRegionProvider(new ITraceRegionProvider() {

        @Override
        public AbstractTraceRegion getTraceRegion() throws TraceNotFoundException {
            return trace;
        }
    });
    String newText = traceProvider.toString();
    text.setText(newText);
}
Also used : StorageAwareTrace(org.eclipse.xtext.ui.generator.trace.StorageAwareTrace) Registry(org.eclipse.xtext.resource.IResourceServiceProvider.Registry) IStorage(org.eclipse.core.resources.IStorage) URI(org.eclipse.emf.common.util.URI) SourceRelativeURI(org.eclipse.xtext.generator.trace.SourceRelativeURI) IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) TraceNotFoundException(org.eclipse.xtext.generator.trace.TraceNotFoundException) AbstractTraceRegion(org.eclipse.xtext.generator.trace.AbstractTraceRegion) SourceRelativeURI(org.eclipse.xtext.generator.trace.SourceRelativeURI) ITraceRegionProvider(org.eclipse.xtext.generator.trace.ITraceRegionProvider)

Example 4 with SourceRelativeURI

use of org.eclipse.xtext.generator.trace.SourceRelativeURI in project xtext-eclipse by eclipse.

the class TraceForTypeRootProvider method getTraceToSource.

public IEclipseTrace getTraceToSource(final IClassFile classFile) {
    IPath sourcePath = getSourcePath(classFile);
    if (sourcePath == null)
        return null;
    IProject project = classFile.getJavaProject().getProject();
    final String javaSimpleFileName = getJavaSimpleFileName(classFile);
    SourceRelativeURI localURI = getPathInFragmentRoot(classFile, javaSimpleFileName);
    class TraceRegionProvider implements ITraceRegionProvider {

        private AbstractTraceWithoutStorage trace;

        TraceRegionProvider(AbstractTraceWithoutStorage trace) {
            this.trace = trace;
        }

        @Override
        public AbstractTraceRegion getTraceRegion() {
            if (javaSimpleFileName == null)
                throw new TraceNotFoundException();
            String traceSimpleFileName = traceFileNameProvider.getTraceFromJava(javaSimpleFileName);
            SourceRelativeURI traceURI = getPathInFragmentRoot(classFile, traceSimpleFileName);
            try {
                InputStream contents = trace.getContents(traceURI, trace.getLocalProject());
                if (contents != null)
                    try {
                        return traceRegionSerializer.readTraceRegionFrom(contents);
                    } finally {
                        contents.close();
                    }
            } catch (IOException e) {
                log.error("Error finding trace region", e);
            }
            throw new TraceNotFoundException();
        }
    }
    if (isZipFile(sourcePath)) {
        ZipFileAwareTrace zipFileAwareTrace = zipFileAwareTraceProvider.get();
        zipFileAwareTrace.setProject(project);
        zipFileAwareTrace.setZipFilePath(sourcePath);
        zipFileAwareTrace.setLocalURI(localURI);
        zipFileAwareTrace.setEncoding(getSourceEncoding(classFile));
        zipFileAwareTrace.setTraceRegionProvider(new TraceRegionProvider(zipFileAwareTrace));
        return zipFileAwareTrace;
    } else {
        FolderAwareTrace folderAwareTrace = folderAwareTraceProvider.get();
        folderAwareTrace.setProject(project);
        folderAwareTrace.setRootFolder(sourcePath.toString());
        folderAwareTrace.setLocalURI(localURI);
        folderAwareTrace.setEncoding(getSourceEncoding(classFile));
        folderAwareTrace.setTraceRegionProvider(new TraceRegionProvider(folderAwareTrace));
        return folderAwareTrace;
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) InputStream(java.io.InputStream) TraceNotFoundException(org.eclipse.xtext.generator.trace.TraceNotFoundException) IOException(java.io.IOException) ITraceRegionProvider(org.eclipse.xtext.generator.trace.ITraceRegionProvider) SourceRelativeURI(org.eclipse.xtext.generator.trace.SourceRelativeURI) ITraceRegionProvider(org.eclipse.xtext.generator.trace.ITraceRegionProvider) IProject(org.eclipse.core.resources.IProject)

Example 5 with SourceRelativeURI

use of org.eclipse.xtext.generator.trace.SourceRelativeURI in project xtext-eclipse by eclipse.

the class DefaultUITraceURIConverterTest method assertConversion.

private void assertConversion(String expected, IFile source) {
    AbsoluteURI sourceURI = new AbsoluteURI(URI.createPlatformResourceURI(source.getFullPath().toString(), true));
    IProjectConfig projectConfig = projectConfigProvider.createProjectConfig(project.getProject());
    SourceRelativeURI traceUri = converter.getURIForTrace(projectConfig, sourceURI);
    assertEquals(expected, traceUri.toString());
}
Also used : IProjectConfig(org.eclipse.xtext.workspace.IProjectConfig) AbsoluteURI(org.eclipse.xtext.generator.trace.AbsoluteURI) SourceRelativeURI(org.eclipse.xtext.generator.trace.SourceRelativeURI)

Aggregations

SourceRelativeURI (org.eclipse.xtext.generator.trace.SourceRelativeURI)22 AbstractTraceRegion (org.eclipse.xtext.generator.trace.AbstractTraceRegion)7 ITraceRegionProvider (org.eclipse.xtext.generator.trace.ITraceRegionProvider)5 IFile (org.eclipse.core.resources.IFile)4 IPath (org.eclipse.core.runtime.IPath)4 URI (org.eclipse.emf.common.util.URI)4 AbsoluteURI (org.eclipse.xtext.generator.trace.AbsoluteURI)4 ILocationData (org.eclipse.xtext.generator.trace.ILocationData)4 TraceNotFoundException (org.eclipse.xtext.generator.trace.TraceNotFoundException)4 InputStream (java.io.InputStream)3 IResource (org.eclipse.core.resources.IResource)3 CoreException (org.eclipse.core.runtime.CoreException)3 IResourceServiceProvider (org.eclipse.xtext.resource.IResourceServiceProvider)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IContainer (org.eclipse.core.resources.IContainer)2 IProject (org.eclipse.core.resources.IProject)2 IStorage (org.eclipse.core.resources.IStorage)2 Path (org.eclipse.core.runtime.Path)2 IClassFile (org.eclipse.jdt.core.IClassFile)2 IEditorInput (org.eclipse.ui.IEditorInput)2