Exemplo n.º 1
0
 public static boolean removeImport(
     String originalPackage,
     final Declaration dec,
     Tree.CompilationUnit cu,
     TextChange tc,
     Set<String> packages) {
   boolean foundOriginal = false;
   Tree.ImportList il = cu.getImportList();
   for (Tree.Import imp : il.getImports()) {
     Referenceable model = imp.getImportPath().getModel();
     if (model != null) {
       if (model.getNameAsString().equals(originalPackage)) {
         Tree.ImportMemberOrTypeList imtl = imp.getImportMemberOrTypeList();
         if (imtl != null) {
           List<ImportMemberOrType> imts = imtl.getImportMemberOrTypes();
           for (int j = 0; j < imts.size(); j++) {
             Tree.ImportMemberOrType imt = imts.get(j);
             Declaration d = imt.getDeclarationModel();
             if (d != null && d.equals(dec)) {
               int offset;
               int length;
               if (j > 0) {
                 offset = getNodeEndOffset(imts.get(j - 1));
                 length = getNodeEndOffset(imt) - offset;
               } else if (j < imts.size() - 1) {
                 offset = getNodeStartOffset(imt);
                 length = getNodeStartOffset(imts.get(j + 1)) - offset;
               } else {
                 if (packages.contains(originalPackage)) {
                   // we're adding to this import statement,
                   // so don't delete the whole import
                   offset = getNodeStartOffset(imt);
                   length = getNodeLength(imt);
                 } else {
                   offset = getNodeStartOffset(imp);
                   length = getNodeLength(imp);
                 }
               }
               tc.addEdit(new DeleteEdit(offset, length));
               foundOriginal = true;
               break;
               // TODO: return the alias!
             }
           }
         }
         break;
       }
     }
   }
   return foundOriginal;
 }
Exemplo n.º 2
0
 private static void addImport(
     String targetPackage, Declaration dec, Tree.CompilationUnit cu, TextChange tc) {
   String name = dec.getName();
   String delim = getDefaultLineDelimiter(getDocument(tc));
   String indent = getDefaultIndent();
   boolean foundMoved = false;
   Tree.ImportList il = cu.getImportList();
   for (Tree.Import i : il.getImports()) {
     Referenceable model = i.getImportPath().getModel();
     if (model != null) {
       if (model.getNameAsString().equals(targetPackage)) {
         Tree.ImportMemberOrTypeList imtl = i.getImportMemberOrTypeList();
         if (imtl != null) {
           String addition;
           int offset;
           List<Tree.ImportMemberOrType> imts = imtl.getImportMemberOrTypes();
           if (imts.isEmpty()) {
             offset = getNodeStartOffset(imtl) + 1;
             addition = delim + indent + name;
             int len = getNodeLength(imtl);
             if (len == 2) {
               addition += delim;
             }
           } else {
             offset = getNodeEndOffset(imts.get(imts.size() - 1));
             addition = "," + delim + indent + name;
           }
           // TODO: the alias!
           tc.addEdit(new InsertEdit(offset, addition));
           foundMoved = true;
         }
         break;
       }
     }
   }
   if (!foundMoved) {
     String text = "import " + targetPackage + " {" + delim + indent + name + delim + "}" + delim;
     tc.addEdit(new InsertEdit(0, text));
   }
 }