langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java
author jjg
Thu, 30 Oct 2014 16:08:43 -0700
changeset 27379 5ae894733e09
parent 26266 2d24bda701dc
child 29291 076c277565f7
permissions -rw-r--r--
8062579: JavacTask, DocumentationTask impls should close file manager when possible Reviewed-by: ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 22163
diff changeset
     2
 * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3149
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3149
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3149
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3149
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3149
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.api;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
27379
5ae894733e09 8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents: 26266
diff changeset
    28
import java.io.IOException;
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 864
diff changeset
    29
import java.nio.CharBuffer;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.util.*;
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
    31
import java.util.concurrent.Callable;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.util.concurrent.atomic.AtomicBoolean;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import javax.annotation.processing.Processor;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import javax.lang.model.element.Element;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import javax.lang.model.element.TypeElement;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import javax.tools.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import com.sun.source.tree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import com.sun.tools.javac.code.*;
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
    41
import com.sun.tools.javac.code.Symbol.ClassSymbol;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import com.sun.tools.javac.comp.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
import com.sun.tools.javac.main.*;
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11708
diff changeset
    44
import com.sun.tools.javac.main.JavaCompiler;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
import com.sun.tools.javac.parser.Parser;
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 864
diff changeset
    46
import com.sun.tools.javac.parser.ParserFactory;
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
    47
import com.sun.tools.javac.processing.AnnotationProcessingError;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
import com.sun.tools.javac.tree.*;
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
    49
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
    50
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
import com.sun.tools.javac.util.*;
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
    52
import com.sun.tools.javac.util.DefinedBy.Api;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
import com.sun.tools.javac.util.List;
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
    54
import com.sun.tools.javac.util.Log.PrefixKind;
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
    55
import com.sun.tools.javac.util.Log.WriterKind;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
/**
5848
c5a4ce47e780 6960407: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5847
diff changeset
    58
 * Provides access to functionality specific to the JDK Java Compiler, javac.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    60
 * <p><b>This is NOT part of any supported API.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
 * If you write code that depends on this, you do so at your own
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
 * risk.  This code and its internal interfaces are subject to change
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
 * or deletion without notice.</b></p>
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
 * @author Peter von der Ah&eacute;
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
 * @author Jonathan Gibbons
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
 */
12016
1990493b64db 7093891: support multiple task listeners
jjg
parents: 11708
diff changeset
    68
