public void testComputeEObjectDescriptionsForEmptyFile() throws Exception {
   Resource res = getResourceAndExpect(new StringInputStream(""), URI.createURI("foo.xtext"), 1);
   Manager manager = get(IResourceDescription.Manager.class);
   IResourceDescription description = manager.getResourceDescription(res);
   Iterable<IEObjectDescription> iterable = description.getExportedObjects();
   assertTrue(Lists.newArrayList(iterable).isEmpty());
 }
Esempio n. 2
0
 protected void findIndexedReferences(
     Set<URI> targetURIs,
     IAcceptor<IReferenceDescription> acceptor,
     Predicate<IReferenceDescription> filter,
     IProgressMonitor monitor) {
   Set<URI> targetResourceURIs =
       newHashSet(
           transform(
               targetURIs,
               new Function<URI, URI>() {
                 public URI apply(URI from) {
                   return from.trimFragment();
                 }
               }));
   int numResources = Iterables.size(index.getAllResourceDescriptions());
   SubMonitor subMonitor =
       SubMonitor.convert(monitor, Messages.ReferenceQuery_monitor, numResources);
   for (IResourceDescription resourceDescription : index.getAllResourceDescriptions()) {
     if (subMonitor.isCanceled()) return;
     if (!targetResourceURIs.contains(resourceDescription.getURI())) {
       for (IReferenceDescription referenceDescription :
           resourceDescription.getReferenceDescriptions()) {
         if (targetURIs.contains(referenceDescription.getTargetEObjectUri())
             && (filter == null || filter.apply(referenceDescription))) {
           acceptor.accept(referenceDescription);
         }
       }
     }
     subMonitor.worked(1);
   }
 }
Esempio n. 3
0
 protected boolean checkImportedNamespaceExists(String importedNamespace, Resource res) {
   if (importedNamespace.endsWith(".*")) {
     importedNamespace = importedNamespace.substring(0, importedNamespace.length() - 2); // remove
     // wildcard
   }
   IResourceDescriptions resourceDescriptions =
       resourceDescriptionsProvider.getResourceDescriptions(res);
   URI uri = res.getURI();
   IResourceDescription resourceDescription = resourceDescriptions.getResourceDescription(uri);
   if (resourceDescription == null) {
     return false; // no resource description could be found, so package
     // cannot be resolved anyway
   }
   for (IContainer container :
       containerManager.getVisibleContainers(resourceDescription, resourceDescriptions)) {
     final Iterable<IResourceDescription> currentDescriptions =
         container.getResourceDescriptions();
     for (IResourceDescription resDesc : currentDescriptions) {
       Iterable<IEObjectDescription> visiblePackages =
           resDesc.getExportedObjectsByType(TypesPackage.Literals.PACKAGE);
       for (IEObjectDescription pkgDesc : visiblePackages) {
         if (pkgDesc.getName().toString().equals(importedNamespace)) {
           return true;
         }
       }
     }
   }
   return false;
 }
 public void testGetExportedEObjectsErroneousResource() throws Exception {
   Resource res =
       getResourceAndExpect(
           new StringInputStream("grammar foo Start : 'main';"), URI.createURI("foo.xtext"), 1);
   Manager manager = get(IResourceDescription.Manager.class);
   IResourceDescription description = manager.getResourceDescription(res);
   Iterable<IEObjectDescription> iterable = description.getExportedObjects();
   assertTrue(Lists.newArrayList(iterable).size() == 2);
 }
Esempio n. 5
0
 // Non-static methods
 public List<ComplexType> getComplexTypes() {
   ArrayList<ComplexType> types = Lists.newArrayList();
   for (IResourceDescription resourceDescription : index.getAllResourceDescriptions()) {
     for (IEObjectDescription objDesc :
         resourceDescription.getExportedObjectsByType(
             DatamodelPackage.eINSTANCE.getComplexType())) {
       types.add((ComplexType) objDesc.getEObjectOrProxy());
     }
   }
   return types;
 }
Esempio n. 6
0
 protected Iterator<IEObjectDescription> getExportedObjects(String model) throws Exception {
   XtendFile file = testHelper.xtendFile("test/Foo", model);
   IResourceDescription rd = resourceDescriptionManager.getResourceDescription(file.eResource());
   Iterable<IEObjectDescription> exportedObjects = rd.getExportedObjects();
   return filter(
           exportedObjects,
           new Predicate<IEObjectDescription>() {
             public boolean apply(IEObjectDescription element) {
               return !searchFilter.reject(element);
             }
           })
       .iterator();
 }
