在Eclipse中刷新文件夹(包)后自动添加包声明

时间:2015-06-23 16:48:22

标签: java eclipse packages

我使用的是一个外部工具,可以在我的Java项目中自动生成一些Java类。我需要经常使用这个工具,这意味着它经常在项目中生成新的类。

我正在使用Eclipse进行开发。要在我的项目中包含新类,我刷新包并完成,但我必须使用eclipse快捷方式(即快速提示)将包声明包含在每个新类中。多次重复相同的程序非常繁琐。

在Eclipse中加载Eclipse时,您是否知道Eclipse是否可以自动将包声明添加到每个新类(在Eclipse之外创建)?

4 个答案:

答案 0 :(得分:1)

尝试创建一个eclipse宏,您可以为此目的尝试Practically Macro

:)

答案 1 :(得分:1)

我同意这一评论,即最好由工具或脚本完成。

但是,如果你真的想从Eclipse中做到这一点,我发现了一个非常奇怪的但是工作的解决方法:

  • 切换到分层包演示文稿:

Hierarchical package presentation

  • 在根程序包上使用 alt + shift + r 将程序包重命名为 tmp 。取消选中更新参考(我假设您没有引用,因为您甚至没有包声明)并检查重命名子包。现在Eclipse应该重命名根包,并修复进程中的导入。

enter image description here

  • 在此之后,我需要做的就是按 ctrl + z 来撤消重构。 Eclipse重新命名包,但也保留包声明。

答案 2 :(得分:1)

此答案基于OP对问题的评论

  

我正在使用antlr-4.5。

所以我找到了一个更具体的解决方案,来自ANTLR Tool Command Line Options

  

-package ___为生成的代码指定包/名称空间

答案 3 :(得分:0)

如果新生成的类不在引用类的同一目录中,则必须显式导入包含该类的包,或者您可能希望在代码中执行以下操作以忽略导入:

// Demo Playground for StackOverflow Question
// http://stackoverflow.com/questions/30807497/nsfilemanager-does-not-properly-work/30807624?noredirect=1#comment50017381_30807624

// Shows how to recursively remove all file in a directory by removing the directory and then recreating it.

// Please see the main function first for more explanation


import Cocoa

//Creates Fake Directory with Fake Files
func createFakeDirectoryAtPath(path: String) -> NSError? {

    var isDir = ObjCBool(false)
    if NSFileManager.defaultManager().fileExistsAtPath(path, isDirectory: &isDir) {
        println("INFO> Will not create fake directory, file exists at: \(path).")
        return nil
    }

    var createDirError: NSError?
    NSFileManager.defaultManager().createDirectoryAtPath(path, withIntermediateDirectories: false, attributes: nil, error: &createDirError)

    if let error = createDirError {
        return error
    }
    println("\tINFO> Created Fake Directory Named: \(path.lastPathComponent).")

    for filename in ["one.txt", "two.jpg", "three.png", "four.doc", "five.txt", "six.mp3"] {
        let filepath = path.stringByAppendingPathComponent(filename)
        if NSFileManager.defaultManager().createFileAtPath(filepath, contents: nil, attributes: nil) {
            println("\t\tINFO> Created Fake File Named: \(filename).")
        }
    }


    return nil
}

func createFakeDirectoryStructureAtPath(path: String) -> Bool {

    //Create base directory
    let createDirError = createFakeDirectoryAtPath(path)
    if let error = createDirError {
        println("!ERROR> " + error.localizedDescription)
        return false
    }

    //Creates fake directories
    for dirname in ["one", "two", "three", "four", "five", "six"] {
        let subDirPath = path.stringByAppendingPathComponent(dirname)
        let createSubDirError = createFakeDirectoryAtPath(subDirPath)

        if let error = createSubDirError {
            println("!ERROR> " + error.localizedDescription)
            return false
        }
    }

    return true
}


//Removes a directory at all it's contents
func removeDirectoryAtPath(path: String) {

    var isDir = ObjCBool(false)
    if NSFileManager.defaultManager().fileExistsAtPath(path, isDirectory: &isDir) && isDir {

        var removeDirError: NSError?
        NSFileManager.defaultManager().removeItemAtPath(path, error: &removeDirError)

        if let error = removeDirError {
            println("!ERROR> " + error.localizedDescription)
        }
    }

}


//This is where the demo execution will begin.

// Start by opening the path that will contain the fake directory.
// Then one by one turn use the boolean controls to turn on each step and explore the results of each step.
// Then look at the code to figure out how it's doing what it's doing.

func main() {
    //Controls Location of Fake Directory
    let fakeDirectoryPath = "~/fake-directory".stringByExpandingTildeInPath

    assert(fakeDirectoryPath.lastPathComponent == "fake-directory", "This code will remove the directory and all contents in the directory at path: \(fakeDirectoryPath). If you are ABSOLUTELY sure you want to do this, please update this assertion. Note playground code will automatically execute so you could go up a creek without a paddle if you remove this assertion.")

    println("Open this path: \(    fakeDirectoryPath.stringByDeletingLastPathComponent)\n\n")

    //These Booleans control whether or not execute a step of the demo.

    let setupFakeDirectory = false
    let removeFakeDirectory = false
    let createEmptyDirectory = false
    let removeEmptyDirectory = false


    //Create Fake Directory and Subdirectories with fake files in them.
    if setupFakeDirectory {

        removeDirectoryAtPath(fakeDirectoryPath)

        let success = createFakeDirectoryStructureAtPath(fakeDirectoryPath)
        if success {
            println("Created Fake Directory Structure at Path: \(fakeDirectoryPath)")
        } else {
            println("Didn't Create Fake Directory Structure At Path: \(fakeDirectoryPath)")
        }
    }

    //Removes the fake directory structure
    if removeFakeDirectory {
        removeDirectoryAtPath(fakeDirectoryPath)
    }

    if createEmptyDirectory {
        var createDirError: NSError?
        NSFileManager.defaultManager().createDirectoryAtPath(fakeDirectoryPath, withIntermediateDirectories: false, attributes: nil, error: &createDirError)

        if let error = createDirError {
            println("!ERROR> " + error.localizedDescription)
        }

    }

    if removeEmptyDirectory {
        removeDirectoryAtPath(fakeDirectoryPath)
    }
}

main()

但老实说,我不知道Eclipse是否可以自动为您进行导入。