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