Skip to content

Commit 1387666

Browse files
committed
Replace manual casts with new instanceof expression syntax (#774)
1 parent 2f10a6a commit 1387666

File tree

16 files changed

+70
-90
lines changed

16 files changed

+70
-90
lines changed

java-compiler-testing/src/it/google-auto-value/src/test/java/io/github/ascopes/jct/acceptancetests/autovalue/AutoValueTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ void autoValueImplementationClassIsCreatedAsExpected(JctCompiler compiler) throw
7272
}
7373

7474
private Object callMethod(Object obj, String method, Object... args) throws Throwable {
75-
var cls = obj instanceof Class<?>
76-
? (Class<?>) obj
75+
var cls = obj instanceof Class<?> castCls
76+
? castCls
7777
: obj.getClass();
7878

7979
// Enforce reflective access to be allowed.

java-compiler-testing/src/it/immutables/src/test/java/io/github/ascopes/jct/acceptancetests/immutables/ImmutablesTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ void immutablesValueProducesTheExpectedClassForModules(JctCompiler compiler) thr
100100
}
101101

102102
private Object callMethod(Object obj, String method, Object... args) throws Throwable {
103-
var cls = obj instanceof Class<?>
104-
? (Class<?>) obj
103+
var cls = obj instanceof Class<?> castCls
104+
? castCls
105105
: obj.getClass();
106106

107107
// Enforce reflective access to be allowed.

java-compiler-testing/src/it/mapstruct/src/test/java/io/github/ascopes/jct/acceptancetests/mapstruct/MapStructTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ void mapStructGeneratesExpectedMappingCodeForModules(JctCompiler compiler) throw
108108
}
109109