Esempio n. 7
0
 protected Map<EObject, URI> createExportedElementsMap(Resource resource) {
   IResourceDescription resourceDescription =
       index.getResourceDescription(EcoreUtil2.getNormalizedURI(resource));
   Map<EObject, URI> exportedElementMap = newHashMap();
   if (resourceDescription != null) {
     for (IEObjectDescription exportedEObjectDescription :
         resourceDescription.getExportedObjects()) {
       EObject eObject =
           resource.getEObject(exportedEObjectDescription.getEObjectURI().fragment());
       if (eObject != null)
         exportedElementMap.put(eObject, exportedEObjectDescription.getEObjectURI());
     }
   }
   return exportedElementMap;
 }
 public ComplexType find(Class source) {
   if (types == null) {
     types = Maps.newTreeMap(Comparators.stringComparator);
     for (IResourceDescription desc : index.getAllResourceDescriptions()) {
       for (IEObjectDescription objDesc :
           desc.getExportedObjectsByType(DatamodelPackage.eINSTANCE.getComplexType())) {
         ComplexType t = (ComplexType) objDesc.getEObjectOrProxy();
         types.put(t.getName(), t);
       }
     }
   }
   if (source.eIsProxy()) {
     throw new IllegalStateException("Proxy not resolved: " + source);
   }
   return types.get(source.getName());
 };
 protected boolean isAffected(
     Collection<QualifiedName> importedNames, IResourceDescription description) {
   if (description != null) {
     for (IEObjectDescription desc : description.getExportedObjects())
       if (importedNames.contains(desc.getName().toLowerCase())) return true;
   }
   return false;
 }
Esempio n. 10
0
  private void buildExportedObjectsIndex(
      IResourceDescription descr, IResourceDescriptions descriptionIndex) {
    // The current (possibly dirty) exported resources
    IResourceDescription dirty =
        resourceServiceProvider.getResourceDescriptionManager().getResourceDescription(resource);
    String pathToCurrent = resource.getURI().path();

    Multimap<String, IEObjectDescription> map = ArrayListMultimap.create();
    // add all (possibly dirty in global index)
    for (IEObjectDescription d : dirty.getExportedObjects())
      map.put(d.getQualifiedName().getLastSegment(), d);
    // add all from global index, except those for current resource
    for (IEObjectDescription d : getExportedObjects(descr, descriptionIndex))
      if (!d.getEObjectURI().path().equals(pathToCurrent))
        map.put(d.getQualifiedName().getLastSegment(), d);
    exportedPerLastSegment = map;
  }
 public void testBug350695() throws Exception {
   Resource res =
       getResource(
           new StringInputStream("grammar org.foo.Zonk generate x \"someURI\" Start : 'main';"),
           URI.createURI("foo.xtext"));
   Manager manager = get(IResourceDescription.Manager.class);
   IResourceDescription description = manager.getResourceDescription(res);
   assertTrue(
       Iterables.isEmpty(
           description.getExportedObjects(
               XtextPackage.Literals.GRAMMAR, QualifiedName.create("org.foo.Zonk"), false)));
   IEObjectDescription element =
       Iterables.getOnlyElement(
           description.getExportedObjects(
               XtextPackage.Literals.GRAMMAR, QualifiedName.create("org", "foo", "Zonk"), false));
   assertNotNull(element);
 }
 protected void addExportedNames(
     Set<QualifiedName> names, IResourceDescription resourceDescriptor) {
   if (resourceDescriptor == null) return;
   Iterable<IEObjectDescription> iterable = resourceDescriptor.getExportedObjects();
   for (IEObjectDescription ieObjectDescription : iterable) {
     names.add(ieObjectDescription.getName().toLowerCase());
   }
 }
Esempio n. 13
0
 /** @since 2.0 */
 public void findIndexedReferences(
     IQueryData queryData,
     URI resourceURI,
     IAcceptor<IReferenceDescription> acceptor,
     IProgressMonitor progressMonitor) {
   IResourceDescription resourceDescription =
       index.getResourceDescription(resourceURI.trimFragment());
   if (resourceDescription != null) {
     for (IReferenceDescription referenceDescription :
         resourceDescription.getReferenceDescriptions()) {
       if (queryData.getTargetURIs().contains(referenceDescription.getTargetEObjectUri())
           && (queryData.getResultFilter() == null
               || queryData.getResultFilter().apply(referenceDescription))) {
         acceptor.accept(referenceDescription);
       }
     }
   }
 }
