Skip to content

Commit

Permalink
8263358: Update java.lang to use instanceof pattern variable
Browse files Browse the repository at this point in the history
Reviewed-by: iris, chegar, mchung, dfuchs
  • Loading branch information
pconcannon committed Mar 24, 2021
1 parent ae9af57 commit 329697b
Show file tree
Hide file tree
Showing 18 changed files with 63 additions and 112 deletions.
3 changes: 1 addition & 2 deletions src/java.base/share/classes/java/lang/ProcessBuilder.java
Expand Up @@ -673,9 +673,8 @@ public String toString() {
public boolean equals(Object obj) {
if (obj == this)
return true;
if (! (obj instanceof Redirect))
if (! (obj instanceof Redirect r))
return false;
Redirect r = (Redirect) obj;
if (r.type() != this.type())
return false;
assert this.file() != null;
Expand Down
11 changes: 3 additions & 8 deletions src/java.base/share/classes/java/lang/ProcessHandleImpl.java
Expand Up @@ -519,14 +519,9 @@ public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof ProcessHandleImpl) {
ProcessHandleImpl other = (ProcessHandleImpl) obj;
return (pid == other.pid) &&
(startTime == other.startTime
|| startTime == 0
|| other.startTime == 0);
}
return false;
return (obj instanceof ProcessHandleImpl other)
&& (pid == other.pid)
&& (startTime == other.startTime || startTime == 0 || other.startTime == 0);
}

