2014-01-09 45 views
0

我從一個非常舊的項目中獲得了一攬子代碼,他們生成了許多冗餘方法和註釋。批量Java代碼文件修改 - 刪除方法,註釋等

反正有這麼快的,從這個包中的所有類中移除-method「doOldThing()」方法;刪除所有類中的所有@AnnotationObsoluted?

我知道我們可以使用搜索和替換,但編寫正則表達式來刪除那些需要很長時間。我猜我們可能會解析一個Java文件,然後刪除「doOldThings()」方法,檢查是否有@AnnotationObsoluted然後刪除。任何想法?韓國社交協會。

+1

我通常只是刪除或註釋掉方法/類,然後轉向所有源代碼文件中存在錯誤並解決它們。即使在大型項目中,通常也不需要很長時間。 –

回答

0

如果刪除doOldThing()的身體:

doOldThing() { 
} 

然後內嵌的方法,它應該消失的地方使用它。如果您想用doNewThing()的調用替換它:

doOldThing() { 
    doNewThing(); 
} 

然後內聯。

+0

我實際上想從代碼文件中刪除所有這些doOldThing()方法。 – lent

+0

這就是這樣做。當你內聯一個空方法時,對它的調用消失。 –

+0

對不起,請稍等一會,但我無法使用您提供的解決方案,因爲它不會清理我們公司非常嚴格的代碼。最後,我必須使用JavaParser寫下一個小的util,更改java代碼模型並寫回。 – lent

0

如果有人感興趣,下面是我編寫的用於清理方法(通過名稱)和導入語句的小型util的代碼。

