JavacTask.java 5.9 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.  Oracle designates this
D
duke 已提交
8
 * particular file as subject to the "Classpath" exception as provided
9
 * by Oracle in the LICENSE file that accompanied this code.
D
duke 已提交
10 11 12 13 14 15 16 17 18 19 20
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
21 22 23
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
24 25 26 27 28
 */

package com.sun.source.util;

import java.io.IOException;
J
jjg 已提交
29 30

import javax.annotation.processing.ProcessingEnvironment;
D
duke 已提交
31 32 33 34 35 36 37
import javax.lang.model.element.Element;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileObject;

J
jjg 已提交
38 39 40 41 42 43
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.Tree;
import com.sun.tools.javac.api.BasicJavacTask;
import com.sun.tools.javac.processing.JavacProcessingEnvironment;
import com.sun.tools.javac.util.Context;

D
duke 已提交
44
/**
45
 * Provides access to functionality specific to the JDK Java Compiler, javac.
D
duke 已提交
46 47 48 49 50
 *
 * @author Peter von der Ahé
 * @author Jonathan Gibbons
 * @since 1.6
 */
51
@jdk.Supported
D
duke 已提交
52 53
public abstract class JavacTask implements CompilationTask {

J
jjg 已提交
54 55 56 57 58
    /**
     * Get the {@code JavacTask} for a {@code ProcessingEnvironment}.
     * If the compiler is being invoked using a
     * {@link javax.tools.JavaCompiler.CompilationTask CompilationTask},
     * then that task will be returned.
59
     * @param processingEnvironment the processing environment
J
jjg 已提交
60 61 62 63 64 65 66 67 68 69 70 71
     * @return the {@code JavacTask} for a {@code ProcessingEnvironment}
     * @since 1.8
     */
    public static JavacTask instance(ProcessingEnvironment processingEnvironment) {
        if (!processingEnvironment.getClass().getName().equals(
                "com.sun.tools.javac.processing.JavacProcessingEnvironment"))
            throw new IllegalArgumentException();
        Context c = ((JavacProcessingEnvironment) processingEnvironment).getContext();
        JavacTask t = c.get(JavacTask.class);
        return (t != null) ? t : new BasicJavacTask(c, true);
    }

D
duke 已提交
72 73 74 75 76
    /**
     * Parse the specified files returning a list of abstract syntax trees.
     *
     * @return a list of abstract syntax trees
     * @throws IOException if an unhandled I/O error occurred in the compiler.
J
jjg 已提交
77
     * @throws IllegalStateException if the operation cannot be performed at this time.
D
duke 已提交
78 79 80 81 82 83 84 85 86
     */
    public abstract Iterable<? extends CompilationUnitTree> parse()
        throws IOException;

    /**
     * Complete all analysis.
     *
     * @return a list of elements that were analyzed
     * @throws IOException if an unhandled I/O error occurred in the compiler.
J
jjg 已提交
87
     * @throws IllegalStateException if the operation cannot be performed at this time.
D
duke 已提交
88 89 90 91 92 93 94 95
     */
    public abstract Iterable<? extends Element> analyze() throws IOException;

    /**
     * Generate code.
     *
     * @return a list of files that were generated
     * @throws IOException if an unhandled I/O error occurred in the compiler.
J
jjg 已提交
96
     * @throws IllegalStateException if the operation cannot be performed at this time.
D
duke 已提交
97 98 99 100
     */
    public abstract Iterable<? extends JavaFileObject> generate() throws IOException;

    /**
J
jjg 已提交
101 102 103 104 105 106 107 108 109 110 111
     * The specified listener will receive notification of events
     * describing the progress of this compilation task.
     *
     * If another listener is receiving notifications as a result of a prior
     * call of this method, then that listener will no longer receive notifications.
     *
     * Informally, this method is equivalent to calling {@code removeTaskListener} for
     * any listener that has been previously set, followed by {@code addTaskListener}
     * for the new listener.
     *
     * @throws IllegalStateException if the specified listener has already been added.
D
duke 已提交
112 113 114
     */
    public abstract void setTaskListener(TaskListener taskListener);

J
jjg 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    /**
     * The specified listener will receive notification of events
     * describing the progress of this compilation task.
     *
     * This method may be called at any time before or during the compilation.
     *
     * @throws IllegalStateException if the specified listener has already been added.
     * @since 1.8
     */
    public abstract void addTaskListener(TaskListener taskListener);

    /**
     * The specified listener will no longer receive notification of events
     * describing the progress of this compilation task.
     *
     * This method may be called at any time before or during the compilation.
     *
     * @since 1.8
     */
    public abstract void removeTaskListener(TaskListener taskListener);

D
duke 已提交
136 137
    /**
     * Get a type mirror of the tree node determined by the specified path.
J
jjg 已提交
138 139 140
     * This method has been superceded by methods on
     * {@link com.sun.source.util.Trees Trees}.
     * @see com.sun.source.util.Trees#getTypeMirror
D
duke 已提交
141 142
     */
    public abstract TypeMirror getTypeMirror(Iterable<? extends Tree> path);
J
jjg 已提交
143

D
duke 已提交
144 145 146 147 148 149 150 151 152 153
    /**
     * Get a utility object for dealing with program elements.
     */
    public abstract Elements getElements();

    /**
     * Get a utility object for dealing with type mirrors.
     */
    public abstract Types getTypes();
}