Skip to content

Commit 86b8dc9

Browse files
committed
8265426: Update java.security to use instanceof pattern variable
Reviewed-by: rriggs, weijun, dfuchs
1 parent 3fcdc50 commit 86b8dc9

23 files changed

+85
-165
lines changed

src/java.base/share/classes/java/security/AccessControlContext.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -750,18 +750,9 @@ public boolean equals(Object obj) {
750750
if (obj == this)
751751
return true;
752752

753-
if (! (obj instanceof AccessControlContext))
754-
return false;
755-
756-
AccessControlContext that = (AccessControlContext) obj;
757-
758-
if (!equalContext(that))
759-
return false;
760-
761-
if (!equalLimitedContext(that))
762-
return false;
763-
764-
return true;
753+
return obj instanceof AccessControlContext that
754+
&& equalContext(that)
755+
&& equalLimitedContext(that);
765756
}
766757

767758
/*

src/java.base/share/classes/java/security/BasicPermission.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -364,27 +364,25 @@ public BasicPermissionCollection(Class<?> clazz) {
364364
*/
365365
@Override
366366
public void add(Permission permission) {
367-
if (! (permission instanceof BasicPermission))
367+
if (!(permission instanceof BasicPermission basicPermission))
368368
throw new IllegalArgumentException("invalid permission: "+
369369
permission);
370370
if (isReadOnly())
371371
throw new SecurityException("attempt to add a Permission to a readonly PermissionCollection");
372372

373-
BasicPermission bp = (BasicPermission) permission;
374-
375373
// make sure we only add new BasicPermissions of the same class
376374
// Also check null for compatibility with deserialized form from
377375
// previous versions.
378376
if (permClass == null) {
379377
// adding first permission
380-
permClass = bp.getClass();
378+
permClass = basicPermission.getClass();
381379
} else {
382-
if (bp.getClass() != permClass)
380+
if (basicPermission.getClass() != permClass)
383381
throw new IllegalArgumentException("invalid permission: " +
384382
permission);
385383
}
386384

387-
String canonName = bp.getCanonicalName();
385+
String canonName = basicPermission.getCanonicalName();
388386
perms.put(canonName, permission);
389387

390388
// No sync on all_allowed; staleness OK
@@ -405,13 +403,11 @@ public void add(Permission permission) {
405403
*/
406404
@Override
407405
public boolean implies(Permission permission) {
408-
if (! (permission instanceof BasicPermission))
406+
if (!(permission instanceof BasicPermission basicPermission))
409407
return false;
410408

411-
BasicPermission bp = (BasicPermission) permission;
412-
413409
// random subclasses of BasicPermission do not imply each other
414-
if (bp.getClass() != permClass)
410+
if (basicPermission.getClass() != permClass)
415411
return false;
416412

417413
// short circuit if the "*" Permission was added
@@ -422,7 +418,7 @@ public boolean implies(Permission permission) {
422418
// Check for full match first. Then work our way up the
423419
// path looking for matches on a.b..*
424420

425-
String path = bp.getCanonicalName();
421+
String path = basicPermission.getCanonicalName();
426422
//System.out.println("check "+path);
427423

428424
Permission x = perms.get(path);

src/java.base/share/classes/java/security/CodeSigner.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,9 @@ public int hashCode() {
126126
* @return true if the objects are considered equal, false otherwise.
127127
*/
128128
public boolean equals(Object obj) {
129-
if (obj == null || (!(obj instanceof CodeSigner))) {
129+
if (obj == null || (!(obj instanceof CodeSigner that))) {
130130
return false;
131131
}
132-
CodeSigner that = (CodeSigner)obj;
133132

134133
if (this == that) {
135134
return true;

src/java.base/share/classes/java/security/CodeSource.java

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import java.io.ByteArrayInputStream;
3535
import java.io.IOException;
3636
import java.security.cert.*;
37+
import java.util.Objects;
38+
3739
import sun.net.util.URLUtil;
3840
import sun.security.util.IOUtils;
3941

@@ -157,22 +159,9 @@ public boolean equals(Object obj) {
157159
return true;
158160

159161
// objects types must be equal
160-
if (!(obj instanceof CodeSource))
161-
return false;
162-
163-
CodeSource cs = (CodeSource) obj;
164-
165-
// URLs must match
166-
if (location == null) {
167-
// if location is null, then cs.location must be null as well
168-
if (cs.location != null) return false;
169-
} else {
170-
// if location is not null, then it must equal cs.location
171-
if (!location.equals(cs.location)) return false;
172-
}
173-
174-
// certs must match
175-
return matchCerts(cs, true);
162+
return (obj instanceof CodeSource other)
163+
&& Objects.equals(location, other.location)
164+
&& matchCerts(other, true);
176165
}
177166

178167
/**

src/java.base/share/classes/java/security/Identity.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -334,20 +334,12 @@ public Certificate[] certificates() {
334334
* @see #identityEquals
335335
*/
336336
public final boolean equals(Object identity) {
337-
338337
if (identity == this) {
339338
return true;
340339
}
341340

342-
if (identity instanceof Identity) {
343-
Identity i = (Identity)identity;
344-
if (this.fullName().equals(i.fullName())) {
345-
return true;
346-
} else {
347-
return identityEquals(i);
348-
}
349-
}
350-
return false;
341+
return identity instanceof Identity other
342+
&& (this.fullName().equals(other.fullName()) || identityEquals(other));
351343
}
352344

353345
/**

src/java.base/share/classes/java/security/KeyFactory.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,9 @@ private KeyFactorySpi nextSpi(KeyFactorySpi oldSpi) {
313313
Service s = serviceIterator.next();
314314
try {
315315
Object obj = s.newInstance(null);
316-
if (obj instanceof KeyFactorySpi == false) {
316+
if (!(obj instanceof KeyFactorySpi spi)) {
317317
continue;
318318
}
319-
KeyFactorySpi spi = (KeyFactorySpi)obj;
320319
provider = s.getProvider();
321320
this.spi = spi;
322321
return spi;

src/java.base/share/classes/java/security/KeyPairGenerator.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -623,13 +623,12 @@ private KeyPairGeneratorSpi nextSpi(KeyPairGeneratorSpi oldSpi,
623623
try {
624624
Object inst = s.newInstance(null);
625625
// ignore non-spis
626-
if (inst instanceof KeyPairGeneratorSpi == false) {
626+
if (!(inst instanceof KeyPairGeneratorSpi spi)) {
627627
continue;
628628
}
629629
if (inst instanceof KeyPairGenerator) {
630630
continue;
631631
}
632-
KeyPairGeneratorSpi spi = (KeyPairGeneratorSpi)inst;
633632
if (reinit) {
634633
if (initType == I_SIZE) {
635634
spi.initialize(initKeySize, initRandom);

src/java.base/share/classes/java/security/KeyStore.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,13 +1961,13 @@ public static Builder newInstance(String type, Provider provider,
19611961
if ((type == null) || (file == null) || (protection == null)) {
19621962
throw new NullPointerException();
19631963
}
1964-
if ((protection instanceof PasswordProtection == false) &&
1965-
(protection instanceof CallbackHandlerProtection == false)) {
1964+
if (!(protection instanceof PasswordProtection) &&
1965+
!(protection instanceof CallbackHandlerProtection)) {
19661966
throw new IllegalArgumentException
19671967
("Protection must be PasswordProtection or " +
19681968
"CallbackHandlerProtection");
19691969
}
1970-
if (file.isFile() == false) {
1970+
if (!file.isFile()) {
19711971
throw new IllegalArgumentException
19721972
("File does not exist or it does not refer " +
19731973
"to a normal file: " + file);
@@ -2056,7 +2056,7 @@ public synchronized KeyStore getKeyStore() throws KeyStoreException
20562056
PrivilegedExceptionAction<KeyStore> action =
20572057
new PrivilegedExceptionAction<KeyStore>() {
20582058
public KeyStore run() throws Exception {
2059-
if (protection instanceof CallbackHandlerProtection == false) {
2059+
if (!(protection instanceof CallbackHandlerProtection)) {
20602060
return run0();
20612061
}
20622062
// when using a CallbackHandler,
@@ -2190,7 +2190,7 @@ public KeyStore run() throws Exception {
21902190
ks = KeyStore.getInstance(type, provider);
21912191
}
21922192
LoadStoreParameter param = new SimpleLoadStoreParameter(protection);
2193-
if (protection instanceof CallbackHandlerProtection == false) {
2193+
if (!(protection instanceof CallbackHandlerProtection)) {
21942194
ks.load(param);
21952195
} else {
21962196
// when using a CallbackHandler,

src/java.base/share/classes/java/security/MessageDigest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,7 @@ public static MessageDigest getInstance(String algorithm,
298298
if (provider == null)
299299
throw new IllegalArgumentException("missing provider");
300300
Object[] objs = Security.getImpl(algorithm, "MessageDigest", provider);
301-
if (objs[0] instanceof MessageDigest) {
302-
MessageDigest md = (MessageDigest)objs[0];
301+
if (objs[0] instanceof MessageDigest md) {
303302
md.provider = (Provider)objs[1];
304303
return md;
305304
} else {

src/java.base/share/classes/java/security/Provider.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,10 +1077,8 @@ public boolean equals(Object obj) {
10771077
if (this == obj) {
10781078
return true;
10791079
}
1080-
if (!(obj instanceof ServiceKey other)) {
1081-
return false;
1082-
}
1083-
return this.type.equals(other.type)
1080+
return obj instanceof ServiceKey other
1081+
&& this.type.equals(other.type)
10841082
&& this.algorithm.equals(other.algorithm);
10851083
}
10861084
boolean matches(String type, String algorithm) {
@@ -1500,11 +1498,8 @@ public boolean equals(Object obj) {
15001498
if (this == obj) {
15011499
return true;
15021500
}
1503-
if (obj instanceof UString == false) {
1504-
return false;
1505-
}
1506-
UString other = (UString)obj;
1507-
return lowerString.equals(other.lowerString);
1501+
return obj instanceof UString other
1502+
&& lowerString.equals(other.lowerString);
15081503
}
15091504

15101505
public String toString() {
@@ -2005,16 +2000,16 @@ public boolean supportsParameter(Object parameter) {
20052000
// unknown engine type, return true by default
20062001
return true;
20072002
}
2008-
if (cap.supportsParameter == false) {
2003+
if (!cap.supportsParameter) {
20092004
throw new InvalidParameterException("supportsParameter() not "
20102005
+ "used with " + type + " engines");
20112006
}
20122007
// allow null for keys without attributes for compatibility
2013-
if ((parameter != null) && (parameter instanceof Key == false)) {
2008+
if ((parameter != null) && (!(parameter instanceof Key))) {
20142009
throw new InvalidParameterException
20152010
("Parameter must be instanceof Key for engine " + type);
20162011
}
2017-
if (hasKeyAttributes() == false) {
2012+
if (!hasKeyAttributes()) {
20182013
return true;
20192014
}
20202015
if (parameter == null) {

0 commit comments

Comments
 (0)