langtools/test/tools/javac/classfiles/attributes/Module/ModuleTestBase.java
author anazarov
Wed, 18 Jan 2017 21:09:19 +0300
changeset 43259 1a8c1621084d
parent 42822 a84956e7ee4d
permissions -rw-r--r--
8170250: update/improve testing of classfile module attribute Reviewed-by: jjg, jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
     2
 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
36526
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.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * questions.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    23
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    24
import com.sun.tools.classfile.ClassFile;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    25
import com.sun.tools.classfile.ConstantPool;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    26
import com.sun.tools.classfile.ConstantPoolException;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    27
import com.sun.tools.classfile.Module_attribute;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    28
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.io.IOException;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.lang.annotation.Retention;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.lang.annotation.RetentionPolicy;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.lang.reflect.Method;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.nio.file.Files;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.nio.file.Path;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.nio.file.Paths;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.util.ArrayList;
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 36778
diff changeset
    37
import java.util.Arrays;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.util.HashMap;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
    39
import java.util.LinkedHashMap;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.util.List;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.util.Map;
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
    42
import java.util.Objects;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.util.regex.Pattern;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.util.stream.Collectors;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    45
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    46
import toolbox.JavacTask;
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    47
import toolbox.Task;
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    48
import toolbox.ToolBox;
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
    49
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    50
public class ModuleTestBase {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    51
    protected final ToolBox tb = new ToolBox();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    52
    private final TestResult tr = new TestResult();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    53
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    54
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    55
    protected void run() throws Exception {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    56
        boolean noTests = true;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    57
        for (Method method : this.getClass().getMethods()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    58
            if (method.isAnnotationPresent(Test.class)) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    59
                noTests = false;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    60
                try {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    61
                    tr.addTestCase(method.getName());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    62
                    method.invoke(this, Paths.get(method.getName()));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    63
                } catch (Throwable th) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    64
                    tr.addFailure(th);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    65
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    66
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    67
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    68
        if (noTests) throw new AssertionError("Tests are not found.");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    69
        tr.checkStatus();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    70
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    71
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    72
    protected void testModuleAttribute(Path modulePath, ModuleDescriptor moduleDescriptor) throws Exception {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    73
        ClassFile classFile = ClassFile.read(modulePath.resolve("module-info.class"));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    74
        Module_attribute moduleAttribute = (Module_attribute) classFile.getAttribute("Module");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    75
        ConstantPool constantPool = classFile.constant_pool;
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
    76
        testModuleName(moduleDescriptor, moduleAttribute, constantPool);
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
    77
        testModuleFlags(moduleDescriptor, moduleAttribute);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    78
        testRequires(moduleDescriptor, moduleAttribute, constantPool);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    79
        testExports(moduleDescriptor, moduleAttribute, constantPool);
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
    80
        testOpens(moduleDescriptor, moduleAttribute, constantPool);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    81
        testProvides(moduleDescriptor, moduleAttribute, constantPool);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    82
        testUses(moduleDescriptor, moduleAttribute, constantPool);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    83
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    84
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
    85
    private void testModuleName(ModuleDescriptor moduleDescriptor, Module_attribute module, ConstantPool constantPool) throws ConstantPoolException {
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
    86
        tr.checkEquals(constantPool.getModuleInfo(module.module_name).getName(), moduleDescriptor.name, "Unexpected module name");
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
    87
    }
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
    88
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
    89
    private void testModuleFlags(ModuleDescriptor moduleDescriptor, Module_attribute module) {
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
    90
        tr.checkEquals(module.module_flags, moduleDescriptor.flags, "Unexpected module flags");
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
    91
    }
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
    92
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    93
    private void testRequires(ModuleDescriptor moduleDescriptor, Module_attribute module, ConstantPool constantPool) throws ConstantPoolException {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    94
        tr.checkEquals(module.requires_count, moduleDescriptor.requires.size(), "Wrong amount of requires.");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    95
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
    96
        List<Requires> actualRequires = new ArrayList<>();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    97
        for (Module_attribute.RequiresEntry require : module.requires) {
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
    98
            actualRequires.add(new Requires(
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
    99
                    require.getRequires(constantPool),
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   100
                    require.requires_flags));
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   101
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   102
        tr.checkContains(actualRequires, moduleDescriptor.requires, "Lists of requires don't match");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   103
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   104
42822
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42407
diff changeset
   105
    private void testExports(ModuleDescriptor moduleDescriptor, Module_attribute module, ConstantPool constantPool) throws ConstantPoolException {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   106
        tr.checkEquals(module.exports_count, moduleDescriptor.exports.size(), "Wrong amount of exports.");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   107
        for (Module_attribute.ExportsEntry export : module.exports) {
42822
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42407
diff changeset
   108
            String pkg = constantPool.getPackageInfo(export.exports_index).getName();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   109
            if (tr.checkTrue(moduleDescriptor.exports.containsKey(pkg), "Unexpected export " + pkg)) {
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   110
                Export expectedExport = moduleDescriptor.exports.get(pkg);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   111
                tr.checkEquals(expectedExport.mask, export.exports_flags, "Wrong export flags");
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   112
                List<String> expectedTo = expectedExport.to;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   113
                tr.checkEquals(export.exports_to_count, expectedTo.size(), "Wrong amount of exports to");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   114
                List<String> actualTo = new ArrayList<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   115
                for (int toIdx : export.exports_to_index) {
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   116
                    actualTo.add(constantPool.getModuleInfo(toIdx).getName());
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   117
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   118
                tr.checkContains(actualTo, expectedTo, "Lists of \"exports to\" don't match.");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   119
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   120
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   121
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   122
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   123
    private void testOpens(ModuleDescriptor moduleDescriptor, Module_attribute module, ConstantPool constantPool) throws ConstantPoolException {
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   124
        tr.checkEquals(module.opens_count, moduleDescriptor.opens.size(), "Wrong amount of opens.");
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   125
        for (Module_attribute.OpensEntry open : module.opens) {
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   126
            String pkg = constantPool.getPackageInfo(open.opens_index).getName();
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   127
            if (tr.checkTrue(moduleDescriptor.opens.containsKey(pkg), "Unexpected open " + pkg)) {
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   128
                Open expectedOpen = moduleDescriptor.opens.get(pkg);
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   129
                tr.checkEquals(expectedOpen.mask, open.opens_flags, "Wrong open flags");
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   130
                List<String> expectedTo = expectedOpen.to;
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   131
                tr.checkEquals(open.opens_to_count, expectedTo.size(), "Wrong amount of opens to");
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   132
                List<String> actualTo = new ArrayList<>();
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   133
                for (int toIdx : open.opens_to_index) {
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   134
                    actualTo.add(constantPool.getModuleInfo(toIdx).getName());
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   135
                }
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   136
                tr.checkContains(actualTo, expectedTo, "Lists of \"opens to\" don't match.");
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   137
            }
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   138
        }
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   139
    }
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   140
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   141
    private void testUses(ModuleDescriptor moduleDescriptor, Module_attribute module, ConstantPool constantPool) throws ConstantPoolException {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   142
        tr.checkEquals(module.uses_count, moduleDescriptor.uses.size(), "Wrong amount of uses.");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   143
        List<String> actualUses = new ArrayList<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   144
        for (int usesIdx : module.uses_index) {
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   145
            String uses = constantPool.getClassInfo(usesIdx).getBaseName();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   146
            actualUses.add(uses);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   147
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   148
        tr.checkContains(actualUses, moduleDescriptor.uses, "Lists of uses don't match");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   149
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   150
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   151
    private void testProvides(ModuleDescriptor moduleDescriptor, Module_attribute module, ConstantPool constantPool) throws ConstantPoolException {
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   152
        int moduleProvidesCount = Arrays.asList(module.provides).stream()
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   153
                .mapToInt(e -> e.with_index.length)
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   154
                .sum();
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   155
        int moduleDescriptorProvidesCount = moduleDescriptor.provides.values().stream()
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   156
                .mapToInt(impls -> impls.size())
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   157
                .sum();
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   158
        tr.checkEquals(moduleProvidesCount, moduleDescriptorProvidesCount, "Wrong amount of provides.");
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   159
        Map<String, List<String>> actualProvides = new HashMap<>();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   160
        for (Module_attribute.ProvidesEntry provide : module.provides) {
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   161
            String provides = constantPool.getClassInfo(provide.provides_index).getBaseName();
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   162
            List<String> impls = new ArrayList<>();
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   163
            for (int i = 0; i < provide.with_count; i++) {
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   164
                String with = constantPool.getClassInfo(provide.with_index[i]).getBaseName();
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   165
                impls.add(with);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   166
            }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   167
            actualProvides.put(provides, impls);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   168
        }
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   169
        tr.checkContains(actualProvides.entrySet(), moduleDescriptor.provides.entrySet(), "Lists of provides don't match");
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   170
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   171
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 36778
diff changeset
   172
    protected void compile(Path base, String... options) throws IOException {
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
   173
        new JavacTask(tb)
40308
274367a99f98 8136930: Simplify use of module-system options by custom launchers
jjg
parents: 36778
diff changeset
   174
                .options(options)
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   175
                .files(findJavaFiles(base))
36778
e04318f39f92 8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents: 36526
diff changeset
   176
                .run(Task.Expect.SUCCESS)
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   177
                .writeAll();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   178
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   179
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   180
    private static Path[] findJavaFiles(Path src) throws IOException {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   181
        return Files.find(src, Integer.MAX_VALUE, (path, attr) -> path.toString().endsWith(".java"))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   182
                .toArray(Path[]::new);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   183
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   184
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   185
    @Retention(RetentionPolicy.RUNTIME)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   186
    @interface Test {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   187
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   188
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   189
    interface Mask {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   190
        int getMask();
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   191
    }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   192
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   193
    public enum ModuleFlag implements Mask {
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   194
        OPEN("open", Module_attribute.ACC_OPEN);
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   195
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   196
        private final String token;
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   197
        private final int mask;
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   198
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   199
        ModuleFlag(String token, int mask) {
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   200
            this.token = token;
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   201
            this.mask = mask;
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   202
        }
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   203
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   204
        @Override
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   205
        public int getMask() {
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   206
            return mask;
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   207
        }
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   208
    }
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   209
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   210
    public enum RequiresFlag implements Mask {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   211
        TRANSITIVE("transitive", Module_attribute.ACC_TRANSITIVE),
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   212
        STATIC("static", Module_attribute.ACC_STATIC_PHASE),
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   213
        MANDATED("", Module_attribute.ACC_MANDATED);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   214
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   215
        private final String token;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   216
        private final int mask;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   217
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   218
        RequiresFlag(String token, int mask) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   219
            this.token = token;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   220
            this.mask = mask;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   221
        }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   222
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   223
        @Override
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   224
        public int getMask() {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   225
            return mask;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   226
        }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   227
    }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   228
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   229
    public enum ExportsFlag implements Mask {
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   230
        SYNTHETIC("", Module_attribute.ACC_SYNTHETIC);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   231
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   232
        private final String token;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   233
        private final int mask;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   234
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   235
        ExportsFlag(String token, int mask) {
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   236
            this.token = token;
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   237
            this.mask = mask;
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   238
        }
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   239
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   240
        @Override
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   241
        public int getMask() {
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   242
            return mask;
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   243
        }
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   244
    }
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   245
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   246
    public enum OpensFlag implements Mask {
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   247
        SYNTHETIC("", Module_attribute.ACC_SYNTHETIC);
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   248
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   249
        private final String token;
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   250
        private final int mask;
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   251
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   252
        OpensFlag(String token, int mask) {
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   253
            this.token = token;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   254
            this.mask = mask;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   255
        }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   256
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   257
        @Override
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   258
        public int getMask() {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   259
            return mask;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   260
        }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   261
    }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   262
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   263
    private class Export {
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   264
        private final String pkg;
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   265
        private final int mask;
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   266
        private final List<String> to = new ArrayList<>();
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   267
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   268
        Export(String pkg, int mask) {
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   269
            this.pkg = pkg;
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   270
            this.mask = mask;
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   271
        }
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   272
    }
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   273
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   274
    private class Open {
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   275
        private final String pkg;
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   276
        private final int mask;
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   277
        private final List<String> to = new ArrayList<>();
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   278
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   279
        Open(String pkg, int mask) {
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   280
            this.pkg = pkg;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   281
            this.mask = mask;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   282
        }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   283
    }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   284
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   285
    private class Requires {
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   286
        private final String module;
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   287
        private final int mask;
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   288
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   289
        Requires(String module, int mask) {
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   290
            this.module = module;
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   291
            this.mask = mask;
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   292
        }
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   293
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   294
        @Override
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   295
        public boolean equals(Object o) {
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   296
            if (this == o) return true;
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   297
            if (o == null || getClass() != o.getClass()) return false;
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   298
            Requires requires = (Requires) o;
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   299
            return mask == requires.mask &&
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   300
                    Objects.equals(module, requires.module);
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   301
        }
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   302
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   303
        @Override
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   304
        public int hashCode() {
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   305
            return Objects.hash(module, mask);
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   306
        }
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   307
    }
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   308
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   309
    protected class ModuleDescriptor {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   310
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   311
        private final String name;
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   312
        private final int flags;
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   313
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   314
        private final List<Requires> requires = new ArrayList<>();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   315
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   316
        {
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   317
            requires.add(new Requires("java.base", computeMask(RequiresFlag.MANDATED)));
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   318
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   319
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   320
        private final Map<String, Export> exports = new HashMap<>();
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   321
        private final Map<String, Open> opens = new HashMap<>();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   322
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   323
        //List of service and implementation
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   324
        private final Map<String, List<String>> provides = new LinkedHashMap<>();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   325
        private final List<String> uses = new ArrayList<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   326
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   327
        private static final String LINE_END = ";\n";
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   328
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   329
        StringBuilder content = new StringBuilder("");
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   330
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   331
        public ModuleDescriptor(String moduleName, ModuleFlag... flags) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   332
            this.name = moduleName;
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   333
            this.flags = computeMask(flags);
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   334
            for (ModuleFlag flag : flags) {
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   335
                content.append(flag.token).append(" ");
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   336
            }
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   337
            content.append("module ").append(moduleName).append('{').append('\n');
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   338
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   339
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   340
        public ModuleDescriptor requires(String module) {
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   341
            this.requires.add(new Requires(module, 0));
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   342
            content.append("    requires ").append(module).append(LINE_END);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   343
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   344
            return this;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   345
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   346
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   347
        public ModuleDescriptor requires(String module, RequiresFlag... flags) {
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   348
            this.requires.add(new Requires(module, computeMask(flags)));
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   349
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   350
            content.append("    requires ");
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   351
            for (RequiresFlag flag : flags) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   352
                content.append(flag.token).append(" ");
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   353
            }
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   354
            content.append(module).append(LINE_END);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   355
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   356
            return this;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   357
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   358
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   359
        public ModuleDescriptor exports(String pkg, ExportsFlag... flags) {
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   360
            this.exports.put(toInternalForm(pkg), new Export(toInternalForm(pkg), computeMask(flags)));
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   361
            content.append("    exports ");
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   362
            for (ExportsFlag flag : flags) {
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   363
                content.append(flag.token).append(" ");
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   364
            }
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   365
            content.append(pkg).append(LINE_END);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   366
            return this;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   367
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   368
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   369
        public ModuleDescriptor exportsTo(String pkg, String to, ExportsFlag... flags) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   370
            List<String> tos = Pattern.compile(",")
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   371
                    .splitAsStream(to)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   372
                    .map(String::trim)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   373
                    .collect(Collectors.toList());
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   374
            this.exports.compute(toInternalForm(pkg), (k,v) -> new Export(k, computeMask(flags)))
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   375
                    .to.addAll(tos);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   376
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   377
            content.append("    exports ");
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   378
            for (ExportsFlag flag : flags) {
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   379
                content.append(flag.token).append(" ");
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   380
            }
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   381
            content.append(pkg).append(" to ").append(to).append(LINE_END);
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   382
            return this;
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   383
        }
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   384
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   385
        public ModuleDescriptor opens(String pkg, OpensFlag... flags) {
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   386
            this.opens.put(toInternalForm(pkg), new Open(toInternalForm(pkg), computeMask(flags)));
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   387
            content.append("    opens ");
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   388
            for (OpensFlag flag : flags) {
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   389
                content.append(flag.token).append(" ");
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   390
            }
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   391
            content.append(pkg).append(LINE_END);
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   392
            return this;
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   393
        }
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   394
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   395
        public ModuleDescriptor opensTo(String pkg, String to, OpensFlag... flags) {
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   396
            List<String> tos = Pattern.compile(",")
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   397
                    .splitAsStream(to)
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   398
                    .map(String::trim)
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   399
                    .collect(Collectors.toList());
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   400
            this.opens.compute(toInternalForm(pkg), (k,v) -> new Open(toInternalForm(k), computeMask(flags)))
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   401
                    .to.addAll(tos);
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   402
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   403
            content.append("    opens ");
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   404
            for (OpensFlag flag : flags) {
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   405
                content.append(flag.token).append(" ");
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   406
            }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   407
            content.append(pkg).append(" to ").append(to).append(LINE_END);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   408
            return this;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   409
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   410
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   411
        public ModuleDescriptor provides(String provides, String... with) {
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   412
            List<String> impls = Arrays.stream(with)
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   413
                    .map(this::toInternalForm)
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   414
                    .collect(Collectors.toList());
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   415
            this.provides.put(toInternalForm(provides), impls);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   416
            content.append("    provides ")
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   417
                    .append(provides)
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   418
                    .append(" with ")
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   419
                    .append(String.join(",", with))
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   420
                    .append(LINE_END);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   421
            return this;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   422
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   423
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   424
        public ModuleDescriptor uses(String... uses) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   425
            for (String use : uses) {
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   426
                this.uses.add(toInternalForm(use));
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   427
                content.append("    uses ").append(use).append(LINE_END);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   428
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   429
            return this;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   430
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   431
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   432
        public ModuleDescriptor write(Path path) throws IOException {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   433
            String src = content.append('}').toString();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   434
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   435
            tb.createDirectories(path);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   436
            tb.writeJavaFiles(path, src);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   437
            return this;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   438
        }
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   439
43259
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   440
        private String toInternalForm(String name) {
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   441
            return name.replace('.', '/');
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   442
        }
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   443
1a8c1621084d 8170250: update/improve testing of classfile module attribute
anazarov
parents: 42822
diff changeset
   444
        private int computeMask(Mask... masks) {
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   445
            return Arrays.stream(masks)
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   446
                    .map(Mask::getMask)
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   447
                    .reduce((a, b) -> a | b)
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   448
                    .orElseGet(() -> 0);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40308
diff changeset
   449
        }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   450
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   451
}