From 2b84123df9ee43c4cef875e811f790d846f8f8d5 Mon Sep 17 00:00:00 2001 From: Akash Yadav Date: Fri, 8 Jul 2022 00:31:44 +0530 Subject: [PATCH] Try not to use modules --- build.sh | 10 + .../javax/lang/model/SourceVersion.java | 2 +- .../com/sun/tools/javac/api/JavacTool.java | 3 +- .../com/sun/tools/javac/code/Source.java | 183 +++++++++------- .../com/sun/tools/javac/file/JRTIndex.java | 8 +- .../classes/com/sun/tools/jdeprscan/Main.java | 199 +++++++++--------- 6 files changed, 219 insertions(+), 186 deletions(-) create mode 100755 build.sh diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..9f27e38 --- /dev/null +++ b/build.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +set -e + +JDK_8="/usr/lib/jvm/java-8-openjdk-amd64" +if [ -d "$JDK_8" ]; then + export JAVA_HOME=$JDK_8 +fi + +ant -f ./make/langtools/netbeans/nb-javac "$@" diff --git a/src/java.compiler/share/classes/javax/lang/model/SourceVersion.java b/src/java.compiler/share/classes/javax/lang/model/SourceVersion.java index 8f02fea..de735ae 100644 --- a/src/java.compiler/share/classes/javax/lang/model/SourceVersion.java +++ b/src/java.compiler/share/classes/javax/lang/model/SourceVersion.java @@ -246,7 +246,7 @@ public enum SourceVersion { * {@return the latest source version that can be modeled} */ public static SourceVersion latest() { - return RELEASE_17; + return RELEASE_8; } private static final SourceVersion latestSupported = getLatestSupported(); diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTool.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTool.java index c8c5d5e..aa40450 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTool.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTool.java @@ -216,8 +216,9 @@ public int run(InputStream in, OutputStream out, OutputStream err, String... arg @Override @DefinedBy(Api.COMPILER) public Set getSourceVersions() { + // AndroidIDE changed: Allow upto Java 11 return Collections.unmodifiableSet(EnumSet.range(SourceVersion.RELEASE_3, - SourceVersion.latest())); + SourceVersion.RELEASE_11)); } @Override @DefinedBy(Api.COMPILER) diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Source.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Source.java index 0f9eef3..2ae38f6 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Source.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Source.java @@ -39,20 +39,22 @@ import static com.sun.tools.javac.main.Option.*; -/** The source language version accepted. +/** + * The source language version accepted. * - *

This is NOT part of any supported API. - * If you write code that depends on this, you do so at your own risk. - * This code and its internal interfaces are subject to change or - * deletion without notice. + *

+ * This is NOT part of any supported API. + * If you write code that depends on this, you do so at your own risk. + * This code and its internal interfaces are subject to change or + * deletion without notice. */ public enum Source { /** 1.0 had no inner classes, and so could not pass the JCK. */ - // public static final Source JDK1_0 = new Source("1.0"); + // public static final Source JDK1_0 = new Source("1.0"); /** 1.1 did not have strictfp, and so could not pass the JCK. */ - // public static final Source JDK1_1 = new Source("1.1"); + // public static final Source JDK1_1 = new Source("1.1"); /** 1.2 introduced strictfp. */ JDK1_2("1.2"), @@ -63,8 +65,10 @@ public enum Source { /** 1.4 introduced assert. */ JDK1_4("1.4"), - /** 1.5 introduced generics, attributes, foreach, boxing, static import, - * covariant return, enums, varargs, et al. */ + /** + * 1.5 introduced generics, attributes, foreach, boxing, static import, + * covariant return, enums, varargs, et al. + */ JDK5("5"), /** 1.6 reports encoding problems as errors instead of warnings. */ @@ -101,18 +105,18 @@ public enum Source { JDK14("14"), /** - * 15, text blocks - */ + * 15, text blocks + */ JDK15("15"), /** - * 16, tbd - */ + * 16, tbd + */ JDK16("16"), /** - * 17, tbd - */ + * 17, tbd + */ JDK17("17"); private static final Context.Key sourceKey = new Context.Key<>(); @@ -122,8 +126,10 @@ public static Source instance(Context context) { if (instance == null) { Options options = Options.instance(context); String sourceString = options.get(SOURCE); - if (sourceString != null) instance = lookup(sourceString); - if (instance == null) instance = DEFAULT; + if (sourceString != null) + instance = lookup(sourceString); + if (instance == null) + instance = DEFAULT; context.put(sourceKey, instance); } return instance; @@ -131,7 +137,7 @@ public static Source instance(Context context) { public final String name; - private static final Map tab = new HashMap<>(); + private static final Map tab = new HashMap<>(); static { for (Source s : values()) { tab.put(s.name, s); @@ -165,34 +171,52 @@ public boolean isSupported() { public Target requiredTarget() { - if (this.compareTo(JDK17) >= 0) return Target.JDK1_17; - if (this.compareTo(JDK16) >= 0) return Target.JDK1_16; - if (this.compareTo(JDK15) >= 0) return Target.JDK1_15; - if (this.compareTo(JDK14) >= 0) return Target.JDK1_14; - if (this.compareTo(JDK13) >= 0) return Target.JDK1_13; - if (this.compareTo(JDK12) >= 0) return Target.JDK1_12; - if (this.compareTo(JDK11) >= 0) return Target.JDK1_11; - if (this.compareTo(JDK10) >= 0) return Target.JDK1_10; - if (this.compareTo(JDK9) >= 0) return Target.JDK1_9; - if (this.compareTo(JDK8) >= 0) return Target.JDK1_8; - if (this.compareTo(JDK7) >= 0) return Target.JDK1_7; - if (this.compareTo(JDK6) >= 0) return Target.JDK1_6; - if (this.compareTo(JDK5) >= 0) return Target.JDK1_5; - if (this.compareTo(JDK1_4) >= 0) return Target.JDK1_4; + if (this.compareTo(JDK17) >= 0) + return Target.JDK1_17; + if (this.compareTo(JDK16) >= 0) + return Target.JDK1_16; + if (this.compareTo(JDK15) >= 0) + return Target.JDK1_15; + if (this.compareTo(JDK14) >= 0) + return Target.JDK1_14; + if (this.compareTo(JDK13) >= 0) + return Target.JDK1_13; + if (this.compareTo(JDK12) >= 0) + return Target.JDK1_12; + if (this.compareTo(JDK11) >= 0) + return Target.JDK1_11; + if (this.compareTo(JDK10) >= 0) + return Target.JDK1_10; + if (this.compareTo(JDK9) >= 0) + return Target.JDK1_9; + if (this.compareTo(JDK8) >= 0) + return Target.JDK1_8; + if (this.compareTo(JDK7) >= 0) + return Target.JDK1_7; + if (this.compareTo(JDK6) >= 0) + return Target.JDK1_6; + if (this.compareTo(JDK5) >= 0) + return Target.JDK1_5; + if (this.compareTo(JDK1_4) >= 0) + return Target.JDK1_4; return Target.JDK1_1; } /** - * Models a feature of the Java programming language. Each feature can be associated with a - * minimum source level, a maximum source level and a diagnostic fragment describing the feature, - * which is used to generate error messages of the kind {@code feature XYZ not supported in source N}. + * Models a feature of the Java programming language. Each feature can be + * associated with a + * minimum source level, a maximum source level and a diagnostic fragment + * describing the feature, + * which is used to generate error messages of the kind + * {@code feature XYZ not supported in source N}. */ public enum Feature { DIAMOND(JDK7, Fragments.FeatureDiamond, DiagKind.NORMAL), MODULES(JDK9, Fragments.FeatureModules, DiagKind.PLURAL), - EFFECTIVELY_FINAL_VARIABLES_IN_TRY_WITH_RESOURCES(JDK9, Fragments.FeatureVarInTryWithResources, DiagKind.PLURAL), + EFFECTIVELY_FINAL_VARIABLES_IN_TRY_WITH_RESOURCES(JDK9, Fragments.FeatureVarInTryWithResources, + DiagKind.PLURAL), DEPRECATION_ON_IMPORT(MIN, JDK8), POLY(JDK8), LAMBDA(JDK8, Fragments.FeatureLambda, DiagKind.PLURAL), @@ -260,6 +284,9 @@ enum DiagKind { } public boolean allowedInSource(Source source) { + if (optFragment == Fragments.FeatureModules) { + return false; + } return source.compareTo(minLevel) >= 0 && source.compareTo(maxLevel) <= 0; } @@ -276,57 +303,57 @@ public Fragment nameFragment() { public Fragment fragment(String sourceName) { Assert.checkNonNull(optFragment); - return optKind == DiagKind.NORMAL ? - Fragments.FeatureNotSupportedInSource(optFragment, sourceName, minLevel.name) : - Fragments.FeatureNotSupportedInSourcePlural(optFragment, sourceName, minLevel.name); + return optKind == DiagKind.NORMAL + ? Fragments.FeatureNotSupportedInSource(optFragment, sourceName, minLevel.name) + : Fragments.FeatureNotSupportedInSourcePlural(optFragment, sourceName, minLevel.name); } public Error error(String sourceName) { Assert.checkNonNull(optFragment); - return optKind == DiagKind.NORMAL ? - Errors.FeatureNotSupportedInSource(optFragment, sourceName, minLevel.name) : - Errors.FeatureNotSupportedInSourcePlural(optFragment, sourceName, minLevel.name); + return optKind == DiagKind.NORMAL + ? Errors.FeatureNotSupportedInSource(optFragment, sourceName, minLevel.name) + : Errors.FeatureNotSupportedInSourcePlural(optFragment, sourceName, minLevel.name); } } public static SourceVersion toSourceVersion(Source source) { - switch(source) { - case JDK1_2: - return RELEASE_2; - case JDK1_3: - return RELEASE_3; - case JDK1_4: - return RELEASE_4; - case JDK5: - return RELEASE_5; - case JDK6: - return RELEASE_6; - case JDK7: - return RELEASE_7; - case JDK8: - return RELEASE_8; - case JDK9: - return RELEASE_9; - case JDK10: - return RELEASE_10; - case JDK11: - return RELEASE_11; - case JDK12: - return RELEASE_12; - case JDK13: - return RELEASE_13; - case JDK14: - return RELEASE_14; - case JDK15: - return RELEASE_15; - case JDK16: - return RELEASE_16; - case JDK17: - return RELEASE_17; - - default: - return null; + switch (source) { + case JDK1_2: + return RELEASE_2; + case JDK1_3: + return RELEASE_3; + case JDK1_4: + return RELEASE_4; + case JDK5: + return RELEASE_5; + case JDK6: + return RELEASE_6; + case JDK7: + return RELEASE_7; + case JDK8: + return RELEASE_8; + case JDK9: + return RELEASE_9; + case JDK10: + return RELEASE_10; + case JDK11: + return RELEASE_11; + case JDK12: + return RELEASE_12; + case JDK13: + return RELEASE_13; + case JDK14: + return RELEASE_14; + case JDK15: + return RELEASE_15; + case JDK16: + return RELEASE_16; + case JDK17: + return RELEASE_17; + + default: + return null; } } diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/file/JRTIndex.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/file/JRTIndex.java index fab7b10..594fbab 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/file/JRTIndex.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/file/JRTIndex.java @@ -80,12 +80,8 @@ public static JRTIndex instance(Context context) { } public static boolean isAvailable() { - try { - FileSystems.getFileSystem(URI.create("jrt:/")); - return true; - } catch (ProviderNotFoundException | FileSystemNotFoundException e) { - return false; - } + // AndroidIDE changed: No JRT file system on Android + return false; } diff --git a/src/jdk.jdeps/share/classes/com/sun/tools/jdeprscan/Main.java b/src/jdk.jdeps/share/classes/com/sun/tools/jdeprscan/Main.java index c67510d..b8b9e6e 100644 --- a/src/jdk.jdeps/share/classes/com/sun/tools/jdeprscan/Main.java +++ b/src/jdk.jdeps/share/classes/com/sun/tools/jdeprscan/Main.java @@ -78,22 +78,23 @@ * a class library for usages of those APIs. * * TODO: - * - audit error handling throughout, but mainly in scan package - * - handling of covariant overrides - * - handling of override of method found in multiple superinterfaces - * - convert type/method/field output to Java source like syntax, e.g. - * instead of java/lang/Character.isJavaLetter(C)Z - * print void java.lang.Character.isJavaLetter(char)boolean - * - more example output in man page - * - more rigorous GNU style option parsing; use joptsimple? + * - audit error handling throughout, but mainly in scan package + * - handling of covariant overrides + * - handling of override of method found in multiple superinterfaces + * - convert type/method/field output to Java source like syntax, e.g. + * instead of java/lang/Character.isJavaLetter(C)Z + * print void java.lang.Character.isJavaLetter(char)boolean + * - more example output in man page + * - more rigorous GNU style option parsing; use joptsimple? * * FUTURES: - * - add module support: --add-modules, --module-path, module arg - * - load deprecation declarations from a designated class library instead - * of the JDK - * - load deprecation declarations from a module - * - scan a module (but a modular jar can be treated just a like an ordinary jar) - * - multi-version jar + * - add module support: --add-modules, --module-path, module arg + * - load deprecation declarations from a designated class library instead + * of the JDK + * - load deprecation declarations from a module + * - scan a module (but a modular jar can be treated just a like an ordinary + * jar) + * - multi-version jar */ public class Main implements DiagnosticListener { final PrintStream out; @@ -109,9 +110,9 @@ public class Main implements DiagnosticListener { // that allows querying of supported releases. final Set releasesWithoutForRemoval = Set.of("6", "7", "8"); final Set releasesWithForRemoval = // "9", "10", "11", ... - IntStream.rangeClosed(9, Runtime.version().feature()) - .mapToObj(Integer::toString) - .collect(Collectors.toUnmodifiableSet()); + IntStream.rangeClosed(9, Runtime.version().feature()) + .mapToObj(Integer::toString) + .collect(Collectors.toUnmodifiableSet()); final Set validReleases; { @@ -145,7 +146,7 @@ boolean doClassNames(Collection classNames) throws IOException { // TODO: not sure this is necessary... if (fm instanceof JavacFileManager) { - ((JavacFileManager)fm).setSymbolFileEnabled(false); + ((JavacFileManager) fm).setSymbolFileEnabled(false); } fm.setLocation(StandardLocation.CLASS_PATH, classPath); @@ -158,15 +159,14 @@ boolean doClassNames(Collection classNames) throws IOException { } LoadProc proc = new LoadProc(); - JavaCompiler.CompilationTask task = - compiler.getTask(null, fm, this, options, classNames, null); + JavaCompiler.CompilationTask task = compiler.getTask(null, fm, this, options, classNames, null); task.setProcessors(List.of(proc)); boolean r = task.call(); if (r) { if (forRemoval) { deprList = proc.getDeprecations().stream() - .filter(DeprData::isForRemoval) - .toList(); + .filter(DeprData::isForRemoval) + .toList(); } else { deprList = proc.getDeprecations(); } @@ -185,12 +185,12 @@ boolean doClassNames(Collection classNames) throws IOException { */ boolean doFileNames(Stream filenames) throws IOException { return doClassNames( - filenames.filter(name -> name.endsWith(".class")) - .filter(name -> !name.endsWith("package-info.class")) - .filter(name -> !name.endsWith("module-info.class")) - .map(s -> s.replaceAll("\\.class$", "")) - .map(s -> s.replace(File.separatorChar, '.')) - .toList()); + filenames.filter(name -> name.endsWith(".class")) + .filter(name -> !name.endsWith("package-info.class")) + .filter(name -> !name.endsWith("module-info.class")) + .map(s -> s.replaceAll("\\.class$", "")) + .map(s -> s.replace(File.separatorChar, '.')) + .toList()); } /** @@ -205,8 +205,8 @@ boolean doFileNames(Stream filenames) throws IOException { String convertModularFileName(String filename) { int slash = filename.indexOf('/'); return filename.substring(0, slash) - + "/" - + filename.substring(slash+1).replace('/', '.'); + + "/" + + filename.substring(slash + 1).replace('/', '.'); } /** @@ -222,12 +222,12 @@ String convertModularFileName(String filename) { */ boolean doModularFileNames(Stream filenames) throws IOException { return doClassNames( - filenames.filter(name -> name.endsWith(".class")) - .filter(name -> !name.endsWith("package-info.class")) - .filter(name -> !name.endsWith("module-info.class")) - .map(s -> s.replaceAll("\\.class$", "")) - .map(this::convertModularFileName) - .toList()); + filenames.filter(name -> name.endsWith(".class")) + .filter(name -> !name.endsWith("package-info.class")) + .filter(name -> !name.endsWith("module-info.class")) + .map(s -> s.replaceAll("\\.class$", "")) + .map(this::convertModularFileName) + .toList()); } /** @@ -235,7 +235,7 @@ boolean doModularFileNames(Stream filenames) throws IOException { * should be the root of a package hierarchy. If classNames is * empty, walks the directory hierarchy to find all classes. * - * @param dirname the name of the directory to process + * @param dirname the name of the directory to process * @param classNames the names of classes to process * @return true for success, false for failure * @throws IOException if an I/O error occurs @@ -252,10 +252,9 @@ boolean processDirectory(String dirname, Collection classNames) throws I Path base = Paths.get(dirname); int baseCount = base.getNameCount(); try (Stream paths = Files.walk(base)) { - Stream files = - paths.filter(p -> p.getNameCount() > baseCount) - .map(p -> p.subpath(baseCount, p.getNameCount())) - .map(Path::toString); + Stream files = paths.filter(p -> p.getNameCount() > baseCount) + .map(p -> p.subpath(baseCount, p.getNameCount())) + .map(Path::toString); return doFileNames(files); } } else { @@ -272,9 +271,8 @@ boolean processDirectory(String dirname, Collection classNames) throws I */ boolean doJarFile(String jarname) throws IOException { try (JarFile jf = new JarFile(jarname)) { - Stream files = - jf.stream() - .map(JarEntry::getName); + Stream files = jf.stream() + .map(JarEntry::getName); return doFileNames(files); } } @@ -283,7 +281,7 @@ boolean doJarFile(String jarname) throws IOException { * Processes named class files from the given jar file, * or all classes if classNames is empty. * - * @param jarname the name of the jar file to process + * @param jarname the name of the jar file to process * @param classNames the names of classes to process * @return true for success, false for failure * @throws IOException if an I/O error occurs @@ -302,7 +300,7 @@ boolean processJarFile(String jarname, Collection classNames) throws IOE * Processes named class files from rt.jar of a JDK version 7 or 8. * If classNames is empty, processes all classes. * - * @param jdkHome the path to the "home" of the JDK to process + * @param jdkHome the path to the "home" of the JDK to process * @param classNames the names of classes to process * @return true for success, false for failure * @throws IOException if an I/O error occurs @@ -344,14 +342,13 @@ boolean processSelf(Collection classes) throws IOException { if (classes.isEmpty()) { Path modules = FileSystems.getFileSystem(URI.create("jrt:/")) - .getPath("/modules"); + .getPath("/modules"); // names are /modules//pkg/.../Classname.class try (Stream paths = Files.walk(modules)) { - Stream files = - paths.filter(p -> p.getNameCount() > 2) - .map(p -> p.subpath(1, p.getNameCount())) - .map(Path::toString); + Stream files = paths.filter(p -> p.getNameCount() > 2) + .map(p -> p.subpath(1, p.getNameCount())) + .map(Path::toString); return doModularFileNames(files); } } else { @@ -383,50 +380,53 @@ boolean processRelease(String release, Collection classes) throws IOExce options.addAll(List.of("--release", release)); - if (hasModules) { - List rootMods = hasJavaSE_EE ? List.of("java.se", "java.se.ee") - : List.of("java.se"); - TraverseProc proc = new TraverseProc(rootMods); - JavaCompiler.CompilationTask task = - compiler.getTask(null, fm, this, - // options - List.of("--add-modules", String.join(",", rootMods), - "--release", release), - // classes - List.of("java.lang.Object"), - null); - task.setProcessors(List.of(proc)); - if (!task.call()) { - return false; - } - Map> types = proc.getPublicTypes(); - options.add("--add-modules"); - options.add(String.join(",", rootMods)); - return doClassNames( - types.values().stream() - .flatMap(List::stream) - .map(TypeElement::toString) - .toList()); - } else { - JDKPlatformProvider pp = new JDKPlatformProvider(); - if (StreamSupport.stream(pp.getSupportedPlatformNames().spliterator(), - false) - .noneMatch(n -> n.equals(release))) { - return false; - } - JavaFileManager fm = pp.getPlatform(release, "").getFileManager(); - List classNames = new ArrayList<>(); - for (JavaFileObject fo : fm.list(StandardLocation.PLATFORM_CLASS_PATH, - "", - EnumSet.of(Kind.CLASS), - true)) { - classNames.add(fm.inferBinaryName(StandardLocation.PLATFORM_CLASS_PATH, fo)); - } - - options.add("-Xlint:-options"); + // AndroidIDE changed: No module support! + /* #region(collapsed) */ + // if (hasModules) { + // List rootMods = hasJavaSE_EE ? List.of("java.se", "java.se.ee") + // : List.of("java.se"); + // TraverseProc proc = new TraverseProc(rootMods); + // JavaCompiler.CompilationTask task = compiler.getTask(null, fm, this, + // // options + // List.of("--add-modules", String.join(",", rootMods), + // "--release", release), + // // classes + // List.of("java.lang.Object"), + // null); + // task.setProcessors(List.of(proc)); + // if (!task.call()) { + // return false; + // } + // Map> types = proc.getPublicTypes(); + // options.add("--add-modules"); + // options.add(String.join(",", rootMods)); + // return doClassNames( + // types.values().stream() + // .flatMap(List::stream) + // .map(TypeElement::toString) + // .toList()); + // } else { + // } + /* #endregion */ - return doClassNames(classNames); + JDKPlatformProvider pp = new JDKPlatformProvider(); + if (StreamSupport.stream(pp.getSupportedPlatformNames().spliterator(), + false) + .noneMatch(n -> n.equals(release))) { + return false; } + JavaFileManager fm = pp.getPlatform(release, "").getFileManager(); + List classNames = new ArrayList<>(); + for (JavaFileObject fo : fm.list(StandardLocation.PLATFORM_CLASS_PATH, + "", + EnumSet.of(Kind.CLASS), + true)) { + classNames.add(fm.inferBinaryName(StandardLocation.PLATFORM_CLASS_PATH, fo)); + } + + options.add("-Xlint:-options"); + + return doClassNames(classNames); } /** @@ -514,8 +514,8 @@ boolean run(String... argArray) { case "--class-path": classPath.clear(); Arrays.stream(args.remove().split(File.pathSeparator)) - .map(File::new) - .forEachOrdered(classPath::add); + .map(File::new) + .forEachOrdered(classPath::add); break; case "--for-removal": forRemoval = true; @@ -601,7 +601,7 @@ boolean run(String... argArray) { throw new UsageException(); } - if ( forRemoval && loadMode == LoadMode.RELEASE && + if (forRemoval && loadMode == LoadMode.RELEASE && releasesWithoutForRemoval.contains(release)) { throw new UsageException(); } @@ -676,8 +676,8 @@ boolean run(String... argArray) { case ARGS: DeprDB db = DeprDB.loadFromList(deprList); List cp = classPath.stream() - .map(File::toString) - .toList(); + .map(File::toString) + .toList(); Scan scan = new Scan(out, err, cp, db, verbose); for (String a : args) { @@ -701,8 +701,7 @@ boolean run(String... argArray) { private void printHelp(PrintStream out) { JDKPlatformProvider pp = new JDKPlatformProvider(); - String supportedReleases = - String.join("|", pp.getSupportedPlatformNames()); + String supportedReleases = String.join("|", pp.getSupportedPlatformNames()); out.println(Messages.get("main.usage", supportedReleases)); }