001    /*
002     * Copyright 2004 Sun Microsystems, Inc.  All Rights Reserved.
003     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004     *
005     * This code is free software; you can redistribute it and/or modify it
006     * under the terms of the GNU General Public License version 2 only, as
007     * published by the Free Software Foundation.  Sun designates this
008     * particular file as subject to the "Classpath" exception as provided
009     * by Sun in the LICENSE file that accompanied this code.
010     *
011     * This code is distributed in the hope that it will be useful, but WITHOUT
012     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014     * version 2 for more details (a copy is included in the LICENSE file that
015     * accompanied this code).
016     *
017     * You should have received a copy of the GNU General Public License version
018     * 2 along with this work; if not, write to the Free Software Foundation,
019     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020     *
021     * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022     * CA 95054 USA or visit www.sun.com if you need additional information or
023     * have any questions.
024     */
025    
026    package com.sun.tools.apt;
027    
028    import java.io.PrintWriter;
029    import com.sun.mirror.apt.AnnotationProcessorFactory;
030    
031    /**
032     * The main program for the command-line tool apt.
033     *
034     * <p>Nothing described in this source file is part of any supported
035     * API.  If you write code that depends on this, you do so at your own
036     * risk.  This code and its internal interfaces are subject to change
037     * or deletion without notice.
038     */
039    public class Main {
040    
041        static {
042            ClassLoader loader = Main.class.getClassLoader();
043            if (loader != null)
044                loader.setPackageAssertionStatus("com.sun.tools.apt", true);
045        }
046    
047        /** Command line interface.  If args is <tt>null</tt>, a
048         * <tt>NullPointerException</tt> is thrown.
049         * @param args   The command line parameters.
050         */
051        public static void main(String... args) {
052            System.exit(process(args));
053        }
054    
055        /** Programatic interface.  If args is <tt>null</tt>, a
056         * <tt>NullPointerException</tt> is thrown.
057         * Output is directed to <tt>System.err</tt>.
058         * @param args   The command line parameters.
059         */
060        public static int process(String... args) {
061            return processing(null, null, args);
062        }
063    
064        /** Programmatic interface.  If any argument
065         * is <tt>null</tt>, a <tt>NullPointerException</tt> is thrown.
066         * @param args   The command line parameters.
067         * @param out    Where the tool's output is directed.
068         */
069        public static int process(PrintWriter out, String... args) {
070            if (out == null)
071                throw new NullPointerException("Parameter out cannot be null.");
072            return processing(null, out, args);
073        }
074    
075        /** Programmatic interface.  If <tt>factory</tt> or <tt>args</tt>
076         * is <tt>null</tt>, a <tt>NullPointerException</tt> is thrown.
077         * The &quot;<tt>-factory</tt>&quot; and &quot;<tt>-factorypath</tt>&quot;
078         * command line parameters are ignored by this entry point.
079         * Output is directed to <tt>System.err</tt>.
080         *
081         * @param factory The annotation processor factory to use
082         * @param args    The command line parameters.
083         */
084        public static int process(AnnotationProcessorFactory factory, String... args) {
085            return process(factory, new PrintWriter(System.err, true), args);
086        }
087    
088        /** Programmatic interface.  If any argument
089         * is <tt>null</tt>, a <tt>NullPointerException</tt> is thrown.
090         * The &quot;<tt>-factory</tt>&quot; and &quot;<tt>-factorypath</tt>&quot;
091         * command line parameters are ignored by this entry point.
092         *
093         * @param factory The annotation processor factory to use
094         * @param args   The command line parameters.
095         * @param out    Where the tool's output is directed.
096         */
097        public static int process(AnnotationProcessorFactory factory, PrintWriter out,
098                                  String... args) {
099            if (out == null)
100                throw new NullPointerException("Parameter out cannot be null.");
101            if (factory == null)
102                throw new NullPointerException("Parameter factory cannot be null");
103            return processing(factory, out, args);
104        }
105    
106        private static int processing(AnnotationProcessorFactory factory,
107                                      PrintWriter out,
108                                      String... args) {
109            if (out == null)
110                out = new PrintWriter(System.err, true);
111            com.sun.tools.apt.main.Main compiler =
112                new com.sun.tools.apt.main.Main("apt", out);
113            return compiler.compile(args, factory);
114        }
115    }