langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java
author jjg
Thu, 03 Nov 2016 13:59:15 -0700
changeset 41938 8e66bf10fcec
parent 36526 3b41f1c69604
child 42827 36468b5fa7f4
permissions -rw-r--r--
8167975: align javac --add-* modules options with launcher Reviewed-by: jlahoda
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 {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   100
            return handleExceptions(new Callable<Main.Result>() {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   101
                @Override
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   102
                public Main.Result call() throws Exception {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   103
                    prepareCompiler(false);
41938
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 36526
diff changeset
   104
                    if (compiler.errorCount() > 0)
8e66bf10fcec 8167975: align javac --add-* modules options with launcher
jjg
parents: 36526
diff changeset
   105
                        return Main.Result.ERROR;
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   106
                    compiler.compile(args.getFileObjects(), args.getClassNames(), processors);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   107
                    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
   108
                }
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   109
            }, Main.Result.SYSERR, Main.Result.ABNORMAL);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   110
        } finally {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   111
            try {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   112
                cleanup();
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   113
            } catch (ClientCodeException e) {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   114
                throw new RuntimeException(e.getCause());
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   115
            }
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   116
        }
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   117
    }
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   118
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   119
    @Override @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
    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
   121
        Objects.requireNonNull(processors);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
        // not mt-safe
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        if (used.get())
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
            throw new IllegalStateException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
        this.processors = processors;
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   128
    @Override @DefinedBy(Api.COMPILER)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
    public void setLocale(Locale locale) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
        if (used.get())
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
            throw new IllegalStateException();
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1355
diff changeset
   132
        this.locale = locale;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   135
    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
   136
        try {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   137
            return c.call();
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   138
        } catch (FatalError ex) {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   139
            Log log = Log.instance(context);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   140
            Options options = Options.instance(context);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   141
            log.printRawLines(ex.getMessage());
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   142
            if (ex.getCause() != null && options.isSet("dev")) {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   143
                ex.getCause().printStackTrace(log.getWriter(WriterKind.NOTICE));
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   144
            }
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   145
            return sysErrorResult;
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   146
        } catch (AnnotationProcessingError | ClientCodeException e) {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   147
            // AnnotationProcessingError is thrown from JavacProcessingEnvironment,
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   148
            // to forward errors thrown from an annotation processor
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   149
            // ClientCodeException is thrown from ClientCodeWrapper,
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   150
            // 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
   151
            // as specified by javax.tools.JavaCompiler#getTask
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   152
            // and javax.tools.JavaCompiler.CompilationTask#call
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   153
            throw new RuntimeException(e.getCause());
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   154
        } catch (PropagatedException e) {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   155
            throw e.getCause();
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   156
        } catch (IllegalStateException e) {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   157
            throw e;
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   158
        } catch (Exception | Error ex) {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   159
            // Nasty.  If we've already reported an error, compensate
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   160
            // for buggy compiler error recovery by swallowing thrown
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   161
            // exceptions.
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   162
            if (compiler == null || compiler.errorCount() == 0
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   163
                    || Options.instance(context).isSet("dev")) {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   164
                Log log = Log.instance(context);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   165
                log.printLines(PrefixKind.JAVAC, "msg.bug", JavaCompiler.version());
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   166
                ex.printStackTrace(log.getWriter(WriterKind.NOTICE));
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
            return abnormalErrorResult;
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   169
        }
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   170
    }
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
    private void prepareCompiler(boolean forParse) {
8844
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents: 6716
diff changeset
   173
        if (used.getAndSet(true)) {
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents: 6716
diff changeset
   174
            if (compiler == null)
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   175
                throw new PropagatedException(new IllegalStateException());
8844
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents: 6716
diff changeset
   176
        } else {
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   177
            args.validate();
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
            //initialize compiler's default locale
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   180
            context.put(Locale.class, locale);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   181
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   182
            // hack
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   183
            JavacMessages messages = context.get(JavacMessages.messagesKey);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   184
            if (messages != null && !messages.getCurrentLocale().equals(locale))
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   185
                messages.setCurrentLocale(locale);
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
            initPlugins(args.getPluginOpts());
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   188
            initDocLint(args.getDocLintOpts());
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   189
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   190
            // init JavaCompiler and queues
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
            compiler = JavaCompiler.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
            compiler.keepComments = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
            compiler.genEndPos = true;
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
   194
            notYetEntered = new HashMap<>();
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   195
            if (forParse) {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   196
                compiler.initProcessAnnotations(processors);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   197
                for (JavaFileObject file: args.getFileObjects())
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   198
                    notYetEntered.put(file, null);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   199
                genList = new ListBuffer<>();
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   200
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
10193
3e1ef5e9f4fd 7061125: Proposed javac argument processing performance improvement
jjg
parents: 9071
diff changeset
   204
    <T> String toString(Iterable<T> items, String sep) {
3e1ef5e9f4fd 7061125: Proposed javac argument processing performance improvement
jjg
parents: 9071
diff changeset
   205
        String currSep = "";
3e1ef5e9f4fd 7061125: Proposed javac argument processing performance improvement
jjg
parents: 9071
diff changeset
   206
        StringBuilder sb = new StringBuilder();
3e1ef5e9f4fd 7061125: Proposed javac argument processing performance improvement
jjg
parents: 9071
diff changeset
   207
        for (T item: items) {
3e1ef5e9f4fd 7061125: Proposed javac argument processing performance improvement
jjg
parents: 9071
diff changeset
   208
            sb.append(currSep);
3e1ef5e9f4fd 7061125: Proposed javac argument processing performance improvement
jjg
parents: 9071
diff changeset
   209
            sb.append(item.toString());
3e1ef5e9f4fd 7061125: Proposed javac argument processing performance improvement
jjg
parents: 9071
diff changeset
   210
            currSep = sep;
3e1ef5e9f4fd 7061125: Proposed javac argument processing performance improvement
jjg
parents: 9071
diff changeset
   211
        }
3e1ef5e9f4fd 7061125: Proposed javac argument processing performance improvement
jjg
parents: 9071
diff changeset
   212
        return sb.toString();
3e1ef5e9f4fd 7061125: Proposed javac argument processing performance improvement
jjg
parents: 9071
diff changeset
   213
    }
3e1ef5e9f4fd 7061125: Proposed javac argument processing performance improvement
jjg
parents: 9071
diff changeset
   214
8844
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents: 6716
diff changeset
   215
    void cleanup() {
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents: 6716
diff changeset
   216
        if (compiler != null)
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents: 6716
diff changeset
   217
            compiler.close();
27379
5ae894733e09 8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents: 26266
diff changeset
   218
        if (fileManager instanceof BaseFileManager && ((BaseFileManager) fileManager).autoClose) {
5ae894733e09 8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents: 26266
diff changeset
   219
            try {
5ae894733e09 8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents: 26266
diff changeset
   220
                fileManager.close();
5ae894733e09 8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents: 26266
diff changeset
   221
            } catch (IOException ignore) {
5ae894733e09 8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents: 26266
diff changeset
   222
            }
5ae894733e09 8062579: JavacTask, DocumentationTask impls should close file manager when possible
jjg
parents: 26266
diff changeset
   223
        }
8844
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents: 6716
diff changeset
   224
        compiler = null;
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents: 6716
diff changeset
   225
        context = null;
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents: 6716
diff changeset
   226
        notYetEntered = null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   229
    @Override @DefinedBy(Api.COMPILER_TREE)
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   230
    public Iterable<? extends CompilationUnitTree> parse() {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   231
        return handleExceptions(new Callable<Iterable<? extends CompilationUnitTree>>() {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   232
            @Override
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   233
            public Iterable<? extends CompilationUnitTree> call() {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   234
                return parseInternal();
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   235
            }
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   236
        }, List.<CompilationUnitTree>nil(), List.<CompilationUnitTree>nil());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   239
    private Iterable<? extends CompilationUnitTree> parseInternal() {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
        try {
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   241
            prepareCompiler(true);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   242
            List<JCCompilationUnit> units = compiler.parseFiles(args.getFileObjects());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
            for (JCCompilationUnit unit: units) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
                JavaFileObject file = unit.getSourceFile();
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
                if (notYetEntered.containsKey(file))
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
                    notYetEntered.put(file, unit);
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
            return units;
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
            parsed = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
            if (compiler != null && compiler.log != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
                compiler.log.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
    private boolean parsed = false;
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 all the abstract syntax trees to elements.
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
     * @return a list of elements corresponding to the top level
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
     * classes in the abstract syntax trees
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
     */
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   265
    public Iterable<? extends Element> enter() {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
        return enter(null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
     * Translate the given abstract syntax trees to elements.
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
     * @param trees a list of abstract syntax trees.
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
     * @return a list of elements corresponding to the top level
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
     * classes in the abstract syntax trees
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
     */
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   276
    public Iterable<? extends Element> enter(Iterable<? extends CompilationUnitTree> trees)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
    {
10631
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 10193
diff changeset
   278
        if (trees == null && notYetEntered != null && notYetEntered.isEmpty())
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 10193
diff changeset
   279
            return List.nil();
d9914010b902 7090249: IllegalStateException from Trees.getScope when called from JSR 199
jjg
parents: 10193
diff changeset
   280
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   281
        boolean wasInitialized = compiler != null;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   282
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   283
        prepareCompiler(true);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
        ListBuffer<JCCompilationUnit> roots = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
        if (trees == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
            // If there are still files which were specified to be compiled
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
            // (i.e. in fileObjects) but which have not yet been entered,
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
            // then we make sure they have been parsed and add them to the
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
            // list to be entered.
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
            if (notYetEntered.size() > 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
                if (!parsed)
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   294
                    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
   295
                for (JavaFileObject file: args.getFileObjects()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
                    JCCompilationUnit unit = notYetEntered.remove(file);
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
                    if (unit != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
                        if (roots == null)
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
   299
                            roots = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
                        roots.append(unit);
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
                notYetEntered.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
            }
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
            for (CompilationUnitTree cu : trees) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
                if (cu instanceof JCCompilationUnit) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
                    if (roots == null)
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
   310
                        roots = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
                    roots.append((JCCompilationUnit)cu);
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
                    notYetEntered.remove(cu.getSourceFile());
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
                    throw new IllegalArgumentException(cu.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   319
        if (roots == null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   320
            if (trees == null && !wasInitialized) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   321
                compiler.initModules(List.nil());
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   322
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
            return List.nil();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   324
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   325
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   326
        List<JCCompilationUnit> units = compiler.initModules(roots.toList());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
        try {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   329
            units = compiler.enterTrees(units);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
            if (notYetEntered.isEmpty())
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 22163
diff changeset
   332
                compiler.processAnnotations(units);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   334
            ListBuffer<Element> elements = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
            for (JCCompilationUnit unit : units) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   336
                boolean isPkgInfo = unit.sourcefile.isNameCompatible("package-info",
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   337
                                                                     JavaFileObject.Kind.SOURCE);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   338
                if (isPkgInfo) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   339
                    elements.append(unit.packge);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   340
                } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   341
                    for (JCTree node : unit.defs) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   342
                        if (node.hasTag(JCTree.Tag.CLASSDEF)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   343
                            JCClassDecl cdef = (JCClassDecl) node;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   344
                            if (cdef.sym != null) // maybe null if errors in anno processing
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   345
                                elements.append(cdef.sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   346
                        } else if (node.hasTag(JCTree.Tag.MODULEDEF)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   347
                            JCModuleDecl mdef = (JCModuleDecl) node;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   348
                            if (mdef.sym != null)
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   349
                                elements.append(mdef.sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   350
                        }
6716
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 5848
diff changeset
   351
                    }
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 5848
diff changeset
   352
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
            return elements.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
            compiler.log.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   361
    @Override @DefinedBy(Api.COMPILER_TREE)
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   362
    public Iterable<? extends Element> analyze() {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   363
        return handleExceptions(new Callable<Iterable<? extends Element>>() {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   364
            @Override
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   365
            public Iterable<? extends Element> call() {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   366
                return analyze(null);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   367
            }
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   368
        }, List.<Element>nil(), List.<Element>nil());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
     * Complete all analysis on the given classes.
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
     * This can be used to ensure that all compile time errors are reported.
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
     * The classes must have previously been returned from {@link #enter}.
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
     * If null is specified, all outstanding classes will be analyzed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
     * @param classes a list of class elements
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   378
     * @return the elements that were analyzed
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
    // This implementation requires that we open up privileges on JavaCompiler.
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
    // An alternative implementation would be to move this code to JavaCompiler and
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
    // wrap it here
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   383
    public Iterable<? extends Element> analyze(Iterable<? extends Element> classes) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
        enter(null);  // ensure all classes have been entered
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
   386
        final ListBuffer<Element> results = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
            if (classes == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
                handleFlowResults(compiler.flow(compiler.attribute(compiler.todo)), results);
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
                Filter f = new Filter() {
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   392
                    @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
                    public void process(Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
                        handleFlowResults(compiler.flow(compiler.attribute(env)), results);
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
                };
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
                f.run(compiler.todo, classes);
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
            compiler.log.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
        return results;
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
    // where
864
b1cf6afb8244 6724551: Use Queues instead of Lists to link compiler phases
jjg
parents: 735
diff changeset
   405
        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
   406
            for (Env<AttrContext> env: queue) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
                switch (env.tree.getTag()) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10638
diff changeset
   408
                    case CLASSDEF:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
                        JCClassDecl cdef = (JCClassDecl) env.tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
                        if (cdef.sym != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
                            elems.append(cdef.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
                        break;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   413
                    case MODULEDEF:
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   414
                        JCModuleDecl mod = (JCModuleDecl) env.tree;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   415
                        if (mod.sym != null)
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   416
                            elems.append(mod.sym);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   417
                        break;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   418
                    case PACKAGEDEF:
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   419
                        JCCompilationUnit unit = env.toplevel;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
                        if (unit.packge != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
                            elems.append(unit.packge);
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
                        break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
            }
864
b1cf6afb8244 6724551: Use Queues instead of Lists to link compiler phases
jjg
parents: 735
diff changeset
   425
            genList.addAll(queue);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 26264
diff changeset
   428
    @Override @DefinedBy(Api.COMPILER_TREE)
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   429
    public Iterable<? extends JavaFileObject> generate() {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   430
        return handleExceptions(new Callable<Iterable<? extends JavaFileObject>>() {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   431
            @Override
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   432
            public Iterable<? extends JavaFileObject> call() {
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   433
                return generate(null);
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   434
            }
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   435
        }, List.<JavaFileObject>nil(), List.<JavaFileObject>nil());
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
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
     * Generate code corresponding to the given classes.
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
     * The classes must have previously been returned from {@link #enter}.
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
     * If there are classes outstanding to be analyzed, that will be done before
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
     * any classes are generated.
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
     * If null is specified, code will be generated for all outstanding classes.
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
     * @param classes a list of class elements
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   446
     * @return the files that were generated
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
     */
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   448
    public Iterable<? extends JavaFileObject> generate(Iterable<? extends Element> classes) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20249
diff changeset
   449
        final ListBuffer<JavaFileObject> results = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
            analyze(null);  // ensure all classes have been parsed, entered, and analyzed
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
            if (classes == null) {
864
b1cf6afb8244 6724551: Use Queues instead of Lists to link compiler phases
jjg
parents: 735
diff changeset
   454
                compiler.generate(compiler.desugar(genList), results);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
                genList.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
            else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
                Filter f = new Filter() {
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   459
                        @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
                        public void process(Env<AttrContext> env) {
864
b1cf6afb8244 6724551: Use Queues instead of Lists to link compiler phases
jjg
parents: 735
diff changeset
   461
                            compiler.generate(compiler.desugar(ListBuffer.of(env)), results);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
                    };
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
                f.run(genList, classes);
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
            if (genList.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
                compiler.reportDeferredDiagnostics();
8844
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents: 6716
diff changeset
   468
                cleanup();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
        finally {
8844
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents: 6716
diff changeset
   472
            if (compiler != null)
18170a311747 7026509: Cannot use JavaCompiler to create multiple CompilationTasks for partial compilations
jjg
parents: 6716
diff changeset
   473
                compiler.log.flush();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
        return results;
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
    public Iterable<? extends Tree> pathFor(CompilationUnitTree unit, Tree node) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
        return TreeInfo.pathFor((JCTree) node, (JCTree.JCCompilationUnit) unit).reverse();
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   482
    public void ensureEntered() {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   483
        args.allowEmpty();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   484
        enter(null);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   485
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   486
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
    abstract class Filter {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   488
        void run(Queue<Env<AttrContext>> list, Iterable<? extends Element> elements) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   489
            Set<Element> set = new HashSet<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   490
            for (Element item: elements) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
                set.add(item);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   492
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 16316
diff changeset
   494
            ListBuffer<Env<AttrContext>> defer = new ListBuffer<>();
1355
74cc97efef51 6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents: 1258
diff changeset
   495
            while (list.peek() != null) {
74cc97efef51 6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents: 1258
diff changeset
   496
                Env<AttrContext> env = list.remove();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   497
                Symbol test = null;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   498
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   499
                if (env.tree.hasTag(Tag.MODULEDEF)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   500
                    test = ((JCModuleDecl) env.tree).sym;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   501
                } else if (env.tree.hasTag(Tag.PACKAGEDEF)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   502
                    test = env.toplevel.packge;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   503
                } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   504
                    ClassSymbol csym = env.enclClass.sym;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   505
                    if (csym != null)
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   506
                        test = csym.outermostClass();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   507
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 32454
diff changeset
   508
                if (test != null && set.contains(test))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
                    process(env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
                else
1355
74cc97efef51 6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents: 1258
diff changeset
   511
                    defer = defer.append(env);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
1355
74cc97efef51 6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents: 1258
diff changeset
   514
            list.addAll(defer);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
        abstract void process(Env<AttrContext> env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
    /**
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
   521
     * For internal use only.  This method will be
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
     * removed without warning.
26264
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   523
     * @param expr the type expression to be analyzed
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   524
     * @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
   525
     * @return the type
a09fedde76be 8044859: javac duplicates option processing when using Compiler API
jjg
parents: 25874
diff changeset
   526
     * @throws IllegalArgumentException if the type expression of null or empty
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
    public Type parseType(String expr, TypeElement scope) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
        if (expr == null || expr.equals(""))
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
            throw new IllegalArgumentException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
        compiler = JavaCompiler.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
        JavaFileObject prev = compiler.log.useSource(null);
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 864
diff changeset
   533
        ParserFactory parserFactory = ParserFactory.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
        Attr attr = Attr.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
        try {
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 864
diff changeset
   536
            CharBuffer buf = CharBuffer.wrap((expr+"\u0000").toCharArray(), 0, expr.length());
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 864
diff changeset
   537
            Parser parser = parserFactory.newParser(buf, false, false, false);
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 864
diff changeset
   538
            JCTree tree = parser.parseType();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
            return attr.attribType(tree, (Symbol.TypeSymbol)scope);
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
            compiler.log.useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
}