public class JavacTaskImpl extends BasicJavacTask {
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
    69
    private final Arguments args;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    private JavaCompiler compiler;
27379
5ae894733e09 8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents: 26266
diff changeset
    71
    private JavaFileManager fileManager;
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1355
diff changeset
    72
    private Locale locale;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    private Map<JavaFileObject, JCCompilationUnit> notYetEntered;
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    private ListBuffer<Env<AttrContext>> genList;
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 12016
diff changeset
    75
    private final AtomicBoolean used = new AtomicBoolean();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    private Iterable<? extends Processor> processors;
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
    78
    JavacTaskImpl(Context context) {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
    79
        super(context, true);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
    80
        args = Arguments.instance(context);
27379
5ae894733e09 8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents: 26266
diff changeset
    81
        fileManager = context.get(JavaFileManager.class);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
    84
    @Override @DefinedBy(Api.COMPILER)
16304
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents: 14952
diff changeset
    85
    public Boolean call() {
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents: 14952
diff changeset
    86
        return doCall().isOK();
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents: 14952
diff changeset
    87
    }
475504933a2d 8006212: javac, convert jtreg tests from shell script to java
vromero
parents: 14952
diff changeset
    88
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
    89
    /* Internal version of call exposing Main.Result. */
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
    90
    public Main.Result doCall() {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
    91
        try {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
    92
            return handleExceptions(new Callable<Main.Result>() {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
    93
                @Override
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
    94
                public Main.Result call() throws Exception {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
    95
                    prepareCompiler(false);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
    96
                    compiler.compile(args.getFileObjects(), args.getClassNames(), processors);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
    97
                    return (compiler.errorCount() > 0) ? Main.Result.ERROR : Main.Result.OK; // FIXME?
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
    98
                }
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
    99
            }, Main.Result.SYSERR, Main.Result.ABNORMAL);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   100
        } finally {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   101
            try {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   102
                cleanup();
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   103
            } catch (ClientCodeException e) {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   104
                throw new RuntimeException(e.getCause());
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   105
            }
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   106
        }
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   107
    }
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   108
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   109
    @Override @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
    public void setProcessors(Iterable<? extends Processor> processors) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        processors.getClass(); // null check
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
        // not mt-safe
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        if (used.get())
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
            throw new IllegalStateException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        this.processors = processors;
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   118
    @Override @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
    public void setLocale(Locale locale) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
        if (used.get())
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
            throw new IllegalStateException();
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1355
diff changeset
   122
        this.locale = locale;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   125
    private <T> T handleExceptions(Callable<T> c, T sysErrorResult, T abnormalErrorResult) {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   126
        try {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   127
            return c.call();
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   128
        } catch (FatalError ex) {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   129
            Log log = Log.instance(context);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   130
            Options options = Options.instance(context);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   131
            log.printRawLines(ex.getMessage());
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   132
            if (ex.getCause() != null && options.isSet("dev")) {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   133
                ex.getCause().printStackTrace(log.getWriter(WriterKind.NOTICE));
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   134
            }
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   135
            return sysErrorResult;
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   136
        } catch (AnnotationProcessingError | ClientCodeException e) {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   137
            // AnnotationProcessingError is thrown from JavacProcessingEnvironment,
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   138
            // to forward errors thrown from an annotation processor
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   139
            // ClientCodeException is thrown from ClientCodeWrapper,
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   140
            // to forward errors thrown from user-supplied code for Compiler API
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   141
            // as specified by javax.tools.JavaCompiler#getTask
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   142
            // and javax.tools.JavaCompiler.CompilationTask#call
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   143
            throw new RuntimeException(e.getCause());
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   144
        } catch (PropagatedException e) {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   145
            throw e.getCause();
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   146
        } catch (IllegalStateException e) {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   147
            throw e;
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   148
        } catch (Exception | Error ex) {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   149
            // Nasty.  If we've already reported an error, compensate
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   150
            // for buggy compiler error recovery by swallowing thrown
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   151
            // exceptions.
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   152
            if (compiler == null || compiler.errorCount() == 0
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   153
                    || Options.instance(context).isSet("dev")) {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   154
                Log log = Log.instance(context);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   155
                log.printLines(PrefixKind.JAVAC, "msg.bug", JavaCompiler.version());
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   156
                ex.printStackTrace(log.getWriter(WriterKind.NOTICE));
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   157
            }
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   158
            return abnormalErrorResult;
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   159
        }
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   160
    }
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   161
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   162
    private void prepareCompiler(boolean forParse) {
8844
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents: 6716
diff changeset
   163
        if (used.getAndSet(true)) {
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents: 6716
diff changeset
   164
            if (compiler == null)
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   165
                throw new PropagatedException(new IllegalStateException());
8844
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents: 6716
diff changeset
   166
        } else {
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   167
            args.validate();
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   168
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   169
            //initialize compiler's default locale
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   170
            context.put(Locale.class, locale);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   171
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   172
            // hack
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   173
            JavacMessages messages = context.get(JavacMessages.messagesKey);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   174
            if (messages != null && !messages.getCurrentLocale().equals(locale))
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   175
                messages.setCurrentLocale(locale);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   176
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   177
            initPlugins(args.getPluginOpts());
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   178
            initDocLint(args.getDocLintOpts());
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   179
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   180
            // init JavaCompiler and queues
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
            compiler = JavaCompiler.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
            compiler.keepComments = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
            compiler.genEndPos = true;
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
   184
            notYetEntered = new HashMap<>();
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   185
            if (forParse) {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   186
                compiler.initProcessAnnotations(processors);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   187
                for (JavaFileObject file: args.getFileObjects())
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   188
                    notYetEntered.put(file, null);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   189
                genList = new ListBuffer<>();
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   190
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
10193
3e1ef5e9f4fd 7061125: Proposed javac argument processing performance improvement
jjg
parents: 9071
diff changeset
   194
    <T> String toString(Iterable<T> items, String sep) {
3e1ef5e9f4fd 7061125: Proposed javac argument processing performance improvement
jjg
parents: 9071
diff changeset
   195
        String currSep = "";
3e1ef5e9f4fd 7061125: Proposed javac argument processing performance improvement
jjg
parents: 9071
diff changeset
   196
        StringBuilder sb = new StringBuilder();
3e1ef5e9f4fd 7061125: Proposed javac argument processing performance improvement
jjg
parents: 9071
diff changeset
   197
        for (T item: items) {
3e1ef5e9f4fd 7061125: Proposed javac argument processing performance improvement
jjg
parents: 9071
diff changeset
   198
            sb.append(currSep);
3e1ef5e9f4fd 7061125: Proposed javac argument processing performance improvement
jjg
parents: 9071
diff changeset
   199
            sb.append(item.toString());
3e1ef5e9f4fd 7061125: Proposed javac argument processing performance improvement
jjg
parents: 9071
diff changeset
   200
            currSep = sep;
3e1ef5e9f4fd 7061125: Proposed javac argument processing performance improvement
jjg
parents: 9071
diff changeset
   201
        }
3e1ef5e9f4fd 7061125: Proposed javac argument processing performance improvement
jjg
parents: 9071
diff changeset
   202
        return sb.toString();
3e1ef5e9f4fd 7061125: Proposed javac argument processing performance improvement
jjg
parents: 9071
diff changeset
   203
    }
3e1ef5e9f4fd 7061125: Proposed javac argument processing performance improvement
jjg
parents: 9071
diff changeset
   204
8844
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents: 6716
diff changeset
   205
    void cleanup() {
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents: 6716
diff changeset
   206
        if (compiler != null)
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents: 6716
diff changeset
   207
            compiler.close();
27379
5ae894733e09 8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents: 26266
diff changeset
   208
        if (fileManager instanceof BaseFileManager && ((BaseFileManager) fileManager).autoClose) {
5ae894733e09 8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents: 26266
diff changeset
   209
            try {
5ae894733e09 8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents: 26266
diff changeset
   210
                fileManager.close();
5ae894733e09 8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents: 26266
diff changeset
   211
            } catch (IOException ignore) {
5ae894733e09 8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents: 26266
diff changeset
   212
            }
5ae894733e09 8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents: 26266
diff changeset
   213
        }
8844
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents: 6716
diff changeset
   214
        compiler = null;
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents: 6716
diff changeset
   215
        context = null;
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents: 6716
diff changeset
   216
        notYetEntered = null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   219
    @Override @DefinedBy(Api.COMPILER_TREE)
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   220
    public Iterable<? extends CompilationUnitTree> parse() {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   221
        return handleExceptions(new Callable<Iterable<? extends CompilationUnitTree>>() {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   222
            @Override
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   223
            public Iterable<? extends CompilationUnitTree> call() {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   224
                return parseInternal();
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   225
            }
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   226
        }, List.<CompilationUnitTree>nil(), List.<CompilationUnitTree>nil());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   229
    private Iterable<? extends CompilationUnitTree> parseInternal() {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
        try {
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   231
            prepareCompiler(true);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   232
            List<JCCompilationUnit> units = compiler.parseFiles(args.getFileObjects());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
            for (JCCompilationUnit unit: units) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
                JavaFileObject file = unit.getSourceFile();
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
                if (notYetEntered.containsKey(file))
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
                    notYetEntered.put(file, unit);
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
            return units;
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
            parsed = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
            if (compiler != null && compiler.log != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
                compiler.log.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
    private boolean parsed = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
     * Translate all the abstract syntax trees to elements.
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
     * @return a list of elements corresponding to the top level
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
     * classes in the abstract syntax trees
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
     */
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   255
    public Iterable<? extends TypeElement> enter() {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
        return enter(null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
     * Translate the given abstract syntax trees to elements.
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
     * @param trees a list of abstract syntax trees.
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
     * @return a list of elements corresponding to the top level
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
     * classes in the abstract syntax trees
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
    public Iterable<? extends TypeElement> enter(Iterable<? extends CompilationUnitTree> trees)
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
    {
10631
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 10193
diff changeset
   268
        if (trees == null && notYetEntered != null && notYetEntered.isEmpty())
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 10193
diff changeset
   269
            return List.nil();
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 10193
diff changeset
   270
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   271
        prepareCompiler(true);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
        ListBuffer<JCCompilationUnit> roots = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
        if (trees == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
            // If there are still files which were specified to be compiled
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
            // (i.e. in fileObjects) but which have not yet been entered,
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
            // then we make sure they have been parsed and add them to the
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
            // list to be entered.
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
            if (notYetEntered.size() > 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
                if (!parsed)
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   282
                    parseInternal(); // TODO would be nice to specify files needed to be parsed
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   283
                for (JavaFileObject file: args.getFileObjects()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
                    JCCompilationUnit unit = notYetEntered.remove(file);
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
                    if (unit != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
                        if (roots == null)
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
   287
                            roots = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
                        roots.append(unit);
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
                notYetEntered.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
        else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
            for (CompilationUnitTree cu : trees) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
                if (cu instanceof JCCompilationUnit) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
                    if (roots == null)
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
   298
                        roots = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
                    roots.append((JCCompilationUnit)cu);
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
                    notYetEntered.remove(cu.getSourceFile());
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
                    throw new IllegalArgumentException(cu.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
        if (roots == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
            return List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
            List<JCCompilationUnit> units = compiler.enterTrees(roots.toList());
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
            if (notYetEntered.isEmpty())
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 22163
diff changeset
   314
                compiler.processAnnotations(units);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
   316
            ListBuffer<TypeElement> elements = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
            for (JCCompilationUnit unit : units) {
6716
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 5848
diff changeset
   318
                for (JCTree node : unit.defs) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10638
diff changeset
   319
                    if (node.hasTag(JCTree.Tag.CLASSDEF)) {
6716
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 5848
diff changeset
   320
                        JCClassDecl cdef = (JCClassDecl) node;
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 5848
diff changeset
   321
                        if (cdef.sym != null) // maybe null if errors in anno processing
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 5848
diff changeset
   322
                            elements.append(cdef.sym);
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 5848
diff changeset
   323
                    }
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 5848
diff changeset
   324
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
            return elements.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
            compiler.log.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   333
    @Override @DefinedBy(Api.COMPILER_TREE)
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   334
    public Iterable<? extends Element> analyze() {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   335
        return handleExceptions(new Callable<Iterable<? extends Element>>() {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   336
            @Override
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   337
            public Iterable<? extends Element> call() {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   338
                return analyze(null);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   339
            }
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   340
        }, List.<Element>nil(), List.<Element>nil());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
     * Complete all analysis on the given classes.
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
     * This can be used to ensure that all compile time errors are reported.
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
     * The classes must have previously been returned from {@link #enter}.
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
     * If null is specified, all outstanding classes will be analyzed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
     * @param classes a list of class elements
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   350
     * @return the elements that were analyzed
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
    // This implementation requires that we open up privileges on JavaCompiler.
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
    // An alternative implementation would be to move this code to JavaCompiler and
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
    // wrap it here
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   355
    public Iterable<? extends Element> analyze(Iterable<? extends TypeElement> classes) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
        enter(null);  // ensure all classes have been entered
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
   358
        final ListBuffer<Element> results = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
            if (classes == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
                handleFlowResults(compiler.flow(compiler.attribute(compiler.todo)), results);
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
                Filter f = new Filter() {
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   364
                    @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
                    public void process(Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
                        handleFlowResults(compiler.flow(compiler.attribute(env)), results);
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
                };
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
                f.run(compiler.todo, classes);
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
            compiler.log.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
        return results;
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
    // where
864
b1cf6afb8244 6724551: Use Queues instead of Lists to link compiler phases
jjg
parents: 735
diff changeset
   377
        private void handleFlowResults(Queue<Env<AttrContext>> queue, ListBuffer<Element> elems) {
b1cf6afb8244 6724551: Use Queues instead of Lists to link compiler phases
jjg
parents: 735
diff changeset
   378
            for (Env<AttrContext> env: queue) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
                switch (env.tree.getTag()) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10638
diff changeset
   380
                    case CLASSDEF:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
                        JCClassDecl cdef = (JCClassDecl) env.tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
                        if (cdef.sym != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
                            elems.append(cdef.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
                        break;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10638
diff changeset
   385
                    case TOPLEVEL:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
                        JCCompilationUnit unit = (JCCompilationUnit) env.tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
                        if (unit.packge != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
                            elems.append(unit.packge);
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
                        break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
            }
864
b1cf6afb8244 6724551: Use Queues instead of Lists to link compiler phases
jjg
parents: 735
diff changeset
   392
            genList.addAll(queue);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   395
    @Override @DefinedBy(Api.COMPILER_TREE)
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   396
    public Iterable<? extends JavaFileObject> generate() {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   397
        return handleExceptions(new Callable<Iterable<? extends JavaFileObject>>() {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   398
            @Override
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   399
            public Iterable<? extends JavaFileObject> call() {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   400
                return generate(null);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   401
            }
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   402
        }, List.<JavaFileObject>nil(), List.<JavaFileObject>nil());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
     * Generate code corresponding to the given classes.
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
     * The classes must have previously been returned from {@link #enter}.
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
     * If there are classes outstanding to be analyzed, that will be done before
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
     * any classes are generated.
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
     * If null is specified, code will be generated for all outstanding classes.
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
     * @param classes a list of class elements
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   413
     * @return the files that were generated
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
     */
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   415
    public Iterable<? extends JavaFileObject> generate(Iterable<? extends TypeElement> classes) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
   416
        final ListBuffer<JavaFileObject> results = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
            analyze(null);  // ensure all classes have been parsed, entered, and analyzed
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
            if (classes == null) {
864
b1cf6afb8244 6724551: Use Queues instead of Lists to link compiler phases
jjg
parents: 735
diff changeset
   421
                compiler.generate(compiler.desugar(genList), results);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
                genList.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
            else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
                Filter f = new Filter() {
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   426
                        @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
                        public void process(Env<AttrContext> env) {
864
b1cf6afb8244 6724551: Use Queues instead of Lists to link compiler phases
jjg
parents: 735
diff changeset
   428
                            compiler.generate(compiler.desugar(ListBuffer.of(env)), results);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
                    };
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
                f.run(genList, classes);
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
            if (genList.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
                compiler.reportDeferredDiagnostics();
8844
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents: 6716
diff changeset
   435
                cleanup();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
        finally {
8844
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents: 6716
diff changeset
   439
            if (compiler != null)
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents: 6716
diff changeset
   440
                compiler.log.flush();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
        return results;
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
    public Iterable<? extends Tree> pathFor(CompilationUnitTree unit, Tree node) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
        return TreeInfo.pathFor((JCTree) node, (JCTree.JCCompilationUnit) unit).reverse();
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
    abstract class Filter {
1355
74cc97efef51 6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents: 1258
diff changeset
   450
        void run(Queue<Env<AttrContext>> list, Iterable<? extends TypeElement> classes) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
   451
            Set<TypeElement> set = new HashSet<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
            for (TypeElement item: classes)
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
                set.add(item);
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 16316
diff changeset
   455
            ListBuffer<Env<AttrContext>> defer = new ListBuffer<>();
1355
74cc97efef51 6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents: 1258
diff changeset
   456
            while (list.peek() != null) {
74cc97efef51 6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents: 1258
diff changeset
   457
                Env<AttrContext> env = list.remove();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
                ClassSymbol csym = env.enclClass.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
                if (csym != null && set.contains(csym.outermostClass()))
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
                    process(env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
                else
1355
74cc97efef51 6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents: 1258
diff changeset
   462
                    defer = defer.append(env);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
1355
74cc97efef51 6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents: 1258
diff changeset
   465
            list.addAll(defer);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
        abstract void process(Env<AttrContext> env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
    /**
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
   472
     * For internal use only.  This method will be
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
     * removed without warning.
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   474
     * @param expr the type expression to be analyzed
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   475
     * @param scope the scope in which to analyze the type expression
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   476
     * @return the type
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   477
     * @throws IllegalArgumentException if the type expression of null or empty
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
    public Type parseType(String expr, TypeElement scope) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
        if (expr == null || expr.equals(""))
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
            throw new IllegalArgumentException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
        compiler = JavaCompiler.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
        JavaFileObject prev = compiler.log.useSource(null);
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 864
diff changeset
   484
        ParserFactory parserFactory = ParserFactory.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
        Attr attr = Attr.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
        try {
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 864
diff changeset
   487
            CharBuffer buf = CharBuffer.wrap((expr+"\u0000").toCharArray(), 0, expr.length());
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 864
diff changeset
   488
            Parser parser = parserFactory.newParser(buf, false, false, false);
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 864
diff changeset
   489
            JCTree tree = parser.parseType();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
            return attr.attribType(tree, (Symbol.TypeSymbol)scope);
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
            compiler.log.useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
}