Skip to content

Commit

Permalink
Replace manual casts with new instanceof expression syntax (#774)
Browse files Browse the repository at this point in the history
  • Loading branch information
ascopes committed Jan 9, 2025
1 parent 6b5ccb1 commit fae0318
Show file tree
Hide file tree
Showing 16 changed files with 70 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ void autoValueImplementationClassIsCreatedAsExpected(JctCompiler compiler) throw
}

private Object callMethod(Object obj, String method, Object... args) throws Throwable {
var cls = obj instanceof Class<?>
? (Class<?>) obj
var cls = obj instanceof Class<?> castCls
? castCls
: obj.getClass();

// Enforce reflective access to be allowed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ void immutablesValueProducesTheExpectedClassForModules(JctCompiler compiler) thr
}

private Object callMethod(Object obj, String method, Object... args) throws Throwable {
var cls = obj instanceof Class<?>
? (Class<?>) obj
var cls = obj instanceof Class<?> castCls
? castCls
: obj.getClass();

// Enforce reflective access to be allowed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ void mapStructGeneratesExpectedMappingCodeForModules(JctCompiler compiler) throw
}

private <T> T getAttr(Object obj, String name) throws Throwable {
final var cls = obj instanceof Class<?>
? (Class<?>) obj
final var cls = obj instanceof Class<?> castCls
? castCls
: obj.getClass();

final var instance = cls == obj
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,9 @@ public ContainerGroupRepositoryImpl(String release) {
* @throws JctIllegalInputException if the location is an output location and is misconfigured.
*/
public void addPath(Location location, PathRoot pathRoot) {
if (location instanceof ModuleLocation) {
if (location instanceof ModuleLocation moduleLocation) {
// If we are adding a specific module, we should resolve where it needs to live
// using custom logic so that we know it gets registered in the right place.
var moduleLocation = (ModuleLocation) location;
addModulePath(moduleLocation, pathRoot);
} else if (location.isModuleOrientedLocation()) {
// If we are adding a module-oriented location of any type, then we should discover
Expand Down Expand Up @@ -268,8 +267,7 @@ public Collection<PackageContainerGroup> getPackageContainerGroups() {
*/
@Nullable
public PackageContainerGroup getPackageOrientedContainerGroup(Location location) {
if (location instanceof ModuleLocation) {
var moduleLocation = (ModuleLocation) location;
if (location instanceof ModuleLocation moduleLocation) {
var group = getModuleOrientedContainerGroup(moduleLocation.getParent());
return group == null
? null
Expand Down Expand Up @@ -298,8 +296,7 @@ public String getRelease() {
* @return {@code true} if the location exists, or {@code false} if it does not.
*/
public boolean hasLocation(Location location) {
if (location instanceof ModuleLocation) {
var moduleLocation = (ModuleLocation) location;
if (location instanceof ModuleLocation moduleLocation) {
var parentLocation = moduleLocation.getParent();
var group = parentLocation.isOutputLocation()
? outputs.get(parentLocation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ public void close() throws IOException {
public boolean contains(PathFileObject fileObject) {
var location = fileObject.getLocation();

if (location instanceof ModuleLocation) {
var module = modules.get((ModuleLocation) location);
if (location instanceof ModuleLocation moduleLocation) {
var module = modules.get(moduleLocation);
if (module != null) {
return module.contains(fileObject);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,11 @@ public boolean isModuleOrientedLocation() {

@Override
public boolean equals(@Nullable Object other) {
if (!(other instanceof ModuleLocation)) {
return false;
if (other instanceof ModuleLocation that) {
return parent.equals(that.parent)
&& moduleName.equals(that.moduleName);
}

var that = (ModuleLocation) other;

return parent.equals(that.parent)
&& moduleName.equals(that.moduleName);
return false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,12 @@ public void close() {
}

@Override
public boolean contains(Location location, FileObject fo) {
if (!(fo instanceof PathFileObject)) {
return false;
public boolean contains(Location location, FileObject fileObject) {
if (fileObject instanceof PathFileObject pathFileObject) {
var group = repository.getContainerGroup(location);
return group != null && group.contains(pathFileObject);
}

var group = repository.getContainerGroup(location);
return group != null && group.contains((PathFileObject) fo);
return false;
}

@Override
Expand Down Expand Up @@ -143,8 +142,7 @@ public FileObject getFileForOutput(
PackageContainerGroup group = null;

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

if (parentGroup != null) {
Expand Down Expand Up @@ -186,8 +184,7 @@ public JavaFileObject getJavaFileForOutput(
PackageContainerGroup group = null;

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

if (parentGroup != null) {
Expand Down Expand Up @@ -216,18 +213,13 @@ public ModuleLocation getLocationForModule(Location location, String moduleName)
public ModuleLocation getLocationForModule(Location location, JavaFileObject fileObject) {
requireOutputOrModuleOrientedLocation(location);

if (fileObject instanceof PathFileObject) {
var pathFileObject = (PathFileObject) fileObject;
var moduleLocation = pathFileObject.getLocation();

if (moduleLocation instanceof ModuleLocation) {
return (ModuleLocation) moduleLocation;
}

if (fileObject instanceof PathFileObject pathFileObject) {
// The expectation is to return null if this is not for a module. Certain frameworks like
// manifold expect this behaviour, despite it not being documented very clearly in the
// Java compiler API.
return null;
return pathFileObject.getLocation() instanceof ModuleLocation moduleLocation
? moduleLocation
: null;
}

throw new JctIllegalInputException(
Expand Down Expand Up @@ -297,27 +289,26 @@ public boolean hasLocation(Location location) {

@Nullable
@Override
public String inferBinaryName(Location location, JavaFileObject file) {
public String inferBinaryName(Location location, JavaFileObject fileObject) {
requirePackageOrientedLocation(location);

if (!(file instanceof PathFileObject)) {
return null;
if (fileObject instanceof PathFileObject pathFileObject) {
var group = repository.getPackageOrientedContainerGroup(location);
if (group != null) {
return group.inferBinaryName(pathFileObject);
}
}

var group = repository.getPackageOrientedContainerGroup(location);

return group == null
? null
: group.inferBinaryName((PathFileObject) file);
return null;
}

@Nullable
@Override
public String inferModuleName(Location location) {
requirePackageOrientedLocation(location);

return location instanceof ModuleLocation
? ((ModuleLocation) location).getModuleName()
return location instanceof ModuleLocation moduleLocation
? moduleLocation.getModuleName()
: null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ public boolean delete() {
@Override
public boolean equals(@Nullable Object other) {
// Roughly the same as what Javac does.
return other instanceof FileObject && uri.equals(((FileObject) other).toUri());
return other instanceof FileObject that
&& uri.equals(that.toUri());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ private JctCompilerConfigurer<?> initializeConfigurer(
return constructor.newInstance();

} catch (ReflectiveOperationException ex) {
if (ex instanceof InvocationTargetException) {
var target = ((InvocationTargetException) ex).getTargetException();
if (ex instanceof InvocationTargetException iee) {
var target = iee.getTargetException();
if (isTestAbortedException(target)) {
target.addSuppressed(ex);
throw (RuntimeException) target;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,10 @@ public ModuleDescriptor getDescriptor() {

@Override
public boolean equals(@Nullable Object other) {
if (!(other instanceof ModuleCandidate)) {
return false;
if (other instanceof ModuleCandidate that) {
return name.equals(that.name) && path.equals(that.path);
}

var that = (ModuleCandidate) other;

return name.equals(that.name) && path.equals(that.path);
return false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ private static void appendToString(StringBuilder builder, @Nullable Object objec
builder.append(NULL);
} else if (TYPES_TO_TREAT_AS_STRINGS.stream().anyMatch(cls -> cls.isInstance(object))) {
appendToStringCharSequence(builder, object.toString());
} else if (object instanceof Map<?, ?>) {
appendToStringMap(builder, (Map<?, ?>) object);
} else if (object instanceof Map<?, ?> map) {
appendToStringMap(builder, map);
} else if (object.getClass().isArray()) {
appendToStringArray(builder, object);
} else if (object instanceof Iterable<?>) {
appendToStringIterable(builder, ((Iterable<?>) object));
} else if (object instanceof Iterable<?> iterable) {
appendToStringIterable(builder, iterable);
} else {
builder.append(object);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ public ManagedDirectory thatIsEmpty() {
}

@Override
public boolean equals(@Nullable Object that) {
return that instanceof AbstractManagedDirectory
&& ((AbstractManagedDirectory) that).uri.equals(uri);
public boolean equals(@Nullable Object other) {
return other instanceof AbstractManagedDirectory that
&& uri.equals(that.uri);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ public void close() {

for (var list : paths.values()) {
for (var path : list) {
if (path instanceof AbstractManagedDirectory) {
if (path instanceof AbstractManagedDirectory dir) {
try {
((AbstractManagedDirectory) path).close();
dir.close();

} catch (Exception ex) {
exceptions.add(ex);
Expand Down Expand Up @@ -206,8 +206,7 @@ public Map<String, List<? extends PathRoot>> getModules(Location location) {
var results = new HashMap<String, List<PathRoot>>();

paths.forEach((pathLocation, pathRoots) -> {
if (pathLocation instanceof ModuleLocation) {
var modulePathLocation = (ModuleLocation) pathLocation;
if (pathLocation instanceof ModuleLocation modulePathLocation) {
if (modulePathLocation.getParent().equals(location)) {
results.computeIfAbsent(modulePathLocation.getModuleName(), name -> new ArrayList<>())
.addAll(pathRoots);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ public URL getUrl() {
}

@Override
public boolean equals(@Nullable Object that) {
return that instanceof WrappingDirectoryImpl
&& ((WrappingDirectoryImpl) that).getUri().equals(uri);
public boolean equals(@Nullable Object other) {
return other instanceof WrappingDirectoryImpl that
&& uri.equals(that.uri);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,17 +465,18 @@ public static <E extends Enum<E>> E oneOf(Class<E> cls) {
* @param <T> the type.
* @return the element.
*/
@SuppressWarnings("unchecked")
public static <T> T oneOf(Collection<T> items) {
var index = RANDOM.nextInt(items.size());
if (items instanceof List) {
return ((List<T>) items).get(index);
} else {
var iter = items.iterator();
for (var i = 0; i < index - 1; ++i) {
iter.next();
}
return iter.next();
if (items instanceof List<?> list) {
return (T) list.get(index);
}

var iter = items.iterator();
for (var i = 0; i < index - 1; ++i) {
iter.next();
}
return iter.next();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,13 @@ private static class LogRecord {
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof LogRecord)) {
return false;
public boolean equals(Object other) {
if (other instanceof LogRecord that) {
return Objects.equals(ex, that.ex)
&& Objects.equals(message, that.message)
&& Arrays.deepEquals(args, that.args);
}

var that = (LogRecord) obj;

return Objects.equals(ex, that.ex)
&& Objects.equals(message, that.message)
&& Arrays.deepEquals(args, that.args);
return false;
}

@Override
Expand Down

0 comments on commit fae0318

Please sign in to comment.