Path: blob/master/src/jdk.compiler/share/classes/com/sun/source/util/JavacTask.java
41175 views
/*1* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.source.util;2627import java.io.IOException;2829import javax.annotation.processing.ProcessingEnvironment;30import javax.lang.model.element.Element;31import javax.lang.model.element.VariableElement;32import javax.lang.model.type.TypeMirror;33import javax.lang.model.util.Elements;34import javax.lang.model.util.Types;35import javax.tools.JavaCompiler.CompilationTask;36import javax.tools.JavaFileObject;3738import com.sun.source.tree.CompilationUnitTree;39import com.sun.source.tree.Tree;40import com.sun.tools.javac.api.BasicJavacTask;41import com.sun.tools.javac.processing.JavacProcessingEnvironment;42import com.sun.tools.javac.util.Context;4344/**45* Provides access to functionality specific to the JDK Java Compiler, javac.46*47* @author Peter von der Ahé48* @author Jonathan Gibbons49* @since 1.650*/51public abstract class JavacTask implements CompilationTask {52/**53* Constructor for subclasses to call.54*/55public JavacTask() {}5657/**58* Returns the {@code JavacTask} for a {@code ProcessingEnvironment}.59* If the compiler is being invoked using a60* {@link javax.tools.JavaCompiler.CompilationTask CompilationTask},61* then that task will be returned.62* @param processingEnvironment the processing environment63* @return the {@code JavacTask} for a {@code ProcessingEnvironment}64* @since 1.865*/66public static JavacTask instance(ProcessingEnvironment processingEnvironment) {67if (!processingEnvironment.getClass().getName().equals(68"com.sun.tools.javac.processing.JavacProcessingEnvironment"))69throw new IllegalArgumentException();70Context c = ((JavacProcessingEnvironment) processingEnvironment).getContext();71JavacTask t = c.get(JavacTask.class);72return (t != null) ? t : new BasicJavacTask(c, true);73}7475/**76* Parses the specified files returning a list of abstract syntax trees.77*78* @return a list of abstract syntax trees79* @throws IOException if an unhandled I/O error occurred in the compiler.80* @throws IllegalStateException if the operation cannot be performed at this time.81*/82public abstract Iterable<? extends CompilationUnitTree> parse()83throws IOException;8485/**86* Completes all analysis.87*88* @return a list of elements that were analyzed89* @throws IOException if an unhandled I/O error occurred in the compiler.90* @throws IllegalStateException if the operation cannot be performed at this time.91*/92public abstract Iterable<? extends Element> analyze() throws IOException;9394/**95* Generates code.96*97* @return a list of files that were generated98* @throws IOException if an unhandled I/O error occurred in the compiler.99* @throws IllegalStateException if the operation cannot be performed at this time.100*/101public abstract Iterable<? extends JavaFileObject> generate() throws IOException;102103/**104* Sets a specified listener to receive notification of events105* describing the progress of this compilation task.106*107* If another listener is receiving notifications as a result of a prior108* call of this method, then that listener will no longer receive notifications.109*110* Informally, this method is equivalent to calling {@code removeTaskListener} for111* any listener that has been previously set, followed by {@code addTaskListener}112* for the new listener.113*114* @param taskListener the task listener115* @throws IllegalStateException if the specified listener has already been added.116*/117public abstract void setTaskListener(TaskListener taskListener);118119/**120* Adds a specified listener so that it receives notification of events121* describing the progress of this compilation task.122*123* This method may be called at any time before or during the compilation.124*125* @param taskListener the task listener126* @throws IllegalStateException if the specified listener has already been added.127* @since 1.8128*/129public abstract void addTaskListener(TaskListener taskListener);130131/**132* Removes the specified listener so that it no longer receives133* notification of events describing the progress of this134* compilation task.135*136* This method may be called at any time before or during the compilation.137*138* @param taskListener the task listener139* @since 1.8140*/141public abstract void removeTaskListener(TaskListener taskListener);142143/**144* Sets the specified {@link ParameterNameProvider}. It may be used when145* {@link VariableElement#getSimpleName()} is called for a method parameter146* for which an authoritative name is not found. The given147* {@code ParameterNameProvider} may infer a user-friendly name148* for the method parameter.149*150* Setting a new {@code ParameterNameProvider} will clear any previously set151* {@code ParameterNameProvider}, which won't be queried any more.152*153* When no {@code ParameterNameProvider} is set, or when it returns null from154* {@link ParameterNameProvider#getParameterName(javax.lang.model.element.VariableElement)},155* an automatically synthesized name is returned from {@code VariableElement.getSimpleName()}.156*157* @implSpec The default implementation of this method does nothing.158*159* @param provider the provider160* @since 13161*/162public void setParameterNameProvider(ParameterNameProvider provider) {}163164/**165* Returns a type mirror of the tree node determined by the specified path.166* This method has been superseded by methods on167* {@link com.sun.source.util.Trees Trees}.168*169* @param path the path170* @return the type mirror171* @see com.sun.source.util.Trees#getTypeMirror172*/173public abstract TypeMirror getTypeMirror(Iterable<? extends Tree> path);174175/**176* Returns a utility object for dealing with program elements.177*178* @return a utility object for dealing with program elements179*/180public abstract Elements getElements();181182/**183* Returns a utility object for dealing with type mirrors.184*185* @return the utility object for dealing with type mirrors186*/187public abstract Types getTypes();188}189190191