views:

51

answers:

2

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" dialog in Eclipse, only on steriods. :-)

Thanks in advance, Kyle

A: 

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
akf
A: 

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;
}
Ray Tayek
you'll probably want to switch the order of these statements: System.out.print("is strange "); super.bar(); Or you'll get 'is strange California'
seanizer
yes. i just wanted to show how to ovveride the bar method for a specific state.
Ray Tayek