import com.googlecode.java2objc.main.Config; 
    import com.googlecode.java2objc.main.Main; 
    import japa.parser.ASTHelper; 
    import japa.parser.JavaParser; 
    import japa.parser.ParseException; 
    import japa.parser.ast.CompilationUnit; 
    import japa.parser.ast.ImportDeclaration; 
    import japa.parser.ast.body.*; 
    import japa.parser.ast.expr.AnnotationExpr; 
    import japa.parser.ast.type.ClassOrInterfaceType; 

    import java.io.File; 
    import java.io.FileInputStream; 
    import java.io.FileOutputStream; 
    import java.io.IOException; 
    import java.util.ArrayList; 
    import java.util.List; 

    /** 
    * User: lent 
    * Date: 10/01/2014 
    */ 
    public class CleanUpAndModelRegeneration 
    { 
     public static void main(String... args) throws IOException, ParseException 
     { 
      List<File> results = 
        listFilesForFolder(new File("\\LocationOfYourCode"), new ArrayList<File>()); 

      List<String> javaFiles = new ArrayList<String>(); 
      for (File codeFile : results) 
      { 
       // creates an input stream for the file to be parsed 
       FileInputStream in = new FileInputStream(codeFile); 

       CompilationUnit cu = null; 
       try 
       { 
        // parse the file 
        cu = JavaParser.parse(in); 
        removeMethod(cu, "toString"); 
        removeMethod(cu, "append"); 
        removeMethod(cu, "appendFields"); 
        removeMethod(cu, "fromValue"); 
        optimizeImport(cu); 
        cleanUpInterfaces("ToString", cu.getTypes()); 
        cleanUpAnnotationForEnum(cu); 

        // prints the resulting compilation unit to default system output 
        System.out.println(cu.toString()); 


       } 
       catch (Exception e) 
       { 
        System.out.println(e.getMessage()); 
       } 
       finally 
       { 
        in.close(); 
       } 

       FileOutputStream fileOutputStream = new FileOutputStream(codeFile, false); 
       fileOutputStream.write(cu.toString().getBytes()); 
       fileOutputStream.flush(); 

       javaFiles.add(codeFile.toString()); 
      } 

      try 
      { 
       Config config = new Config(); 
       Main main = new Main(config, javaFiles); 
       main.execute(); 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 
     } 

     private static void cleanUpAnnotationForEnum(CompilationUnit cu) 
     { 
      for (TypeDeclaration type : cu.getTypes()) 
      { 
       if (type instanceof EnumDeclaration) 
       { 
        EnumDeclaration enumDeclaration = (EnumDeclaration) type; 
        if (enumDeclaration.getAnnotations() != null) 
        { 
         enumDeclaration.getAnnotations().clear(); 
        } 

        for (BodyDeclaration member : enumDeclaration.getMembers()) 
        { 
         List<AnnotationExpr> annotations = member.getAnnotations(); 
         if (annotations != null) 
         { 
          annotations.clear(); 
         } 
        } 
        for (EnumConstantDeclaration member : enumDeclaration.getEntries()) 
        { 
         List<AnnotationExpr> annotations = member.getAnnotations(); 
         if (annotations != null) 
         { 
          annotations.clear(); 
         } 
        } 
       } 
      } 
     } 

     private static void cleanUpInterfaces(String interfaceName, List<? extends BodyDeclaration> types) 
     { 
      for (BodyDeclaration type : types) 
      { 
       cleanUpInterface(type, interfaceName); 
       if (type instanceof TypeDeclaration) 
       { 
        List<BodyDeclaration> members = ((TypeDeclaration) type).getMembers(); 
        if (members == null) 
        { 
         continue; 
        } 
        for (BodyDeclaration body : members) 
        { 
         if (body instanceof ClassOrInterfaceDeclaration) 
         { 
          cleanUpInterface(body, interfaceName); 
          cleanUpInterfaces(interfaceName, ((ClassOrInterfaceDeclaration) body).getMembers()); 
         } 
        } 
       } 
      } 
     } 

     private static void cleanUpInterface(BodyDeclaration typeDeclaration, String interfaceName) 
     { 
      if (typeDeclaration instanceof ClassOrInterfaceDeclaration) 
      { 
       List<ClassOrInterfaceType> interfaceTypes = ((ClassOrInterfaceDeclaration) typeDeclaration).getImplements(); 
       if (interfaceTypes == null) 
       { 
        return; 
       } 
       List<ClassOrInterfaceType> toBeRemove = new ArrayList<ClassOrInterfaceType>(); 
       for (ClassOrInterfaceType interfaceType : interfaceTypes) 
       { 
        if (interfaceType.toString().equals(interfaceName)) 
        { 
         toBeRemove.add(interfaceType); 
        } 
       } 
       interfaceTypes.removeAll(toBeRemove); 
      } 
     } 

     private static void optimizeImport(CompilationUnit cu) 
     { 
      List<ImportDeclaration> toBeRemove = new ArrayList<ImportDeclaration>(); 
      if (cu.getImports() == null) 
      { 
       return; 
      } 
      for (ImportDeclaration importDeclaration : cu.getImports()) 
      { 
       String importPackageName = importDeclaration.getName().toString(); 
       if (importPackageName.startsWith("java.io") 
         || importPackageName.startsWith("javax.xml.datatype") 
         || importPackageName.startsWith("java.util") 
         || importPackageName.startsWith("java.math") 
         || importPackageName.startsWith("java.text")) 
       { 
        continue; 
       } 
       toBeRemove.add(importDeclaration); 
      } 
      cu.getImports().removeAll(toBeRemove); 
     } 

     public static List<File> listFilesForFolder(final File folder, List<File> result) 
     { 
      for (final File fileEntry : folder.listFiles()) 
      { 
       if (fileEntry.isDirectory()) 
       { 
        listFilesForFolder(fileEntry, result); 
       } 
       else 
       { 
        if (result == null) 
        { 
         result = new ArrayList<File>(); 
        } 
        result.add(fileEntry); 
       } 
      } 
      return result; 
     } 

     private static void removeMethod(CompilationUnit cu, String methodName) 
     { 
      List<TypeDeclaration> types = cu.getTypes(); 
      for (TypeDeclaration type : types) 
      { 
       List<BodyDeclaration> members = type.getMembers(); 
       cleanUp(methodName, type, members); 

       if (type.getAnnotations() != null) 
       { 
        type.getAnnotations().clear(); 
       } 
       type.setJavaDoc(null); 
       type.setComment(null); 
      } 
     } 

     private static void cleanUp(String methodName, BodyDeclaration type, List<BodyDeclaration> members) 
     { 
      List<BodyDeclaration> membersToRemove = new ArrayList<BodyDeclaration>(); 
      for (BodyDeclaration member : members) 
      { 
       try 
       { 
        member.setJavaDoc(null); 
        member.setComment(null); 
        if (member.getAnnotations() != null) 
        { 
         member.getAnnotations().clear(); 
        } 
        if (member instanceof MethodDeclaration && methodName.equals(((MethodDeclaration) member).getName())) 
        { 
         membersToRemove.add(member); 
        } 
        if (member instanceof ClassOrInterfaceDeclaration) 
        { 
         cleanUp(methodName, member, ((ClassOrInterfaceDeclaration) member).getMembers()); 
        } 
       } 
       catch (Exception e) 
       { 
        System.out.println(e.getMessage()); 
       } 
      } 
      if (type instanceof TypeDeclaration) 
      { 
       ((TypeDeclaration) type).getMembers().removeAll(membersToRemove); 
      } 
      else if (type instanceof ClassOrInterfaceDeclaration) 
      { 
       ((ClassOrInterfaceDeclaration) type).getMembers().removeAll(membersToRemove); 
      } 
      else if (type instanceof EnumDeclaration) 
      { 
       ((EnumDeclaration) type).getMembers().removeAll(membersToRemove); 
      } 
     } 


     private static void changeMethods(CompilationUnit cu) 
     { 
      List<TypeDeclaration> types = cu.getTypes(); 
      for (TypeDeclaration type : types) 
      { 
       List<BodyDeclaration> members = type.getMembers(); 
       for (BodyDeclaration member : members) 
       { 
        if (member instanceof MethodDeclaration) 
        { 
         MethodDeclaration method = (MethodDeclaration) member; 
         changeMethod(method); 
        } 
       } 
      } 
     } 

     private static void changeMethod(MethodDeclaration n) 
     { 
      // change the name of the method to upper case 
      n.setName(n.getName().toUpperCase()); 

      // create the new parameter 
      Parameter newArg = ASTHelper.createParameter(ASTHelper.INT_TYPE, "value"); 

      // add the parameter to the method 
      ASTHelper.addParameter(n, newArg); 
     } 
    }