langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/code/ModuleFinder.java
author mcimadamore
Mon, 05 Dec 2016 19:00:56 +0000
changeset 42416 1cfad0990b99
parent 42261 bb52b5514ad5
child 43772 4e5350b7be75
permissions -rw-r--r--
8170410: inference: javac doesn't implement 18.2.5 correctly Summary: javac does not generate constraints of the kind 'throws alpha' as described in the spec Reviewed-by: vromero, dlsmith
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    23
 * questions.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    24
 */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    25
package com.sun.tools.javac.code;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    26
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    27
import java.io.IOException;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.util.Arrays;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.util.HashMap;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.util.Iterator;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.util.Map;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.util.NoSuchElementException;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.util.Set;
41254
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    34
import java.util.function.Function;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    35
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import javax.tools.JavaFileManager;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import javax.tools.JavaFileManager.Location;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import javax.tools.JavaFileObject;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import javax.tools.JavaFileObject.Kind;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import javax.tools.StandardLocation;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    41
41254
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    42
import com.sun.tools.javac.code.Symbol.Completer;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import com.sun.tools.javac.code.Symbol.CompletionFailure;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import com.sun.tools.javac.code.Symbol.ModuleSymbol;
41254
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    45
import com.sun.tools.javac.jvm.ModuleNameReader;
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    46
import com.sun.tools.javac.jvm.ModuleNameReader.BadClassFile;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import com.sun.tools.javac.resources.CompilerProperties.Errors;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import com.sun.tools.javac.resources.CompilerProperties.Fragments;
41254
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    49
import com.sun.tools.javac.util.Assert;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    50
import com.sun.tools.javac.util.Context;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    51
import com.sun.tools.javac.util.JCDiagnostic;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    52
import com.sun.tools.javac.util.JCDiagnostic.Fragment;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    53
import com.sun.tools.javac.util.List;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    54
import com.sun.tools.javac.util.ListBuffer;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    55
import com.sun.tools.javac.util.Log;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    56
import com.sun.tools.javac.util.Name;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    57
import com.sun.tools.javac.util.Names;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    58
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    59
import static com.sun.tools.javac.code.Kinds.Kind.*;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    60
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    61
/**
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    62
 *  This class provides operations to locate module definitions
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    63
 *  from the source and class files on the paths provided to javac.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    64
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    65
 *  <p><b>This is NOT part of any supported API.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    66
 *  If you write code that depends on this, you do so at your own risk.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    67
 *  This code and its internal interfaces are subject to change or
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    68
 *  deletion without notice.</b>
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    69
 */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    70
