Skip to content

Commit 2f5d2ab

Browse files
author
ehennum
committed
exception throws and toString() nulls from static analysis #1159
1 parent 72f43ce commit 2f5d2ab

20 files changed

+124
-102
lines changed

marklogic-client-api/src/main/java/com/marklogic/client/extra/dom4j/DOM4JHandle.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public byte[] toBuffer() {
213213
public String toString() {
214214
try {
215215
byte[] buffer = toBuffer();
216-
return (buffer == null) ? "" : new String(buffer,"UTF-8");
216+
return (buffer == null) ? null : new String(buffer,"UTF-8");
217217
} catch (UnsupportedEncodingException e) {
218218
throw new MarkLogicIOException(e);
219219
}

marklogic-client-api/src/main/java/com/marklogic/client/extra/jdom/JDOMHandle.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public byte[] toBuffer() {
207207
public String toString() {
208208
try {
209209
byte[] buffer = toBuffer();
210-
return (buffer == null) ? "" : new String(buffer,"UTF-8");
210+
return (buffer == null) ? null : new String(buffer,"UTF-8");
211211
} catch (UnsupportedEncodingException e) {
212212
throw new MarkLogicIOException(e);
213213
}

marklogic-client-api/src/main/java/com/marklogic/client/impl/BaseProxy.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,7 @@ public SingleCallResponse responseSingle(boolean isNullable, Format returnFormat
891891
(returnFormat == null) ? Format.TEXT : returnFormat
892892
);
893893
if (responsedef.isNull() && !isNullable) {
894+
responsedef.close();
894895
throw new RequiredReturnException("null for required single return value");
895896
}
896897
return responsedef;

marklogic-client-api/src/main/java/com/marklogic/client/impl/BaseTypeImpl.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,7 @@ public StringBuilder exportAst(StringBuilder strb) {
316316
}
317317
@Override
318318
public String toString() {
319-
if (value == null) {
320-
return null;
321-
}
322-
return value.toString();
319+
return (value == null) ? null : value.toString();
323320
}
324321
}
325322

marklogic-client-api/src/main/java/com/marklogic/client/impl/BasicPage.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ public long size() {
101101

102102
@Override
103103
public long getTotalPages() {
104-
if ( getPageSize() == 0 ) return 0;
105-
return (long) Math.ceil((double) getTotalSize() / (double) getPageSize());
104+
return ceilingDivision(getTotalSize(), getPageSize());
106105
}
107106

108107
@Override
@@ -122,11 +121,13 @@ public boolean hasPreviousPage() {
122121

123122
@Override
124123
public long getPageNumber() {
125-
if ( getPageSize() == 0 ) return 0;
126-
double _start = (double) start;
127-
double _pageSize = (double) getPageSize();
128-
if ( _start % _pageSize == 0 ) return new Double(_start / _pageSize).longValue();
129-
else return (long) Math.floor(_start / _pageSize) + 1;
124+
return ceilingDivision(getStart(), getPageSize());
125+
}
126+
127+
private long ceilingDivision(long dividend, long divisor) {
128+
if (divisor == 0) return 0;
129+
long quotient = dividend / divisor;
130+
return (dividend % divisor == 0) ? quotient : quotient + 1;
130131
}
131132

132133
@Override

marklogic-client-api/src/main/java/com/marklogic/client/impl/DocumentManagerImpl.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -373,11 +373,10 @@ public <T extends R> T read(DocumentDescriptor desc,
373373
HandleImplementation metadataBase = HandleAccessor.checkHandle(
374374
metadataHandle, "metadata");
375375
Format metadataFormat = metadataBase.getFormat();
376-
if (metadataFormat == null
377-
|| (metadataFormat != Format.JSON && metadataFormat != Format.XML)) {
376+
if (metadataFormat != Format.JSON && metadataFormat != Format.XML) {
378377
if (logger.isWarnEnabled())
379378
logger.warn("Unsupported metadata format {}, using XML",
380-
metadataFormat.name());
379+
(metadataFormat == null) ? "null" : metadataFormat.name());
381380
metadataBase.setFormat(Format.XML);
382381
}
383382
}
@@ -535,7 +534,6 @@ private DocumentPage search(QueryDefinition querydef, long start,
535534
}
536535
}
537536

538-
String tid = transaction == null ? null : transaction.getTransactionId();
539537
// the default for bulk is no metadata, which differs from the normal
540538
// default of ALL
541539
Set<Metadata> metadata = isProcessedMetadataModified ? processedMetadata
@@ -904,11 +902,10 @@ protected TemporalDescriptor write(DocumentDescriptor desc, String temporalDocum
904902
HandleImplementation metadataBase = HandleAccessor.checkHandle(
905903
metadataHandle, "metadata");
906904
Format metadataFormat = metadataBase.getFormat();
907-
if (metadataFormat == null
908-
|| (metadataFormat != Format.JSON && metadataFormat != Format.XML)) {
905+
if (metadataFormat != Format.JSON && metadataFormat != Format.XML) {
909906
if (logger.isWarnEnabled())
910907
logger.warn("Unsupported metadata format {}, using XML",
911-
metadataFormat.name());
908+
(metadataFormat == null) ? "null" : metadataFormat.name());
912909
metadataBase.setFormat(Format.XML);
913910
}
914911
}
@@ -1228,11 +1225,10 @@ protected DocumentDescriptorImpl create(DocumentUriTemplate template,
12281225
HandleImplementation metadataBase = HandleAccessor.checkHandle(
12291226
metadataHandle, "metadata");
12301227
Format metadataFormat = metadataBase.getFormat();
1231-
if (metadataFormat == null
1232-
|| (metadataFormat != Format.JSON && metadataFormat != Format.XML)) {
1228+
if (metadataFormat != Format.JSON && metadataFormat != Format.XML) {
12331229
if (logger.isWarnEnabled())
12341230
logger.warn("Unsupported metadata format {}, using XML",
1235-
metadataFormat.name());
1231+
(metadataFormat == null) ? "null" : metadataFormat.name());
12361232
metadataBase.setFormat(Format.XML);
12371233
}
12381234
}

marklogic-client-api/src/main/java/com/marklogic/client/impl/HTTPKerberosAuthInterceptor.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public HTTPKerberosAuthInterceptor(String host, Map<String,String> krbOptions) {
6969
* Login Module to be used for authentication.
7070
*
7171
*/
72-
private class KerberosLoginConfiguration extends Configuration {
72+
static private class KerberosLoginConfiguration extends Configuration {
7373
Map<String,String> krbOptions = null;
7474

7575
public KerberosLoginConfiguration() {}
@@ -117,13 +117,20 @@ private void buildSubjectCredentials() throws LoginException {
117117
* principal is present
118118
*/
119119
private String getClientPrincipalName() {
120-
final Set<Principal> principalSet = loginContext.getSubject().getPrincipals();
120+
final Set<Principal> principalSet = getContextSubject().getPrincipals();
121121
if (principalSet.size() != 1)
122122
throw new IllegalStateException(
123123
"Only one principal is expected. Found 0 or more than one principals :" + principalSet);
124124
return principalSet.iterator().next().getName();
125125
}
126126

127+
private Subject getContextSubject() {
128+
Subject subject = loginContext.getSubject();
129+
if (subject == null)
130+
throw new IllegalStateException("Kerberos login context without subject");
131+
return subject;
132+
}
133+
127134
/**
128135
* This method builds the Authorization header for Kerberos. It
129136
* generates a request token based on the service ticket, client principal name and
@@ -150,7 +157,7 @@ private String buildAuthorizationHeader(String serverPrincipalName) throws Login
150157
* we build the Subject's private credentials again from valid TGT in the
151158
* Kerberos client cache.
152159
*/
153-
Set<Object> privateCreds = loginContext.getSubject().getPrivateCredentials();
160+
Set<Object> privateCreds = getContextSubject().getPrivateCredentials();
154161
for (Object privateCred : privateCreds) {
155162
if (privateCred instanceof KerberosTicket) {
156163
String serverPrincipalTicketName = ((KerberosTicket) privateCred).getServer().getName();
@@ -220,6 +227,9 @@ public Object run() {
220227
GSSContext.DEFAULT_LIFETIME);
221228
byte[] inToken = new byte[0];
222229
byte[] outToken = context.initSecContext(inToken, 0, inToken.length);
230+
if (outToken == null) {
231+
throw new FailedRequestException("could not initialize the security context");
232+
}
223233
context.requestMutualAuth(true);
224234
outputToken.append(new String(Base64.getEncoder().encode(outToken)));
225235
context.dispose();

marklogic-client-api/src/main/java/com/marklogic/client/impl/JacksonBaseHandle.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ public byte[] toBuffer() {
8585
@Override
8686
public String toString() {
8787
try {
88-
return new String(toBuffer(),"UTF-8");
88+
byte[] buffer = toBuffer();
89+
return (buffer == null) ? null : new String(buffer,"UTF-8");
8990
} catch (UnsupportedEncodingException e) {
9091
throw new MarkLogicIOException(e);
9192
}

0 commit comments

Comments
 (0)