1 /******************************************************************************* 2 * Copyright (c) 2017 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 *******************************************************************************/ 14 package org.eclipse.jdt.internal.compiler.env; 15 16 import java.util.jar.Manifest; 17 18 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; 19 20 public interface IModule { 21 22 public static IModuleReference[] NO_MODULE_REFS = new IModuleReference[0]; 23 public static IPackageExport[] NO_EXPORTS = new IPackageExport[0]; 24 public static char[][] NO_USES = new char[0][]; 25 public static IService[] NO_PROVIDES = new IService[0]; 26 public static IModule[] NO_MODULES = new IModule[0]; 27 public static IPackageExport[] NO_OPENS = new IPackageExport[0]; 28 29 public String MODULE_INFO = "module-info"; //$NON-NLS-1$ 30 public String MODULE_INFO_JAVA = "module-info.java"; //$NON-NLS-1$ 31 public String MODULE_INFO_CLASS = "module-info.class"; //$NON-NLS-1$ 32 name()33 public char[] name(); 34 requires()35 public IModuleReference[] requires(); 36 exports()37 public IPackageExport[] exports(); 38 uses()39 public char[][] uses(); 40 provides()41 public IService[] provides(); 42 43 /* 44 * the opens package statement is very similar to package export statement, hence 45 * the same internal models are being used here. 46 */ opens()47 public IPackageExport[] opens(); 48 49 public interface IModuleReference { name()50 public char[] name(); isTransitive()51 public default boolean isTransitive() { 52 return (getModifiers() & ClassFileConstants.ACC_TRANSITIVE) != 0; 53 } getModifiers()54 public int getModifiers(); isStatic()55 public default boolean isStatic() { 56 return (getModifiers() & ClassFileConstants.ACC_STATIC_PHASE) != 0; 57 } 58 } 59 60 public interface IPackageExport { name()61 public char[] name(); targets()62 public char[][] targets(); isQualified()63 public default boolean isQualified() { 64 char[][] targets = targets(); 65 return targets != null && targets.length > 0; 66 } 67 } 68 69 public interface IService { name()70 public char[] name(); with()71 char[][] with(); 72 } 73 isAutomatic()74 public default boolean isAutomatic() { 75 return false; 76 } isAutoNameFromManifest()77 public default boolean isAutoNameFromManifest() { 78 return false; 79 } isOpen()80 public abstract boolean isOpen(); 81 82 createAutomatic(char[] moduleName, boolean fromManifest)83 public static IModule createAutomatic(char[] moduleName, boolean fromManifest) { 84 final class AutoModule implements IModule { 85 char[] name; 86 boolean nameFromManifest; 87 public AutoModule(char[] name, boolean nameFromManifest) { 88 this.name = name; 89 this.nameFromManifest = nameFromManifest; 90 } 91 @Override 92 public char[] name() { 93 return this.name; 94 } 95 96 @Override 97 public IModuleReference[] requires() { 98 return IModule.NO_MODULE_REFS; 99 } 100 101 @Override 102 public IPackageExport[] exports() { 103 return IModule.NO_EXPORTS; 104 } 105 106 @Override 107 public char[][] uses() { 108 return IModule.NO_USES; 109 } 110 111 @Override 112 public IService[] provides() { 113 return IModule.NO_PROVIDES; 114 } 115 116 @Override 117 public IPackageExport[] opens() { 118 return NO_OPENS; 119 } 120 121 @Override 122 public boolean isAutomatic() { 123 return true; 124 } 125 @Override 126 public boolean isAutoNameFromManifest() { 127 return this.nameFromManifest; 128 } 129 @Override 130 public boolean isOpen() { 131 return false; 132 } 133 } 134 return new AutoModule(moduleName, fromManifest); 135 } 136 createAutomatic(String fileName, boolean isFile, Manifest manifest)137 public static IModule createAutomatic(String fileName, boolean isFile, Manifest manifest) { 138 boolean fromManifest = true; 139 char[] inferredName = AutomaticModuleNaming.determineAutomaticModuleNameFromManifest(manifest); 140 if (inferredName == null) { 141 fromManifest = false; 142 inferredName = AutomaticModuleNaming.determineAutomaticModuleNameFromFileName(fileName, true, isFile); 143 } 144 return createAutomatic(inferredName, fromManifest); 145 } 146 } 147