Skip to content

Commit 082abbd

Browse files
lgxbslgxmcimadamore
authored andcommitted
8265900: Use pattern matching for instanceof at module jdk.compiler(part 2)
Reviewed-by: mcimadamore
1 parent 851b219 commit 082abbd

29 files changed

+204
-276
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2021, 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
@@ -168,8 +168,8 @@ private Attribute.Compound[] unpackContained(Attribute.Compound container) {
168168
ListBuffer<Attribute.Compound> compounds = new ListBuffer<>();
169169
if (contained0 != null) {
170170
for (Attribute a : contained0)
171-
if (a instanceof Attribute.Compound)
172-
compounds = compounds.append((Attribute.Compound)a);
171+
if (a instanceof Attribute.Compound attributeCompound)
172+
compounds = compounds.append(attributeCompound);
173173
}
174174
return compounds.toArray(new Attribute.Compound[compounds.size()]);
175175
}

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2021, 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
@@ -87,8 +87,8 @@ public Object getValue() {
8787
}
8888
@DefinedBy(Api.LANGUAGE_MODEL)
8989
public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
90-
if (value instanceof String)
91-
return v.visitString((String) value, p);
90+
if (value instanceof String str)
91+
return v.visitString(str, p);
9292
if (value instanceof Integer) {
9393
int i = (Integer) value;
9494
switch (type.getTag()) {
@@ -198,12 +198,10 @@ private Compound getFirstEmbeddedTC() {
198198
if (values.size() == 1) {
199199
Pair<MethodSymbol, Attribute> val = values.get(0);
200200
if (val.fst.getSimpleName().contentEquals("value")
201-
&& val.snd instanceof Array) {
202-
Array arr = (Array) val.snd;
203-
if (arr.values.length != 0
204-
&& arr.values[0] instanceof Attribute.TypeCompound)
205-
return (Attribute.TypeCompound) arr.values[0];
206-
}
201+
&& val.snd instanceof Array arr
202+
&& arr.values.length != 0
203+
&& arr.values[0] instanceof Attribute.TypeCompound compound)
204+
return compound;
207205
}
208206
return null;
209207
}

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2021, 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
@@ -209,12 +209,11 @@ protected ClassFinder(Context context) {
209209
// Temporary, until more info is available from the module system.
210210
boolean useCtProps;
211211
JavaFileManager fm = context.get(JavaFileManager.class);
212-
if (fm instanceof DelegatingJavaFileManager) {
213-
fm = ((DelegatingJavaFileManager) fm).getBaseFileManager();
212+
if (fm instanceof DelegatingJavaFileManager delegatingJavaFileManager) {
213+
fm = delegatingJavaFileManager.getBaseFileManager();
214214
}
215-
if (fm instanceof JavacFileManager) {
216-
JavacFileManager jfm = (JavacFileManager) fm;
217-
useCtProps = jfm.isDefaultBootClassPath() && jfm.isSymbolFileEnabled();
215+
if (fm instanceof JavacFileManager javacFileManager) {
216+
useCtProps = javacFileManager.isDefaultBootClassPath() && javacFileManager.isSymbolFileEnabled();
218217
} else if (fm.getClass().getName().equals("com.sun.tools.sjavac.comp.SmartFileManager")) {
219218
useCtProps = !options.isSet("ignore.symbol.file");
220219
} else {
@@ -642,27 +641,26 @@ private void scanUserPaths(PackageSymbol p, boolean includeSourcePath) throws IO
642641

643642
if (verbose && verbosePath) {
644643
verbosePath = false; // print once per compile
645-
if (fileManager instanceof StandardJavaFileManager) {
646-
StandardJavaFileManager fm = (StandardJavaFileManager)fileManager;
644+
if (fileManager instanceof StandardJavaFileManager standardJavaFileManager) {
647645
if (haveSourcePath && wantSourceFiles) {
648646
List<Path> path = List.nil();
649-
for (Path sourcePath : fm.getLocationAsPaths(SOURCE_PATH)) {
647+
for (Path sourcePath : standardJavaFileManager.getLocationAsPaths(SOURCE_PATH)) {
650648
path = path.prepend(sourcePath);
651649
}
652650
log.printVerbose("sourcepath", path.reverse().toString());
653651
} else if (wantSourceFiles) {
654652
List<Path> path = List.nil();
655-
for (Path classPath : fm.getLocationAsPaths(CLASS_PATH)) {
653+
for (Path classPath : standardJavaFileManager.getLocationAsPaths(CLASS_PATH)) {
656654
path = path.prepend(classPath);
657655
}
658656
log.printVerbose("sourcepath", path.reverse().toString());
659657
}
660658
if (wantClassFiles) {
661659
List<Path> path = List.nil();
662-
for (Path platformPath : fm.getLocationAsPaths(PLATFORM_CLASS_PATH)) {
660+
for (Path platformPath : standardJavaFileManager.getLocationAsPaths(PLATFORM_CLASS_PATH)) {
663661
path = path.prepend(platformPath);
664662
}
665-
for (Path classPath : fm.getLocationAsPaths(CLASS_PATH)) {
663+
for (Path classPath : standardJavaFileManager.getLocationAsPaths(CLASS_PATH)) {
666664
path = path.prepend(classPath);
667665
}
668666
log.printVerbose("classpath", path.reverse().toString());

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2009, 2021, 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
@@ -263,11 +263,9 @@ public <R, P> R accept(DirectiveVisitor<R, P> v, P p) {
263263
// TODO: delete?
264264
@Override
265265
public boolean equals(Object obj) {
266-
if (!(obj instanceof ProvidesDirective)) {
267-
return false;
268-
}
269-
ProvidesDirective other = (ProvidesDirective)obj;
270-
return service == other.service && impls.equals(other.impls);
266+
return (obj instanceof ProvidesDirective directive)
267+
&& service == directive.service
268+
&& impls.equals(directive.impls);
271269
}
272270

273271
// TODO: delete?
@@ -359,11 +357,8 @@ public <R, P> R accept(DirectiveVisitor<R, P> v, P p) {
359357
// TODO: delete?
360358
@Override
361359
public boolean equals(Object obj) {
362-
if (!(obj instanceof UsesDirective)) {
363-
return false;
364-
}
365-
UsesDirective other = (UsesDirective)obj;
366-
return service == other.service;
360+
return (obj instanceof UsesDirective directive)
361+
&& service == directive.service;
367362
}
368363

369364
// TODO: delete?

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -753,8 +753,9 @@ public void finalizeScope() {
753753
}
754754

755755
protected Scope finalizeSingleScope(Scope impScope) {
756-
if (impScope instanceof FilterImportScope && impScope.owner.kind == Kind.TYP &&
757-
((FilterImportScope) impScope).isStaticallyImported()) {
756+
if (impScope instanceof FilterImportScope filterImportScope
757+
&& impScope.owner.kind == Kind.TYP
758+
&& filterImportScope.isStaticallyImported()) {
758759
WriteableScope finalized = WriteableScope.create(impScope.owner);
759760

760761
for (Symbol sym : impScope.getSymbols()) {

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

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,13 +1420,12 @@ public void complete() throws CompletionFailure {
14201420
@DefinedBy(Api.LANGUAGE_MODEL)
14211421
public List<Type> getInterfaces() {
14221422
apiComplete();
1423-
if (type instanceof ClassType) {
1424-
ClassType t = (ClassType)type;
1425-
if (t.interfaces_field == null) // FIXME: shouldn't be null
1426-
t.interfaces_field = List.nil();
1427-
if (t.all_interfaces_field != null)
1428-
return Type.getModelTypes(t.all_interfaces_field);
1429-
return t.interfaces_field;
1423+
if (type instanceof ClassType classType) {
1424+
if (classType.interfaces_field == null) // FIXME: shouldn't be null
1425+
classType.interfaces_field = List.nil();
1426+
if (classType.all_interfaces_field != null)
1427+
return Type.getModelTypes(classType.all_interfaces_field);
1428+
return classType.interfaces_field;
14301429
} else {
14311430
return List.nil();
14321431
}
@@ -1435,14 +1434,13 @@ public List<Type> getInterfaces() {
14351434
@DefinedBy(Api.LANGUAGE_MODEL)
14361435
public Type getSuperclass() {
14371436
apiComplete();
1438-
if (type instanceof ClassType) {
1439-
ClassType t = (ClassType)type;
1440-
if (t.supertype_field == null) // FIXME: shouldn't be null
1441-
t.supertype_field = Type.noType;
1437+
if (type instanceof ClassType classType) {
1438+
if (classType.supertype_field == null) // FIXME: shouldn't be null
1439+
classType.supertype_field = Type.noType;
14421440
// An interface has no superclass; its supertype is Object.
1443-
return t.isInterface()
1441+
return classType.isInterface()
14441442
? Type.noType
1445-
: t.supertype_field.getModelType();
1443+
: classType.supertype_field.getModelType();
14461444
} else {
14471445
return Type.noType;
14481446
}
@@ -1585,15 +1583,14 @@ public void reset() {
15851583
erasure_field = null;
15861584
members_field = null;
15871585
flags_field = 0;
1588-
if (type instanceof ClassType) {
1589-
ClassType t = (ClassType)type;
1590-
t.setEnclosingType(Type.noType);
1591-
t.rank_field = -1;
1592-
t.typarams_field = null;
1593-
t.allparams_field = null;
1594-
t.supertype_field = null;
1595-
t.interfaces_field = null;
1596-
t.all_interfaces_field = null;
1586+
if (type instanceof ClassType classType) {
1587+
classType.setEnclosingType(Type.noType);
1588+
classType.rank_field = -1;
1589+
classType.typarams_field = null;
1590+
classType.allparams_field = null;
1591+
classType.supertype_field = null;
1592+
classType.interfaces_field = null;
1593+
classType.all_interfaces_field = null;
15971594
}
15981595
clearAnnotationMetadata();
15991596
}
@@ -1754,13 +1751,12 @@ public Object getConstValue() {
17541751
if (data == ElementKind.EXCEPTION_PARAMETER ||
17551752
data == ElementKind.RESOURCE_VARIABLE) {
17561753
return null;
1757-
} else if (data instanceof Callable<?>) {
1754+
} else if (data instanceof Callable<?> callableData) {
17581755
// In this case, this is a final variable, with an as
17591756
// yet unevaluated initializer.
1760-
Callable<?> eval = (Callable<?>)data;
17611757
data = null; // to make sure we don't evaluate this twice.
17621758
try {
1763-
data = eval.call();
1759+
data = callableData.call();
17641760
} catch (Exception ex) {
17651761
throw new AssertionError(ex);
17661762
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2021, 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
@@ -276,8 +276,7 @@ public void removeDeclarationMetadata(Attribute.Compound compound) {
276276
if (attrCompound.isSynthesized() && !attrCompound.values.isEmpty()) {
277277
Pair<Symbol.MethodSymbol, Attribute> val = attrCompound.values.get(0);
278278
if (val.fst.getSimpleName().contentEquals("value") &&
279-
val.snd instanceof Attribute.Array) {
280-
Attribute.Array arr = (Attribute.Array) val.snd;
279+
val.snd instanceof Attribute.Array arr) {
281280
if (arr.values.length != 0
282281
&& arr.values[0] instanceof Attribute.Compound
283282
&& arr.values[0].type == compound.type) {

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,13 +1359,8 @@ public String toString() {
13591359

13601360
@Override @DefinedBy(Api.LANGUAGE_MODEL)
13611361
public boolean equals(Object obj) {
1362-
if (obj instanceof ArrayType) {
1363-
ArrayType that = (ArrayType)obj;
1364-
return this == that ||
1365-
elemtype.equals(that.elemtype);
1366-
}
1367-
1368-
return false;
1362+
return (obj instanceof ArrayType arrayType)
1363+
&& (this == arrayType || elemtype.equals(arrayType.elemtype));
13691364
}
13701365

13711366
@DefinedBy(Api.LANGUAGE_MODEL)

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,9 @@ public String toString() {
103103

104104
@Override
105105
public boolean equals(Object other) {
106-
if (! (other instanceof TypePathEntry)) {
107-
return false;
108-
}
109-
TypePathEntry tpe = (TypePathEntry) other;
110-
return this.tag == tpe.tag && this.arg == tpe.arg;
106+
return (other instanceof TypePathEntry entry)
107+
&& this.tag == entry.tag
108+
&& this.arg == entry.arg;
111109
}
112110

113111
@Override

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2009, 2021, 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
@@ -155,11 +155,11 @@ public List<Attribute> annotationTargets(TypeSymbol tsym) {
155155
}
156156

157157
Attribute atValue = atTarget.member(names.value);
158-
if (!(atValue instanceof Attribute.Array)) {
158+
if (!(atValue instanceof Attribute.Array arrayVal)) {
159159
return null;
160160
}
161161

162-
List<Attribute> targets = ((Array)atValue).getValue();
162+
List<Attribute> targets = arrayVal.getValue();
163163
if (targets.stream().anyMatch(a -> !(a instanceof Attribute.Enum))) {
164164
return null;
165165
}

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

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3161,12 +3161,9 @@ class Entry {
31613161

31623162
@Override
31633163
public boolean equals(Object obj) {
3164-
if (obj instanceof Entry) {
3165-
Entry e = (Entry)obj;
3166-
return e.msym == msym && isSameType(site, e.site);
3167-
} else {
3168-
return false;
3169-
}
3164+
return (obj instanceof Entry entry)
3165+
&& entry.msym == msym
3166+
&& isSameType(site, entry.site);
31703167
}
31713168

31723169
@Override
@@ -3850,11 +3847,9 @@ public int hashCode() {
38503847
}
38513848
@Override
38523849
public boolean equals(Object obj) {
3853-
if (!(obj instanceof TypePair))
3854-
return false;
3855-
TypePair typePair = (TypePair)obj;
3856-
return isSameType(t1, typePair.t1)
3857-
&& isSameType(t2, typePair.t2);
3850+
return (obj instanceof TypePair typePair)
3851+
&& isSameType(t1, typePair.t1)
3852+
&& isSameType(t2, typePair.t2);
38583853
}
38593854
}
38603855
Set<TypePair> mergeCache = new HashSet<>();
@@ -4877,8 +4872,8 @@ public int hashCode() {
48774872
}
48784873

48794874
public boolean equals(Object obj) {
4880-
return (obj instanceof UniqueType) &&
4881-
types.isSameType(type, ((UniqueType)obj).type);
4875+
return (obj instanceof UniqueType uniqueType) &&
4876+
types.isSameType(type, uniqueType.type);
48824877
}
48834878

48844879
public String toString() {
@@ -5032,8 +5027,8 @@ public RetentionPolicy getRetention(TypeSymbol sym) {
50325027
Attribute.Compound c = sym.attribute(syms.retentionType.tsym);
50335028
if (c != null) {
50345029
Attribute value = c.member(names.value);
5035-
if (value != null && value instanceof Attribute.Enum) {
5036-
Name levelName = ((Attribute.Enum)value).value.name;
5030+
if (value != null && value instanceof Attribute.Enum attributeEnum) {
5031+
Name levelName = attributeEnum.value.name;
50375032
if (levelName == names.SOURCE) vis = RetentionPolicy.SOURCE;
50385033
else if (levelName == names.CLASS) vis = RetentionPolicy.CLASS;
50395034
else if (levelName == names.RUNTIME) vis = RetentionPolicy.RUNTIME;

0 commit comments

Comments
 (0)