我从事一个大型的java项目(移动应用程序),并且由于性能不佳(高复杂性)而得到了“感恩”的工作来审查和优化/重构代码。 注意:我对Java完全不熟悉(我的背景是C / C ++),因此我会针对任何愚蠢的问题进行applog。 我做的第一件事就是使用Findbugs并修复所有报告的问题。之后我使用度量工具了解以获得概述 方法具有很高的圈复杂度。不幸的是,有很多方法具有高于2 ^ 20范围的cyclometic复杂性:-( 其中一个是我需要一些帮助或好主意的地方......
简短说明:
对于与服务器的通信,必须序列化数据。此移动框架上没有可用的Serializable接口。
因此,编写所有代码(单独)的同事实现了一个Serializable接口,其中包含一个名为toByteArray()
的方法。
例如,class Customer
:
class Customer
{
Address address;
AttributeCollection attributes;
LocationCollection locations;
int recId;
int recStatus;
DateTime recCreated;
String recCreatedBy;
String recCreatedByProg;
DateTime recChanged;
String recChangedBy;
String recChangedByProg;
int refAddressesId;
int refMandatorsId;
CustomerPropertyUsage usage;
/**
* Serialize the properties of a class into a byte array.
* @param destData Byte array, where the serialized data should be stored. Minimum 2 bytes.
* @param serializationIndex Offset within the passed byte array, from which the serialized data of the class
* should be entered. The offset is increased by the registered number of bytes so that after this method the
* next call points to the serialized data subsequent byte.
*/
void toByteArray(byte[] destData, IntClass serializationIndex)
{
if (this.address == null)
this.usage.value &= ~CustomerPropertyUsage.ADDRESS;
if (this.attributes == null)
this.usage.value &= ~CustomerPropertyUsage.ATTRIBUTES;
if (this.locations == null)
this.usage.value &= ~CustomerPropertyUsage.LOCATIONS;
this.usage.toByteArray(destData, serializationIndex);
CatrString catrString = null;
if ((this.usage.value & CustomerPropertyUsage.RECORD_HEADER) != CustomerPropertyUsage.NONE)
{
// Call static method getBytes from SerializationHelper class
SerializationHelper.getBytes(this.recId, 4, destData, serializationIndex.value);
serializationIndex.value += 4;
SerializationHelper.getBytes(this.recStatus, 4, destData, serializationIndex.value);
serializationIndex.value += 4;
// recChanged is a DateTime object. For the serialization we need minimum a 7 bytes array,
// Call method toByteArray() from DateTime class.
this.recChanged.toByteArray(destData, serializationIndex);
// call toByteArray of CatrString class
catrString = new CatrString(this.recChangedBy);
catrString.toByteArray(destData, serializationIndex);
catrString.setValue(this.recChangedByProg);
catrString.toByteArray(destData, serializationIndex);
// Same as recChanged
this.recCreated.toByteArray(destData, serializationIndex);
catrString = new CatrString(this.recCreatedBy);
catrString.toByteArray(destData, serializationIndex);
catrString.setValue(this.recCreatedByProg);
catrString.toByteArray(destData, serializationIndex);
SerializationHelper.getBytes(this.refAddressesId, 4, destData, serializationIndex.value);
serializationIndex.value += 4;
SerializationHelper.getBytes(this.refMandatorsId, 4, destData, serializationIndex.value);
serializationIndex.value += 4;
}
if (next property...)
{
... Serialization ...
}
if (next property...)
{
... Serialization ...
}
}
}
为了保持GPRS低,服务器在this.usage.value中设置一个值,因此只有一个特定的属性将被序列化并传回服务器 - > 传输的消息很小。 这种方法会产生大量的if-cases,具体取决于类中的属性数量,因此路径计数越来越高。 我认为这不是一个美丽的解决方案,但它没关系。我想改变的是if-cases中的序列化调用。 目前他们看起来像这样:
---- class SerializationHelper ----
static void getBytes(long valueToConvert, int numOfBytesToConvert, byte[] destinationBytes, int destinationBytesOffset)
{
destinationBytes[destinationBytesOffset] = (byte)(valueToConvert & 0x000000FF);
destinationBytes[destinationBytesOffset + 1] = (byte)((valueToConvert & 0x0000FF00) >> 8);
if (numOfBytesToConvert > 2)
{
destinationBytes[destinationBytesOffset + 2] = (byte)((valueToConvert & 0x00FF0000) >> 16);
destinationBytes[destinationBytesOffset + 3] = (byte)((valueToConvert & 0xFF000000) >> 24);
if (numOfBytesToConvert > 4)
{
destinationBytes[destinationBytesOffset + 4] = (byte)((valueToConvert & 0x000000FF00000000L) >> 32);
destinationBytes[destinationBytesOffset + 5] = (byte)((valueToConvert & 0x0000FF0000000000L) >> 40);
destinationBytes[destinationBytesOffset + 6] = (byte)((valueToConvert & 0x00FF000000000000L) >> 48);
destinationBytes[destinationBytesOffset + 7] = (byte)((valueToConvert & 0xFF00000000000000L) >> 56);
}
}
}
---- class CatrString ----
void toByteArray(byte[] destData, IntClass serializationIndex)
{
// Number of unicode characters
SerializationHelper.getBytes(this.textLength, 2, destData, serializationIndex.value);
serializationIndex.value += 2;
// Text UTF-16 unicode characters
for (int charIndex = 0; charIndex < this.textLength; charIndex++)
{
destData[serializationIndex.value] = (byte)(this.charCodes[charIndex] & 0x00FF);
serializationIndex.value++;
destData[serializationIndex.value] = (byte)((this.charCodes[charIndex] & 0xFF00) >> 8);
serializationIndex.value++;
}
// Code End of string as UTF-16 unicode character
destData[serializationIndex.value] = 0x00;
serializationIndex.value++;
destData[serializationIndex.value] = 0x00;
serializationIndex.value++;
}
---- class DateTime ----
void toByteArray(byte[] destData, IntClass serializationIndex)
{
destData[serializationIndex.value + 0] = (byte) (m_year % 0x0100); // year low-Byte.
destData[serializationIndex.value + 1] = (byte) (m_year / 0x0100); // year high-Byte.
destData[serializationIndex.value + 2] = (byte) (m_month);
destData[serializationIndex.value + 3] = (byte) m_day;
destData[serializationIndex.value + 4] = (byte) m_hour;
destData[serializationIndex.value + 5] = (byte) m_minute;
destData[serializationIndex.value + 6] = (byte) m_second;
serializationIndex.value += 7;
}
应该可以写一个更“通用”的类来完成所有序列化的东西,我说序列化xy
字节,就是这样。但我不明白的是那些像“Strings”(UTF-16编码)这样的“特殊”toByteArray()
方法
还是日期和时间?如果我把它们打包成一个类是一个很好的解决方案?我有没有获得一些东西?主要代码?高性能代码??
你的方法是什么?
THX
答案 0 :(得分:1)
可能最好为每个类创建一个属性Map。默认情况下,该集合为空,但在通话时,例如setAddress(address)我们调用fieldsMap.put(ADDRESS_KEY_STRING,address)而不是分配给类'字段。
要存储所有acesses(现有)属性,我们只需浏览存储每个条目的fieldsMap。