public RewritableImportSection(
     XtextResource resource,
     IImportsConfiguration importsConfiguration,
     XImportSection originalImportSection,
     String lineSeparator,
     ImportSectionRegionUtil regionUtil,
     IValueConverter<String> nameConverter) {
   this.importsConfiguration = importsConfiguration;
   this.resource = resource;
   this.lineSeparator = lineSeparator;
   this.regionUtil = regionUtil;
   this.nameValueConverter = nameConverter;
   this.implicitlyImportedPackages = importsConfiguration.getImplicitlyImportedPackages(resource);
   this.importRegion = regionUtil.computeRegion(resource);
   if (originalImportSection != null) {
     for (XImportDeclaration originalImportDeclaration :
         originalImportSection.getImportDeclarations()) {
       this.originalImportDeclarations.add(originalImportDeclaration);
       JvmDeclaredType importedType = originalImportDeclaration.getImportedType();
       if (originalImportDeclaration.isStatic()) {
         String memberName = originalImportDeclaration.getMemberName();
         if (originalImportDeclaration.isExtension()) {
           Maps2.putIntoSetMap(importedType, memberName, staticExtensionImports);
         } else {
           Maps2.putIntoSetMap(importedType, memberName, staticImports);
         }
       } else if (importedType != null) {
         Maps2.putIntoListMap(importedType.getSimpleName(), importedType, plainImports);
       }
     }
   }
 }
 protected IScope doGetTypeScope(XFeatureCall call, JvmType type) {
   if (call.isPackageFragment()) {
     if (type instanceof JvmDeclaredType) {
       String packageName = ((JvmDeclaredType) type).getPackageName();
       int dot = packageName.indexOf('.');
       if (dot == -1) {
         return new SingletonScope(EObjectDescription.create(packageName, type), IScope.NULLSCOPE);
       } else {
         String firstSegment = packageName.substring(0, dot);
         return new SingletonScope(
             EObjectDescription.create(firstSegment, type), IScope.NULLSCOPE);
       }
     }
     return IScope.NULLSCOPE;
   } else {
     if (type instanceof JvmDeclaredType && ((JvmDeclaredType) type).getDeclaringType() != null) {
       Resource resource = call.eResource();
       if (resource instanceof XtextResource) {
         XImportSection importSection =
             importsConfiguration.getImportSection((XtextResource) resource);
         if (importSection != null) {
           List<XImportDeclaration> importDeclarations = importSection.getImportDeclarations();
           List<IEObjectDescription> descriptions = Lists.newArrayList();
           for (XImportDeclaration importDeclaration : importDeclarations) {
             if (!importDeclaration.isStatic()
                 && !importDeclaration.isWildcard()
                 && !importDeclaration.isExtension()) {
               JvmDeclaredType importedType = importDeclaration.getImportedType();
               if (importedType == type) {
                 String syntax = importsConfiguration.getLegacyImportSyntax(importDeclaration);
                 if (syntax != null /* no node model attached */
                     && syntax.equals(type.getQualifiedName())) {
                   String packageName = importedType.getPackageName();
                   descriptions.add(
                       EObjectDescription.create(
                           syntax.substring(packageName.length() + 1), type));
                 }
               }
               if (EcoreUtil.isAncestor(importedType, type)) {
                 String name = type.getSimpleName();
                 JvmType worker = type;
                 while (worker != importedType) {
                   worker = (JvmType) worker.eContainer();
                   name = worker.getSimpleName() + "$" + name;
                 }
                 descriptions.add(EObjectDescription.create(name, type));
               }
             }
           }
           return new SimpleScope(descriptions);
         }
       }
       return new SingletonScope(
           EObjectDescription.create(type.getSimpleName(), type), IScope.NULLSCOPE);
     } else {
       return new SingletonScope(
           EObjectDescription.create(type.getSimpleName(), type), IScope.NULLSCOPE);
     }
   }
 }
 protected XImportDeclaration createImport(String importedNamespace, final String member) {
   XImportDeclaration importDeclaration = XtypeFactory.eINSTANCE.createXImportDeclaration();
   importDeclaration.setImportedNamespace(importedNamespace);
   if (member != null) {
     importDeclaration.setMemberName(member);
   }
   return importDeclaration;
 }
 public boolean addImport(JvmDeclaredType type) {
   if (plainImports.containsKey(type.getSimpleName()) || !needsImport(type)) return false;
   Maps2.putIntoListMap(type.getSimpleName(), type, plainImports);
   XImportDeclaration importDeclaration = XtypeFactory.eINSTANCE.createXImportDeclaration();
   importDeclaration.setImportedType(type);
   addedImportDeclarations.add(importDeclaration);
   return true;
 }
 /**
  * @param typeFqn The fully qualified name of the type to import. E.g. <code>java.util.List</code>
  *     . May not be <code>null</code>.
  * @param member member name to import. May not be <code>null</code>. For wildcard use <code>*
  *     </code>
  */
 public boolean addStaticImport(final String typeFqn, final String member) {
   if (typeFqn == null || member == null) {
     throw new IllegalArgumentException("Type name " + typeFqn + ". Member name: " + member);
   }
   if (hasStaticImport(typeFqn, member, false)) {
     return false;
   }
   XImportDeclaration importDecl = createImport(typeFqn, member);
   importDecl.setStatic(true);
   return addedImportDeclarations.add(importDecl);
 }
 protected List<ImportNormalizer> getImportedNamespaceResolvers(
     XImportSection importSection, boolean ignoreCase) {
   List<XImportDeclaration> importDeclarations = importSection.getImportDeclarations();
   List<ImportNormalizer> result = Lists.newArrayListWithExpectedSize(importDeclarations.size());
   for (XImportDeclaration imp : importDeclarations) {
     if (!imp.isStatic()) {
       String value = imp.getImportedNamespace();
       if (value == null) value = imp.getImportedTypeName();
       ImportNormalizer resolver = createImportedNamespaceResolver(value, ignoreCase);
       if (resolver != null) result.add(resolver);
     }
   }
   return result;
 }
 public boolean addStaticImport(JvmDeclaredType type, String memberName) {
   if (hasStaticImport(staticImports, type, memberName)) {
     return false;
   }
   Maps2.putIntoSetMap(type, memberName, staticImports);
   XImportDeclaration importDeclaration = XtypeFactory.eINSTANCE.createXImportDeclaration();
   importDeclaration.setImportedType(type);
   importDeclaration.setStatic(true);
   if (memberName == null) {
     importDeclaration.setWildcard(true);
   } else {
     importDeclaration.setMemberName(memberName);
   }
   addedImportDeclarations.add(importDeclaration);
   return true;
 }
 protected List<XImportDeclaration> findOriginalImports(
     JvmDeclaredType type,
     String memberName,
     Collection<XImportDeclaration> list,
     boolean isStatic,
     boolean isExtension) {
   List<XImportDeclaration> result = newArrayList();
   for (XImportDeclaration importDeclaration : list) {
     if (!(isStatic ^ importDeclaration.isStatic())
         && !(isExtension ^ importDeclaration.isExtension())
         && importDeclaration.getImportedType() == type
         && (memberName == null || memberName.equals(importDeclaration.getMemberName()))) {
       result.add(importDeclaration);
     }
   }
   return result;
 }
 private boolean hasStaticImport(String typeName, String memberName, boolean extension) {
   for (String string : implicitlyImportedPackages) {
     if (typeName.startsWith(string)) {
       return typeName.substring(string.length()).lastIndexOf('.') == 0;
     }
   }
   Map<JvmDeclaredType, Set<String>> imports = staticImports;
   if (extension) {
     imports = staticExtensionImports;
   }
   for (JvmDeclaredType type : imports.keySet()) {
     if (typeName.equals(type.getIdentifier())) {
       Set<String> members = imports.get(type);
       return members != null && ((members.contains(memberName) || members.contains(null)));
     }
   }
   for (XImportDeclaration importDeclr : addedImportDeclarations) {
     String identifier = importDeclr.getImportedTypeName();
     if (importDeclr.isStatic() && typeName.equals(identifier)) {
       if (Objects.equal(importDeclr.getMemberName(), memberName)
           || importDeclr.isWildcard()
           || "*".equals(importDeclr.getMemberName())) {
         return true;
       }
     }
   }
   return false;
 }
 protected void appendImport(StringBuilder builder, XImportDeclaration newImportDeclaration) {
   builder.append("import ");
   if (newImportDeclaration.isStatic()) {
     builder.append("static ");
     if (newImportDeclaration.isExtension()) {
       builder.append("extension ");
     }
   }
   String qualifiedTypeName = newImportDeclaration.getImportedNamespace();
   if (newImportDeclaration.getImportedType() != null) {
     qualifiedTypeName = serializeType(newImportDeclaration.getImportedType());
   }
   String escapedTypeName = nameValueConverter.toString(qualifiedTypeName);
   builder.append(escapedTypeName);
   if (newImportDeclaration.isStatic()) {
     builder.append(".");
     if (newImportDeclaration.isWildcard()) {
       builder.append("*");
     } else {
       builder.append(newImportDeclaration.getMemberName());
     }
   }
   builder.append(lineSeparator);
 }
 protected boolean needsImport(final String fqn) {
   for (String string : implicitlyImportedPackages) {
     if (fqn.startsWith(string)) {
       return fqn.substring(string.length() + 1).lastIndexOf('.') > 0;
     }
   }
   for (XImportDeclaration importDeclr : originalImportDeclarations) {
     if (!importDeclr.isStatic() && fqn.equals(importDeclr.getImportedTypeName())) {
       return false;
     }
   }
   for (XImportDeclaration importDeclr : addedImportDeclarations) {
     String identifier = importDeclr.getImportedTypeName();
     if (!importDeclr.isStatic() && fqn.equals(identifier)) {
       return false;
     }
   }
   return true;
 }