通过eclipse格式化程序在现有和新类的标题中包含版权注释

时间:2015-06-16 12:27:12

标签: java eclipse eclipse-plugin formatting

我希望在每个java文件的顶部包含版权评论。我想通过eclipse格式化程序来做。

如果无法实现,请以任何其他方式建议在现有java文件中包含版权。

2 个答案:

答案 0 :(得分:0)

  1. 尝试Eclipse Releng Tools here

  2. 编写脚本来更新每个文件并添加第一行 - 使用eclipse查找并替换正则表达式 http://java.dzone.com/articles/using-regular-expressions

答案 1 :(得分:0)

虽然它可能不完全适合预期的方法(也就是说,使用eclipse插件):一旦我创建了一个小实用程序类,就完成了这个。

它收集给定源目录中所有.java个文件的列表。对于每个生成的文件,它会查找以单词"package"开头的第一行。如果此行 部分 为空,则它将假定已存在标题,并跳过此文件。否则,它将在以"package"开头的行之前插入一个标题(包含在某个模板文件中)

这可以随意改进和推广,但我只是为了快速将版权标题插入到现有代码库的文件中而将其写下来,并且它运行良好。也许你(或其他人)认为它有用。

import java.io.File;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class HeaderInserter
{
    public static void main(String[] args)
    {
        String headerTemplateFileName = "HeaderTemplate.txt";
        String path = "C:/Workspace/HeaderInserter/src";
        insertHeaders(path, headerTemplateFileName);
    }

    private static void insertHeaders(String path, String headerTemplateFileName)
    {
        FilenameFilter filenameFilter = new FilenameFilter()
        {
            @Override
            public boolean accept(File dir, String name)
            {
                return name.toLowerCase().endsWith(".java");
            }
        };
        List<String> headerLines = readLines(headerTemplateFileName); 
        List<File> files = listFiles(new File(path), filenameFilter);
        for (File file : files)
        {
            System.out.println("Inserting header into "+file);
            handle(file, headerLines);
        }
        System.out.println("Done");
    }

    private static void handle(File inputFile, List<String> headerLines)
    {
        List<String> lines = readLines(inputFile.getPath());

        int index = lineIndexStartingWith(lines, "package");
        if (index == -1)
        {
            System.err.println("No 'package' line found in "+inputFile);
            return;
        }
        if (index > 0)
        {
            List<String> removedLines = lines.subList(0, index);
            String removedPart = createString(removedLines);
            String removedContents = removedPart.replaceAll("\n", "");
            if (removedContents.trim().length() > 0)
            {
                System.err.println("Non-empty header found in "+inputFile);
                System.err.println(removedPart);
                System.err.println("Skipping");
                return;
            }
        }
        List<String> keptLines = lines.subList(index, lines.size());
        List<String> writtenLines = new ArrayList<String>();
        writtenLines.addAll(headerLines);
        writtenLines.addAll(keptLines);
        String writtenContent = createString(writtenLines);

        File outputFile = new File(inputFile.getName()+"_header");
        boolean written = writeContent(outputFile, writtenContent);
        if (written)
        {
            boolean deleted = inputFile.delete();
            if (!deleted)
            {
                System.err.println(
                    "Could not delete old input file: "+inputFile);
                return;
            }

            boolean renamed = outputFile.renameTo(inputFile);
            if (!renamed)
            {
                System.err.println("Could not rename "+outputFile);
                System.err.println("              to "+inputFile);
                return;
            }
            System.out.println("Inserted  header into "+inputFile);
        }
    }

    private static int lineIndexStartingWith(
        List<String> lines, String prefix)
    {
        for (int i=0; i<lines.size(); i++)
        {
            String line = lines.get(i);
            if (line.trim().startsWith(prefix))
            {
                return i;
            }
        }
        return -1;
    }

    private static String createString(List<String> lines)
    {
        StringBuilder sb = new StringBuilder();
        for (String line : lines)
        {
            sb.append(line).append("\n");
        }
        return sb.toString();
    }

    private static boolean writeContent(
        File outputFile, String writtenContent)
    {
        try (FileWriter fw = new FileWriter(outputFile))
        {
            fw.write(writtenContent);
            fw.close();
            return true;
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return false;
    }

    private static List<String> readLines(String fileName) 
    {
        try
        {
            return Files.readAllLines(
                Paths.get(fileName), Charset.defaultCharset());
        }
        catch (IOException e)
        {
            e.printStackTrace();
            return null;
        }
    }

    private static List<File> listFiles(
        File rootDirectory, FilenameFilter filenameFilter)
    {
        List<File> result = new ArrayList<File>();
        listFiles(rootDirectory, filenameFilter, result);
        return result;
    }

    private static void listFiles(
        File file, FilenameFilter filenameFilter, List<File> result)
    {
        if (!file.isDirectory())
        {
            if (filenameFilter.accept(file.getParentFile(), file.getName()))
            {
                result.add(file);
            }
        }
        else
        {
            File files[] = file.listFiles();
            for (File f : files)
            {
                listFiles(f, filenameFilter, result);
            }
        }
    }
}