aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Patrick Concannon <pconcannon@openjdk.org> 2021-03-24 09:57:22 +0000
committerGravatar Patrick Concannon <pconcannon@openjdk.org> 2021-03-24 09:57:22 +0000
commit329697b02ee66b5bb767634dbf3ba19f6624c8d3 (patch)
tree5bb64331690d3cb32d5f3efda45db3e753ffa35f
parentae9af57bf694ff9dded1230e6dc675ead9aa4d10 (diff)
downloadjdk-329697b02ee66b5bb767634dbf3ba19f6624c8d3.tar.gz
jdk-329697b02ee66b5bb767634dbf3ba19f6624c8d3.zip
8263358: Update java.lang to use instanceof pattern variable
Reviewed-by: iris, chegar, mchung, dfuchs
-rw-r--r--src/java.base/share/classes/java/lang/ProcessBuilder.java3
-rw-r--r--src/java.base/share/classes/java/lang/ProcessHandleImpl.java11
-rw-r--r--src/java.base/share/classes/java/lang/PublicMethods.java7
-rw-r--r--src/java.base/share/classes/java/lang/Runtime.java7
-rw-r--r--src/java.base/share/classes/java/lang/Shutdown.java3
-rw-r--r--src/java.base/share/classes/java/lang/StackTraceElement.java18
-rw-r--r--src/java.base/share/classes/java/lang/String.java10
-rw-r--r--src/java.base/share/classes/java/lang/constant/DynamicConstantDesc.java11
-rw-r--r--src/java.base/share/classes/java/lang/invoke/InvokerBytecodeGenerator.java9
-rw-r--r--src/java.base/share/classes/java/lang/invoke/LambdaForm.java15
-rw-r--r--src/java.base/share/classes/java/lang/invoke/LambdaFormEditor.java6
-rw-r--r--src/java.base/share/classes/java/lang/invoke/MemberName.java12
-rw-r--r--src/java.base/share/classes/java/lang/invoke/MethodHandles.java3
-rw-r--r--src/java.base/share/classes/java/lang/invoke/VarHandles.java3
-rw-r--r--src/java.base/share/classes/java/lang/module/ModuleDescriptor.java36
-rw-r--r--src/java.base/share/classes/java/lang/module/ResolvedModule.java9
-rw-r--r--src/java.base/share/classes/java/lang/reflect/Method.java3
-rw-r--r--src/java.base/share/classes/java/lang/reflect/Parameter.java9
18 files changed, 63 insertions, 112 deletions
diff --git a/src/java.base/share/classes/java/lang/ProcessBuilder.java b/src/java.base/share/classes/java/lang/ProcessBuilder.java
index efc296b16da..c909ca6789f 100644
--- a/src/java.base/share/classes/java/lang/ProcessBuilder.java
+++ b/src/java.base/share/classes/java/lang/ProcessBuilder.java
@@ -673,9 +673,8 @@ public final class ProcessBuilder
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;
diff --git a/src/java.base/share/classes/java/lang/ProcessHandleImpl.java b/src/java.base/share/classes/java/lang/ProcessHandleImpl.java
index 92634d9b120..883a15ebb02 100644
--- a/src/java.base/share/classes/java/lang/ProcessHandleImpl.java
+++ b/src/java.base/share/classes/java/lang/ProcessHandleImpl.java
@@ -519,14 +519,9 @@ final class ProcessHandleImpl implements ProcessHandle {
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);
}
/**
diff --git a/src/java.base/share/classes/java/lang/PublicMethods.java b/src/java.base/share/classes/java/lang/PublicMethods.java
index eee131c206a..31e87873387 100644
--- a/src/java.base/share/classes/java/lang/PublicMethods.java
+++ b/src/java.base/share/classes/java/lang/PublicMethods.java
@@ -113,11 +113,10 @@ final class PublicMethods {
@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
diff --git a/src/java.base/share/classes/java/lang/Runtime.java b/src/java.base/share/classes/java/lang/Runtime.java
index 626cc4904ff..5348bf4d7d4 100644
--- a/src/java.base/share/classes/java/lang/Runtime.java
+++ b/src/java.base/share/classes/java/lang/Runtime.java
@@ -1427,11 +1427,8 @@ public class Runtime {
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()));
}
diff --git a/src/java.base/share/classes/java/lang/Shutdown.java b/src/java.base/share/classes/java/lang/Shutdown.java
index 9e774c9d94f..e67164d036f 100644
--- a/src/java.base/share/classes/java/lang/Shutdown.java
+++ b/src/java.base/share/classes/java/lang/Shutdown.java
@@ -129,8 +129,7 @@ class Shutdown {
}
if (hook != null) hook.run();
} catch (Throwable t) {
- if (t instanceof ThreadDeath) {
- ThreadDeath td = (ThreadDeath)t;
+ if (t instanceof ThreadDeath td) {
throw td;
}
}
diff --git a/src/java.base/share/classes/java/lang/StackTraceElement.java b/src/java.base/share/classes/java/lang/StackTraceElement.java
index 438c875847d..259f9255986 100644
--- a/src/java.base/share/classes/java/lang/StackTraceElement.java
+++ b/src/java.base/share/classes/java/lang/StackTraceElement.java
@@ -406,16 +406,14 @@ public final class StackTraceElement implements java.io.Serializable {
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);
}
/**
diff --git a/src/java.base/share/classes/java/lang/String.java b/src/java.base/share/classes/java/lang/String.java
index f757f2bd64d..8f81bb4a0da 100644
--- a/src/java.base/share/classes/java/lang/String.java
+++ b/src/java.base/share/classes/java/lang/String.java
@@ -1818,13 +1818,9 @@ public final class String
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);
}
/**
diff --git a/src/java.base/share/classes/java/lang/constant/DynamicConstantDesc.java b/src/java.base/share/classes/java/lang/constant/DynamicConstantDesc.java
index c9e148fea27..96d7677a567 100644
--- a/src/java.base/share/classes/java/lang/constant/DynamicConstantDesc.java
+++ b/src/java.base/share/classes/java/lang/constant/DynamicConstantDesc.java
@@ -350,12 +350,11 @@ public abstract class DynamicConstantDesc<T>
@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
diff --git a/src/java.base/share/classes/java/lang/invoke/InvokerBytecodeGenerator.java b/src/java.base/share/classes/java/lang/invoke/InvokerBytecodeGenerator.java
index 0524f3347ed..78e21ead506 100644
--- a/src/java.base/share/classes/java/lang/invoke/InvokerBytecodeGenerator.java
+++ b/src/java.base/share/classes/java/lang/invoke/InvokerBytecodeGenerator.java
@@ -302,8 +302,7 @@ class InvokerBytecodeGenerator {
}
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();
@@ -627,8 +626,7 @@ class InvokerBytecodeGenerator {
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;
@@ -1679,8 +1677,7 @@ class InvokerBytecodeGenerator {
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) {
diff --git a/src/java.base/share/classes/java/lang/invoke/LambdaForm.java b/src/java.base/share/classes/java/lang/invoke/LambdaForm.java
index 264966e7753..f70a753b3cf 100644
--- a/src/java.base/share/classes/java/lang/invoke/LambdaForm.java
+++ b/src/java.base/share/classes/java/lang/invoke/LambdaForm.java
@@ -568,8 +568,7 @@ class LambdaForm {
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);
@@ -1133,9 +1132,9 @@ class LambdaForm {
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
@@ -1407,8 +1406,7 @@ class LambdaForm {
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])
@@ -1435,8 +1433,7 @@ class LambdaForm {
@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);
}
diff --git a/src/java.base/share/classes/java/lang/invoke/LambdaFormEditor.java b/src/java.base/share/classes/java/lang/invoke/LambdaFormEditor.java
index 77f30636d1b..63240f9f260 100644
--- a/src/java.base/share/classes/java/lang/invoke/LambdaFormEditor.java
+++ b/src/java.base/share/classes/java/lang/invoke/LambdaFormEditor.java
@@ -337,9 +337,8 @@ class LambdaFormEditor {
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;
@@ -389,8 +388,7 @@ class LambdaFormEditor {
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) {
diff --git a/src/java.base/share/classes/java/lang/invoke/MemberName.java b/src/java.base/share/classes/java/lang/invoke/MemberName.java
index c11e2837c3b..3a0b4d4ebe0 100644
--- a/src/java.base/share/classes/java/lang/invoke/MemberName.java
+++ b/src/java.base/share/classes/java/lang/invoke/MemberName.java
@@ -147,12 +147,10 @@ final class MemberName implements Member, Cloneable {
// 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);
@@ -235,8 +233,7 @@ final class MemberName implements Member, Cloneable {
// 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;
@@ -938,8 +935,7 @@ final class MemberName implements Member, Cloneable {
} 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();
diff --git a/src/java.base/share/classes/java/lang/invoke/MethodHandles.java b/src/java.base/share/classes/java/lang/invoke/MethodHandles.java
index 9cf6d1f9693..4bba735dfe2 100644
--- a/src/java.base/share/classes/java/lang/invoke/MethodHandles.java
+++ b/src/java.base/share/classes/java/lang/invoke/MethodHandles.java
@@ -2262,10 +2262,9 @@ public class MethodHandles {
// 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");
}
diff --git a/src/java.base/share/classes/java/lang/invoke/VarHandles.java b/src/java.base/share/classes/java/lang/invoke/VarHandles.java
index a874993e665..ed38e475207 100644
--- a/src/java.base/share/classes/java/lang/invoke/VarHandles.java
+++ b/src/java.base/share/classes/java/lang/invoke/VarHandles.java
@@ -605,8 +605,7 @@ final class VarHandles {
}
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,
diff --git a/src/java.base/share/classes/java/lang/module/ModuleDescriptor.java b/src/java.base/share/classes/java/lang/module/ModuleDescriptor.java
index 648fb8b6c80..93ac7365624 100644
--- a/src/java.base/share/classes/java/lang/module/ModuleDescriptor.java
+++ b/src/java.base/share/classes/java/lang/module/ModuleDescriptor.java
@@ -310,10 +310,8 @@ public class ModuleDescriptor
*/
@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);
}
@@ -531,10 +529,8 @@ public class ModuleDescriptor
*/
@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);
}
@@ -736,12 +732,10 @@ public class ModuleDescriptor
*/
@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);
}
/**
@@ -872,11 +866,9 @@ public class ModuleDescriptor
*/
@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);
}
/**
@@ -2241,10 +2233,8 @@ public class ModuleDescriptor
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)
diff --git a/src/java.base/share/classes/java/lang/module/ResolvedModule.java b/src/java.base/share/classes/java/lang/module/ResolvedModule.java
index c4f4a0fa531..7309e291b4b 100644
--- a/src/java.base/share/classes/java/lang/module/ResolvedModule.java
+++ b/src/java.base/share/classes/java/lang/module/ResolvedModule.java
@@ -138,12 +138,9 @@ public final class ResolvedModule {
*/
@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);
}
/**
diff --git a/src/java.base/share/classes/java/lang/reflect/Method.java b/src/java.base/share/classes/java/lang/reflect/Method.java
index eba5a499b16..a4ea1e9ec09 100644
--- a/src/java.base/share/classes/java/lang/reflect/Method.java
+++ b/src/java.base/share/classes/java/lang/reflect/Method.java
@@ -714,8 +714,7 @@ public final class Method extends Executable {
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);
diff --git a/src/java.base/share/classes/java/lang/reflect/Parameter.java b/src/java.base/share/classes/java/lang/reflect/Parameter.java
index efda02e1e8c..9d2df265049 100644
--- a/src/java.base/share/classes/java/lang/reflect/Parameter.java
+++ b/src/java.base/share/classes/java/lang/reflect/Parameter.java
@@ -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;
}
/**