1+ /*
2+ * Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved.
3+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+ *
5+ * This code is free software; you can redistribute it and/or modify it
6+ * under the terms of the GNU General Public License version 2 only, as
7+ * published by the Free Software Foundation. Oracle designates this
8+ * particular file as subject to the "Classpath" exception as provided
9+ * by Oracle in the LICENSE file that accompanied this code.
10+ *
11+ * This code is distributed in the hope that it will be useful, but WITHOUT
12+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+ * version 2 for more details (a copy is included in the LICENSE file that
15+ * accompanied this code).
16+ *
17+ * You should have received a copy of the GNU General Public License version
18+ * 2 along with this work; if not, write to the Free Software Foundation,
19+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+ *
21+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+ * or visit www.oracle.com if you need additional information or have any
23+ * questions.
24+ */
25+ package previewfeature ;
26+
27+ import com .sun .source .util .JavacTask ;
28+ import com .sun .source .util .Trees ;
29+ import java .io .StringWriter ;
30+ import java .lang .reflect .Field ;
31+ import java .nio .file .Files ;
32+ import java .nio .file .Path ;
33+ import java .util .HashSet ;
34+ import java .util .Set ;
35+ import java .util .stream .Collectors ;
36+ import javax .lang .model .element .ElementKind ;
37+ import javax .tools .ToolProvider ;
38+
39+ /* Construct a hybrid PreviewFeature.Feature enum that includes constants both
40+ * from the current JDK sources (so that they can be used in the javac API sources),
41+ * and from the bootstrap JDK (so that they can be used in the bootstrap classfiles).
42+ *
43+ * This hybrid enum is only used for the interim javac.
44+ */
45+ public class SetupPreviewFeature {
46+ public static void main (String ... args ) throws Exception {
47+ Class <?> runtimeFeature = Class .forName ("jdk.internal.javac.PreviewFeature$Feature" );
48+ Set <String > constantsToAdd = new HashSet <>();
49+ for (Field runtimeField : runtimeFeature .getDeclaredFields ()) {
50+ if (runtimeField .isEnumConstant ()) {
51+ constantsToAdd .add (runtimeField .getName ());
52+ }
53+ }
54+ var dummy = new StringWriter ();
55+ var compiler = ToolProvider .getSystemJavaCompiler ();
56+ var source = Path .of (args [0 ]);
57+ try (var fm = compiler .getStandardFileManager (null , null , null )) {
58+ JavacTask task =
59+ (JavacTask ) compiler .getTask (dummy , null , null , null , null , fm .getJavaFileObjects (source ));
60+ task .analyze ();
61+ var sourceFeature = task .getElements ()
62+ .getTypeElement ("jdk.internal.javac.PreviewFeature.Feature" );
63+ int insertPosition = -1 ;
64+ for (var el : sourceFeature .getEnclosedElements ()) {
65+ if (el .getKind () == ElementKind .ENUM_CONSTANT ) {
66+ constantsToAdd .remove (el .getSimpleName ().toString ());
67+ if (insertPosition == (-1 )) {
68+ var trees = Trees .instance (task );
69+ var elPath = trees .getPath (el );
70+ insertPosition = (int ) trees .getSourcePositions ()
71+ .getStartPosition (elPath .getCompilationUnit (),
72+ elPath .getLeaf ());
73+ }
74+ }
75+ }
76+ var target = Path .of (args [1 ]);
77+ Files .createDirectories (target .getParent ());
78+ if (constantsToAdd .isEmpty ()) {
79+ Files .copy (source , target );
80+ } else {
81+ String sourceCode = Files .readString (source );
82+ try (var out = Files .newBufferedWriter (target )) {
83+ out .write (sourceCode , 0 , insertPosition );
84+ out .write (constantsToAdd .stream ()
85+ .collect (Collectors .joining (", " ,
86+ "/*compatibility constants:*/ " ,
87+ ",\n " )));
88+ out .write (sourceCode , insertPosition , sourceCode .length () - insertPosition );
89+ }
90+ }
91+ }
92+ }
93+ }
0 commit comments