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