Esempio n. 14
0
 private boolean atLeastOnePackageExistsInIndex(Resource res) {
   IResourceDescriptions resourceDescriptions =
       resourceDescriptionsProvider.getResourceDescriptions(res);
   URI uri = res.getURI();
   IResourceDescription resourceDescription = resourceDescriptions.getResourceDescription(uri);
   for (IContainer container :
       containerManager.getVisibleContainers(resourceDescription, resourceDescriptions)) {
     final Iterable<IResourceDescription> currentDescriptions =
         container.getResourceDescriptions();
     for (IResourceDescription resDesc : currentDescriptions) {
       Iterable<IEObjectDescription> visisblePackages =
           resDesc.getExportedObjectsByType(TypesPackage.Literals.PACKAGE);
       if (!Iterables.isEmpty(visisblePackages)) {
         return true;
       }
     }
   }
   return false;
 }
 @Override
 public void doGenerateStubs(IFileSystemAccess access, IResourceDescription description) {
   for (IEObjectDescription objectDesc : description.getExportedObjects()) {
     String javaFileName = getJavaFileName(objectDesc);
     if (javaFileName != null) {
       String javaStubSource = getJavaStubSource(objectDesc);
       access.generateFile(javaFileName, javaStubSource);
     }
   }
 }
 public ToBeBuilt removeProject(IProject project, IProgressMonitor monitor) {
   SubMonitor progress =
       SubMonitor.convert(monitor, Iterables.size(builderState.getAllResourceDescriptions()));
   ToBeBuilt result = new ToBeBuilt();
   Iterable<IResourceDescription> allResourceDescriptions =
       builderState.getAllResourceDescriptions();
   for (IResourceDescription description : allResourceDescriptions) {
     Iterable<Pair<IStorage, IProject>> storages = mapper.getStorages(description.getURI());
     boolean onlyOnThisProject = true;
     Iterator<Pair<IStorage, IProject>> iterator = storages.iterator();
     while (iterator.hasNext() && onlyOnThisProject) {
       Pair<IStorage, IProject> storage2Project = iterator.next();
       onlyOnThisProject = project.equals(storage2Project.getSecond());
     }
     if (onlyOnThisProject) result.getToBeDeleted().add(description.getURI());
     progress.worked(1);
   }
   return result;
 }
Esempio n. 17
0
 public void testResourceDescriptionsAreCorrect()
     throws IOException, IllegalAccessException, NoSuchFieldException {
   Resource _newResource = this.newResource("return s.toUpperCase");
   final Resource resource = _newResource;
   Field _declaredField =
       org.eclipse.xtext.resource.DerivedStateAwareResource.class.getDeclaredField(
           "fullyInitialized");
   final Field field = _declaredField;
   field.setAccessible(true);
   Object _get = field.get(resource);
   Assert.assertFalse(((Boolean) _get));
   IResourceDescription _resourceDescription = this.manager.getResourceDescription(resource);
   final IResourceDescription desc = _resourceDescription;
   Iterable<IEObjectDescription> _exportedObjects = desc.getExportedObjects();
   ArrayList<Iterable<IEObjectDescription>> _newArrayList =
       CollectionLiterals.<Iterable<IEObjectDescription>>newArrayList(_exportedObjects);
   final ArrayList<Iterable<IEObjectDescription>> list = _newArrayList;
   int _size = list.size();
   Assert.assertEquals(1, _size);
   Object _get_1 = field.get(resource);
   Assert.assertFalse(((Boolean) _get_1));
 }
  @Override
  public void build(IBuildContext context, IProgressMonitor monitor) throws CoreException {
    if (!context.getBuiltProject().hasNature(KarelNature.NATURE_ID)) return;

    for (Delta delta : context.getDeltas()) {
      IResourceDescription newRes = delta.getNew();
      if (newRes == null) continue;

      try {
        IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
        URI uri = CommonPlugin.resolve(newRes.getURI());
        if (!uri.isFile()) continue;
        IPath path = new Path(uri.toFileString());
        IFile file = workspaceRoot.getFileForLocation(path);
        if (file == null) continue;
        generate(context.getBuiltProject(), file, monitor);
      } catch (Exception e) {
        IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e);
        Activator.getDefault().getLog().log(status);
      }
    }

    context.getResourceSet();
  }
Esempio n. 19
0
 public boolean namesIntersect(
     final IResourceDescription resourceDescription, final Set<QualifiedName> names) {
   boolean _xblockexpression = false;
   {
     boolean _equals = Objects.equal(resourceDescription, null);
     if (_equals) {
       return false;
     }
     Iterable<IEObjectDescription> _exportedObjects = resourceDescription.getExportedObjects();
     for (final IEObjectDescription objectDescription : _exportedObjects) {
       QualifiedName _qualifiedName = objectDescription.getQualifiedName();
       boolean _contains = names.contains(_qualifiedName);
       if (_contains) {
         return true;
       }
     }
     _xblockexpression = (false);
   }
   return _xblockexpression;
 }
