use of org.eclipse.xtext.resource.DerivedStateAwareResource in project xtext-xtend by eclipse.
the class UnloadingTest method testProperUnloading.
@Test
public void testProperUnloading() {
try {
StringConcatenation _builder = new StringConcatenation();
_builder.append("class B {");
_builder.newLine();
_builder.append("\t");
_builder.append("def void foo() {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("new A(this)");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
final String fileB = _builder.toString();
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("class A {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("new (B b) {");
_builder_1.newLine();
_builder_1.append("\t");
_builder_1.append("}");
_builder_1.newLine();
_builder_1.append("}");
_builder_1.newLine();
final List<XtendFile> parsedFiles = IterableExtensions.<XtendFile>toList(this.files(true, _builder_1.toString(), fileB));
Resource _eResource = parsedFiles.get(1).eResource();
final DerivedStateAwareResource resource = ((DerivedStateAwareResource) _eResource);
final Resource resourceA = IterableExtensions.<XtendFile>head(parsedFiles).eResource();
resource.reparse(fileB);
EObject _head = IterableExtensions.<EObject>head(resourceA.getContents());
final XtendFile file = ((XtendFile) _head);
XtendTypeDeclaration _head_1 = IterableExtensions.<XtendTypeDeclaration>head(file.getXtendTypes());
XtendMember _head_2 = IterableExtensions.<XtendMember>head(((XtendClass) _head_1).getMembers());
Assert.assertNotNull(IterableExtensions.<XtendParameter>head(((XtendConstructor) _head_2).getParameters()).getParameterType().getType().eResource());
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
use of org.eclipse.xtext.resource.DerivedStateAwareResource in project xtext-core by eclipse.
the class XtextLinkingService method getUsedGrammar.
private List<EObject> getUsedGrammar(Grammar grammar, INode node) {
try {
String grammarName = (String) valueConverterService.toValue("", "GrammarID", node);
if (grammarName != null) {
final ResourceSet resourceSet = grammar.eResource().getResourceSet();
List<Resource> resources = resourceSet.getResources();
for (int i = 0; i < resources.size(); i++) {
Resource resource = resources.get(i);
EObject rootElement = null;
if (resource instanceof XtextResource) {
IParseResult parseResult = ((XtextResource) resource).getParseResult();
if (parseResult != null)
rootElement = parseResult.getRootASTElement();
} else if (!resource.getContents().isEmpty()) {
rootElement = resource.getContents().get(0);
}
if (rootElement instanceof Grammar) {
Grammar otherGrammar = (Grammar) rootElement;
if (grammarName.equals(otherGrammar.getName())) {
if (resource instanceof DerivedStateAwareResource)
resource.getContents();
return Collections.<EObject>singletonList(otherGrammar);
}
}
}
URI classpathURI = URI.createURI(ClasspathUriUtil.CLASSPATH_SCHEME + ":/" + grammarName.replace('.', '/') + "." + fileExtension);
URI normalizedURI = null;
if (resourceSet instanceof XtextResourceSet) {
XtextResourceSet set = (XtextResourceSet) resourceSet;
normalizedURI = set.getClasspathUriResolver().resolve(set.getClasspathURIContext(), classpathURI);
} else {
normalizedURI = resourceSet.getURIConverter().normalize(classpathURI);
}
final Resource resource = resourceSet.getResource(normalizedURI, true);
if (!resource.getContents().isEmpty()) {
final Grammar usedGrammar = (Grammar) resource.getContents().get(0);
if (grammarName.equals(usedGrammar.getName()))
return Collections.<EObject>singletonList(usedGrammar);
}
}
return Collections.emptyList();
} catch (ClasspathUriResolutionException e) {
log.debug("Cannot load used grammar.", e);
return Collections.emptyList();
} catch (ValueConverterException e) {
log.debug("Cannot load used grammar.", e);
return Collections.emptyList();
}
}
use of org.eclipse.xtext.resource.DerivedStateAwareResource in project xtext-core by eclipse.
the class EcoreUtil2Test method testClone_2.
@Test
public void testClone_2() throws Exception {
ResourceSetImpl sourceSet = new DerivedStateAwareResourceSet();
DerivedStateAwareResource resource = (DerivedStateAwareResource) sourceSet.createResource(URI.createURI("http://derived.res"));
boolean stateToCheck = !resource.isFullyInitialized();
resource.setFullyInitialized(stateToCheck);
Resource targetRes = EcoreUtil2.clone(new DerivedStateAwareResourceSet(), sourceSet).getResources().get(0);
assertTrue(targetRes instanceof DerivedStateAwareResource);
assertEquals("FullyInitialized flag not copied ", stateToCheck, ((DerivedStateAwareResource) targetRes).isFullyInitialized());
}
use of org.eclipse.xtext.resource.DerivedStateAwareResource in project xtext-core by eclipse.
the class EcoreUtil2 method clone.
/**
* copies contents of a resource set into a new one
*/
public static <T extends ResourceSet> T clone(T target, ResourceSet source) {
EList<Resource> resources = source.getResources();
EcoreUtil.Copier copier = new EcoreUtil.Copier();
for (Resource resource : resources) {
Resource targetResource = target.createResource(resource.getURI());
targetResource.getContents().addAll(copier.copyAll(resource.getContents()));
// mark all resources as fully initialized
if (resource instanceof DerivedStateAwareResource && targetResource instanceof DerivedStateAwareResource) {
((DerivedStateAwareResource) targetResource).setFullyInitialized(((DerivedStateAwareResource) resource).isFullyInitialized());
}
}
copier.copyReferences();
return target;
}
use of org.eclipse.xtext.resource.DerivedStateAwareResource in project dsl-devkit by dsldevkit.
the class AbstractFastLinkingService method getUsedGrammar.
/**
* Tries to find a grammar.
*
* @param resourceSet
* to use for loading
* @param grammarName
* qualified grammar name
* @return A singleton list containing the grammar, or an empty list if not found.
*/
protected List<EObject> getUsedGrammar(final ResourceSet resourceSet, final String grammarName) {
// copied from XtextLinkingService#getUsedGrammar()
try {
if (grammarName != null) {
List<Resource> resources = resourceSet.getResources();
for (int i = 0; i < resources.size(); i++) {
Resource resource = resources.get(i);
EObject rootElement = null;
if (resource instanceof XtextResource) {
IParseResult parseResult = ((XtextResource) resource).getParseResult();
if (parseResult != null) {
rootElement = parseResult.getRootASTElement();
}
} else if (!resource.getContents().isEmpty()) {
rootElement = resource.getContents().get(0);
}
if (rootElement instanceof Grammar) {
Grammar otherGrammar = (Grammar) rootElement;
if (grammarName.equals(otherGrammar.getName())) {
if (resource instanceof DerivedStateAwareResource) {
resource.getContents();
}
return Collections.<EObject>singletonList(otherGrammar);
}
}
}
// $NON-NLS-1$ //$NON-NLS-2$
URI classpathURI = URI.createURI(ClasspathUriUtil.CLASSPATH_SCHEME + ":/" + grammarName.replace('.', '/') + ".xtext");
URI normalizedURI = null;
if (resourceSet instanceof XtextResourceSet) {
XtextResourceSet set = (XtextResourceSet) resourceSet;
normalizedURI = set.getClasspathUriResolver().resolve(set.getClasspathURIContext(), classpathURI);
} else {
normalizedURI = resourceSet.getURIConverter().normalize(classpathURI);
}
final Resource resource = resourceSet.getResource(normalizedURI, true);
if (!resource.getContents().isEmpty()) {
final Grammar usedGrammar = (Grammar) resource.getContents().get(0);
if (grammarName.equals(usedGrammar.getName())) {
return Collections.<EObject>singletonList(usedGrammar);
}
}
}
return Collections.emptyList();
} catch (ClasspathUriResolutionException e) {
return Collections.emptyList();
} catch (ValueConverterException e) {
return Collections.emptyList();
}
}
Aggregations