Search in sources :

Example 1 with ITraceToBytecodeInstaller

use of org.eclipse.xtext.generator.trace.ITraceToBytecodeInstaller 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 ITraceToBytecodeInstaller

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

the class AbstractXtendInstallDebugInfoMojo method installTrace.

protected void installTrace(File traceFile, Collection<File> classFiles) throws FileNotFoundException, IOException {
    ITraceToBytecodeInstaller traceToBytecodeInstaller = createTraceToBytecodeInstaller();
    InputStream in = new FileInputStream(traceFile);
    try {
        AbstractTraceRegion traceRegion = traceRegionSerializer.readTraceRegionFrom(in);
        traceToBytecodeInstaller.setTrace(traceFileNameProvider.getJavaFromTrace(traceFile.getName()), traceRegion);
        if (getLog().isDebugEnabled())
            getLog().debug("Installing trace " + traceFile + " into:");
        for (File classFile : classFiles) {
            if (getLog().isDebugEnabled())
                getLog().debug("  " + classFile);
            byte[] bytecodeWithTraces = traceToBytecodeInstaller.installTrace(Files.toByteArray(classFile));
            if (bytecodeWithTraces != null)
                Files.write(bytecodeWithTraces, classFile);
        }
    } finally {
        in.close();
    }
}
Also used : ITraceToBytecodeInstaller(org.eclipse.xtext.generator.trace.ITraceToBytecodeInstaller) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) AbstractTraceRegion(org.eclipse.xtext.generator.trace.AbstractTraceRegion) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

InputStream (java.io.InputStream)2 AbstractTraceRegion (org.eclipse.xtext.generator.trace.AbstractTraceRegion)2 ITraceToBytecodeInstaller (org.eclipse.xtext.generator.trace.ITraceToBytecodeInstaller)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IFile (org.eclipse.core.resources.IFile)1 CoreException (org.eclipse.core.runtime.CoreException)1 IJavaElement (org.eclipse.jdt.core.IJavaElement)1 BuildContext (org.eclipse.jdt.core.compiler.BuildContext)1 OutputConfiguration (org.eclipse.xtext.generator.OutputConfiguration)1 SourceRelativeURI (org.eclipse.xtext.generator.trace.SourceRelativeURI)1 IEclipseTrace (org.eclipse.xtext.ui.generator.trace.IEclipseTrace)1 StoppedTask (org.eclipse.xtext.util.internal.Stopwatches.StoppedTask)1