Skip to content
Permalink
Browse files

fix: fix some sonarqube warnings

  • Loading branch information
vlsi committed Jun 23, 2016
1 parent 893c1a4 commit 7311b4ef160231780323573911c1543d0515340b
@@ -540,7 +540,9 @@ public static Properties parseURL(String url, Properties defaults) {
if (portIdx != -1 && address.lastIndexOf(']') < portIdx) {
String portStr = address.substring(portIdx + 1);
try {
Integer.parseInt(portStr);
// squid:S2201 The return value of "parseInt" must be used.
// The side effect is NumberFormatException, thus ignore sonar error here
Integer.parseInt(portStr); //NOSONAR
} catch (NumberFormatException ex) {
return null;
}
@@ -60,6 +60,6 @@ public int getExecuteCount() {
public long getSize() {
int queryLength = String.valueOf(key).length() * 2 /* 2 bytes per char */;
return queryLength * 2 /* original query and native sql */
+ 100 /* entry in hash map, CachedQuery wrapper, etc */;
+ 100L /* entry in hash map, CachedQuery wrapper, etc */;
}
}
@@ -245,7 +245,7 @@ public static Encoding defaultEncoding() {
*/
private static boolean isAvailable(String encodingName) {
try {
"DUMMY".getBytes(encodingName);
"DUMMY".getBytes(encodingName); //NOSONAR
return true;
} catch (java.io.UnsupportedEncodingException e) {
return false;
@@ -110,8 +110,6 @@ public void sendQueryCancel() throws SQLException {
cancelStream.SendInteger4(cancelKey);
cancelStream.flush();
cancelStream.ReceiveEOF();
cancelStream.close();
cancelStream = null;
} catch (IOException e) {
// Safe to ignore.
if (logger.logDebug()) {
@@ -111,8 +111,6 @@ public void sendQueryCancel() throws SQLException {
cancelStream.SendInteger4(cancelKey);
cancelStream.flush();
cancelStream.ReceiveEOF();
cancelStream.close();
cancelStream = null;
} catch (IOException e) {
// Safe to ignore.
if (logger.logDebug()) {
@@ -117,6 +117,7 @@ private void waitOnLock() throws PSQLException {
try {
this.wait();
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw new PSQLException(
GT.tr("Interrupted while waiting to obtain lock on database connection"),
PSQLState.OBJECT_NOT_IN_STATE, ie);
@@ -259,7 +259,7 @@ public ConnectionHandler(Connection con) {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
final String methodName = method.getName();
// From Object
if (method.getDeclaringClass().getName().equals("java.lang.Object")) {
if (method.getDeclaringClass() == Object.class) {
if (methodName.equals("toString")) {
return "Pooled connection wrapping physical connection " + con;
}
@@ -387,7 +387,7 @@ public StatementHandler(ConnectionHandler con, Statement st) {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
final String methodName = method.getName();
// From Object
if (method.getDeclaringClass().getName().equals("java.lang.Object")) {
if (method.getDeclaringClass() == Object.class) {
if (methodName.equals("toString")) {
return "Pooled statement wrapping physical statement " + st;
}
@@ -167,4 +167,9 @@ public String getValue() {
return "{" + a + "," + b + "," + c + "}";
}

@Override
public Object clone() throws CloneNotSupportedException {
// squid:S2157 "Cloneables" should implement "clone
return super.clone();
}
}
@@ -187,4 +187,10 @@ public void setLocation(int x, int y) {
public void setLocation(Point p) {
setLocation(p.x, p.y);
}

@Override
public Object clone() throws CloneNotSupportedException {
// squid:S2157 "Cloneables" should implement "clone
return super.clone();
}
}
@@ -72,8 +72,8 @@ public long getSize() {
return columnName.length() * 2
+ tableName.length() * 2
+ schemaName.length() * 2
+ 4
+ 1;
+ 4L
+ 1L;
}

@Override
@@ -12,6 +12,7 @@
import org.postgresql.core.BaseConnection;
import org.postgresql.core.Field;
import org.postgresql.util.GT;
import org.postgresql.util.JdbcBlackHole;
import org.postgresql.util.LruCache;
import org.postgresql.util.PSQLException;
import org.postgresql.util.PSQLState;
@@ -279,9 +280,10 @@ private void fetchFieldMetaData() throws SQLException {
}

Statement stmt = connection.createStatement();
ResultSet rs = null;
try {
LruCache<FieldMetadata.Key, FieldMetadata> metadataCache = connection.getFieldMetadataCache();
ResultSet rs = stmt.executeQuery(sql.toString());
rs = stmt.executeQuery(sql.toString());
while (rs.next()) {
int table = (int) rs.getLong(1);
int column = (int) rs.getLong(2);
@@ -297,11 +299,8 @@ private void fetchFieldMetaData() throws SQLException {
metadataCache.put(key, fieldMetadata);
}
} finally {
try {
stmt.close();
} catch (SQLException e) {
/* ignore */
}
JdbcBlackHole.close(rs);
JdbcBlackHole.close(stmt);
}
populateFieldsWithCachedMetadata();
}
@@ -1163,7 +1163,8 @@ private void killTimerTask() {
// Note: wait timeout here is irrelevant since synchronized(connection) would block until
// .cancel finishes
connection.wait(10);
} catch (InterruptedException e) {
} catch (InterruptedException e) { // NOSONAR
// Either re-interrupt this method or rethrow the "InterruptedException"
interrupted = true;
}
}
@@ -143,7 +143,7 @@ public LargeObjectManager(BaseConnection conn) throws SQLException {
Statement stmt = conn.createStatement();
ResultSet res = stmt.executeQuery(sql);

if (res == null) {
if (res == null) { // NOSONAR
throw new PSQLException(GT.tr("Failed to initialize LargeObject API"),
PSQLState.SYSTEM_ERROR);
}
@@ -164,7 +164,7 @@ public PrivateKey getPrivateKey(String alias) {
}

try {
raf = new RandomAccessFile(new File(keyfile), "r");
raf = new RandomAccessFile(new File(keyfile), "r"); // NOSONAR
} catch (FileNotFoundException ex) {
if (!defaultfile) {
// It is not an error if there is no file at the default location
@@ -117,7 +117,7 @@ public LibPQFactory(Properties info) throws PSQLException {
}
FileInputStream fis;
try {
fis = new FileInputStream(sslrootcertfile);
fis = new FileInputStream(sslrootcertfile); // NOSONAR
} catch (FileNotFoundException ex) {
throw new PSQLException(
GT.tr("Could not open SSL root certificate file {0}.", new Object[]{sslrootcertfile}),
@@ -128,7 +128,6 @@ public LibPQFactory(Properties info) throws PSQLException {
// Certificate[] certs = cf.generateCertificates(fis).toArray(new Certificate[]{}); //Does
// not work in java 1.4
Object[] certs = cf.generateCertificates(fis).toArray(new Certificate[]{});
fis.close();
ks.load(null, null);
for (int i = 0; i < certs.length; i++) {
ks.setCertificateEntry("cert" + i, (Certificate) certs[i]);
@@ -143,6 +142,12 @@ public LibPQFactory(Properties info) throws PSQLException {
GT.tr("Loading the SSL root certificate {0} into a TrustManager failed.",
new Object[]{sslrootcertfile}),
PSQLState.CONNECTION_FAILURE, gsex);
} finally {
try {
fis.close();
} catch (IOException e) {
/* ignore */
}
}
tm = tmf.getTrustManagers();
} else { // server validation is not required
@@ -428,4 +428,10 @@ public int hashCode() {
return ((((((7 * 31 + (int) Double.doubleToLongBits(seconds)) * 31 + minutes) * 31 + hours) * 31
+ days) * 31 + months) * 31 + years) * 31;
}

@Override
public Object clone() throws CloneNotSupportedException {
// squid:S2157 "Cloneables" should implement "clone
return super.clone();
}
}
@@ -40,7 +40,9 @@
for (int i = 0; i < output.length; i++) {
byte b1 = gethex(s[2 + i * 2]);
byte b2 = gethex(s[2 + i * 2 + 1]);
output[i] = (byte) ((b1 << 4) | b2);
// squid:S3034
// Raw byte values should not be used in bitwise operations in combination with shifts
output[i] = (byte) ((b1 << 4) | (b2 & 0xff));
}
return output;
}
@@ -92,4 +92,10 @@ public String getValue() {
return "$" + val;
}
}

@Override
public Object clone() throws CloneNotSupportedException {
// squid:S2157 "Cloneables" should implement "clone
return super.clone();
}
}
@@ -18,8 +18,6 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
@@ -486,23 +484,23 @@ public void testExecuteUpdateFailsOnMultiStatementSelect() throws SQLException {

public void testSetQueryTimeout() throws SQLException {
Statement stmt = con.createStatement();
Timer timer = new Timer(true);
long start = 0;
boolean cancelReceived = false;
try {

timer.schedule(new TimerTask() {
public void run() {
fail("Query timeout should have occurred and cleaned this up");
}
}, 5000);
stmt.setQueryTimeout(1);
start = System.currentTimeMillis();
stmt.execute("select pg_sleep(10)");

} catch (SQLException sqle) {
// state for cancel
if (sqle.getSQLState().compareTo("57014") == 0) {
timer.cancel();
if ("57014".equals(sqle.getSQLState())) {
cancelReceived = true;
}
}
long duration = System.currentTimeMillis() - start;
if (!cancelReceived || duration > 5000) {
fail("Query should have been cancelled since the timeout was set to 1 sec."
+ " Cancel state: " + cancelReceived + ", duration: " + duration);
}
}

/**

0 comments on commit 7311b4e

Please sign in to comment.
You can’t perform that action at this time.