Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Code coverage for Xtend (outside of maven build)
Code coverage for Xtend (outside of maven build) [message #1782462] Fri, 23 February 2018 16:48 Go to next message
Lutz Wrage is currently offline Lutz WrageFriend
Messages: 181
Registered: July 2009
Senior Member
I'm using jacoco to get code coverage reports. With Eclemma I can run a test with code coverage manually to get markup in the Java editor. All this works fine for Java code that is generated from xtend code.
I found a blog post https://christiandietrich.wordpress.com/2016/01/12/code-coverage-for-xtend/ that describes how to get the markup in xtend, but that works only when running a maven build. Is there a way to get colored markup in the xtend editor after running a test manually? I can't see any preferences in eclipse that correspond to the configuration settings for the xtend-maven-plugin described in the blog post.
Re: Code coverage for Xtend (outside of maven build) [message #1782467 is a reply to message #1782462] Fri, 23 February 2018 17:53 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
i dont know any.

you have to set the following in the prefs,

outlet.DEFAULT_OUTPUT.installDslAsPrimarySource=true

but e.g. eclemma ignores that?
why: => you need to investigate


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Code coverage for Xtend (outside of maven build) [message #1782472 is a reply to message #1782467] Fri, 23 February 2018 18:54 Go to previous messageGo to next message
Lutz Wrage is currently offline Lutz WrageFriend
Messages: 181
Registered: July 2009
Senior Member
Ah, it's "Ignore generated Java source when debugging (Use for Android)" in the Xtend compiler settings.
Re: Code coverage for Xtend (outside of maven build) [message #1782475 is a reply to message #1782472] Fri, 23 February 2018 19:14 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
yes. but eclemma does not seem to care.

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Code coverage for Xtend (outside of maven build) [message #1782627 is a reply to message #1782475] Tue, 27 February 2018 09:40 Go to previous messageGo to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1812
Registered: July 2009
Location: Firenze, Italy
Senior Member
Sonarqube will ignore that as well...

Re: Code coverage for Xtend (outside of maven build) [message #1841951 is a reply to message #1782627] Fri, 04 June 2021 06:51 Go to previous messageGo to next message
Heinrich Weichert is currently offline Heinrich WeichertFriend
Messages: 1
Registered: June 2021
Junior Member
I know this is very old, but there is a solution.
(Working code, but could be cleaned up).

import java.util.List;
import java.util.Optional;

import javax.inject.Inject;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.eclemma.core.CoverageTools;
import org.eclipse.eclemma.internal.core.analysis.JavaElementCoverageAdapterFactory;
import org.eclipse.emf.common.util.URI;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.xtext.ui.generator.IDerivedResourceMarkers;
import org.jacoco.core.analysis.ICoverageNode;

@SuppressWarnings("restriction")
public class JavaElementAdapterFactory implements IAdapterFactory {

	@Inject
	private IDerivedResourceMarkers derivedResourceMarkers;

	@Inject
	private IWorkspaceRoot wsRoot;

	@Override
	public <T> T getAdapter(final Object adaptableObject, final Class<T> adapterType) {
		if (adaptableObject instanceof IFileEditorInput && adapterType == IJavaElement.class) {
			final IFileEditorInput input = (IFileEditorInput) adaptableObject;
			try {
				final List<IFile> resources = derivedResourceMarkers.findDerivedResources(wsRoot,
						URI.createPlatformResourceURI(input.getFile().getFullPath().toString(), true).toString());
				if (!resources.isEmpty()) {
					final String fileName = input.getAdapter(IFile.class).getFullPath().removeFileExtension()
							.lastSegment()
							+ ".java";
					final Optional<IFile> file = resources.stream()
							.filter(r -> r.getName().equals(fileName))
							.findFirst();
					if (file.isPresent()) {
						return adapterType.cast(JavaCore.create(file.get()));
					}
				}
			} catch (final Exception e) {
				// ignore
			}
		}
		if (adaptableObject instanceof ICompilationUnit && adapterType == ICoverageNode.class) {
			final ICoverageNode coverageInfo = (ICoverageNode) new JavaElementCoverageAdapterFactory()
					.getAdapter(adaptableObject, adapterType);
			if (coverageInfo != null && coverageInfo.containsCode()) {
				return adapterType.cast(coverageInfo);
			}
			final ICompilationUnit unit = (ICompilationUnit) adaptableObject;
			return adapterType
					.cast(CoverageTools.getCoverageInfo(unit.getType(unit.getElementName().replace(".java", ""))));
		}
		return null;
	}

	@Override
	public Class<?>[] getAdapterList() {
		return new Class<?>[] { IFileEditorInput.class, ICompilationUnit.class };
	}

}


This must be registered via an extension point:

<extension
            point="org.eclipse.core.runtime.adapters">
         <factory
               adaptableType="org.eclipse.ui.IFileEditorInput"
               class="com.company.JavaElementAdapterFactory">
            <adapter
                  type="org.eclipse.jdt.core.IJavaElement">
            </adapter>
         </factory>
         <factory
               adaptableType="org.eclipse.jdt.core.ICompilationUnit"
               class="com.company.JavaElementAdapterFactory">
            <adapter
                  type="org.jacoco.core.analysis.ICoverageNode">
            </adapter>
         </factory>
      </extension>


and additionally if using custom shortcuts, mode "coverage" needs to be added to these shortcuts.

 <extension
         point="org.eclipse.debug.ui.launchShortcuts">
      <shortcut
            label="My test"
            helpContextId="org.eclipse.jdt.junit.launch_shortcut"
            class="com.company.TestLaunchShortcut"
            modes="debug,run,coverage"
            id="com.company.debugLaunchShortCutProject">


Some UI widgets don't work this way, this can perhaps be improved. But you get the idea to go forward. Highlighting works in every xtext editor.
Re: Code coverage for Xtend (outside of maven build) [message #1842197 is a reply to message #1841951] Fri, 11 June 2021 13:14 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
@Heinrich did you get this running with primarysource debug info only or with sourcedebugextension/smap too?

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:How to export custom hovers as plug in ?
Next Topic:XBase EMF
Goto Forum:
  


Current Time: Thu Apr 18 00:16:51 GMT 2024

Powered by FUDForum. Page generated in 0.01885 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top