110110
private <T> T getAttr(Object obj, String name) throws Throwable {
111-
final var cls = obj instanceof Class<?>
112-
? (Class<?>) obj
111+
final var cls = obj instanceof Class<?> castCls
112+
? castCls
113113
: obj.getClass();
114114

115115
final var instance = cls == obj

java-compiler-testing/src/main/java/io/github/ascopes/jct/containers/impl/ContainerGroupRepositoryImpl.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,9 @@ public ContainerGroupRepositoryImpl(String release) {
7676
* @throws JctIllegalInputException if the location is an output location and is misconfigured.
7777
*/
7878
public void addPath(Location location, PathRoot pathRoot) {
79-
if (location instanceof ModuleLocation) {
79+
if (location instanceof ModuleLocation moduleLocation) {
8080
// If we are adding a specific module, we should resolve where it needs to live
8181
// using custom logic so that we know it gets registered in the right place.
82-
var moduleLocation = (ModuleLocation) location;
8382
addModulePath(moduleLocation, pathRoot);
8483
} else if (location.isModuleOrientedLocation()) {
8584
// If we are adding a module-oriented location of any type, then we should discover
@@ -268,8 +267,7 @@ public Collection<PackageContainerGroup> getPackageContainerGroups() {
268267
*/
269268
@Nullable
270269
public PackageContainerGroup getPackageOrientedContainerGroup(Location location) {
271-
if (location instanceof ModuleLocation) {
272-
var moduleLocation = (ModuleLocation) location;
270+
if (location instanceof ModuleLocation moduleLocation) {
273271
var group = getModuleOrientedContainerGroup(moduleLocation.getParent());
274272
return group == null
275273
? null
@@ -298,8 +296,7 @@ public String getRelease() {
298296
* @return {@code true} if the location exists, or {@code false} if it does not.
299297
*/
300298
public boolean hasLocation(Location location) {
301-
if (location instanceof ModuleLocation) {
302-
var moduleLocation = (ModuleLocation) location;
299+
if (location instanceof ModuleLocation moduleLocation) {
303300
var parentLocation = moduleLocation.getParent();
304301
var group = parentLocation.isOutputLocation()
305302
? outputs.get(parentLocation)

java-compiler-testing/src/main/java/io/github/ascopes/jct/containers/impl/ModuleContainerGroupImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ public void close() throws IOException {
114114
public boolean contains(PathFileObject fileObject) {
115115
var location = fileObject.getLocation();
116116

117-
if (location instanceof ModuleLocation) {
118-
var module = modules.get((ModuleLocation) location);
117+
if (location instanceof ModuleLocation moduleLocation) {
118+
var module = modules.get(moduleLocation);
119119
if (module != null) {
120120
return module.contains(fileObject);
121121
}

java-compiler-testing/src/main/java/io/github/ascopes/jct/filemanagers/ModuleLocation.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,11 @@ public boolean isModuleOrientedLocation() {
9595

9696
@Override
9797
public boolean equals(@Nullable Object other) {
98-
if (!(other instanceof ModuleLocation)) {
99-
return false;
98+
if (other instanceof ModuleLocation that) {
99+
return parent.equals(that.parent)
100+
&& moduleName.equals(that.moduleName);
100101
}
101-
102-
var that = (ModuleLocation) other;
103-
104-
return parent.equals(that.parent)
105-
&& moduleName.equals(that.moduleName);
102+
return false;
106103
}
107104

108105
@Override

java-compiler-testing/src/main/java/io/github/ascopes/jct/filemanagers/impl/JctFileManagerImpl.java

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,12 @@ public void close() {
7474
}
7575

7676
@Override
77-
public boolean contains(Location location, FileObject fo) {
78-
if (!(fo instanceof PathFileObject)) {
79-
return false;
77+
public boolean contains(Location location, FileObject fileObject) {
78+
if (fileObject instanceof PathFileObject pathFileObject) {
79+
var group = repository.getContainerGroup(location);
80+
return group != null && group.contains(pathFileObject);
8081
}
81-
82-
var group = repository.getContainerGroup(location);
83-
return group != null && group.contains((PathFileObject) fo);
82+
return false;
8483
}
8584

8685
@Override
@@ -143,8 +142,7 @@ public FileObject getFileForOutput(
143142
PackageContainerGroup group = null;
144143

145144
// If we have a module, we may need to create a brand-new location for it.
146-
if (location instanceof ModuleLocation) {
147-
var moduleLocation = ((ModuleLocation) location);
145+
if (location instanceof ModuleLocation moduleLocation) {
148146
var parentGroup = repository.getOutputContainerGroup(moduleLocation.getParent());
149147

150148
if (parentGroup != null) {
@@ -186,8 +184,7 @@ public JavaFileObject getJavaFileForOutput(
186184
PackageContainerGroup group = null;
187185

188186
// If we have a module, we may need to create a brand-new location for it.
189-
if (location instanceof ModuleLocation) {
190-
var moduleLocation = ((ModuleLocation) location);
187+
if (location instanceof ModuleLocation moduleLocation) {
191188
var parentGroup = repository.getOutputContainerGroup(moduleLocation.getParent());
192189

193190
if (parentGroup != null) {
@@ -216,18 +213,13 @@ public ModuleLocation getLocationForModule(Location location, String moduleName)
216213
public ModuleLocation getLocationForModule(Location location, JavaFileObject fileObject) {
217214
requireOutputOrModuleOrientedLocation(location);
218215

219-
if (fileObject instanceof PathFileObject) {
220-
var pathFileObject = (PathFileObject) fileObject;
221-
var moduleLocation = pathFileObject.getLocation();
222-
223-
if (moduleLocation instanceof ModuleLocation) {
224-
return (ModuleLocation) moduleLocation;
225-
}
226-
216+
if (fileObject instanceof PathFileObject pathFileObject) {
227217
// The expectation is to return null if this is not for a module. Certain frameworks like
228218
// manifold expect this behaviour, despite it not being documented very clearly in the
229219
// Java compiler API.
230-
return null;
220+
return pathFileObject.getLocation() instanceof ModuleLocation moduleLocation
221+
? moduleLocation
222+
: null;
231223
}
232224

233225
throw new JctIllegalInputException(
@@ -297,27 +289,26 @@ public boolean hasLocation(Location location) {
297289

298290
@Nullable
299291
@Override
300-
public String inferBinaryName(Location location, JavaFileObject file) {
292+
public String inferBinaryName(Location location, JavaFileObject fileObject) {
301293
requirePackageOrientedLocation(location);
302294

303-
if (!(file instanceof PathFileObject)) {
304-
return null;
295+
if (fileObject instanceof PathFileObject pathFileObject) {
296+
var group = repository.getPackageOrientedContainerGroup(location);
297+
if (group != null) {
298+
return group.inferBinaryName(pathFileObject);
299+
}
305300
}
306301

307-
var group = repository.getPackageOrientedContainerGroup(location);
308-
309-
return group == null
310-
? null
311-
: group.inferBinaryName((PathFileObject) file);
302+
return null;
312303
}
313304

314305
@Nullable
315306
@Override
316307
public String inferModuleName(Location location) {
317308
requirePackageOrientedLocation(location);
318309

319-
return location instanceof ModuleLocation
320-
? ((ModuleLocation) location).getModuleName()
310+
return location instanceof ModuleLocation moduleLocation
311+
? moduleLocation.getModuleName()
321312
: null;
322313
}
323314

java-compiler-testing/src/main/java/io/github/ascopes/jct/filemanagers/impl/PathFileObjectImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ public boolean delete() {
115115
@Override
116116
public boolean equals(@Nullable Object other) {
117117
// Roughly the same as what Javac does.
118-
return other instanceof FileObject && uri.equals(((FileObject) other).toUri());
118+
return other instanceof FileObject that
119+
&& uri.equals(that.toUri());
119120
}
120121

121122
@Override

java-compiler-testing/src/main/java/io/github/ascopes/jct/junit/AbstractCompilersProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,8 @@ private JctCompilerConfigurer<?> initializeConfigurer(
280280
return constructor.newInstance();
281281

282282
} catch (ReflectiveOperationException ex) {
283-
if (ex instanceof InvocationTargetException) {
284-
var target = ((InvocationTargetException) ex).getTargetException();
283+
if (ex instanceof InvocationTargetException iee) {
284+
var target = iee.getTargetException();
285285
if (isTestAbortedException(target)) {
286286
target.addSuppressed(ex);
287287
throw (RuntimeException) target;

java-compiler-testing/src/main/java/io/github/ascopes/jct/utils/ModuleDiscoverer.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,10 @@ public ModuleDescriptor getDescriptor() {
132132

133133
@Override
134134
public boolean equals(@Nullable Object other) {
135-
if (!(other instanceof ModuleCandidate)) {
136-
return false;
135+
if (other instanceof ModuleCandidate that) {
136+
return name.equals(that.name) && path.equals(that.path);
137137
}
138-
139-
var that = (ModuleCandidate) other;
140-
141-
return name.equals(that.name) && path.equals(that.path);
138+
return false;
142139
}
143140

144141
@Override

0 commit comments

Comments
 (0)