Skip to content

Commit 851b219

Browse files
lgxbslgxJoel Borggrén-Franck
authored and
Joel Borggrén-Franck
committed
8265901: Use pattern matching for instanceof at module jdk.compiler(part 3)
Reviewed-by: mcimadamore, jfranck
1 parent fb8f0c5 commit 851b219

File tree

9 files changed

+40
-45
lines changed

9 files changed

+40
-45
lines changed

src/jdk.compiler/share/classes/com/sun/tools/sjavac/Module.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2015, 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
@@ -62,7 +62,7 @@ public Module(String n, String dn) {
6262

6363
@Override
6464
public boolean equals(Object o) {
65-
return (o instanceof Module) && name.equals(((Module)o).name);
65+
return (o instanceof Module module) && name.equals(module.name);
6666
}
6767

6868
@Override

src/jdk.compiler/share/classes/com/sun/tools/sjavac/Package.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2015, 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
@@ -117,7 +117,7 @@ public Package(Module m, String n) {
117117

118118
@Override
119119
public boolean equals(Object o) {
120-
return (o instanceof Package) && name.equals(((Package)o).name);
120+
return (o instanceof Package pac) && name.equals(pac.name);
121121
}
122122

123123
@Override

src/jdk.compiler/share/classes/com/sun/tools/sjavac/Source.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2019, 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
@@ -72,7 +72,7 @@ public class Source implements Comparable<Source> {
7272

7373
@Override
7474
public boolean equals(Object o) {
75-
return (o instanceof Source) && name.equals(((Source)o).name);
75+
return (o instanceof Source source) && name.equals(source.name);
7676
}
7777

7878
@Override

src/jdk.compiler/share/classes/com/sun/tools/sjavac/comp/JavaFileObjectWithLocation.java

Lines changed: 4 additions & 5 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
@@ -60,9 +60,8 @@ public int hashCode() {
6060

6161
@Override
6262
public boolean equals(Object obj) {
63-
if (!(obj instanceof JavaFileObjectWithLocation))
64-
return false;
65-
JavaFileObjectWithLocation<?> other = (JavaFileObjectWithLocation<?>) obj;
66-
return loc.equals(other.loc) && fileObject.equals(other.fileObject);
63+
return (obj instanceof JavaFileObjectWithLocation<?> javaFileObjectWithLocation)
64+
&& loc.equals(javaFileObjectWithLocation.loc)
65+
&& fileObject.equals(javaFileObjectWithLocation.fileObject);
6766
}
6867
}

src/jdk.compiler/share/classes/com/sun/tools/sjavac/comp/PathAndPackageVerifier.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
@@ -138,8 +138,8 @@ public boolean hasNext() {
138138
@Override
139139
public String next() {
140140
Name name;
141-
if (next instanceof JCIdent) {
142-
name = ((JCIdent) next).name;
141+
if (next instanceof JCIdent identNext) {
142+
name = identNext.name;
143143
next = null;
144144
} else {
145145
JCFieldAccess fa = (JCFieldAccess) next;

src/jdk.compiler/share/classes/com/sun/tools/sjavac/comp/SjavacImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2016, 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
@@ -122,9 +122,9 @@ public Result compile(String[] args) {
122122

123123
// Clean up
124124
JavaFileManager fileManager = context.get(JavaFileManager.class);
125-
if (fileManager instanceof JavacFileManager) {
125+
if (fileManager instanceof JavacFileManager javacFileManager) {
126126
try {
127-
((JavacFileManager) fileManager).close();
127+
javacFileManager.close();
128128
} catch (IOException es) {
129129
throw new UncheckedIOException(es);
130130
}

src/jdk.compiler/share/classes/com/sun/tools/sjavac/comp/SmartFileManager.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2019, 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
@@ -82,9 +82,9 @@ public void cleanArtifacts() {
8282
* Set whether or not to use ct.sym as an alternate to rt.jar.
8383
*/
8484
public void setSymbolFileEnabled(boolean b) {
85-
if (!(fileManager instanceof JavacFileManager))
85+
if (!(fileManager instanceof JavacFileManager javacFileManager))
8686
throw new IllegalStateException();
87-
((JavacFileManager) fileManager).setSymbolFileEnabled(b);
87+
javacFileManager.setSymbolFileEnabled(b);
8888
}
8989

9090
@DefinedBy(Api.COMPILER)
@@ -175,12 +175,9 @@ public FileObject getFileForInput(Location location,
175175
}
176176

177177
private boolean isModuleInfo(FileObject fo) {
178-
if (fo instanceof JavaFileObject) {
179-
JavaFileObject jfo = (JavaFileObject) fo;
180-
return jfo.isNameCompatible("module-info", Kind.SOURCE)
181-
|| jfo.isNameCompatible("module-info", Kind.CLASS);
182-
}
183-
return false;
178+
return (fo instanceof JavaFileObject javaFileObject)
179+
&& (javaFileObject.isNameCompatible("module-info", Kind.SOURCE)
180+
|| javaFileObject.isNameCompatible("module-info", Kind.CLASS));
184181
}
185182

186183
@Override @DefinedBy(Api.COMPILER)
@@ -243,8 +240,8 @@ public static JavaFileObject locWrap(JavaFileObject jfo, Location loc) {
243240
}
244241

245242
private static FileObject locWrap(FileObject fo, Location loc) {
246-
if (fo instanceof JavaFileObject)
247-
return locWrap((JavaFileObject) fo, loc);
243+
if (fo instanceof JavaFileObject javaFileObject)
244+
return locWrap(javaFileObject, loc);
248245
return fo == null ? null : new FileObjectWithLocation<>(fo, loc);
249246
}
250247

@@ -263,16 +260,16 @@ private static ListBuffer<JavaFileObject> locWrapMany(Iterable<JavaFileObject> j
263260
}
264261

265262
private static FileObject locUnwrap(FileObject fo) {
266-
if (fo instanceof FileObjectWithLocation<?>)
267-
return ((FileObjectWithLocation<?>) fo).getDelegate();
268-
if (fo instanceof JavaFileObjectWithLocation<?>)
269-
return ((JavaFileObjectWithLocation<?>) fo).getDelegate();
263+
if (fo instanceof FileObjectWithLocation<?> fileObjectWithLocation)
264+
return fileObjectWithLocation.getDelegate();
265+
if (fo instanceof JavaFileObjectWithLocation<?> javaFileObjectWithLocation)
266+
return javaFileObjectWithLocation.getDelegate();
270267
return fo;
271268
}
272269

273270
private static JavaFileObject locUnwrap(JavaFileObject fo) {
274-
if (fo instanceof JavaFileObjectWithLocation<?>)
275-
return ((JavaFileObjectWithLocation<?>) fo).getDelegate();
271+
if (fo instanceof JavaFileObjectWithLocation<?> javaFileObjectWithLocation)
272+
return javaFileObjectWithLocation.getDelegate();
276273
return fo;
277274
}
278275
}

src/jdk.compiler/share/classes/com/sun/tools/sjavac/comp/dependencies/NewDependencyCollector.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 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
@@ -111,8 +111,8 @@ private void collectPubApisOfDependencies(Context context,
111111

112112
private Location getLocationOf(ClassSymbol cs) {
113113
JavaFileObject jfo = cs.outermostClass().classfile;
114-
if (jfo instanceof JavaFileObjectWithLocation) {
115-
return ((JavaFileObjectWithLocation<?>) jfo).getLocation();
114+
if (jfo instanceof JavaFileObjectWithLocation<?> javaFileObjectWithLocation) {
115+
return javaFileObjectWithLocation.getLocation();
116116
}
117117

118118
// jfo is most likely on PLATFORM_CLASS_PATH.
@@ -177,14 +177,13 @@ public boolean isSymbolRelevant(boolean cp, ClassSymbol cs) {
177177
}
178178

179179
private Set<ClassSymbol> allSupertypes(TypeSymbol t) {
180-
if (t == null || !(t instanceof ClassSymbol)) {
180+
if (t == null || !(t instanceof ClassSymbol classSymbol)) {
181181
return Collections.emptySet();
182182
}
183183
Set<ClassSymbol> result = new HashSet<>();
184-
ClassSymbol cs = (ClassSymbol) t;
185-
result.add(cs);
186-
result.addAll(allSupertypes(cs.getSuperclass().tsym));
187-
for (Type it : cs.getInterfaces()) {
184+
result.add(classSymbol);
185+
result.addAll(allSupertypes(classSymbol.getSuperclass().tsym));
186+
for (Type it : classSymbol.getInterfaces()) {
188187
result.addAll(allSupertypes(it.tsym));
189188
}
190189
return result;

src/jdk.compiler/share/classes/com/sun/tools/sjavac/comp/dependencies/PublicApiCollector.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
@@ -90,8 +90,8 @@ public void finished(TaskEvent e) {
9090

9191
private void collectClassSymbols(JCCompilationUnit cu) {
9292
for (Tree t : cu.getTypeDecls()) {
93-
if (t instanceof JCClassDecl) // Can also be a JCSkip
94-
classSymbols.add(((JCClassDecl) t).sym);
93+
if (t instanceof JCClassDecl classDecl) // Can also be a JCSkip
94+
classSymbols.add(classDecl.sym);
9595
}
9696
}
9797

0 commit comments

Comments
 (0)