public class ModuleFinder {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    71
    /** The context key for the module finder. */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    72
    protected static final Context.Key<ModuleFinder> moduleFinderKey = new Context.Key<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    73
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    74
    /** The log to use for verbose output. */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    75
    private final Log log;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    76
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    77
    /** The symbol table. */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    78
    private final Symtab syms;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    79
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    80
    /** The name table. */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    81
    private final Names names;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    82
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    83
    private final ClassFinder classFinder;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    84
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    85
    /** Access to files
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    86
     */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    87
    private final JavaFileManager fileManager;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    88
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    89
    private final JCDiagnostic.Factory diags;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    90
41254
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    91
    private ModuleNameReader moduleNameReader;
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    92
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    93
    public ModuleInfoSourceFileCompleter sourceFileCompleter;
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
    94
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    95
    /** Get the ModuleFinder instance for this invocation. */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    96
    public static ModuleFinder instance(Context context) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    97
        ModuleFinder instance = context.get(moduleFinderKey);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    98
        if (instance == null)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    99
            instance = new ModuleFinder(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   100
        return instance;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   101
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   102
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   103
    /** Construct a new module finder. */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   104
    protected ModuleFinder(Context context) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   105
        context.put(moduleFinderKey, this);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   106
        names = Names.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   107
        syms = Symtab.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   108
        fileManager = context.get(JavaFileManager.class);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   109
        log = Log.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   110
        classFinder = ClassFinder.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   111
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   112
        diags = JCDiagnostic.Factory.instance(context);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   113
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   114
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   115
    class ModuleLocationIterator implements Iterator<Set<Location>> {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   116
        StandardLocation outer;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   117
        Set<Location> next = null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   118
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   119
        Iterator<StandardLocation> outerIter = Arrays.asList(
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   120
                StandardLocation.MODULE_SOURCE_PATH,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   121
                StandardLocation.UPGRADE_MODULE_PATH,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   122
                StandardLocation.SYSTEM_MODULES,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   123
                StandardLocation.MODULE_PATH
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   124
        ).iterator();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   125
        Iterator<Set<Location>> innerIter = null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   126
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   127
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   128
        public boolean hasNext() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   129
            while (next == null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   130
                while (innerIter == null || !innerIter.hasNext()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   131
                    if (outerIter.hasNext()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   132
                        outer = outerIter.next();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   133
                        try {
42261
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 41254
diff changeset
   134
                            innerIter = fileManager.listLocationsForModules(outer).iterator();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   135
                        } catch (IOException e) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   136
                            System.err.println("error listing module locations for " + outer + ": " + e);  // FIXME
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   137
                        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   138
                    } else
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   139
                        return false;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   140
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   141
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   142
                if (innerIter.hasNext())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   143
                    next = innerIter.next();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   144
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   145
            return true;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   146
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   147
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   148
        @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   149
        public Set<Location> next() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   150
            hasNext();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   151
            if (next != null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   152
                Set<Location> result = next;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   153
                next = null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   154
                return result;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   155
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   156
            throw new NoSuchElementException();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   157
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   158
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   159
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   160
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   161
    ModuleLocationIterator moduleLocationIterator = new ModuleLocationIterator();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   162
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   163
    public ModuleSymbol findModule(Name name) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   164
        return findModule(syms.enterModule(name));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   165
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   166
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   167
    public ModuleSymbol findModule(ModuleSymbol msym) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   168
        if (msym.kind != ERR && msym.sourceLocation == null && msym.classLocation == null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   169
            // fill in location
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   170
            List<ModuleSymbol> list = scanModulePath(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   171
            if (list.isEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   172
                msym.kind = ERR;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   173
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   174
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   175
        if (msym.kind != ERR && msym.module_info.sourcefile == null && msym.module_info.classfile == null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   176
            // fill in module-info
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   177
            findModuleInfo(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   178
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   179
        return msym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   180
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   181
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   182
    public List<ModuleSymbol> findAllModules() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   183
        List<ModuleSymbol> list = scanModulePath(null);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   184
        for (ModuleSymbol msym: list) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   185
            if (msym.kind != ERR && msym.module_info.sourcefile == null && msym.module_info.classfile == null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   186
                // fill in module-info
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   187
                findModuleInfo(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   188
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   189
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   190
        return list;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   191
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   192
41254
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   193
    private boolean inFindSingleModule;
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   194
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   195
    public ModuleSymbol findSingleModule() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   196
        try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   197
            JavaFileObject src_fo = getModuleInfoFromLocation(StandardLocation.SOURCE_PATH, Kind.SOURCE);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   198
            JavaFileObject class_fo = getModuleInfoFromLocation(StandardLocation.CLASS_OUTPUT, Kind.CLASS);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   199
            JavaFileObject fo = (src_fo == null) ? class_fo
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   200
                    : (class_fo == null) ? src_fo
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   201
                            : classFinder.preferredFileObject(src_fo, class_fo);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   202
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   203
            ModuleSymbol msym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   204
            if (fo == null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   205
                msym = syms.unnamedModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   206
            } else {
41254
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   207
                switch (fo.getKind()) {
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   208
                    case SOURCE:
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   209
                        if (!inFindSingleModule) {
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   210
                            try {
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   211
                                inFindSingleModule = true;
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   212
                                // Note: the following will trigger a re-entrant call to Modules.enter
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   213
                                msym = sourceFileCompleter.complete(fo);
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   214
                                msym.module_info.classfile = fo;
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   215
                            } finally {
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   216
                                inFindSingleModule = false;
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   217
                            }
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   218
                        } else {
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   219
                            //the module-info.java does not contain a module declaration,
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   220
                            //avoid infinite recursion:
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   221
                            msym = syms.unnamedModule;
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   222
                        }
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   223
                        break;
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   224
                    case CLASS:
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   225
                        Name name;
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   226
                        try {
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   227
                            name = names.fromString(readModuleName(fo));
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   228
                        } catch (BadClassFile | IOException ex) {
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   229
                            //fillIn will report proper errors:
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   230
                            name = names.error;
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   231
                        }
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   232
                        msym = syms.enterModule(name);
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   233
                        msym.module_info.classfile = fo;
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   234
                        msym.completer = Completer.NULL_COMPLETER;
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   235
                        classFinder.fillIn(msym.module_info);
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   236
                        break;
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   237
                    default:
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   238
                        Assert.error();
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   239
                        msym = syms.unnamedModule;
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   240
                        break;
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   241
                }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   242
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   243
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   244
            msym.classLocation = StandardLocation.CLASS_OUTPUT;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   245
            return msym;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   246
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   247
        } catch (IOException e) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   248
            throw new Error(e); // FIXME
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   249
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   250
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   251
41254
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   252
    private String readModuleName(JavaFileObject jfo) throws IOException, ModuleNameReader.BadClassFile {
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   253
        if (moduleNameReader == null)
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   254
            moduleNameReader = new ModuleNameReader();
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   255
        return moduleNameReader.readModuleName(jfo);
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   256
    }
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   257
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   258
    private JavaFileObject getModuleInfoFromLocation(Location location, Kind kind) throws IOException {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   259
        if (!fileManager.hasLocation(location))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   260
            return null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   261
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   262
        return fileManager.getJavaFileForInput(location,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   263
                                               names.module_info.toString(),
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   264
                                               kind);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   265
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   266
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   267
    private List<ModuleSymbol> scanModulePath(ModuleSymbol toFind) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   268
        ListBuffer<ModuleSymbol> results = new ListBuffer<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   269
        Map<Name, Location> namesInSet = new HashMap<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   270
        while (moduleLocationIterator.hasNext()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   271
            Set<Location> locns = (moduleLocationIterator.next());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   272
            namesInSet.clear();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   273
            for (Location l: locns) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   274
                try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   275
                    Name n = names.fromString(fileManager.inferModuleName(l));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   276
                    if (namesInSet.put(n, l) == null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   277
                        ModuleSymbol msym = syms.enterModule(n);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   278
                        if (msym.sourceLocation != null || msym.classLocation != null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   279
                            // module has already been found, so ignore this instance
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   280
                            continue;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   281
                        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   282
                        if (moduleLocationIterator.outer == StandardLocation.MODULE_SOURCE_PATH) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   283
                            msym.sourceLocation = l;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   284
                            if (fileManager.hasLocation(StandardLocation.CLASS_OUTPUT)) {
42261
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 41254
diff changeset
   285
                                msym.classLocation = fileManager.getLocationForModule(StandardLocation.CLASS_OUTPUT, msym.name.toString());
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   286
                            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   287
                        } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   288
                            msym.classLocation = l;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   289
                        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   290
                        if (moduleLocationIterator.outer == StandardLocation.SYSTEM_MODULES ||
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   291
                            moduleLocationIterator.outer == StandardLocation.UPGRADE_MODULE_PATH) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   292
                            msym.flags_field |= Flags.SYSTEM_MODULE;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   293
                        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   294
                        if (toFind == msym || toFind == null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   295
                            // Note: cannot return msym directly, because we must finish
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   296
                            // processing this set first
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   297
                            results.add(msym);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   298
                        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   299
                    } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   300
                        log.error(Errors.DuplicateModuleOnPath(
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   301
                                getDescription(moduleLocationIterator.outer), n));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   302
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   303
                } catch (IOException e) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   304
                    // skip location for now?  log error?
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   305
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   306
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   307
            if (toFind != null && results.nonEmpty())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   308
                return results.toList();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   309
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   310
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   311
        return results.toList();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   312
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   313
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   314
    private void findModuleInfo(ModuleSymbol msym) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   315
        try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   316
            JavaFileObject src_fo = (msym.sourceLocation == null) ? null
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   317
                    : fileManager.getJavaFileForInput(msym.sourceLocation,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   318
                            names.module_info.toString(), Kind.SOURCE);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   319
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   320
            JavaFileObject class_fo = (msym.classLocation == null) ? null
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   321
                    : fileManager.getJavaFileForInput(msym.classLocation,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   322
                            names.module_info.toString(), Kind.CLASS);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   323
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   324
            JavaFileObject fo = (src_fo == null) ? class_fo :
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   325
                    (class_fo == null) ? src_fo :
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   326
                    classFinder.preferredFileObject(src_fo, class_fo);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   327
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   328
            if (fo == null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   329
                String moduleName = msym.sourceLocation == null && msym.classLocation != null ?
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   330
                    fileManager.inferModuleName(msym.classLocation) : null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   331
                if (moduleName != null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   332
                    msym.module_info.classfile = null;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   333
                    msym.flags_field |= Flags.AUTOMATIC_MODULE;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   334
                } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   335
                    msym.kind = ERR;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   336
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   337
            } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   338
                msym.module_info.classfile = fo;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   339
                msym.module_info.completer = new Symbol.Completer() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   340
                    @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   341
                    public void complete(Symbol sym) throws CompletionFailure {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   342
                        classFinder.fillIn(msym.module_info);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   343
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   344
                    @Override
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   345
                    public String toString() {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   346
                        return "ModuleInfoCompleter";
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   347
                    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   348
                };
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   349
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   350
        } catch (IOException e) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   351
            msym.kind = ERR;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   352
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   353
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   354
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   355
    Fragment getDescription(StandardLocation l) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   356
        switch (l) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   357
            case MODULE_PATH: return Fragments.LocnModule_path;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   358
            case MODULE_SOURCE_PATH: return Fragments.LocnModule_source_path;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   359
            case SYSTEM_MODULES: return Fragments.LocnSystem_modules;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   360
            case UPGRADE_MODULE_PATH: return Fragments.LocnUpgrade_module_path;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   361
            default:
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   362
                throw new AssertionError();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   363
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   364
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   365
41254
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   366
    public interface ModuleInfoSourceFileCompleter {
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   367
        public ModuleSymbol complete(JavaFileObject file);
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   368
    }
08f8dbf7741e 8152911: javac assertion error when compiling overlay sources
jlahoda
parents: 36526
diff changeset
   369
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   370
}