use of org.eclipse.xtext.resource.DerivedStateAwareResource in project xtext-xtend by eclipse.
the class XtendResourceSetBasedResourceDescriptionsTest method testUnloadedInstallDerivedStateThrowsException.
@Test
public void testUnloadedInstallDerivedStateThrowsException() {
try {
StringConcatenation _builder = new StringConcatenation();
_builder.append("package foo class ClassA extends bar.ClassB {}");
Pair<String, String> _mappedTo = Pair.<String, String>of("foo/ClassA.xtend", _builder.toString());
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("package bar class ClassB { public foo.ClassA myField }");
Pair<String, String> _mappedTo_1 = Pair.<String, String>of("bar/ClassB.xtend", _builder_1.toString());
final ResourceSet resourceSet = this.compiler.unLoadedResourceSet(_mappedTo, _mappedTo_1);
final List<? extends Resource> resources = resourceSet.getResources();
ArrayList<Resource> _arrayList = new ArrayList<Resource>(resources);
for (final Resource res : _arrayList) {
{
Assert.assertFalse(res.isLoaded());
try {
((DerivedStateAwareResource) res).installDerivedState(true);
Assert.fail("expected exception");
} catch (final Throwable _t) {
if (_t instanceof IllegalStateException) {
} else {
throw Exceptions.sneakyThrow(_t);
}
}
}
}
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
use of org.eclipse.xtext.resource.DerivedStateAwareResource in project xtext-xtend by eclipse.
the class IndexingTest method doTestResourceDescriptionWithoutGetContents.
private void doTestResourceDescriptionWithoutGetContents(final String input) throws IOException {
XtextResourceSet resourceSet = getResourceSet();
new ClasspathTypeProvider(classLoader, resourceSet, null, null);
final Wrapper<Boolean> wrapper = Wrapper.wrap(Boolean.FALSE);
for (int i = 0; i < 10; i++) {
DerivedStateAwareResource resource = (DerivedStateAwareResource) resourceSet.createResource(URI.createURI("Dummy" + i + ".xtend"));
resource.setDerivedStateComputer(new IDerivedStateComputer() {
@Override
public void installDerivedState(DerivedStateAwareResource resource, boolean preLinkingPhase) {
if (!preLinkingPhase) {
wrapper.set(Boolean.TRUE);
}
derivedStateComputer.installDerivedState(resource, preLinkingPhase);
}
@Override
public void discardDerivedState(DerivedStateAwareResource resource) {
derivedStateComputer.discardDerivedState(resource);
}
});
String actualInput = input;
if (i != 0) {
actualInput = "import C" + (i + 1) + " " + actualInput;
actualInput = "import C" + (i - 1) + " " + actualInput;
}
actualInput = String.format(actualInput, i, i + 1);
resource.load(new StringInputStream(actualInput), null);
}
for (int i = 0; i < 10; i++) {
Resource resource = resourceSet.getResources().get(i);
for (IEObjectDescription description : resourceDescriptionManager.getResourceDescription(resource).getExportedObjects()) {
description.getEObjectOrProxy();
}
}
assertFalse(wrapper.get());
}
use of org.eclipse.xtext.resource.DerivedStateAwareResource in project xtext-xtend by eclipse.
the class Case_14 method testInference.
@Test
public void testInference() {
final Provider<IJvmModelInferrer> _function = () -> {
final IJvmModelInferrer _function_1 = (EObject obj, IJvmDeclaredTypeAcceptor acceptor, boolean preIndexing) -> {
final JvmGenericType firstType = this._jvmTypesBuilder.toClass(obj, "foo.Bar");
final JvmGenericType secondType = this._jvmTypesBuilder.toClass(obj, "foo.Baz");
Assert.assertNull(secondType.eResource());
final Procedure1<JvmGenericType> _function_2 = (JvmGenericType it) -> {
it.setAbstract(true);
Assert.assertNotNull(firstType.eResource());
Assert.assertNotNull(secondType.eResource());
};
acceptor.<JvmGenericType>accept(firstType, _function_2);
final Procedure1<JvmGenericType> _function_3 = (JvmGenericType it) -> {
it.setAbstract(true);
Assert.assertNotNull(firstType.eResource());
};
acceptor.<JvmGenericType>accept(secondType, _function_3);
};
return _function_1;
};
this.assoc.setInferrerProvider(_function);
this.resource.setDerivedStateComputer(null);
this.resource.setURI(URI.createURI("foo.txt"));
this.resourceSet.setClasspathURIContext(this.getClass());
EList<Resource> _resources = this.resourceSet.getResources();
this._jvmTypesBuilder.<DerivedStateAwareResource>operator_add(_resources, this.resource);
EList<EObject> _contents = this.resource.getContents();
EClass _createEClass = EcoreFactory.eINSTANCE.createEClass();
this._jvmTypesBuilder.<EClass>operator_add(_contents, _createEClass);
this.assoc.installDerivedState(this.resource, true);
EObject _get = this.resource.getContents().get(1);
Assert.assertFalse(((JvmDeclaredType) _get).isAbstract());
this.resource.getContents().clear();
EList<EObject> _contents_1 = this.resource.getContents();
EClass _createEClass_1 = EcoreFactory.eINSTANCE.createEClass();
this._jvmTypesBuilder.<EClass>operator_add(_contents_1, _createEClass_1);
this.assoc.installDerivedState(this.resource, false);
EObject _get_1 = this.resource.getContents().get(1);
final JvmGenericType type = ((JvmGenericType) _get_1);
Assert.assertTrue(type.isAbstract());
Assert.assertEquals(1, IterableExtensions.size(Iterables.<JvmConstructor>filter(type.getMembers(), JvmConstructor.class)));
JvmTypeReference _head = IterableExtensions.<JvmTypeReference>head(type.getSuperTypes());
String _qualifiedName = null;
if (_head != null) {
_qualifiedName = _head.getQualifiedName();
}
Assert.assertEquals("java.lang.Object", _qualifiedName);
}
use of org.eclipse.xtext.resource.DerivedStateAwareResource in project xtext-eclipse by eclipse.
the class DefaultJvmModelRenameStrategy method setInferredJvmElementName.
protected void setInferredJvmElementName(String name, EObject renamedElement) {
// the inferred model.
if (renamedElement.eResource() instanceof DerivedStateAwareResource) {
DerivedStateAwareResource resource = (DerivedStateAwareResource) renamedElement.eResource();
resource.discardDerivedState();
resource.installDerivedState(false);
}
}
use of org.eclipse.xtext.resource.DerivedStateAwareResource in project dsl-devkit by dsldevkit.
the class TransformUtil method lookupEPackage.
/**
* Looks up an EPackage visible in the context of a grammar.
*
* @param grammar
* context grammar
* @param packageName
* package name
* @return EPackage or null
*/
public static EPackage lookupEPackage(final Grammar grammar, final String packageName) {
try {
for (AbstractMetamodelDeclaration decl : grammar.getMetamodelDeclarations()) {
if (decl.getEPackage() != null && packageName.equals(decl.getEPackage().getName())) {
return decl.getEPackage();
}
}
ResourceSet resourceSet = grammar.eResource().getResourceSet();
loadImplicitMetamodels(resourceSet);
// With DerivedStateAwareResources, calling getContents() may cause linking, which may load additional resources into the resource set.
// If that happens, we get a ConcurrentModificationException in this loop if we just do
//
// for (Resource res : resourceSet.getResources()) { ... }
//
// Try to guard against that by explicitly copying the list. Since there may be other resource types that may do equally crazy things
// upon getContents(), we do *not* try to guard by only calling getContents() if (!(res instanceof DerivedStateAwareResource)).
List<Resource> resources = new ArrayList<Resource>(resourceSet.getResources());
for (Resource res : resources) {
// language resources. GrammarResources invoke linking here!
if (!(res instanceof DerivedStateAwareResource) && !res.getContents().isEmpty() && res.getContents().get(0) instanceof EPackage) {
EPackage epkg = (EPackage) res.getContents().get(0);
if (packageName.equals(epkg.getName())) {
return epkg;
}
}
}
Registry reg = resourceSet.getPackageRegistry();
for (String uri : reg.keySet()) {
EPackage epkg = reg.getEPackage(uri);
if (packageName.equals(epkg.getName())) {
return epkg;
}
}
// CHECKSTYLE:OFF
} catch (Exception e) {
// CHECKSTYLE:ON
LOG.error("error looking up package " + packageName, e);
}
LOG.info("no package found with name " + packageName);
return null;
}
Aggregations