Skip to content

Commit 0d4de8a

Browse files
committed
8316971: Add Lint warning for restricted method calls
Reviewed-by: ihse, vromero
1 parent d4c904d commit 0d4de8a

File tree

15 files changed

+119
-4
lines changed

15 files changed

+119
-4
lines changed

make/modules/java.base/Java.gmk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
# questions.
2424
#
2525

26-
DISABLED_WARNINGS_java += this-escape
26+
DISABLED_WARNINGS_java += this-escape restricted
2727

2828
DOCLINT += -Xdoclint:all/protected \
2929
'-Xdoclint/package:java.*,javax.*'

make/test/BuildMicrobenchmark.gmk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ $(eval $(call SetupJavaCompilation, BUILD_JDK_MICROBENCHMARK, \
9191
TARGET_RELEASE := $(TARGET_RELEASE_NEWJDK_UPGRADED), \
9292
SMALL_JAVA := false, \
9393
CLASSPATH := $(MICROBENCHMARK_CLASSPATH), \
94-
DISABLED_WARNINGS := this-escape processing rawtypes cast serial preview, \
94+
DISABLED_WARNINGS := restricted this-escape processing rawtypes cast serial preview, \
9595
SRC := $(MICROBENCHMARK_SRC), \
9696
BIN := $(MICROBENCHMARK_CLASSES), \
9797
JAVAC_FLAGS := --add-exports java.base/sun.security.util=ALL-UNNAMED \

src/jdk.compiler/share/classes/com/sun/tools/javac/code/Flags.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,13 +389,18 @@ public static EnumSet<Flag> asFlagSet(long flags) {
389389
*/
390390
public static final long SEALED = 1L<<62; // ClassSymbols
391391

392+
/**
393+
* Flag to indicate restricted method declaration.
394+
*/
395+
public static final long RESTRICTED = 1L<<62; // MethodSymbols
396+
392397
/**
393398
* Flag to indicate that the class/interface was declared with the non-sealed modifier.
394399
*/
395400
public static final long NON_SEALED = 1L<<63; // ClassSymbols
396401

397402
/**
398-
* Describe modifier flags as they migh appear in source code, i.e.,
403+
* Describe modifier flags as they might appear in source code, i.e.,
399404
* separated by spaces and in the order suggested by JLS 8.1.1.
400405
*/
401406
public static String toSource(long flags) {

src/jdk.compiler/share/classes/com/sun/tools/javac/code/Lint.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,12 @@ public enum LintCategory {
335335
/**
336336
* Warn about use of preview features.
337337
*/
338-
PREVIEW("preview");
338+
PREVIEW("preview"),
339+
340+
/**
341+
* Warn about use of restricted methods.
342+
*/
343+
RESTRICTED("restricted");
339344

340345
LintCategory(String option) {
341346
this(option, false);

src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symtab.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ public static Symtab instance(Context context) {
220220
public final Type functionalInterfaceType;
221221
public final Type previewFeatureType;
222222
public final Type previewFeatureInternalType;
223+
public final Type restrictedType;
223224
public final Type typeDescriptorType;
224225
public final Type recordType;
225226
public final Type switchBootstrapsType;
@@ -610,6 +611,7 @@ public <R, P> R accept(ElementVisitor<R, P> v, P p) {
610611
functionalInterfaceType = enterClass("java.lang.FunctionalInterface");
611612
previewFeatureType = enterClass("jdk.internal.javac.PreviewFeature");
612613
previewFeatureInternalType = enterSyntheticAnnotation("jdk.internal.PreviewFeature+Annotation");
614+
restrictedType = enterClass("jdk.internal.javac.Restricted");
613615
typeDescriptorType = enterClass("java.lang.invoke.TypeDescriptor");
614616
recordType = enterClass("java.lang.Record");
615617
switchBootstrapsType = enterClass("java.lang.runtime.SwitchBootstraps");

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Annotate.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,11 @@ private <T extends Attribute.Compound> void annotateNow(Symbol toAnnotate,
379379
&& types.isSameType(c.type, syms.valueBasedType)) {
380380
toAnnotate.flags_field |= Flags.VALUE_BASED;
381381
}
382+
383+
if (!c.type.isErroneous()
384+
&& types.isSameType(c.type, syms.restrictedType)) {
385+
toAnnotate.flags_field |= Flags.RESTRICTED;
386+
}
382387
}
383388

384389
List<T> buf = List.nil();

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4742,6 +4742,7 @@ else if (ownOuter.hasTag(CLASS) && site != ownOuter) {
47424742
new ResultInfo(resultInfo.pkind, resultInfo.pt.getReturnType(), resultInfo.checkContext, resultInfo.checkMode),
47434743
env, TreeInfo.args(env.tree), resultInfo.pt.getParameterTypes(),
47444744
resultInfo.pt.getTypeArguments());
4745+
chk.checkRestricted(tree.pos(), sym);
47454746
break;
47464747
}
47474748
case PCK: case ERR:

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,15 @@ public void warnDeclaredUsingPreview(DiagnosticPosition pos, Symbol sym) {
283283
preview.reportPreviewWarning(pos, Warnings.DeclaredUsingPreview(kindName(sym), sym));
284284
}
285285

286+
/** Log a preview warning.
287+
* @param pos Position to be used for error reporting.
288+
* @param msg A Warning describing the problem.
289+
*/
290+
public void warnRestrictedAPI(DiagnosticPosition pos, Symbol sym) {
291+
if (lint.isEnabled(LintCategory.RESTRICTED))
292+
log.warning(LintCategory.RESTRICTED, pos, Warnings.RestrictedMethod(sym.enclClass(), sym));
293+
}
294+
286295
/** Warn about unchecked operation.
287296
* @param pos Position to be used for error reporting.
288297
* @param msg A string describing the problem.
@@ -3850,6 +3859,12 @@ void checkPreview(DiagnosticPosition pos, Symbol other, Symbol s) {
38503859
}
38513860
}
38523861

3862+
void checkRestricted(DiagnosticPosition pos, Symbol s) {
3863+
if (s.kind == MTH && (s.flags() & RESTRICTED) != 0) {
3864+
deferredLintHandler.report(() -> warnRestrictedAPI(pos, s));
3865+
}
3866+
}
3867+
38533868
/* *************************************************************************
38543869
* Check for recursive annotation elements.
38553870
**************************************************************************/

src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,6 +1509,9 @@ else if (proxy.type.tsym.flatName() == syms.profileType.tsym.flatName()) {
15091509
} else if (proxy.type.tsym.flatName() == syms.valueBasedInternalType.tsym.flatName()) {
15101510
Assert.check(sym.kind == TYP);
15111511
sym.flags_field |= VALUE_BASED;
1512+
} else if (proxy.type.tsym.flatName() == syms.restrictedType.tsym.flatName()) {
1513+
Assert.check(sym.kind == MTH);
1514+
sym.flags_field |= RESTRICTED;
15121515
} else {
15131516
if (proxy.type.tsym == syms.annotationTargetType.tsym) {
15141517
target = proxy;
@@ -1522,6 +1525,9 @@ else if (proxy.type.tsym.flatName() == syms.profileType.tsym.flatName()) {
15221525
setFlagIfAttributeTrue(proxy, sym, names.reflective, PREVIEW_REFLECTIVE);
15231526
} else if (proxy.type.tsym == syms.valueBasedType.tsym && sym.kind == TYP) {
15241527
sym.flags_field |= VALUE_BASED;
1528+
} else if (proxy.type.tsym == syms.restrictedType.tsym) {
1529+
Assert.check(sym.kind == MTH);
1530+
sym.flags_field |= RESTRICTED;
15251531
}
15261532
proxies.append(proxy);
15271533
}

src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,6 +1917,11 @@ compiler.err.is.preview=\
19171917
compiler.warn.is.preview.reflective=\
19181918
{0} is a reflective preview API and may be removed in a future release.
19191919

1920+
# 0: symbol, 1: symbol
1921+
compiler.warn.restricted.method=\
1922+
{0}.{1} is a restricted method.\n\
1923+
(Restricted methods are unsafe and, if used incorrectly, might crash the Java runtime or corrupt memory)
1924+
19201925
# 0: symbol
19211926
compiler.warn.has.been.deprecated.module=\
19221927
module {0} has been deprecated

0 commit comments

Comments
 (0)