开发者

Batch Create Java Class Stubs

Given a list of potential class names: 1. Alaska . . . 50. Wyoming

Is there a tool that will create empty java class files for each with supplied parameters? I'm thinking of something like the "New...Class" di开发者_开发问答alog in Eclipse, only on steriods. :-)

Thanks in advance, Kyle


I am not sure if a batch new class wizard exists, but it would take as long to find one as it would to roll your own in a simple bat. I would use a for loop, iterating over the contents of a file that lists the Class names to be created, and in the body of the loop echo the template to the newly created file, using the value from the file to both name the .java as well as to fill in the classname in the template.

EDIT: an example bat which reads class names from a file called classnames.txt and creates very simple stubs:

for /F "tokens=1" %%a in (classnames.txt) do call :createClass %%a

dir *.java

goto :eof

:createClass 
   echo package com.abc; > %1.java 
   echo.  >> %1.java 
   echo public class %1 {>> %1.java 
   echo      public %1() { >> %1.java 
   echo      } >> %1.java 
   echo } >> %1.java


maybe try enums:

package p;

interface Foo {
    void bar();

}

enum State implements Foo {
    Alabama, Alaska, Arizona, Arkansas, California(new Integer(42)) {
        public void bar() {
            System.out.print("is strange ");
            super.bar();
        }

    },
    Colorado, Connecticut, Delaware, Florida, Georgia, Hawaii, Idaho, Illinois, Indiana, Iowa, Kansas, Kentucky, Louisiana, Maine, Maryland, Massachusetts, Michigan, Minnesota, Mississippi, Missouri, Montana, Nebraska, Nevada, NewHampshire, NewJersey, NewMexico, NewYork, NorthCarolina, NorthDakota, Ohio, Oklahoma, Oregon, Pennsylvania, RhodeIsland, SouthCarolina, SouthDakota, Tennessee, Texas, Utah, Vermont, Virginia, Washington, WestVirginia, Wisconsin, Wyoming;

    State() {
        this(null);
    }

    State(Object object) {
        this.object = object;
    }

    public void bar() {
        System.out.println(this + " " + object);
    }

    public static void main(String[] arguments) {
        for (State state : State.values())
            state.bar();
    }

    final Object object;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