Skip to content
Permalink
Browse files
8263432: javac may report an invalid package/class clash on case inse…
…nsitive filesystems

Reviewed-by: clanger
Backport-of: 0a4c338
  • Loading branch information
TheRealMDoerr committed Aug 5, 2021
1 parent f2240cc commit b878e3f8d15f4c8f9a48cf89989349ad8b741d79
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 5 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -648,6 +648,10 @@ public ClassSymbol getClass(ModuleSymbol msym, Name flatName) {
}

public PackageSymbol lookupPackage(ModuleSymbol msym, Name flatName) {
return lookupPackage(msym, flatName, false);
}

private PackageSymbol lookupPackage(ModuleSymbol msym, Name flatName, boolean onlyExisting) {
Assert.checkNonNull(msym);

if (flatName.isEmpty()) {
@@ -670,7 +674,7 @@ public PackageSymbol lookupPackage(ModuleSymbol msym, Name flatName) {

pack = getPackage(msym, flatName);

if (pack != null && pack.exists())
if ((pack != null && pack.exists()) || onlyExisting)
return pack;

boolean dependsOnUnnamed = msym.requires != null &&
@@ -742,7 +746,8 @@ public ClassSymbol enterClass(ModuleSymbol msym, Name flatname) {
*/
public boolean packageExists(ModuleSymbol msym, Name fullname) {
Assert.checkNonNull(msym);
return lookupPackage(msym, fullname).exists();
PackageSymbol pack = lookupPackage(msym, fullname, true);
return pack != null && pack.exists();
}

/** Make a package, given its fully qualified name.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -23,7 +23,7 @@

/**
* @test
* @bug 8133884 8162711 8133896 8172158 8172262 8173636 8175119 8189747
* @bug 8133884 8162711 8133896 8172158 8172262 8173636 8175119 8189747 8263432
* @summary Verify that annotation processing works.
* @library /tools/lib
* @modules
@@ -44,6 +44,7 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@@ -78,6 +79,7 @@
import javax.lang.model.util.ElementScanner9;
import javax.tools.Diagnostic.Kind;
import javax.tools.FileObject;
import javax.tools.ForwardingJavaFileManager;
import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileManager;
@@ -1551,6 +1553,101 @@ public void testWrongDefaultTargetModule(Path base) throws Exception {
}
}

@Test
public void testClassPhantomPackageClash(Path base) throws Exception {
Path classes = base.resolve("classes");

Files.createDirectories(classes);

Path src = base.resolve("src");

tb.writeJavaFiles(src,
"module m {}",
"package api; public class Nested {}",
"package api.nested; public class C {}");

class TestFileManager extends ForwardingJavaFileManager<JavaFileManager>
implements StandardJavaFileManager {

public TestFileManager(StandardJavaFileManager fileManager) {
super(fileManager);
}

@Override
public Iterable<JavaFileObject> list(Location location, String packageName, Set<JavaFileObject.Kind> kinds, boolean recurse) throws IOException {
if ("api.Nested".equals(packageName)) {
//simulate case insensitive filesystem:
packageName = "api.nested";
}
return super.list(location, packageName, kinds, recurse);
}

@Override
public void setLocationFromPaths(Location location, Collection<? extends Path> paths) throws IOException {
((StandardJavaFileManager) fileManager).setLocationFromPaths(location, paths);
}

@Override
public Iterable<? extends Path> getLocationAsPaths(Location location) {
return ((StandardJavaFileManager) fileManager).getLocationAsPaths(location);
}

@Override
public Iterable<? extends JavaFileObject> getJavaFileObjectsFromFiles(Iterable<? extends File> files) {
throw new UnsupportedOperationException();
}

@Override
public Iterable<? extends JavaFileObject> getJavaFileObjects(File... files) {
throw new UnsupportedOperationException();
}

@Override
public Iterable<? extends JavaFileObject> getJavaFileObjectsFromStrings(Iterable<String> names) {
throw new UnsupportedOperationException();
}

@Override
public Iterable<? extends JavaFileObject> getJavaFileObjects(String... names) {
throw new UnsupportedOperationException();
}

@Override
public void setLocation(Location location, Iterable<? extends File> files) throws IOException {
throw new UnsupportedOperationException();
}

@Override
public Iterable<? extends File> getLocation(Location location) {
throw new UnsupportedOperationException();
}
}

try (StandardJavaFileManager fm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null);
JavaFileManager testFM = new TestFileManager(fm)) {
new JavacTask(tb)
.fileManager(testFM)
.options("-processor", NoOpTestAP.class.getName(),
"-sourcepath", src.toString())
.outdir(classes)
.files(src.resolve("module-info.java"),
src.resolve("api").resolve("Nested.java"))
.run()
.writeAll()
.getOutputLines(OutputKind.STDERR);
}
}

@SupportedAnnotationTypes("*")
public static final class NoOpTestAP extends AbstractProcessor {

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
return false;
}

}

private static void assertNonNull(String msg, Object val) {
if (val == null) {
throw new AssertionError(msg);

1 comment on commit b878e3f

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on b878e3f Aug 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.