Skip to content

Commit 41daa88

Browse files
lgxbslgxmcimadamore
authored andcommitted
8265899: Use pattern matching for instanceof at module jdk.compiler(part 1)
Reviewed-by: mcimadamore
1 parent a6f2863 commit 41daa88

35 files changed

+251
-289
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/api/ClientCodeWrapper.java

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 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
@@ -119,9 +119,9 @@ protected ClientCodeWrapper(Context context) {
119119
public JavaFileManager wrap(JavaFileManager fm) {
120120
if (isTrusted(fm))
121121
return fm;
122-
if (fm instanceof StandardJavaFileManager)
123-
return new WrappedStandardJavaFileManager((StandardJavaFileManager) fm);
124-
return new WrappedJavaFileManager(fm);
122+
return (fm instanceof StandardJavaFileManager standardJavaFileManager) ?
123+
new WrappedStandardJavaFileManager(standardJavaFileManager) :
124+
new WrappedJavaFileManager(fm);
125125
}
126126

127127
public FileObject wrap(FileObject fo) {
@@ -131,10 +131,8 @@ public FileObject wrap(FileObject fo) {
131131
}
132132

133133
FileObject unwrap(FileObject fo) {
134-
if (fo instanceof WrappedFileObject)
135-
return ((WrappedFileObject) fo).clientFileObject;
136-
else
137-
return fo;
134+
return (fo instanceof WrappedFileObject wrappedFileObject) ?
135+
wrappedFileObject.clientFileObject : fo;
138136
}
139137

140138
public JavaFileObject wrap(JavaFileObject fo) {
@@ -151,10 +149,8 @@ public Iterable<JavaFileObject> wrapJavaFileObjects(Iterable<? extends JavaFileO
151149
}
152150

153151
JavaFileObject unwrap(JavaFileObject fo) {
154-
if (fo instanceof WrappedJavaFileObject)
155-
return ((JavaFileObject) ((WrappedJavaFileObject) fo).clientFileObject);
156-
else
157-
return fo;
152+
return (fo instanceof WrappedJavaFileObject wrappedJavaFileObject) ?
153+
((JavaFileObject) wrappedJavaFileObject.clientFileObject) : fo;
158154
}
159155

160156
public <T /*super JavaFileObject*/> DiagnosticListener<T> wrap(DiagnosticListener<T> dl) {
@@ -170,10 +166,8 @@ TaskListener wrap(TaskListener tl) {
170166
}
171167

172168
TaskListener unwrap(TaskListener l) {
173-
if (l instanceof WrappedTaskListener)
174-
return ((WrappedTaskListener) l).clientTaskListener;
175-
else
176-
return l;
169+
return (l instanceof WrappedTaskListener wrappedTaskListener) ?
170+
wrappedTaskListener.clientTaskListener : l;
177171
}
178172

179173
Collection<TaskListener> unwrap(Collection<? extends TaskListener> listeners) {
@@ -185,12 +179,8 @@ Collection<TaskListener> unwrap(Collection<? extends TaskListener> listeners) {
185179

186180
@SuppressWarnings("unchecked")
187181
private <T> Diagnostic<T> unwrap(final Diagnostic<T> diagnostic) {
188-
if (diagnostic instanceof JCDiagnostic) {
189-
JCDiagnostic d = (JCDiagnostic) diagnostic;
190-
return (Diagnostic<T>) new DiagnosticSourceUnwrapper(d);
191-
} else {
192-
return diagnostic;
193-
}
182+
return (diagnostic instanceof JCDiagnostic jcDiagnostic) ?
183+
(Diagnostic<T>) new DiagnosticSourceUnwrapper(jcDiagnostic) : diagnostic;
194184
}
195185

196186
protected boolean isTrusted(Object o) {

src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacScope.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,9 @@ public boolean isStarImportScope() {
127127
}
128128

129129
public boolean equals(Object other) {
130-
if (other instanceof JavacScope) {
131-
JavacScope s = (JavacScope) other;
132-
return (env.equals(s.env)
133-
&& isStarImportScope() == s.isStarImportScope());
134-
} else
135-
return false;
130+
return other instanceof JavacScope javacScope
131+
&& env.equals(javacScope.env)
132+
&& isStarImportScope() == javacScope.isStarImportScope();
136133
}
137134

138135
public int hashCode() {

src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2018, 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
@@ -232,7 +232,7 @@ <T> String toString(Iterable<T> items, String sep) {
232232
void cleanup() {
233233
if (compiler != null)
234234
compiler.close();
235-
if (fileManager instanceof BaseFileManager && ((BaseFileManager) fileManager).autoClose) {
235+
if (fileManager instanceof BaseFileManager baseFileManager && baseFileManager.autoClose) {
236236
try {
237237
fileManager.close();
238238
} catch (IOException ignore) {
@@ -321,10 +321,10 @@ public Iterable<? extends Element> enter(Iterable<? extends CompilationUnitTree>
321321
}
322322
else {
323323
for (CompilationUnitTree cu : trees) {
324-
if (cu instanceof JCCompilationUnit) {
324+
if (cu instanceof JCCompilationUnit compilationUnit) {
325325
if (roots == null)
326326
roots = new ListBuffer<>();
327-
roots.append((JCCompilationUnit)cu);
327+
roots.append(compilationUnit);
328328
notYetEntered.remove(cu.getSourceFile());
329329
}
330330
else

src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTaskPool.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,9 @@ void clear() {
295295
TreeScanner<Void, Symtab> pollutionScanner = new TreeScanner<Void, Symtab>() {
296296
@Override @DefinedBy(Api.COMPILER_TREE)
297297
public Void scan(Tree tree, Symtab syms) {
298-
if (tree instanceof LetExpr) {
299-
LetExpr le = (LetExpr) tree;
300-
scan(le.defs, syms);
301-
scan(le.expr, syms);
298+
if (tree instanceof LetExpr letExpr) {
299+
scan(letExpr.defs, syms);
300+
scan(letExpr.expr, syms);
302301
return null;
303302
} else {
304303
return super.scan(tree, syms);

src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTool.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2016, 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
@@ -173,8 +173,8 @@ public JavacTask getTask(Writer out,
173173

174174
if (fileManager == null) {
175175
fileManager = getStandardFileManager(diagnosticListener, null, null);
176-
if (fileManager instanceof BaseFileManager) {
177-
((BaseFileManager) fileManager).autoClose = true;
176+
if (fileManager instanceof BaseFileManager baseFileManager) {
177+
baseFileManager.autoClose = true;
178178
}
179179
}
180180
fileManager = ccw.wrap(fileManager);

src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacTrees.java

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2020, 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
@@ -188,16 +188,16 @@ public class JavacTrees extends DocTrees {
188188

189189
// called reflectively from Trees.instance(CompilationTask task)
190190
public static JavacTrees instance(JavaCompiler.CompilationTask task) {
191-
if (!(task instanceof BasicJavacTask))
191+
if (!(task instanceof BasicJavacTask basicJavacTask))
192192
throw new IllegalArgumentException();
193-
return instance(((BasicJavacTask)task).getContext());
193+
return instance(basicJavacTask.getContext());
194194
}
195195

196196
// called reflectively from Trees.instance(ProcessingEnvironment env)
197197
public static JavacTrees instance(ProcessingEnvironment env) {
198-
if (!(env instanceof JavacProcessingEnvironment))
198+
if (!(env instanceof JavacProcessingEnvironment javacProcessingEnvironment))
199199
throw new IllegalArgumentException();
200-
return instance(((JavacProcessingEnvironment)env).getContext());
200+
return instance(javacProcessingEnvironment.getContext());
201201
}
202202

203203
public static JavacTrees instance(Context context) {
@@ -234,8 +234,8 @@ private void init(Context context) {
234234
syms = Symtab.instance(context);
235235
fileManager = context.get(JavaFileManager.class);
236236
JavacTask t = context.get(JavacTask.class);
237-
if (t instanceof JavacTaskImpl)
238-
javacTaskImpl = (JavacTaskImpl) t;
237+
if (t instanceof JavacTaskImpl taskImpl)
238+
javacTaskImpl = taskImpl;
239239
}
240240

241241
@Override @DefinedBy(Api.COMPILER_TREE)
@@ -264,8 +264,8 @@ public long getStartPosition(CompilationUnitTree file, DocCommentTree comment, D
264264
@Override @DefinedBy(Api.COMPILER_TREE) @SuppressWarnings("fallthrough")
265265
public long getEndPosition(CompilationUnitTree file, DocCommentTree comment, DocTree tree) {
266266
DCDocComment dcComment = (DCDocComment) comment;
267-
if (tree instanceof DCEndPosTree) {
268-
int endPos = ((DCEndPosTree) tree).getEndPos(dcComment);
267+
if (tree instanceof DCEndPosTree<?> dcEndPosTree) {
268+
int endPos = dcEndPosTree.getEndPos(dcComment);
269269

270270
if (endPos != Position.NOPOS) {
271271
return endPos;
@@ -431,11 +431,11 @@ public Symbol getElement(TreePath path) {
431431
@Override @DefinedBy(Api.COMPILER_TREE)
432432
public Element getElement(DocTreePath path) {
433433
DocTree tree = path.getLeaf();
434-
if (tree instanceof DCReference)
435-
return attributeDocReference(path.getTreePath(), ((DCReference) tree));
434+
if (tree instanceof DCReference dcReference)
435+
return attributeDocReference(path.getTreePath(), dcReference);
436436
if (tree instanceof DCIdentifier) {
437-
if (path.getParentPath().getLeaf() instanceof DCParam) {
438-
return attributeParamIdentifier(path.getTreePath(), (DCParam) path.getParentPath().getLeaf());
437+
if (path.getParentPath().getLeaf() instanceof DCParam dcParam) {
438+
return attributeParamIdentifier(path.getTreePath(), dcParam);
439439
}
440440
}
441441
return null;
@@ -444,14 +444,14 @@ public Element getElement(DocTreePath path) {
444444
@Override @DefinedBy(Api.COMPILER_TREE)
445445
public TypeMirror getType(DocTreePath path) {
446446
DocTree tree = path.getLeaf();
447-
if (tree instanceof DCReference) {
448-
JCTree qexpr = ((DCReference)tree).qualifierExpression;
447+
if (tree instanceof DCReference dcReference) {
448+
JCTree qexpr = dcReference.qualifierExpression;
449449
if (qexpr != null) {
450450
Log.DeferredDiagnosticHandler deferredDiagnosticHandler =
451451
new Log.DeferredDiagnosticHandler(log);
452452
try {
453453
Env<AttrContext> env = getAttrContext(path.getTreePath());
454-
Type t = attr.attribType(((DCReference) tree).qualifierExpression, env);
454+
Type t = attr.attribType(dcReference.qualifierExpression, env);
455455
if (t != null && !t.isErroneous()) {
456456
return t;
457457
}
@@ -549,8 +549,8 @@ private Symbol attributeDocReference(TreePath path, DCReference ref) {
549549
} else {
550550
Type e = t;
551551
// If this is an array type convert to element type
552-
while (e instanceof ArrayType)
553-
e = ((ArrayType) e).elemtype;
552+
while (e instanceof ArrayType arrayType)
553+
e = arrayType.elemtype;
554554
tsym = e.tsym;
555555
memberName = (Name) ref.memberName;
556556
}
@@ -808,10 +808,9 @@ public JavacScope getScope(TreePath path) {
808808
public String getDocComment(TreePath path) {
809809
CompilationUnitTree t = path.getCompilationUnit();
810810
Tree leaf = path.getLeaf();
811-
if (t instanceof JCTree.JCCompilationUnit && leaf instanceof JCTree) {
812-
JCCompilationUnit cu = (JCCompilationUnit) t;
813-
if (cu.docComments != null) {
814-
return cu.docComments.getCommentText((JCTree) leaf);
811+
if (t instanceof JCTree.JCCompilationUnit compilationUnit && leaf instanceof JCTree tree) {
812+
if (compilationUnit.docComments != null) {
813+
return compilationUnit.docComments.getCommentText(tree);
815814
}
816815
}
817816
return null;
@@ -821,10 +820,9 @@ public String getDocComment(TreePath path) {
821820
public DocCommentTree getDocCommentTree(TreePath path) {
822821
CompilationUnitTree t = path.getCompilationUnit();
823822
Tree leaf = path.getLeaf();
824-
if (t instanceof JCTree.JCCompilationUnit && leaf instanceof JCTree) {
825-
JCCompilationUnit cu = (JCCompilationUnit) t;
826-
if (cu.docComments != null) {
827-
return cu.docComments.getCommentTree((JCTree) leaf);
823+
if (t instanceof JCTree.JCCompilationUnit compilationUnit && leaf instanceof JCTree tree) {
824+
if (compilationUnit.docComments != null) {
825+
return compilationUnit.docComments.getCommentTree(tree);
828826
}
829827
}
830828
return null;
@@ -853,22 +851,17 @@ public DocCommentTree getDocCommentTree(Element e, String relativeFileName) thro
853851

854852
@Override @DefinedBy(Api.COMPILER_TREE)
855853
public boolean isAccessible(Scope scope, TypeElement type) {
856-
if (scope instanceof JavacScope && type instanceof ClassSymbol) {
857-
Env<AttrContext> env = ((JavacScope) scope).env;
858-
return resolve.isAccessible(env, (ClassSymbol)type, true);
859-
} else
860-
return false;
854+
return (scope instanceof JavacScope javacScope)
855+
&& (type instanceof ClassSymbol classSymbol)
856+
&& resolve.isAccessible(javacScope.env, classSymbol, true);
861857
}
862858

863859
@Override @DefinedBy(Api.COMPILER_TREE)
864860
public boolean isAccessible(Scope scope, Element member, DeclaredType type) {
865-
if (scope instanceof JavacScope
866-
&& member instanceof Symbol
867-
&& type instanceof com.sun.tools.javac.code.Type) {
868-
Env<AttrContext> env = ((JavacScope) scope).env;
869-
return resolve.isAccessible(env, (com.sun.tools.javac.code.Type)type, (Symbol)member, true);
870-
} else
871-
return false;
861+
return (scope instanceof JavacScope javacScope)
862+
&& (member instanceof Symbol symbol)
863+
&& (type instanceof com.sun.tools.javac.code.Type codeType)
864+
&& resolve.isAccessible(javacScope.env, codeType, symbol, true);
872865
}
873866

874867
private Env<AttrContext> getAttrContext(TreePath path) {
@@ -1071,10 +1064,9 @@ public void visitVarDef(JCVariableDecl tree) {
10711064
static JavaFileObject asJavaFileObject(FileObject fileObject) {
10721065
JavaFileObject jfo = null;
10731066

1074-
if (fileObject instanceof JavaFileObject) {
1075-
jfo = (JavaFileObject) fileObject;
1067+
if (fileObject instanceof JavaFileObject javaFileObject) {
10761068
checkHtmlKind(fileObject, Kind.HTML);
1077-
return jfo;
1069+
return javaFileObject;
10781070
}
10791071

10801072
checkHtmlKind(fileObject);
@@ -1217,17 +1209,16 @@ protected Copier createCopier(TreeMaker maker) {
12171209
*/
12181210
@Override @DefinedBy(Api.COMPILER_TREE)
12191211
public TypeMirror getOriginalType(javax.lang.model.type.ErrorType errorType) {
1220-
if (errorType instanceof com.sun.tools.javac.code.Type.ErrorType) {
1221-
return ((com.sun.tools.javac.code.Type.ErrorType)errorType).getOriginalType();
1212+
if (errorType instanceof com.sun.tools.javac.code.Type.ErrorType targetErrorType) {
1213+
return targetErrorType.getOriginalType();
12221214
}
1223-
if (errorType instanceof com.sun.tools.javac.code.Type.ClassType &&
1215+
if (errorType instanceof com.sun.tools.javac.code.Type.ClassType classType &&
12241216
errorType.getKind() == TypeKind.ERROR) {
1225-
ClassType ct = (ClassType) errorType;
1226-
return extraType2OriginalMap.computeIfAbsent(ct, tt ->
1227-
new ClassType(ct.getEnclosingType(), ct.typarams_field,
1228-
ct.tsym, ct.getMetadata()) {
1217+
return extraType2OriginalMap.computeIfAbsent(classType, tt ->
1218+
new ClassType(classType.getEnclosingType(), classType.typarams_field,
1219+
classType.tsym, classType.getMetadata()) {
12291220
@Override
1230-
public Type baseType() { return ct; }
1221+
public Type baseType() { return classType; }
12311222
@Override
12321223
public TypeKind getKind() {
12331224
return TypeKind.DECLARED;

src/jdk.compiler/share/classes/com/sun/tools/javac/file/JRTIndex.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 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
@@ -229,8 +229,8 @@ synchronized Entry getEntry(RelativeDirectory rd) throws IOException {
229229
}
230230

231231
public boolean isInJRT(FileObject fo) {
232-
if (fo instanceof PathFileObject) {
233-
Path path = ((PathFileObject) fo).getPath();
232+
if (fo instanceof PathFileObject pathFileObject) {
233+
Path path = pathFileObject.getPath();
234234
return (path.getFileSystem() == jrtfs);
235235
} else {
236236
return false;

0 commit comments

Comments
 (0)