Esempio n. 20
0
 @Test
 public void testLoadFromStorage() {
   try {
     StringConcatenation _builder = new StringConcatenation();
     _builder.append("package mypack");
     _builder.newLine();
     _builder.newLine();
     _builder.append("class MyClass {");
     _builder.newLine();
     _builder.append("\t");
     _builder.append("public def void foo() {");
     _builder.newLine();
     _builder.append("\t");
     _builder.append("}");
     _builder.newLine();
     _builder.append("}");
     _builder.newLine();
     final IFile file = this.helper.createFile("src/mypack/MyClass.xtend", _builder.toString());
     StringConcatenation _builder_1 = new StringConcatenation();
     _builder_1.append("package mypack");
     _builder_1.newLine();
     _builder_1.newLine();
     _builder_1.append("class OtherClass extends MyClass {");
     _builder_1.newLine();
     _builder_1.append("\t");
     _builder_1.append("override foo() {");
     _builder_1.newLine();
     _builder_1.append("\t");
     _builder_1.append("}");
     _builder_1.newLine();
     _builder_1.append("}");
     _builder_1.newLine();
     final IFile file2 =
         this.helper.createFile("src/mypack/OtherClass.xtend", _builder_1.toString());
     IResourcesSetupUtil.waitForAutoBuild();
     final URI uri = this.uriMapper.getUri(file);
     final URI uri2 = this.uriMapper.getUri(file2);
     IProject _project = file.getProject();
     final ResourceSet resourceSet = this.resourceSetProvider.get(_project);
     SourceLevelURIsAdapter.setSourceLevelUris(
         resourceSet, Collections.<URI>unmodifiableList(CollectionLiterals.<URI>newArrayList()));
     Resource _resource = resourceSet.getResource(uri2, true);
     final StorageAwareResource resource2 = ((StorageAwareResource) _resource);
     boolean _isLoadedFromStorage = resource2.isLoadedFromStorage();
     Assert.assertTrue(_isLoadedFromStorage);
     EcoreUtil.resolveAll(resource2);
     IResourceDescription _resourceDescription = resource2.getResourceDescription();
     Iterable<IEObjectDescription> _exportedObjects = _resourceDescription.getExportedObjects();
     final Function1<IEObjectDescription, String> _function =
         new Function1<IEObjectDescription, String>() {
           @Override
           public String apply(final IEObjectDescription it) {
             QualifiedName _name = it.getName();
             return _name.toString();
           }
         };
     Iterable<String> _map =
         IterableExtensions.<IEObjectDescription, String>map(_exportedObjects, _function);
     String _join = IterableExtensions.join(_map, ",");
     Assert.assertEquals("mypack.OtherClass", _join);
     Resource _resource_1 = resourceSet.getResource(uri, false);
     final StorageAwareResource resource = ((StorageAwareResource) _resource_1);
     boolean _isLoadedFromStorage_1 = resource.isLoadedFromStorage();
     Assert.assertTrue(_isLoadedFromStorage_1);
     IResourceDescription _resourceDescription_1 = resource.getResourceDescription();
     Iterable<IEObjectDescription> _exportedObjects_1 =
         _resourceDescription_1.getExportedObjects();
     final Function1<IEObjectDescription, String> _function_1 =
         new Function1<IEObjectDescription, String>() {
           @Override
           public String apply(final IEObjectDescription it) {
             QualifiedName _name = it.getName();
             return _name.toString();
           }
         };
     Iterable<String> _map_1 =
         IterableExtensions.<IEObjectDescription, String>map(_exportedObjects_1, _function_1);
     String _join_1 = IterableExtensions.join(_map_1, ",");
     Assert.assertEquals("mypack.MyClass", _join_1);
   } catch (Throwable _e) {
     throw Exceptions.sneakyThrow(_e);
   }
 }
Esempio n. 21
0
 public Iterable<IEObjectDescription> getExportedEObjectDescriptions(final EObject o) {
   IResourceDescription _resourceDescription = this.getResourceDescription(o);
   return _resourceDescription.getExportedObjects();
 }
 protected Collection<QualifiedName> getImportedNames(IResourceDescription candidate) {
   return Sets.newHashSet(candidate.getImportedNames());
 }