1
1
/*
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.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
23
23
24
24
/*
25
25
* @test
26
- * @bug 6380018 6392177 6993311
26
+ * @bug 6380018 6392177 6993311 8193462
27
27
* @summary Test the ability to create and process package-info.java files
28
28
* @author Joseph D. Darcy
29
29
* @library /tools/javac/lib
35
35
*/
36
36
37
37
import java .util .Set ;
38
- import java .util .HashSet ;
39
38
import java .util .Map ;
40
39
import javax .annotation .processing .*;
41
40
import javax .lang .model .SourceVersion ;
@@ -61,8 +60,8 @@ public boolean process(Set<? extends TypeElement> annotations,
61
60
round ++;
62
61
63
62
// 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" ));
66
65
67
66
if (!roundEnv .processingOver ()) {
68
67
System .out .println ("\n Round " + round );
@@ -91,28 +90,32 @@ public boolean process(Set<? extends TypeElement> annotations,
91
90
throw new RuntimeException ("Created class file for \" package-info\" ." );
92
91
} catch (FilerException fe ) {}
93
92
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
+ }
98
98
99
+ attemptReopening ("foo.package-info" );
100
+ attemptReopening ("TestPackageInfo" ); // Initial input
101
+ attemptReopening ("foo.bar.package-info" ); // Initial input
99
102
} catch (IOException ioe ) {
100
103
throw new RuntimeException (ioe );
101
104
}
102
105
break ;
103
106
104
107
case 2 :
105
108
// 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" ));
109
110
if (!expectedElement .equals (roundEnv .getRootElements ()))
110
111
throw new RuntimeException ("Unexpected root element set " + roundEnv .getRootElements ());
111
112
112
113
if (!expectedAnnotations .equals (annotations )) {
113
114
throw new RuntimeException ("Unexpected annotations: " + annotations );
114
115
}
115
116
117
+ attemptReopening ("foo.package-info" );
118
+
116
119
break ;
117
120
118
121
default :
@@ -121,4 +124,26 @@ public boolean process(Set<? extends TypeElement> annotations,
121
124
}
122
125
return false ;
123
126
}
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
+ }
124
149
}
0 commit comments