169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal/*
269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Javassist, a Java-bytecode translator toolkit.
369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Copyright (C) 1999-2005 Shigeru Chiba. All Rights Reserved.
469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * The contents of this file are subject to the Mozilla Public License Version
669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 1.1 (the "License"); you may not use this file except in compliance with
769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * the License.  Alternatively, the contents of this file may be used under
869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * the terms of the GNU Lesser General Public License Version 2.1 or later.
969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
1069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Software distributed under the License is distributed on an "AS IS" basis,
1169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
1269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * for the specific language governing rights and limitations under the
1369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * License.
1469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */
1569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
1669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpackage sample.preproc;
1769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
1869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport java.io.IOException;
1969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport java.io.BufferedReader;
2069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport java.io.FileReader;
2169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport java.io.BufferedWriter;
2269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport java.io.FileWriter;
2369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport java.util.Vector;
2469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.CannotCompileException;
2569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.CtClass;
2669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.ClassPool;
2769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
2869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal/**
2969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * This is a preprocessor for Java source programs using annotated
3069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * import declarations.
3169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
3269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <ul><pre>
3369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * import <i>class-name</i> by <i>assistant-name</i> [(<i>arg1, arg2, ...</i>)]
3469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * </pre></ul>
3569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
3669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <p>To process this annotation, run this class as follows:
3769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
3869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <ul><pre>
3969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * java sample.preproc.Compiler sample.j
4069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * </pre></ul>
4169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
4269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <p>This command produces <code>sample.java</code>, which only includes
4369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * regular import declarations.  Also, the Javassist program
4469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * specified by <i>assistant-name</i> is executed so that it produces
4569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * class files under the <code>./tmpjvst</code> directory.  The class
4669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * specified by <i>assistant-name</i> must implement
4769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * <code>sample.preproc.Assistant</code>.
4869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
4969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * @see sample.preproc.Assistant
5069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */
5169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
5269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpublic class Compiler {
5369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    protected BufferedReader input;
5469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    protected BufferedWriter output;
5569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    protected ClassPool classPool;
5669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
5769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
5869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Constructs a <code>Compiler</code> with a source file.
5969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
6069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param inputname         the name of the source file.
6169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
6269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public Compiler(String inputname) throws CannotCompileException {
6369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        try {
6469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            input = new BufferedReader(new FileReader(inputname));
6569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
6669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        catch (IOException e) {
6769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            throw new CannotCompileException("cannot open: " + inputname);
6869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
6969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
7069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        String outputname = getOutputFilename(inputname);
7169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (outputname.equals(inputname))
7269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            throw new CannotCompileException("invalid source name: "
7369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                                             + inputname);
7469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
7569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        try {
7669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            output = new BufferedWriter(new FileWriter(outputname));
7769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
7869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        catch (IOException e) {
7969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            throw new CannotCompileException("cannot open: " + outputname);
8069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
8169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
8269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        classPool = ClassPool.getDefault();
8369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
8469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
8569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
8669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Starts preprocessing.
8769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
8869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public void process() throws IOException, CannotCompileException {
8969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int c;
9069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        CommentSkipper reader = new CommentSkipper(input, output);
9169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        while ((c = reader.read()) != -1) {
9269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            output.write(c);
9369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            if (c == 'p') {
9469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                if (skipPackage(reader))
9569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    break;
9669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            }
9769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            else if (c == 'i')
9869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                readImport(reader);
9969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            else if (c != ' ' && c != '\t' && c != '\n' && c != '\r')
10069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                break;
10169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
10269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
10369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        while ((c = input.read()) != -1)
10469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            output.write(c);
10569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
10669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        input.close();
10769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        output.close();
10869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
10969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
11069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    private boolean skipPackage(CommentSkipper reader) throws IOException {
11169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int c;
11269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        c = reader.read();
11369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        output.write(c);
11469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (c != 'a')
11569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return true;
11669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
11769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        while ((c = reader.read()) != -1) {
11869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            output.write(c);
11969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            if (c == ';')
12069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                break;
12169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
12269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
12369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return false;
12469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
12569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
12669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    private void readImport(CommentSkipper reader)
12769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                                throws IOException, CannotCompileException
12869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    {
12969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int word[] = new int[5];
13069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int c;
13169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        for (int i = 0; i < 5; ++i) {
13269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            word[i] = reader.read();
13369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            output.write(word[i]);
13469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
13569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
13669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (word[0] != 'm' || word[1] != 'p' || word[2] != 'o'
13769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            || word[3] != 'r' || word[4] != 't')
13869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return;     // syntax error?
13969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
14069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        c = skipSpaces(reader, ' ');
14169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        StringBuffer classbuf = new StringBuffer();
14269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        while (c != ' ' && c != '\t' && c != '\n' && c != '\r'
14369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal               && c != ';' && c != -1) {
14469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            classbuf.append((char)c);
14569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            c = reader.read();
14669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
14769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
14869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        String importclass = classbuf.toString();
14969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        c = skipSpaces(reader, c);
15069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (c == ';') {
15169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            output.write(importclass);
15269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            output.write(';');
15369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return;
15469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
15569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (c != 'b')
15669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            syntaxError(importclass);
15769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
15869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        reader.read();  // skip 'y'
15969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
16069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        StringBuffer assistant = new StringBuffer();
16169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        Vector args = new Vector();
16269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        c = readAssistant(reader, importclass, assistant, args);
16369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        c = skipSpaces(reader, c);
16469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (c != ';')
16569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            syntaxError(importclass);
16669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
16769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        runAssistant(importclass, assistant.toString(), args);
16869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
16969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
17069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    void syntaxError(String importclass) throws CannotCompileException {
17169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        throw new CannotCompileException("Syntax error.  Cannot import "
17269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                                         + importclass);
17369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
17469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
17569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    int readAssistant(CommentSkipper reader, String importclass,
17669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                      StringBuffer assistant, Vector args)
17769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        throws IOException, CannotCompileException
17869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    {
17969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int c = readArgument(reader, assistant);
18069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        c = skipSpaces(reader, c);
18169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (c == '(') {
18269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            do {
18369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                StringBuffer arg = new StringBuffer();
18469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                c = readArgument(reader, arg);
18569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                args.addElement(arg.toString());
18669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                c = skipSpaces(reader, c);
18769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            } while (c == ',');
18869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
18969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            if (c != ')')
19069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                syntaxError(importclass);
19169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
19269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            return reader.read();
19369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
19469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
19569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return c;
19669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
19769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
19869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    int readArgument(CommentSkipper reader, StringBuffer buf)
19969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        throws IOException
20069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    {
20169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int c = skipSpaces(reader, ' ');
20269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        while ('A' <= c && c <= 'Z' || 'a' <= c && c <= 'z'
20369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal               || '0' <= c && c <= '9' || c == '.' || c == '_') {
20469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            buf.append((char)c);
20569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            c = reader.read();
20669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
20769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
20869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return c;
20969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
21069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
21169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    int skipSpaces(CommentSkipper reader, int c) throws IOException {
21269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        while (c == ' ' || c == '\t' || c == '\n' || c == '\r') {
21369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            if (c == '\n' || c == '\r')
21469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                output.write(c);
21569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
21669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            c = reader.read();
21769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
21869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
21969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return c;
22069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
22169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
22269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
22369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Is invoked if this compiler encoutenrs:
22469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
22569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * <ul><pre>
22669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * import <i>class name</i> by <i>assistant</i> (<i>args1</i>, <i>args2</i>, ...);
22769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * </pre></ul>
22869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     *
22969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param   classname       class name
23069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param   assistantname   assistant
23169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * @param   argv            args1, args2, ...
23269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
23369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    private void runAssistant(String importname, String assistantname,
23469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                              Vector argv)
23569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        throws IOException, CannotCompileException
23669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    {
23769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        Class assistant;
23869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        Assistant a;
23969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int s = argv.size();
24069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        String[] args = new String[s];
24169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        for (int i = 0; i < s; ++i)
24269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            args[i] = (String)argv.elementAt(i);
24369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
24469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        try {
24569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            assistant = Class.forName(assistantname);
24669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
24769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        catch (ClassNotFoundException e) {
24869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            throw new CannotCompileException("Cannot find " + assistantname);
24969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
25069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
25169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        try {
25269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            a = (Assistant)assistant.newInstance();
25369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
25469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        catch (Exception e) {
25569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            throw new CannotCompileException(e);
25669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
25769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
25869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        CtClass[] imports = a.assist(classPool, importname, args);
25969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        s = imports.length;
26069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (s < 1)
26169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            output.write(" java.lang.Object;");
26269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        else {
26369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            output.write(' ');
26469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            output.write(imports[0].getName());
26569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            output.write(';');
26669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            for (int i = 1; i < s; ++i) {
26769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                output.write(" import ");
26869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                output.write(imports[1].getName());
26969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                output.write(';');
27069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            }
27169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
27269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
27369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
27469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    private String getOutputFilename(String input) {
27569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int i = input.lastIndexOf('.');
27669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (i < 0)
27769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            i = input.length();
27869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
27969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return input.substring(0, i) + ".java";
28069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
28169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
28269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public static void main(String[] args) {
28369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (args.length > 0)
28469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            try {
28569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                Compiler c = new Compiler(args[0]);
28669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                c.process();
28769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            }
28869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            catch (IOException e) {
28969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                System.err.println(e);
29069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            }
29169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            catch (CannotCompileException e) {
29269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                System.err.println(e);
29369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            }
29469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        else {
29569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            System.err.println("Javassist version " + CtClass.version);
29669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            System.err.println("No source file is specified.");
29769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
29869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
29969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal}
30069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
30169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalclass CommentSkipper {
30269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    private BufferedReader input;
30369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    private BufferedWriter output;
30469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
30569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public CommentSkipper(BufferedReader reader, BufferedWriter writer) {
30669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        input = reader;
30769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        output = writer;
30869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
30969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
31069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public int read() throws IOException {
31169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int c;
31269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        while ((c = input.read()) != -1)
31369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            if (c != '/')
31469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                return c;
31569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            else {
31669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                c = input.read();
31769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                if (c == '/')
31869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    skipCxxComments();
31969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                else if (c == '*')
32069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    skipCComments();
32169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                else
32269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    output.write('/');
32369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            }
32469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
32569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return c;
32669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
32769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
32869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    private void skipCxxComments() throws IOException {
32969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int c;
33069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        output.write("//");
33169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        while ((c = input.read()) != -1) {
33269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            output.write(c);
33369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            if (c == '\n' || c == '\r')
33469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                break;
33569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
33669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
33769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
33869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    private void skipCComments() throws IOException {
33969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int c;
34069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        boolean star = false;
34169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        output.write("/*");
34269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        while ((c = input.read()) != -1) {
34369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            output.write(c);
34469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            if (c == '*')
34569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                star = true;
34669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            else if(star && c == '/')
34769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                break;
34869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            else
34969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                star = false;
35069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
35169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
35269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal}
353