Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Preview.java
41241 views
1
/*
2
* Copyright (c) 2018, 2021, 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
26
package com.sun.tools.javac.code;
27
28
import com.sun.tools.javac.code.Lint.LintCategory;
29
import com.sun.tools.javac.code.Source.Feature;
30
import com.sun.tools.javac.jvm.Target;
31
import com.sun.tools.javac.resources.CompilerProperties.Errors;
32
import com.sun.tools.javac.resources.CompilerProperties.Warnings;
33
import com.sun.tools.javac.util.Assert;
34
import com.sun.tools.javac.util.Context;
35
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
36
import com.sun.tools.javac.util.JCDiagnostic.Error;
37
import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition;
38
import com.sun.tools.javac.util.JCDiagnostic.Warning;
39
import com.sun.tools.javac.util.Log;
40
import com.sun.tools.javac.util.MandatoryWarningHandler;
41
import com.sun.tools.javac.util.Options;
42
43
import javax.tools.JavaFileObject;
44
import java.util.HashMap;
45
import java.util.HashSet;
46
import java.util.Map;
47
import java.util.Set;
48
49
import static com.sun.tools.javac.code.Flags.RECORD;
50
import static com.sun.tools.javac.code.Flags.SEALED;
51
import static com.sun.tools.javac.code.Flags.NON_SEALED;
52
import static com.sun.tools.javac.main.Option.PREVIEW;
53
54
/**
55
* Helper class to handle preview language features. This class maps certain language features
56
* (see {@link Feature} into 'preview' features; the mapping is completely ad-hoc, so as to allow
57
* for maximum flexibility, which allows to migrate preview feature into supported features with ease.
58
*
59
* This class acts as a centralized point against which usages of preview features are reported by
60
* clients (e.g. other javac classes). Internally, this class collects all such usages and generates
61
* diagnostics to inform the user of such usages. Such diagnostics can be enabled using the
62
* {@link LintCategory#PREVIEW} lint category, and are suppressible by usual means.
63
*/
64
public class Preview {
65
66
/** flag: are preview features enabled */
67
private final boolean enabled;
68
69
/** the diag handler to manage preview feature usage diagnostics */
70
private final MandatoryWarningHandler previewHandler;
71
72
/** test flag: should all features be considered as preview features? */
73
private final boolean forcePreview;
74
75
/** a mapping from classfile numbers to Java SE versions */
76
private final Map<Integer, Source> majorVersionToSource;
77
78
private final Set<JavaFileObject> sourcesWithPreviewFeatures = new HashSet<>();
79
80
private final Lint lint;
81
private final Log log;
82
83
private static final Context.Key<Preview> previewKey = new Context.Key<>();
84
85
public static Preview instance(Context context) {
86
Preview instance = context.get(previewKey);
87
if (instance == null) {
88
instance = new Preview(context);
89
}
90
return instance;
91
}
92
93
Preview(Context context) {
94
context.put(previewKey, this);
95
Options options = Options.instance(context);
96
enabled = options.isSet(PREVIEW);
97
log = Log.instance(context);
98
lint = Lint.instance(context);
99
Source source = Source.instance(context);
100
this.previewHandler =
101
new MandatoryWarningHandler(log, source, lint.isEnabled(LintCategory.PREVIEW), true, "preview", LintCategory.PREVIEW);
102
forcePreview = options.isSet("forcePreview");
103
majorVersionToSource = initMajorVersionToSourceMap();
104
}
105
106
private Map<Integer, Source> initMajorVersionToSourceMap() {
107
Map<Integer, Source> majorVersionToSource = new HashMap<>();
108
for (Target t : Target.values()) {
109
int major = t.majorVersion;
110
Source source = Source.lookup(t.name);
111
if (source != null) {
112
majorVersionToSource.put(major, source);
113
}
114
}
115
return majorVersionToSource;
116
}
117
118
/**
119
* Report usage of a preview feature. Usages reported through this method will affect the
120
* set of sourcefiles with dependencies on preview features.
121
* @param pos the position at which the preview feature was used.
122
* @param feature the preview feature used.
123
*/
124
public void warnPreview(int pos, Feature feature) {
125
warnPreview(new SimpleDiagnosticPosition(pos), feature);
126
}
127
128
/**
129
* Report usage of a preview feature. Usages reported through this method will affect the
130
* set of sourcefiles with dependencies on preview features.
131
* @param pos the position at which the preview feature was used.
132
* @param feature the preview feature used.
133
*/
134
public void warnPreview(DiagnosticPosition pos, Feature feature) {
135
Assert.check(isEnabled());
136
Assert.check(isPreview(feature));
137
if (!lint.isSuppressed(LintCategory.PREVIEW)) {
138
sourcesWithPreviewFeatures.add(log.currentSourceFile());
139
previewHandler.report(pos, feature.isPlural() ?
140
Warnings.PreviewFeatureUsePlural(feature.nameFragment()) :
141
Warnings.PreviewFeatureUse(feature.nameFragment()));
142
}
143
}
144
145
/**
146
* Report usage of a preview feature in classfile.
147
* @param classfile the name of the classfile with preview features enabled
148
* @param majorVersion the major version found in the classfile.
149
*/
150
public void warnPreview(JavaFileObject classfile, int majorVersion) {
151
Assert.check(isEnabled());
152
if (lint.isEnabled(LintCategory.PREVIEW)) {
153
sourcesWithPreviewFeatures.add(log.currentSourceFile());
154
log.mandatoryWarning(LintCategory.PREVIEW, null,
155
Warnings.PreviewFeatureUseClassfile(classfile, majorVersionToSource.get(majorVersion).name));
156
}
157
}
158
159
public void markUsesPreview(DiagnosticPosition pos) {
160
sourcesWithPreviewFeatures.add(log.currentSourceFile());
161
}
162
163
public void reportPreviewWarning(DiagnosticPosition pos, Warning warnKey) {
164
previewHandler.report(pos, warnKey);
165
}
166
167
public boolean usesPreview(JavaFileObject file) {
168
return sourcesWithPreviewFeatures.contains(file);
169
}
170
171
/**
172
* Are preview features enabled?
173
* @return true, if preview features are enabled.
174
*/
175
public boolean isEnabled() {
176
return enabled;
177
}
178
179
/**
180
* Is given feature a preview feature?
181
* @param feature the feature to be tested.
182
* @return true, if given feature is a preview feature.
183
*/
184
public boolean isPreview(Feature feature) {
185
return switch (feature) {
186
//Note: this is a backdoor which allows to optionally treat all features as 'preview' (for testing).
187
//When real preview features will be added, this method can be implemented to return 'true'
188
//for those selected features, and 'false' for all the others.
189
default -> forcePreview;
190
};
191
}
192
193
/**
194
* Generate an error key which captures the fact that a given preview feature could not be used
195
* due to the preview feature support being disabled.
196
* @param feature the feature for which the diagnostic has to be generated.
197
* @return the diagnostic.
198
*/
199
public Error disabledError(Feature feature) {
200
Assert.check(!isEnabled());
201
return feature.isPlural() ?
202
Errors.PreviewFeatureDisabledPlural(feature.nameFragment()) :
203
Errors.PreviewFeatureDisabled(feature.nameFragment());
204
}
205
206
/**
207
* Generate an error key which captures the fact that a preview classfile cannot be loaded
208
* due to the preview feature support being disabled.
209
* @param classfile the name of the classfile with preview features enabled
210
* @param majorVersion the major version found in the classfile.
211
*/
212
public Error disabledError(JavaFileObject classfile, int majorVersion) {
213
Assert.check(!isEnabled());
214
return Errors.PreviewFeatureDisabledClassfile(classfile, majorVersionToSource.get(majorVersion).name);
215
}
216
217
/**
218
* Check whether the given symbol has been declared using
219
* a preview language feature.
220
*
221
* @param sym Symbol to check
222
* @return true iff sym has been declared using a preview language feature
223
*/
224
public boolean declaredUsingPreviewFeature(Symbol sym) {
225
return false;
226
}
227
228
/**
229
* Report any deferred diagnostics.
230
*/
231
public void reportDeferredDiagnostics() {
232
previewHandler.reportDeferredDiagnostic();
233
}
234
235
public void clear() {
236
previewHandler.clear();
237
}
238
239
}
240
241