IDEMPIERE-308 Performance: Replace use of StringBuffer and String concatenation with StringBuilder / replace String.concat used in loops - cases detected using findbugs / thanks to Richard Morales and David Peñuela

This commit is contained in:
Carlos Ruiz 2012-09-17 22:12:48 -05:00
parent 340acefdf6
commit fa5b24e7e2
8 changed files with 71 additions and 57 deletions

View File

@ -151,14 +151,22 @@ public class UUIDGenerator extends SvrProcess {
}
sql.append(" FROM ").append(table.getTableName());
sql.append(" WHERE ").append(column.getColumnName()).append(" IS NULL ");
String updateSQL = "UPDATE "+table.getTableName()+" SET "+column.getColumnName()+"=? WHERE ";
StringBuffer updateSQL = new StringBuffer();
updateSQL.append("UPDATE ");
updateSQL.append(table.getTableName());
updateSQL.append(" SET ");
updateSQL.append(column.getColumnName());
updateSQL.append("=? WHERE ");
if (AD_Column_ID > 0) {
updateSQL = updateSQL + keyColumn + "=?";
updateSQL.append(keyColumn);
updateSQL.append("=?");
} else {
for(String s : compositeKeys) {
updateSQL = updateSQL + s + "=? AND ";
updateSQL.append(s);
updateSQL.append("=? AND ");
}
updateSQL = updateSQL.substring(0, updateSQL.length() - " AND ".length());
int length = updateSQL.length();
updateSQL.delete(length-5, length); // delete last AND
}
boolean localTrx = false;
@ -180,7 +188,7 @@ public class UUIDGenerator extends SvrProcess {
int recordId = rs.getInt(1);
if (recordId > MTable.MAX_OFFICIAL_ID) {
UUID uuid = UUID.randomUUID();
DB.executeUpdateEx(updateSQL,new Object[]{uuid.toString(), recordId}, trx.getTrxName());
DB.executeUpdateEx(updateSQL.toString(),new Object[]{uuid.toString(), recordId}, trx.getTrxName());
}
} else {
UUID uuid = UUID.randomUUID();
@ -189,7 +197,7 @@ public class UUIDGenerator extends SvrProcess {
for (String s : compositeKeys) {
params.add(rs.getObject(s));
}
DB.executeUpdateEx(updateSQL,params.toArray(),trx.getTrxName());
DB.executeUpdateEx(updateSQL.toString(),params.toArray(),trx.getTrxName());
}
}
if (localTrx) {

View File

@ -45,6 +45,7 @@ import org.compiere.model.MTable;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.Util;
/**
* Executes search and opens windows for defined transaction codes
@ -67,12 +68,12 @@ public abstract class AbstractDocumentSearch {
log.fine("Search started with String: " + searchString);
// Check if / how many transaction-codes are used
if (searchString != null && !"".equals(searchString)) {
if (! Util.isEmpty(searchString)) {
String[] codes = searchString.trim().replaceAll(" ", " ").split(" ");
List<String> codeList = new ArrayList<String>();
boolean codeSearch = true;
searchString = "";
StringBuffer search = new StringBuffer();
// Analyze String to separate transactionCodes from searchString
for (int i = 0; i < codes.length; i++) {
@ -84,9 +85,9 @@ public abstract class AbstractDocumentSearch {
// Build the searchString with eventually appearing
// whitespaces
codeSearch = false;
searchString += s;
search.append(s);
if (i != (codes.length - 1)) {
searchString += " ";
search.append(" ");
}
}
} catch (SQLException e) {
@ -99,12 +100,12 @@ public abstract class AbstractDocumentSearch {
if (codeList.size() > 0) {
for (int i = 0; i < codeList.size(); i++) {
log.fine("Search with Transaction: '" + codeList.get(i) + "' for: '"
+ searchString + "'");
getID(codeList.get(i), searchString);
+ search.toString() + "'");
getID(codeList.get(i), search.toString());
}
} else {
log.fine("Search without Transaction: " + searchString);
getID(null, searchString);
log.fine("Search without Transaction: " + search.toString());
getID(null, search.toString());
}
} else {
log.fine("Search String is invalid");
@ -252,32 +253,35 @@ public abstract class AbstractDocumentSearch {
if (ids == null || ids.size() == 0) {
return;
}
String whereString = " " + tableName + "_ID";
StringBuffer whereString = new StringBuffer();
whereString.append(" ");
whereString.append(tableName);
whereString.append("_ID");
// create query string
if (ids.size() == 1) {
if (ids.get(0).intValue() == 0) {
whereString = null;
} else {
whereString += "=" + ids.get(0).intValue();
whereString.append("=");
whereString.append(ids.get(0).intValue());
}
} else {
whereString += " IN (";
whereString.append(" IN (");
for (int i = 0; i < ids.size(); i++) {
whereString += ids.get(i).intValue();
whereString.append(ids.get(i).intValue());
if (i < ids.size() - 1) {
whereString += ",";
whereString.append(",");
} else {
whereString += ") ";
whereString.append(") ");
}
}
}
log.fine(whereString);
final MQuery query = new MQuery(tableName);
query.addRestriction(whereString);
query.addRestriction(whereString.toString());
final boolean ok = openWindow(windowId, query);
if (!ok) {
log.severe("Unable to open window: " + whereString);
log.severe("Unable to open window: " + whereString.toString());
}
if (!windowOpened && ok)
windowOpened = true;

View File

@ -839,10 +839,10 @@ public class ModelClassGenerator
if (!tableLike.startsWith("'") || !tableLike.endsWith("'"))
tableLike = "'" + tableLike + "'";
String entityTypeFilter = null;
StringBuffer entityTypeFilter = new StringBuffer();
if (entityType != null && entityType.trim().length() > 0)
{
entityTypeFilter = "EntityType IN (";
entityTypeFilter.append("EntityType IN (");
StringTokenizer tokenizer = new StringTokenizer(entityType, ",");
int i = 0;
while(tokenizer.hasMoreTokens()) {
@ -850,15 +850,15 @@ public class ModelClassGenerator
if (!token.startsWith("'") || !token.endsWith("'"))
token = "'" + token + "'";
if (i > 0)
entityTypeFilter = entityTypeFilter + ",";
entityTypeFilter = entityTypeFilter + token;
entityTypeFilter.append(",");
entityTypeFilter.append(token);
i++;
}
entityTypeFilter = entityTypeFilter+")";
entityTypeFilter.append(")");
}
else
{
entityTypeFilter = "EntityType IN ('U','A')";
entityTypeFilter.append("EntityType IN ('U','A')");
}
String directory = sourceFolder.trim();
@ -884,7 +884,7 @@ public class ModelClassGenerator
.append(" OR IsView='N')")
.append(" AND IsActive = 'Y' AND TableName NOT LIKE '%_Trl' ");
sql.append(" AND TableName LIKE ").append(tableLike);
sql.append(" AND ").append(entityTypeFilter);
sql.append(" AND ").append(entityTypeFilter.toString());
sql.append(" ORDER BY TableName");
//

View File

@ -774,10 +774,10 @@ public class ModelInterfaceGenerator
if (!tableLike.startsWith("'") || !tableLike.endsWith("'"))
tableLike = "'" + tableLike + "'";
String entityTypeFilter = null;
StringBuffer entityTypeFilter = new StringBuffer();
if (entityType != null && entityType.trim().length() > 0)
{
entityTypeFilter = "EntityType IN (";
entityTypeFilter.append("EntityType IN (");
StringTokenizer tokenizer = new StringTokenizer(entityType, ",");
int i = 0;
while(tokenizer.hasMoreTokens()) {
@ -785,15 +785,15 @@ public class ModelInterfaceGenerator
if (!token.startsWith("'") || !token.endsWith("'"))
token = "'" + token + "'";
if (i > 0)
entityTypeFilter = entityTypeFilter + ",";
entityTypeFilter = entityTypeFilter + token;
entityTypeFilter.append(",");
entityTypeFilter.append(token);
i++;
}
entityTypeFilter = entityTypeFilter+")";
entityTypeFilter.append(")");
}
else
{
entityTypeFilter = "EntityType IN ('U','A')";
entityTypeFilter.append("EntityType IN ('U','A')");
}
String directory = sourceFolder.trim();
@ -819,7 +819,7 @@ public class ModelInterfaceGenerator
.append(" OR IsView='N')")
.append(" AND IsActive = 'Y' AND TableName NOT LIKE '%_Trl' ");
sql.append(" AND TableName LIKE ").append(tableLike);
sql.append(" AND ").append(entityTypeFilter);
sql.append(" AND ").append(entityTypeFilter.toString());
sql.append(" ORDER BY TableName");
//

View File

@ -217,10 +217,10 @@ public abstract class OFXBankStatementHandler extends DefaultHandler
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
reader.mark(HEADER_SIZE + 100);
String header = "";
StringBuffer header = new StringBuffer("");
for (int i = 0; i < HEADER_SIZE; i++)
{
header = header + reader.readLine();
header.append(reader.readLine());
}
if ((header.indexOf("<?OFX") != -1) || (header.indexOf("<?ofx") != -1))
{

View File

@ -214,18 +214,18 @@ public class MLanguage extends X_AD_Language
// some short formats have only one M and d (e.g. ths US)
if (sFormat.indexOf("MM") == -1 && sFormat.indexOf("dd") == -1)
{
String nFormat = "";
StringBuffer nFormat = new StringBuffer("");
for (int i = 0; i < sFormat.length(); i++)
{
if (sFormat.charAt(i) == 'M')
nFormat += "MM";
nFormat.append("MM");
else if (sFormat.charAt(i) == 'd')
nFormat += "dd";
nFormat.append("dd");
else
nFormat += sFormat.charAt(i);
nFormat.append(sFormat.charAt(i));
}
// System.out.println(sFormat + " => " + nFormat);
m_dateFormat.applyPattern(nFormat);
m_dateFormat.applyPattern(nFormat.toString());
}
// Unknown short format => use JDBC
if (m_dateFormat.toPattern().length() != 8)
@ -235,15 +235,15 @@ public class MLanguage extends X_AD_Language
if (m_dateFormat.toPattern().indexOf("yyyy") == -1)
{
sFormat = m_dateFormat.toPattern();
String nFormat = "";
StringBuffer nFormat = new StringBuffer("");
for (int i = 0; i < sFormat.length(); i++)
{
if (sFormat.charAt(i) == 'y')
nFormat += "yy";
nFormat.append("yy");
else
nFormat += sFormat.charAt(i);
nFormat.append(sFormat.charAt(i));
}
m_dateFormat.applyPattern(nFormat);
m_dateFormat.applyPattern(nFormat.toString());
}
}
//

View File

@ -197,12 +197,14 @@ public class MPasswordRule extends X_AD_PasswordRule {
passwordData.setUsername(username);
RuleResult result = validator.validate(passwordData);
if (!result.isValid()) {
String error = Msg.getMsg(getCtx(), "PasswordErrors") + ": [";
StringBuffer error = new StringBuffer(Msg.getMsg(getCtx(), "PasswordErrors"));
error.append(": [");
for (String msg : validator.getMessages(result)) {
error = error + " " + msg;
error.append(" ");
error.append(msg);
}
error = error + " ]";
throw new AdempiereException(error);
error.append(" ]");
throw new AdempiereException(error.toString());
}
}
}

View File

@ -626,15 +626,15 @@ public class Language implements Serializable
if (m_dateFormat.toPattern().indexOf("yyyy") == -1)
{
sFormat = m_dateFormat.toPattern();
String nFormat = "";
StringBuffer nFormat = new StringBuffer("");
for (int i = 0; i < sFormat.length(); i++)
{
if (sFormat.charAt(i) == 'y')
nFormat += "yy";
nFormat.append("yy");
else
nFormat += sFormat.charAt(i);
nFormat.append(sFormat.charAt(i));
}
m_dateFormat.applyPattern(nFormat);
m_dateFormat.applyPattern(nFormat.toString());
}
m_dateFormat.setLenient(true);
}