/**
Expand Down
7 changes: 3 additions & 4 deletions src/java.base/share/classes/java/lang/PublicMethods.java
Expand Up @@ -113,11 +113,10 @@ static boolean matches(Method method,
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Key)) return false;
Key that = (Key) o;
//noinspection StringEquality (guaranteed interned String(s))
return name == that.name &&
Arrays.equals(ptypes, that.ptypes);
return (o instanceof Key that)
&& name == that.name
&& Arrays.equals(ptypes, that.ptypes);
}

@Override
Expand Down
7 changes: 2 additions & 5 deletions src/java.base/share/classes/java/lang/Runtime.java
Expand Up @@ -1427,11 +1427,8 @@ public boolean equals(Object obj) {
public boolean equalsIgnoreOptional(Object obj) {
if (this == obj)
return true;
if (!(obj instanceof Version))
return false;

Version that = (Version)obj;
return (this.version().equals(that.version())
return (obj instanceof Version that)
&& (this.version().equals(that.version())
&& this.pre().equals(that.pre())
&& this.build().equals(that.build()));
}
Expand Down
3 changes: 1 addition & 2 deletions src/java.base/share/classes/java/lang/Shutdown.java
Expand Up @@ -129,8 +129,7 @@ private static void runHooks() {
}
if (hook != null) hook.run();
} catch (Throwable t) {
if (t instanceof ThreadDeath) {
ThreadDeath td = (ThreadDeath)t;
if (t instanceof ThreadDeath td) {
throw td;
}
}
Expand Down
18 changes: 8 additions & 10 deletions src/java.base/share/classes/java/lang/StackTraceElement.java
Expand Up @@ -406,16 +406,14 @@ public String toString() {
public boolean equals(Object obj) {
if (obj==this)
return true;
if (!(obj instanceof StackTraceElement))
return false;
StackTraceElement e = (StackTraceElement)obj;
return Objects.equals(classLoaderName, e.classLoaderName) &&
Objects.equals(moduleName, e.moduleName) &&
Objects.equals(moduleVersion, e.moduleVersion) &&
e.declaringClass.equals(declaringClass) &&
e.lineNumber == lineNumber &&
Objects.equals(methodName, e.methodName) &&
Objects.equals(fileName, e.fileName);
return (obj instanceof StackTraceElement e)
&& e.lineNumber == lineNumber
&& e.declaringClass.equals(declaringClass)
&& Objects.equals(classLoaderName, e.classLoaderName)
&& Objects.equals(moduleName, e.moduleName)
&& Objects.equals(moduleVersion, e.moduleVersion)
&& Objects.equals(methodName, e.methodName)
&& Objects.equals(fileName, e.fileName);
}

/**
Expand Down
10 changes: 3 additions & 7 deletions src/java.base/share/classes/java/lang/String.java
Expand Up @@ -1818,13 +1818,9 @@ public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String aString = (String)anObject;
if (!COMPACT_STRINGS || this.coder == aString.coder) {
return StringLatin1.equals(value, aString.value);
}
}
return false;
return (anObject instanceof String aString)
&& (!COMPACT_STRINGS || this.coder == aString.coder)
&& StringLatin1.equals(value, aString.value);
}

/**
Expand Down
Expand Up @@ -350,12 +350,11 @@ private static ConstantDesc canonicalizeArrayVarHandle(DynamicConstantDesc<?> de
@Override
public final boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof DynamicConstantDesc)) return false;
DynamicConstantDesc<?> desc = (DynamicConstantDesc<?>) o;
return Objects.equals(bootstrapMethod, desc.bootstrapMethod) &&
Arrays.equals(bootstrapArgs, desc.bootstrapArgs) &&
Objects.equals(constantName, desc.constantName) &&
Objects.equals(constantType, desc.constantType);
return (o instanceof DynamicConstantDesc<?> desc)
&& Objects.equals(bootstrapMethod, desc.bootstrapMethod)
&& Arrays.equals(bootstrapArgs, desc.bootstrapArgs)
&& Objects.equals(constantName, desc.constantName)
&& Objects.equals(constantType, desc.constantType);
}

@Override
Expand Down
Expand Up @@ -302,8 +302,7 @@ List<Object> classDataValues() {
}

private static String debugString(Object arg) {
if (arg instanceof MethodHandle) {
MethodHandle mh = (MethodHandle) arg;
if (arg instanceof MethodHandle mh) {
MemberName member = mh.internalMemberName();
if (member != null)
return member.toString();
Expand Down Expand Up @@ -627,8 +626,7 @@ private boolean assertStaticType(Class<?> cls, Name n) {

private void emitReferenceCast(Class<?> cls, Object arg) {
Name writeBack = null; // local to write back result
if (arg instanceof Name) {
Name n = (Name) arg;
if (arg instanceof Name n) {
if (lambdaForm.useCount(n) > 1) {
// This guy gets used more than once.
writeBack = n;
Expand Down Expand Up @@ -1679,8 +1677,7 @@ private void emitPushArgument(Name name, int paramIndex) {

private void emitPushArgument(Class<?> ptype, Object arg) {
BasicType bptype = basicType(ptype);
if (arg instanceof Name) {
Name n = (Name) arg;
if (arg instanceof Name n) {
emitLoadInsn(n.type, n.index());
emitImplicitConversion(n.type, ptype, n);
} else if (arg == null && bptype == L_TYPE) {
Expand Down
15 changes: 6 additions & 9 deletions src/java.base/share/classes/java/lang/invoke/LambdaForm.java
Expand Up @@ -568,8 +568,7 @@ boolean nameRefsAreLegal() {
Name n = names[i];
assert(n.index() == i);
for (Object arg : n.arguments) {
if (arg instanceof Name) {
Name n2 = (Name) arg;
if (arg instanceof Name n2) {
int i2 = n2.index;
assert(0 <= i2 && i2 < names.length) : n.debugString() + ": 0 <= i2 && i2 < names.length: 0 <= " + i2 + " < " + names.length;
assert(names[i2] == n2) : Arrays.asList("-1-", i, "-2-", n.debugString(), "-3-", i2, "-4-", n2.debugString(), "-5-", names[i2].debugString(), "-6-", this);
Expand Down Expand Up @@ -1133,9 +1132,9 @@ synchronized void resolve() {
public boolean equals(Object other) {
if (this == other) return true;
if (other == null) return false;
if (!(other instanceof NamedFunction)) return false;
NamedFunction that = (NamedFunction) other;
return this.member != null && this.member.equals(that.member);
return (other instanceof NamedFunction that)
&& this.member != null
&& this.member.equals(that.member);
}

@Override
Expand Down Expand Up @@ -1407,8 +1406,7 @@ Name replaceNames(Name[] oldNames, Name[] newNames, int start, int end) {
boolean replaced = false;
eachArg:
for (int j = 0; j < arguments.length; j++) {
if (arguments[j] instanceof Name) {
Name n = (Name) arguments[j];
if (arguments[j] instanceof Name n) {
int check = n.index;
// harmless check to see if the thing is already in newNames:
if (check >= 0 && check < newNames.length && n == newNames[check])
Expand All @@ -1435,8 +1433,7 @@ void internArguments() {
@SuppressWarnings("LocalVariableHidesMemberVariable")
Object[] arguments = this.arguments;
for (int j = 0; j < arguments.length; j++) {
if (arguments[j] instanceof Name) {
Name n = (Name) arguments[j];
if (arguments[j] instanceof Name n) {
if (n.isParam() && n.index < INTERNED_ARGUMENT_LIMIT)
arguments[j] = internArgument(n);
}
Expand Down
Expand Up @@ -337,9 +337,8 @@ private LambdaForm getInCache(TransformKey key) {
k = m.get(key);
} else if (c == null) {
return null;
} else if (c instanceof Transform) {
} else if (c instanceof Transform t) {
// one-element cache avoids overhead of an array
Transform t = (Transform)c;
if (t.equals(key)) k = t;
} else {
Transform[] ta = (Transform[])c;
Expand Down Expand Up @@ -389,8 +388,7 @@ private LambdaForm putInCache(TransformKey key, LambdaForm form) {
return form;
}
Transform[] ta;
if (c instanceof Transform) {
Transform k = (Transform)c;
if (c instanceof Transform k) {
if (k.equals(key)) {
LambdaForm result = k.get();
if (result == null) {
Expand Down
12 changes: 4 additions & 8 deletions src/java.base/share/classes/java/lang/invoke/MemberName.java
Expand Up @@ -147,12 +147,10 @@ public MethodType getMethodType() {

// type is not a MethodType yet. Convert it thread-safely.
synchronized (this) {
if (type instanceof String) {
String sig = (String) type;
if (type instanceof String sig) {
MethodType res = MethodType.fromDescriptor(sig, getClassLoader());
type = res;
} else if (type instanceof Object[]) {
Object[] typeInfo = (Object[]) type;
} else if (type instanceof Object[] typeInfo) {
Class<?>[] ptypes = (Class<?>[]) typeInfo[1];
Class<?> rtype = (Class<?>) typeInfo[0];
MethodType res = MethodType.makeImpl(rtype, ptypes, true);
Expand Down Expand Up @@ -235,8 +233,7 @@ public Class<?> getFieldType() {

// type is not a Class yet. Convert it thread-safely.
synchronized (this) {
if (type instanceof String) {
String sig = (String) type;
if (type instanceof String sig) {
MethodType mtype = MethodType.fromDescriptor("()"+sig, getClassLoader());
Class<?> res = mtype.returnType();
type = res;
Expand Down Expand Up @@ -938,8 +935,7 @@ public IllegalAccessException makeAccessException(String message, Object from) {
} else {
Module m;
Class<?> plc;
if (from instanceof MethodHandles.Lookup) {
MethodHandles.Lookup lookup = (MethodHandles.Lookup)from;
if (from instanceof MethodHandles.Lookup lookup) {
from = lookup.lookupClass();
m = lookup.lookupClass().getModule();
plc = lookup.previousLookupClass();
Expand Down
Expand Up @@ -2262,10 +2262,9 @@ static ClassFile newInstance(byte[] bytes, String pkgName) {
// workaround to read `this_class` using readConst and validate the value
int thisClass = reader.readUnsignedShort(reader.header + 2);
Object constant = reader.readConst(thisClass, new char[reader.getMaxStringLength()]);
if (!(constant instanceof Type)) {
if (!(constant instanceof Type type)) {
throw new ClassFormatError("this_class item: #" + thisClass + " not a CONSTANT_Class_info");
}
Type type = ((Type) constant);
if (!type.getDescriptor().startsWith("L")) {
throw new ClassFormatError("this_class item: #" + thisClass + " not a CONSTANT_Class_info");
}
Expand Down
3 changes: 1 addition & 2 deletions src/java.base/share/classes/java/lang/invoke/VarHandles.java
Expand Up @@ -605,8 +605,7 @@ public static VarHandle dropCoordinates(VarHandle target, int pos, Class<?>... v
}

private static void noCheckedExceptions(MethodHandle handle) {
if (handle instanceof DirectMethodHandle) {
DirectMethodHandle directHandle = (DirectMethodHandle)handle;
if (handle instanceof DirectMethodHandle directHandle) {
byte refKind = directHandle.member.getReferenceKind();
MethodHandleInfo info = new InfoFromMemberName(
MethodHandles.Lookup.IMPL_LOOKUP,
Expand Down
36 changes: 13 additions & 23 deletions src/java.base/share/classes/java/lang/module/ModuleDescriptor.java
Expand Up @@ -310,10 +310,8 @@ public int compareTo(Requires that) {
*/
@Override
public boolean equals(Object ob) {
if (!(ob instanceof Requires))
return false;
Requires that = (Requires)ob;
return name.equals(that.name) && mods.equals(that.mods)
return (ob instanceof Requires that)
&& name.equals(that.name) && mods.equals(that.mods)
&& Objects.equals(compiledVersion, that.compiledVersion)
&& Objects.equals(rawCompiledVersion, that.rawCompiledVersion);
}
Expand Down Expand Up @@ -531,10 +529,8 @@ public int hashCode() {
*/
@Override
public boolean equals(Object ob) {
if (!(ob instanceof Exports))
return false;
Exports other = (Exports)ob;
return Objects.equals(this.mods, other.mods)
return (ob instanceof Exports other)
&& Objects.equals(this.mods, other.mods)
&& Objects.equals(this.source, other.source)
&& Objects.equals(this.targets, other.targets);
}
Expand Down Expand Up @@ -736,12 +732,10 @@ public int hashCode() {
*/
@Override
public boolean equals(Object ob) {
if (!(ob instanceof Opens))
return false;
Opens other = (Opens)ob;
return Objects.equals(this.mods, other.mods)
&& Objects.equals(this.source, other.source)
&& Objects.equals(this.targets, other.targets);
return (ob instanceof Opens other)
&& Objects.equals(this.mods, other.mods)
&& Objects.equals(this.source, other.source)
&& Objects.equals(this.targets, other.targets);
}

/**
Expand Down Expand Up @@ -872,11 +866,9 @@ public int hashCode() {
*/
@Override
public boolean equals(Object ob) {
if (!(ob instanceof Provides))
return false;
Provides other = (Provides)ob;
return Objects.equals(this.service, other.service) &&
Objects.equals(this.providers, other.providers);
return (ob instanceof Provides other)
&& Objects.equals(this.service, other.service)
&& Objects.equals(this.providers, other.providers);
}

/**
Expand Down Expand Up @@ -2241,10 +2233,8 @@ public int compareTo(ModuleDescriptor that) {
public boolean equals(Object ob) {
if (ob == this)
return true;
if (!(ob instanceof ModuleDescriptor))
return false;
ModuleDescriptor that = (ModuleDescriptor)ob;
return (name.equals(that.name)
return (ob instanceof ModuleDescriptor that)
&& (name.equals(that.name)
&& modifiers.equals(that.modifiers)
&& requires.equals(that.requires)
&& Objects.equals(packages, that.packages)
Expand Down
Expand Up @@ -138,12 +138,9 @@ public int hashCode() {
*/
@Override
public boolean equals(Object ob) {
if (!(ob instanceof ResolvedModule))
return false;

ResolvedModule that = (ResolvedModule) ob;
return Objects.equals(this.cf, that.cf)
&& Objects.equals(this.mref, that.mref);
return (ob instanceof ResolvedModule that)
&& Objects.equals(this.cf, that.cf)
&& Objects.equals(this.mref, that.mref);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/java.base/share/classes/java/lang/reflect/Method.java
Expand Up @@ -714,8 +714,7 @@ public Object getDefaultValue() {
getConstantPool(getDeclaringClass()),
getDeclaringClass());
if (result instanceof ExceptionProxy) {
if (result instanceof TypeNotPresentExceptionProxy) {
TypeNotPresentExceptionProxy proxy = (TypeNotPresentExceptionProxy)result;
if (result instanceof TypeNotPresentExceptionProxy proxy) {
throw new TypeNotPresentException(proxy.typeName(), proxy.getCause());
}
throw new AnnotationFormatError("Invalid default: " + this);
Expand Down
9 changes: 3 additions & 6 deletions src/java.base/share/classes/java/lang/reflect/Parameter.java
Expand Up @@ -77,12 +77,9 @@ public final class Parameter implements AnnotatedElement {
*/
@Override
public boolean equals(Object obj) {
if(obj instanceof Parameter) {
Parameter other = (Parameter)obj;
return (other.executable.equals(executable) &&
other.index == index);
}
return false;
return (obj instanceof Parameter other)
&& other.executable.equals(executable)
&& other.index == index;
}

/**
Expand Down

1 comment on commit 329697b

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.