8193462: Fix Filer handling of package-info initial elements

Reviewed-by: vromero
This commit is contained in:
Joe Darcy 2018-07-18 00:23:06 -07:00
parent 19c364592c
commit fafd1b7536
3 changed files with 56 additions and 13 deletions

View File

@ -379,6 +379,7 @@ public class Enter extends JCTree.Visitor {
c.completer = Completer.NULL_COMPLETER;
c.members_field = WriteableScope.create(c);
tree.packge.package_info = c;
tree.packge.sourcefile = tree.sourcefile;
}
classEnter(tree.defs, topEnv);
if (addEnv) {

View File

@ -53,6 +53,7 @@ import static javax.tools.StandardLocation.CLASS_OUTPUT;
import com.sun.tools.javac.code.Lint;
import com.sun.tools.javac.code.Symbol.ClassSymbol;
import com.sun.tools.javac.code.Symbol.ModuleSymbol;
import com.sun.tools.javac.code.Symbol.TypeSymbol;
import com.sun.tools.javac.code.Symtab;
import com.sun.tools.javac.comp.Modules;
import com.sun.tools.javac.model.JavacElements;
@ -717,7 +718,7 @@ public class JavacFiler implements Filer, Closeable {
boolean alreadySeen = aggregateGeneratedSourceNames.contains(Pair.of(mod, typename)) ||
aggregateGeneratedClassNames.contains(Pair.of(mod, typename)) ||
initialClassNames.contains(typename) ||
(existing != null && initialInputs.contains(existing.sourcefile));
containedInInitialInputs(typename);
if (alreadySeen) {
if (lint)
log.warning(Warnings.ProcTypeRecreate(typename));
@ -731,6 +732,22 @@ public class JavacFiler implements Filer, Closeable {
}
}
private boolean containedInInitialInputs(String typename) {
// Name could be a type name or the name of a package-info file
JavaFileObject sourceFile = null;
ClassSymbol existingClass = elementUtils.getTypeElement(typename);
if (existingClass != null) {
sourceFile = existingClass.sourcefile;
} else if (typename.endsWith(".package-info")) {
String targetName = typename.substring(0, typename.length() - ".package-info".length());
PackageSymbol existingPackage = elementUtils.getPackageElement(targetName);
if (existingPackage != null)
sourceFile = existingPackage.sourcefile;
}
return (sourceFile == null) ? false : initialInputs.contains(sourceFile);
}
/**
* Check to see if the file has already been opened; if so, throw
* an exception, otherwise add it to the set of files.

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2006, 2018, 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 6380018 6392177 6993311
* @bug 6380018 6392177 6993311 8193462
* @summary Test the ability to create and process package-info.java files
* @author Joseph D. Darcy
* @library /tools/javac/lib
@ -35,7 +35,6 @@
*/
import java.util.Set;
import java.util.HashSet;
import java.util.Map;
import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
@ -61,8 +60,8 @@ public class TestPackageInfo extends JavacTestingAbstractProcessor {
round++;
// Verify annotations are as expected
Set<TypeElement> expectedAnnotations = new HashSet<TypeElement>();
expectedAnnotations.add(eltUtils.getTypeElement("java.lang.Deprecated"));
Set<TypeElement> expectedAnnotations =
Set.of(eltUtils.getTypeElement("java.lang.Deprecated"));
if (!roundEnv.processingOver()) {
System.out.println("\nRound " + round);
@ -91,11 +90,15 @@ public class TestPackageInfo extends JavacTestingAbstractProcessor {
throw new RuntimeException("Created class file for \"package-info\".");
} catch(FilerException fe) {}
PrintWriter pw = new PrintWriter(filer.createSourceFile("foo.package-info").openWriter());
pw.println("@Deprecated");
pw.println("package foo;");
pw.close();
try(PrintWriter pw =
new PrintWriter(filer.createSourceFile("foo.package-info").openWriter())) {
pw.println("@Deprecated");
pw.println("package foo;");
}
attemptReopening("foo.package-info");
attemptReopening("TestPackageInfo"); // Initial input
attemptReopening("foo.bar.package-info"); // Initial input
} catch(IOException ioe) {
throw new RuntimeException(ioe);
}
@ -103,9 +106,7 @@ public class TestPackageInfo extends JavacTestingAbstractProcessor {
case 2:
// Expect foo.package-info
Set<Element> expectedElement = new HashSet<Element>();
expectedElement.add(eltUtils.getPackageElement("foo"));
Set<Element> expectedElement = Set.of(eltUtils.getPackageElement("foo"));
if (!expectedElement.equals(roundEnv.getRootElements()))
throw new RuntimeException("Unexpected root element set " + roundEnv.getRootElements());
@ -113,6 +114,8 @@ public class TestPackageInfo extends JavacTestingAbstractProcessor {
throw new RuntimeException("Unexpected annotations: " + annotations);
}
attemptReopening("foo.package-info");
break;
default:
@ -121,4 +124,26 @@ public class TestPackageInfo extends JavacTestingAbstractProcessor {
}
return false;
}
private void attemptReopening(String name) {
final String SHOULD_NOT_REACH = "Should not reach: created ";
try {
try {
filer.createSourceFile(name);
throw new AssertionError(SHOULD_NOT_REACH + name + ".java");
} catch (FilerException fe) {
; // Expected
}
try {
filer.createClassFile(name);
throw new AssertionError(SHOULD_NOT_REACH + name + ".class");
} catch (FilerException fe) {
; // Expected
}
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
}
}