Skip to content

Commit 8b56027

Browse files
lgxbslgxRealCLanger
authored andcommitted
8193462: Fix Filer handling of package-info initial elements
Backport-of: fafd1b7
1 parent a27448e commit 8b56027

File tree

3 files changed

+56
-13
lines changed

3 files changed

+56
-13
lines changed

Diff for: src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Enter.java

+1
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ public void visitTopLevel(JCCompilationUnit tree) {
379379
c.completer = Completer.NULL_COMPLETER;
380380
c.members_field = WriteableScope.create(c);
381381
tree.packge.package_info = c;
382+
tree.packge.sourcefile = tree.sourcefile;
382383
}
383384
classEnter(tree.defs, topEnv);
384385
if (addEnv) {

Diff for: src/jdk.compiler/share/classes/com/sun/tools/javac/processing/JavacFiler.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import com.sun.tools.javac.code.Lint;
5454
import com.sun.tools.javac.code.Symbol.ClassSymbol;
5555
import com.sun.tools.javac.code.Symbol.ModuleSymbol;
56+
import com.sun.tools.javac.code.Symbol.TypeSymbol;
5657
import com.sun.tools.javac.code.Symtab;
5758
import com.sun.tools.javac.comp.Modules;
5859
import com.sun.tools.javac.model.JavacElements;
@@ -717,7 +718,7 @@ private void checkNameAndExistence(ModuleSymbol mod, String typename, boolean al
717718
boolean alreadySeen = aggregateGeneratedSourceNames.contains(Pair.of(mod, typename)) ||
718719
aggregateGeneratedClassNames.contains(Pair.of(mod, typename)) ||
719720
initialClassNames.contains(typename) ||
720-
(existing != null && initialInputs.contains(existing.sourcefile));
721+
containedInInitialInputs(typename);
721722
if (alreadySeen) {
722723
if (lint)
723724
log.warning(Warnings.ProcTypeRecreate(typename));
@@ -731,6 +732,22 @@ private void checkNameAndExistence(ModuleSymbol mod, String typename, boolean al
731732
}
732733
}
733734

735+
private boolean containedInInitialInputs(String typename) {
736+
// Name could be a type name or the name of a package-info file
737+
JavaFileObject sourceFile = null;
738+
739+
ClassSymbol existingClass = elementUtils.getTypeElement(typename);
740+
if (existingClass != null) {
741+
sourceFile = existingClass.sourcefile;
742+
} else if (typename.endsWith(".package-info")) {
743+
String targetName = typename.substring(0, typename.length() - ".package-info".length());
744+
PackageSymbol existingPackage = elementUtils.getPackageElement(targetName);
745+
if (existingPackage != null)
746+
sourceFile = existingPackage.sourcefile;
747+
}
748+
return (sourceFile == null) ? false : initialInputs.contains(sourceFile);
749+
}
750+
734751
/**
735752
* Check to see if the file has already been opened; if so, throw
736753
* an exception, otherwise add it to the set of files.

Diff for: test/langtools/tools/javac/processing/filer/TestPackageInfo.java

+37-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @bug 6380018 6392177 6993311
26+
* @bug 6380018 6392177 6993311 8193462
2727
* @summary Test the ability to create and process package-info.java files
2828
* @author Joseph D. Darcy
2929
* @library /tools/javac/lib
@@ -35,7 +35,6 @@
3535
*/
3636

3737
import java.util.Set;
38-
import java.util.HashSet;
3938
import java.util.Map;
4039
import javax.annotation.processing.*;
4140
import javax.lang.model.SourceVersion;
@@ -61,8 +60,8 @@ public boolean process(Set<? extends TypeElement> annotations,
6160
round++;
6261

6362
// Verify annotations are as expected
64-
Set<TypeElement> expectedAnnotations = new HashSet<TypeElement>();
65-
expectedAnnotations.add(eltUtils.getTypeElement("java.lang.Deprecated"));
63+
Set<TypeElement> expectedAnnotations =
64+
Set.of(eltUtils.getTypeElement("java.lang.Deprecated"));
6665

6766
if (!roundEnv.processingOver()) {
6867
System.out.println("\nRound " + round);
@@ -91,28 +90,32 @@ public boolean process(Set<? extends TypeElement> annotations,
9190
throw new RuntimeException("Created class file for \"package-info\".");
9291
} catch(FilerException fe) {}
9392

94-
PrintWriter pw = new PrintWriter(filer.createSourceFile("foo.package-info").openWriter());
95-
pw.println("@Deprecated");
96-
pw.println("package foo;");
97-
pw.close();
93+
try(PrintWriter pw =
94+
new PrintWriter(filer.createSourceFile("foo.package-info").openWriter())) {
95+
pw.println("@Deprecated");
96+
pw.println("package foo;");
97+
}
9898

99+
attemptReopening("foo.package-info");
100+
attemptReopening("TestPackageInfo"); // Initial input
101+
attemptReopening("foo.bar.package-info"); // Initial input
99102
} catch(IOException ioe) {
100103
throw new RuntimeException(ioe);
101104
}
102105
break;
103106

104107
case 2:
105108
// Expect foo.package-info
106-
107-
Set<Element> expectedElement = new HashSet<Element>();
108-
expectedElement.add(eltUtils.getPackageElement("foo"));
109+
Set<Element> expectedElement = Set.of(eltUtils.getPackageElement("foo"));
109110
if (!expectedElement.equals(roundEnv.getRootElements()))
110111
throw new RuntimeException("Unexpected root element set " + roundEnv.getRootElements());
111112

112113
if (!expectedAnnotations.equals(annotations)) {
113114
throw new RuntimeException("Unexpected annotations: " + annotations);
114115
}
115116

117+
attemptReopening("foo.package-info");
118+
116119
break;
117120

118121
default:
@@ -121,4 +124,26 @@ public boolean process(Set<? extends TypeElement> annotations,
121124
}
122125
return false;
123126
}
127+
128+
private void attemptReopening(String name) {
129+
final String SHOULD_NOT_REACH = "Should not reach: created ";
130+
try {
131+
try {
132+
filer.createSourceFile(name);
133+
throw new AssertionError(SHOULD_NOT_REACH + name + ".java");
134+
} catch (FilerException fe) {
135+
; // Expected
136+
}
137+
138+
try {
139+
filer.createClassFile(name);
140+
throw new AssertionError(SHOULD_NOT_REACH + name + ".class");
141+
} catch (FilerException fe) {
142+
; // Expected
143+
}
144+
} catch (IOException ioe) {
145+
throw new RuntimeException(ioe);
146+
}
147+
148+
}
124149
}

0 commit comments

Comments
 (0)