Skip to content

Commit 2ee1f96

Browse files
turbanoffAlekseiEfimov
authored andcommitted
8273484: Cleanup unnecessary null comparison before instanceof check in java.naming
Reviewed-by: aefimov, dfuchs
1 parent f189dff commit 2ee1f96

File tree

13 files changed

+33
-44
lines changed

13 files changed

+33
-44
lines changed

src/java.naming/share/classes/com/sun/jndi/ldap/LdapReferralContext.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -198,8 +198,8 @@ public void close() throws NamingException {
198198

199199
void setHopCount(int hopCount) {
200200
this.hopCount = hopCount;
201-
if ((refCtx != null) && (refCtx instanceof LdapCtx)) {
202-
((LdapCtx)refCtx).setHopCount(hopCount);
201+
if (refCtx instanceof LdapCtx ldapCtx) {
202+
ldapCtx.setHopCount(hopCount);
203203
}
204204
}
205205

src/java.naming/share/classes/com/sun/jndi/ldap/SimpleClientId.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -69,12 +69,10 @@ class SimpleClientId extends ClientId {
6969
}
7070

7171
public boolean equals(Object obj) {
72-
if (obj == null || !(obj instanceof SimpleClientId)) {
72+
if (!(obj instanceof SimpleClientId other)) {
7373
return false;
7474
}
7575

76-
SimpleClientId other = (SimpleClientId)obj;
77-
7876
return super.equals(obj)
7977
&& (username == other.username // null OK
8078
|| (username != null && username.equals(other.username)))

src/java.naming/share/classes/com/sun/jndi/ldap/pool/ConnectionDesc.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -65,9 +65,8 @@ final class ConnectionDesc {
6565
* PooledConnection.
6666
*/
6767
public boolean equals(Object obj) {
68-
return obj != null
69-
&& obj instanceof ConnectionDesc
70-
&& ((ConnectionDesc)obj).conn == conn;
68+
return (obj instanceof ConnectionDesc other)
69+
&& other.conn == conn;
7170
}
7271

7372
/**

src/java.naming/share/classes/com/sun/jndi/toolkit/ctx/AtomicContext.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -256,7 +256,7 @@ protected Object c_lookup(Name name, Continuation cont)
256256
Object ret = null;
257257
if (resolve_to_penultimate_context(name, cont)) {
258258
ret = a_lookup(name.toString(), cont);
259-
if (ret != null && ret instanceof LinkRef) {
259+
if (ret instanceof LinkRef) {
260260
cont.setContinue(ret, name, this);
261261
ret = null;
262262
}
@@ -348,7 +348,7 @@ protected Object c_resolveIntermediate_nns(Name name, Continuation cont)
348348
Object ret = null;
349349
if (resolve_to_penultimate_context_nns(name, cont)) {
350350
ret = a_resolveIntermediate_nns(name.toString(), cont);
351-
if (ret != null && ret instanceof LinkRef) {
351+
if (ret instanceof LinkRef) {
352352
cont.setContinue(ret, name, this);
353353
ret = null;
354354
}
@@ -368,7 +368,7 @@ protected Object c_lookup_nns(Name name, Continuation cont)
368368
Object ret = null;
369369
if (resolve_to_penultimate_context_nns(name, cont)) {
370370
ret = a_lookup_nns(name.toString(), cont);
371-
if (ret != null && ret instanceof LinkRef) {
371+
if (ret instanceof LinkRef) {
372372
cont.setContinue(ret, name, this);
373373
ret = null;
374374
}

src/java.naming/share/classes/javax/naming/BinaryRefAddr.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -121,8 +121,7 @@ public Object getContent() {
121121
* @return true if the object is equal; false otherwise.
122122
*/
123123
public boolean equals(Object obj) {
124-
if ((obj != null) && (obj instanceof BinaryRefAddr)) {
125-
BinaryRefAddr target = (BinaryRefAddr)obj;
124+
if (obj instanceof BinaryRefAddr target) {
126125
if (addrType.compareTo(target.addrType) == 0) {
127126
if (buf == null && target.buf == null)
128127
return true;

src/java.naming/share/classes/javax/naming/CompositeName.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -278,9 +278,8 @@ public String toString() {
278278
* @see #hashCode
279279
*/
280280
public boolean equals(Object obj) {
281-
return (obj != null &&
282-
obj instanceof CompositeName &&
283-
impl.equals(((CompositeName)obj).impl));
281+
return (obj instanceof CompositeName other) &&
282+
impl.equals(other.impl);
284283
}
285284

286285
/**

src/java.naming/share/classes/javax/naming/CompoundName.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -244,9 +244,8 @@ public String toString() {
244244
*/
245245
public boolean equals(Object obj) {
246246
// %%% check syntax too?
247-
return (obj != null &&
248-
obj instanceof CompoundName &&
249-
impl.equals(((CompoundName)obj).impl));
247+
return (obj instanceof CompoundName other) &&
248+
impl.equals(other.impl);
250249
}
251250

252251
/**

src/java.naming/share/classes/javax/naming/LinkRef.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -103,8 +103,8 @@ public LinkRef(String linkName) {
103103
public String getLinkName() throws NamingException {
104104
if (className != null && className.equals(linkClassName)) {
105105
RefAddr addr = get(linkAddrType);
106-
if (addr != null && addr instanceof StringRefAddr) {
107-
return (String)((StringRefAddr)addr).getContent();
106+
if (addr instanceof StringRefAddr stringRefAddr) {
107+
return (String) stringRefAddr.getContent();
108108
}
109109
}
110110
throw new MalformedLinkException();

src/java.naming/share/classes/javax/naming/NameImpl.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -473,8 +473,7 @@ public String toString() {
473473
}
474474

475475
public boolean equals(Object obj) {
476-
if ((obj != null) && (obj instanceof NameImpl)) {
477-
NameImpl target = (NameImpl)obj;
476+
if (obj instanceof NameImpl target) {
478477
if (target.size() == this.size()) {
479478
Enumeration<String> mycomps = getAll();
480479
Enumeration<String> comps = target.getAll();

src/java.naming/share/classes/javax/naming/RefAddr.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -104,8 +104,7 @@ public String getType() {
104104
* @see #getType
105105
*/
106106
public boolean equals(Object obj) {
107-
if ((obj != null) && (obj instanceof RefAddr)) {
108-
RefAddr target = (RefAddr)obj;
107+
if (obj instanceof RefAddr target) {
109108
if (addrType.compareTo(target.addrType) == 0) {
110109
Object thisobj = this.getContent();
111110
Object thatobj = target.getContent();

src/java.naming/share/classes/javax/naming/Reference.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -326,8 +326,7 @@ public void clear() {
326326
* @return true if obj is equal to this reference; false otherwise.
327327
*/
328328
public boolean equals(Object obj) {
329-
if ((obj != null) && (obj instanceof Reference)) {
330-
Reference target = (Reference)obj;
329+
if (obj instanceof Reference target) {
331330
// ignore factory information
332331
if (target.className.equals(this.className) &&
333332
target.size() == this.size()) {

src/java.naming/share/classes/javax/naming/directory/BasicAttribute.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -130,8 +130,7 @@ public Object clone() {
130130
* @see #contains
131131
*/
132132
public boolean equals(Object obj) {
133-
if ((obj != null) && (obj instanceof Attribute)) {
134-
Attribute target = (Attribute)obj;
133+
if (obj instanceof Attribute target) {
135134

136135
// Check order first
137136
if (isOrdered() != target.isOrdered()) {

src/java.naming/share/classes/javax/naming/directory/BasicAttributes.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -225,8 +225,7 @@ public String toString() {
225225
* @see #hashCode
226226
*/
227227
public boolean equals(Object obj) {
228-
if ((obj != null) && (obj instanceof Attributes)) {
229-
Attributes target = (Attributes)obj;
228+
if (obj instanceof Attributes target) {
230229

231230
// Check case first
232231
if (ignoreCase != target.isCaseIgnored()) {

0 commit comments

Comments
 (0)