langtools/src/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java
author mcimadamore
Fri, 12 Nov 2010 12:32:43 +0000
changeset 7209 889e80aa1d2e
parent 6716 71df48777dd1
child 8844 18170a311747
permissions -rw-r--r--
6598108: com.sun.source.util.Trees.isAccessible incorrect Summary: JavacTrees' version of isAccessible should take into account enclosing class accessibility Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
6716
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 5848
diff changeset
     2
 * Copyright (c) 2005, 2010, 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
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.io.File;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 864
diff changeset
    30
import java.nio.CharBuffer;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.util.concurrent.atomic.AtomicBoolean;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import javax.annotation.processing.Processor;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import javax.lang.model.element.Element;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import javax.lang.model.element.TypeElement;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import javax.lang.model.type.TypeMirror;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import javax.tools.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import com.sun.source.tree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import com.sun.source.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import com.sun.tools.javac.code.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
import com.sun.tools.javac.code.Symbol.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
import com.sun.tools.javac.comp.*;
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 10
diff changeset
    45
import com.sun.tools.javac.file.JavacFileManager;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
import com.sun.tools.javac.main.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
import com.sun.tools.javac.model.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
import com.sun.tools.javac.parser.Parser;
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 864
diff changeset
    49
import com.sun.tools.javac.parser.ParserFactory;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
import com.sun.tools.javac.tree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
import com.sun.tools.javac.tree.JCTree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
import com.sun.tools.javac.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
import com.sun.tools.javac.util.List;
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
import com.sun.tools.javac.main.JavaCompiler;
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
/**
5848
c5a4ce47e780 6960407: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5847
diff changeset
    57
 * Provides access to functionality specific to the JDK Java Compiler, javac.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    59
 * <p><b>This is NOT part of any supported API.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
 * If you write code that depends on this, you do so at your own
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
 * risk.  This code and its internal interfaces are subject to change
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
 * or deletion without notice.</b></p>
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
 * @author Peter von der Ah&eacute;
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
 * @author Jonathan Gibbons
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
public class JavacTaskImpl extends JavacTask {
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    private JavacTool tool;
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    private Main compilerMain;
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    private JavaCompiler compiler;
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1355
diff changeset
    71
    private Locale locale;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    private String[] args;
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    private Context context;
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    private List<JavaFileObject> fileObjects;
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    private Map<JavaFileObject, JCCompilationUnit> notYetEntered;
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    private ListBuffer<Env<AttrContext>> genList;
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    private TaskListener taskListener;
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
    private AtomicBoolean used = new AtomicBoolean();
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    private Iterable<? extends Processor> processors;
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    private Integer result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    JavacTaskImpl(JavacTool tool,
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
                Main compilerMain,
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
                String[] args,
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
                Context context,
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
                List<JavaFileObject> fileObjects) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        this.tool = tool;
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        this.compilerMain = compilerMain;
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        this.args = args;
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
        this.context = context;
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
        this.fileObjects = fileObjects;
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1355
diff changeset
    93
        setLocale(Locale.getDefault());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        // null checks
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        compilerMain.getClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        args.getClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        context.getClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        fileObjects.getClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
    JavacTaskImpl(JavacTool tool,
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
                Main compilerMain,
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
                Iterable<String> flags,
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
                Context context,
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
                Iterable<String> classes,
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
                Iterable<? extends JavaFileObject> fileObjects) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        this(tool, compilerMain, toArray(flags, classes), context, toList(fileObjects));
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
    static private String[] toArray(Iterable<String> flags, Iterable<String> classes) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        ListBuffer<String> result = new ListBuffer<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
        if (flags != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
            for (String flag : flags)
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
                result.append(flag);
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        if (classes != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
            for (String cls : classes)
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
                result.append(cls);
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
        return result.toArray(new String[result.length()]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    static private List<JavaFileObject> toList(Iterable<? extends JavaFileObject> fileObjects) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
        if (fileObjects == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
            return List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
        ListBuffer<JavaFileObject> result = new ListBuffer<JavaFileObject>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
        for (JavaFileObject fo : fileObjects)
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
            result.append(fo);
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        return result.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
    public Boolean call() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        if (!used.getAndSet(true)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
            beginContext();
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1471
diff changeset
   133
            notYetEntered = new HashMap<JavaFileObject, JCCompilationUnit>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
                compilerMain.setFatalErrors(true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
                result = compilerMain.compile(args, context, fileObjects, processors);
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
            } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
                endContext();
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
            compilerMain = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
            args = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
            context = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
            fileObjects = null;
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1471
diff changeset
   144
            notYetEntered = null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
            return result == 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
            throw new IllegalStateException("multiple calls to method 'call'");
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
    public void setProcessors(Iterable<? extends Processor> processors) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        processors.getClass(); // null check
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        // not mt-safe
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
        if (used.get())
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
            throw new IllegalStateException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
        this.processors = processors;
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
    public void setLocale(Locale locale) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
        if (used.get())
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
            throw new IllegalStateException();
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1355
diff changeset
   162
        this.locale = locale;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
    private void prepareCompiler() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
        if (!used.getAndSet(true)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
            beginContext();
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
            compilerMain.setOptions(Options.instance(context));
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
            compilerMain.filenames = new ListBuffer<File>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
            List<File> filenames = compilerMain.processArgs(CommandLine.parse(args));
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
            if (!filenames.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
                throw new IllegalArgumentException("Malformed arguments " + filenames.toString(" "));
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
            compiler = JavaCompiler.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
            compiler.keepComments = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
            compiler.genEndPos = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
            // NOTE: this value will be updated after annotation processing
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
            compiler.initProcessAnnotations(processors);
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
            notYetEntered = new HashMap<JavaFileObject, JCCompilationUnit>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
            for (JavaFileObject file: fileObjects)
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
                notYetEntered.put(file, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
            genList = new ListBuffer<Env<AttrContext>>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
            // endContext will be called when all classes have been generated
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
            // TODO: should handle the case after each phase if errors have occurred
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
            args = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
    private void beginContext() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
        context.put(JavacTaskImpl.class, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
        if (context.get(TaskListener.class) != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
            context.put(TaskListener.class, (TaskListener)null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
        if (taskListener != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
            context.put(TaskListener.class, wrap(taskListener));
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
        tool.beginContext(context);
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1355
diff changeset
   195
        //initialize compiler's default locale
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1355
diff changeset
   196
        JavacMessages.instance(context).setCurrentLocale(locale);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
    private TaskListener wrap(final TaskListener tl) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
        tl.getClass(); // null check
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
        return new TaskListener() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
            public void started(TaskEvent e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
                try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
                    tl.started(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
                } catch (Throwable t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
                    throw new ClientCodeException(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
            public void finished(TaskEvent e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
                try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
                    tl.finished(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
                } catch (Throwable t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
                    throw new ClientCodeException(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
    private void endContext() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
        tool.endContext();
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
     * Construct a JavaFileObject from the given file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
     * <p><b>TODO: this method is useless here</b></p>
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
     * @param file a file
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
     * @return a JavaFileObject from the standard file manager.
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
    public JavaFileObject asJavaFileObject(File file) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
        JavacFileManager fm = (JavacFileManager)context.get(JavaFileManager.class);
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
        return fm.getRegularFile(file);
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
    public void setTaskListener(TaskListener taskListener) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
        this.taskListener = taskListener;
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
     * Parse the specified files returning a list of abstract syntax trees.
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
     * @throws java.io.IOException TODO
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
     * @return a list of abstract syntax trees
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
    public Iterable<? extends CompilationUnitTree> parse() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
            prepareCompiler();
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
            List<JCCompilationUnit> units = compiler.parseFiles(fileObjects);
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
            for (JCCompilationUnit unit: units) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
                JavaFileObject file = unit.getSourceFile();
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
                if (notYetEntered.containsKey(file))
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
                    notYetEntered.put(file, unit);
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
            return units;
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
            parsed = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
            if (compiler != null && compiler.log != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
                compiler.log.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
    private boolean parsed = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
     * Translate all the abstract syntax trees to elements.
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
     * @throws IOException TODO
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
     * @return a list of elements corresponding to the top level
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
     * classes in the abstract syntax trees
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
    public Iterable<? extends TypeElement> enter() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
        return enter(null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
     * Translate the given abstract syntax trees to elements.
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
     * @param trees a list of abstract syntax trees.
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
     * @throws java.io.IOException TODO
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
     * @return a list of elements corresponding to the top level
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
     * classes in the abstract syntax trees
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
    public Iterable<? extends TypeElement> enter(Iterable<? extends CompilationUnitTree> trees)
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
        throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
        prepareCompiler();
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
        ListBuffer<JCCompilationUnit> roots = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
        if (trees == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
            // If there are still files which were specified to be compiled
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
            // (i.e. in fileObjects) but which have not yet been entered,
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
            // then we make sure they have been parsed and add them to the
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
            // list to be entered.
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
            if (notYetEntered.size() > 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
                if (!parsed)
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
                    parse(); // TODO would be nice to specify files needed to be parsed
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
                for (JavaFileObject file: fileObjects) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
                    JCCompilationUnit unit = notYetEntered.remove(file);
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
                    if (unit != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
                        if (roots == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
                            roots = new ListBuffer<JCCompilationUnit>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
                        roots.append(unit);
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
                notYetEntered.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
        else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
            for (CompilationUnitTree cu : trees) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
                if (cu instanceof JCCompilationUnit) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
                    if (roots == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
                        roots = new ListBuffer<JCCompilationUnit>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
                    roots.append((JCCompilationUnit)cu);
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
                    notYetEntered.remove(cu.getSourceFile());
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
                    throw new IllegalArgumentException(cu.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
        if (roots == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
            return List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
            List<JCCompilationUnit> units = compiler.enterTrees(roots.toList());
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
            if (notYetEntered.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
                compiler = compiler.processAnnotations(units);
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
            ListBuffer<TypeElement> elements = new ListBuffer<TypeElement>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
            for (JCCompilationUnit unit : units) {
6716
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 5848
diff changeset
   337
                for (JCTree node : unit.defs) {
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 5848
diff changeset
   338
                    if (node.getTag() == JCTree.CLASSDEF) {
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 5848
diff changeset
   339
                        JCClassDecl cdef = (JCClassDecl) node;
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 5848
diff changeset
   340
                        if (cdef.sym != null) // maybe null if errors in anno processing
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 5848
diff changeset
   341
                            elements.append(cdef.sym);
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 5848
diff changeset
   342
                    }
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 5848
diff changeset
   343
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
            return elements.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
        finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
            compiler.log.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
     * Complete all analysis.
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
     * @throws IOException TODO
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
    @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
    public Iterable<? extends Element> analyze() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
        return analyze(null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
     * Complete all analysis on the given classes.
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
     * This can be used to ensure that all compile time errors are reported.
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
     * The classes must have previously been returned from {@link #enter}.
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
     * If null is specified, all outstanding classes will be analyzed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
     * @param classes a list of class elements
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
    // This implementation requires that we open up privileges on JavaCompiler.
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
    // An alternative implementation would be to move this code to JavaCompiler and
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
    // wrap it here
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
    public Iterable<? extends Element> analyze(Iterable<? extends TypeElement> classes) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
        enter(null);  // ensure all classes have been entered
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
        final ListBuffer<Element> results = new ListBuffer<Element>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
            if (classes == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
                handleFlowResults(compiler.flow(compiler.attribute(compiler.todo)), results);
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
                Filter f = new Filter() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
                    public void process(Env<AttrContext> env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
                        handleFlowResults(compiler.flow(compiler.attribute(env)), results);
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
                };
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
                f.run(compiler.todo, classes);
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
            compiler.log.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
        return results;
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
    // where
864
b1cf6afb8244 6724551: Use Queues instead of Lists to link compiler phases
jjg
parents: 735
diff changeset
   393
        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
   394
            for (Env<AttrContext> env: queue) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
                switch (env.tree.getTag()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
                    case JCTree.CLASSDEF:
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
                        JCClassDecl cdef = (JCClassDecl) env.tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
                        if (cdef.sym != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
                            elems.append(cdef.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
                        break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
                    case JCTree.TOPLEVEL:
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
                        JCCompilationUnit unit = (JCCompilationUnit) env.tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
                        if (unit.packge != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
                            elems.append(unit.packge);
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
                        break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
            }
864
b1cf6afb8244 6724551: Use Queues instead of Lists to link compiler phases
jjg
parents: 735
diff changeset
   408
            genList.addAll(queue);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
     * Generate code.
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
     * @throws IOException TODO
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
    @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
    public Iterable<? extends JavaFileObject> generate() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
        return generate(null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
     * Generate code corresponding to the given classes.
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
     * The classes must have previously been returned from {@link #enter}.
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
     * If there are classes outstanding to be analyzed, that will be done before
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
     * any classes are generated.
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
     * If null is specified, code will be generated for all outstanding classes.
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
     * @param classes a list of class elements
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
    public Iterable<? extends JavaFileObject> generate(Iterable<? extends TypeElement> classes) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
        final ListBuffer<JavaFileObject> results = new ListBuffer<JavaFileObject>();
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() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
                        public void process(Env<AttrContext> env) {
864
b1cf6afb8244 6724551: Use Queues instead of Lists to link compiler phases
jjg
parents: 735
diff changeset
   442
                            compiler.generate(compiler.desugar(ListBuffer.of(env)), results);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
                    };
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
                f.run(genList, classes);
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
            if (genList.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
                compiler.reportDeferredDiagnostics();
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
                compiler.log.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
                endContext();
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 {
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
            compiler.log.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
        return results;
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
    public TypeMirror getTypeMirror(Iterable<? extends Tree> path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
        // TODO: Should complete attribution if necessary
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
        Tree last = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
        for (Tree node : path)
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
            last = node;
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
        return ((JCTree)last).type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
    public JavacElements getElements() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
        if (context == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
            throw new IllegalStateException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
        return JavacElements.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
    public JavacTypes getTypes() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
        if (context == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
            throw new IllegalStateException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
        return JavacTypes.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
    public Iterable<? extends Tree> pathFor(CompilationUnitTree unit, Tree node) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
        return TreeInfo.pathFor((JCTree) node, (JCTree.JCCompilationUnit) unit).reverse();
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
    abstract class Filter {
1355
74cc97efef51 6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents: 1258
diff changeset
   484
        void run(Queue<Env<AttrContext>> list, Iterable<? extends TypeElement> classes) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
            Set<TypeElement> set = new HashSet<TypeElement>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
            for (TypeElement item: classes)
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
                set.add(item);
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
1355
74cc97efef51 6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents: 1258
diff changeset
   489
            ListBuffer<Env<AttrContext>> defer = ListBuffer.<Env<AttrContext>>lb();
74cc97efef51 6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents: 1258
diff changeset
   490
            while (list.peek() != null) {
74cc97efef51 6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents: 1258
diff changeset
   491
                Env<AttrContext> env = list.remove();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
                ClassSymbol csym = env.enclClass.sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
                if (csym != null && set.contains(csym.outermostClass()))
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
                    process(env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
                else
1355
74cc97efef51 6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents: 1258
diff changeset
   496
                    defer = defer.append(env);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
1355
74cc97efef51 6420151: need to improve byfile compile policy to eliminate footprint issues
jjg
parents: 1258
diff changeset
   499
            list.addAll(defer);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
        abstract void process(Env<AttrContext> env);
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
    /**
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
   506
     * For internal use only.  This method will be
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
     * removed without warning.
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
    public Context getContext() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
        return context;
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
    /**
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
   514
     * For internal use only.  This method will be
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
     * removed without warning.
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
    public void updateContext(Context newContext) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
        context = newContext;
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
    /**
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
   522
     * For internal use only.  This method will be
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
     * removed without warning.
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
    public Type parseType(String expr, TypeElement scope) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
        if (expr == null || expr.equals(""))
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
            throw new IllegalArgumentException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
        compiler = JavaCompiler.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
        JavaFileObject prev = compiler.log.useSource(null);
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 864
diff changeset
   530
        ParserFactory parserFactory = ParserFactory.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
        Attr attr = Attr.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
        try {
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 864
diff changeset
   533
            CharBuffer buf = CharBuffer.wrap((expr+"\u0000").toCharArray(), 0, expr.length());
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 864
diff changeset
   534
            Parser parser = parserFactory.newParser(buf, false, false, false);
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 864
diff changeset
   535
            JCTree tree = parser.parseType();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
            return attr.attribType(tree, (Symbol.TypeSymbol)scope);
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
            compiler.log.useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
}