我有两种方法,一种检查给定的值为NULL,字符串“NULL”或一个空字符串。另一个用html编码值替换html特定的字符并插入空格以启用换行符等。(请参阅下面的代码)。第二种方法使用第一种方法在做任何工作之前检查给定的值。如果子句在Java7_25中评估错误64Bit
public static String checkForNull(String aString, String aReturnValue)
{
String tBack = aString;
if ((aString == null) || (aString.equalsIgnoreCase("null")) || (aString.trim().equals("")))
{
tBack = aReturnValue;
}
return tBack;
}
public static String encodeAsHTMLEntities(String aValue, boolean aLineBreakAsBR, String[] aUnencodedParts,
boolean aInsertRatedSpace, int aMinimalWordSize)
{
StringBuilder tResult = new StringBuilder();
if(StringUtils.checkForNull(aValue, null) != null)
{
String tTempValue = aValue;
List<String> tUnencodedPartList = new ArrayList<String>();
if(aUnencodedParts != null)
{
tUnencodedPartList.addAll(Arrays.asList(aUnencodedParts));
}
/* Replace all linebreaks by HTML-tag if needed. */
if (aLineBreakAsBR == true)
{
tTempValue = tTempValue.replaceAll("\n", "<br />");
/* Add the br tag to the array containing parts that must not be encoded. */
tUnencodedPartList.add("<[Bb][Rr]\\s*[/]?>");
}
/* HTML-encode the value. */
int tCharsAfterLastSplitSymbol = 1;
Pattern tPattern = Pattern.compile("[\\s\\-,.;:]");
String tSplitterInvisible = "​";
String tSplitterVisible = "­";
if (aMinimalWordSize < 1)
{
aMinimalWordSize = Constants.MINIMAL_WORD_SIZE_BEFORE_SEPARATING;
}
for (int i = 0; i < tTempValue.length(); i++)
{
/* Test if we have an exception for the following value. */
boolean tIsAllowed = false;
String tStringToCheck = tTempValue.substring(i);
for (int t = 0; t < tUnencodedPartList.size() && tIsAllowed == false; t++)
{
String tUnencodedPart = tUnencodedPartList.get(t);
String tMatchingString = tStringToCheck.substring(0, tStringToCheck.length() - tStringToCheck.replaceFirst("^(" + tUnencodedPart + ")", "").length());
if (tMatchingString.length() > 0)
{
if (aInsertRatedSpace == true)
{
tResult.append(tSplitterInvisible);
}
tIsAllowed = true;
i += tMatchingString.length() - 1;
tResult.append(tMatchingString);
if (aInsertRatedSpace == true)
{
tResult.append(tSplitterInvisible);
}
}
}
if (tIsAllowed == false)
{
char tChar = tTempValue.charAt(i);
/* Add the encoded char */
tResult.append(encodeAsHTMLEntity(tChar));
/* Add splitter */
if (aInsertRatedSpace == true)
{
/* Check the character for beeing one of our split symbols */
Matcher tMatcher = tPattern.matcher(Character.toString(tChar));
String tSplitter = "";
if (tCharsAfterLastSplitSymbol >= aMinimalWordSize)
{
boolean tUseVisibleSplitter = true;
if (tMatcher.find())
{
tUseVisibleSplitter = false;
}
else
{
/* Check if next character matches to our reg exp */
if (tTempValue.length() >= (i + 2))
{
tChar = tTempValue.charAt(i+1);
tMatcher = tPattern.matcher(Character.toString(tChar));
if (tMatcher.find())
{
tUseVisibleSplitter = false;
}
}
/* Check if the next characters matches to one of our unencoded parts */
if (tUseVisibleSplitter)
{
String tNextStringToCheck = tTempValue.substring(i+1);
for (int t = 0; t < tUnencodedPartList.size() && tUseVisibleSplitter == true; t++)
{
String tUnencodedPart = tUnencodedPartList.get(t);
String tMatchingString = tNextStringToCheck.substring(0, tNextStringToCheck.length() - tNextStringToCheck.replaceFirst("^(" + tUnencodedPart + ")", "").length());
if (tMatchingString.length() > 0)
{
tUseVisibleSplitter = false;
}
}
}
}
/* Choose the correct splitting symbol */
if (tUseVisibleSplitter)
{
tSplitter = tSplitterVisible;
}
else
{
tSplitter = tSplitterInvisible;
}
tCharsAfterLastSplitSymbol = 1;
}
else
{
if (tMatcher.find())
{
tSplitter = tSplitterInvisible;
tCharsAfterLastSplitSymbol = 1;
}
else
{
tCharsAfterLastSplitSymbol++;
}
}
tResult.append(tSplitter);
}
}
else
{
tCharsAfterLastSplitSymbol = 1;
}
}
}
return tResult.toString();
}
当我添加一个System.out.println()
检查其返回checkForNull()
方法总是返回正确的值。在方法encodeAsHTMLEntities()
中,调用checkForNull()
方法突然停止工作,并且不再输入if(StringUtils.checkForNull(aValue, null) != null)
。我可以将StringUtils.checkForNull(aValue, null)
的结果放在另一个变量中并打印出来,并且返回的值总是正确的。如果我使用If
来检查此变量是否为空,则代码也会失败。
我发现了两种方法来使代码工作。 第一个是写checkForNull()
这样的:
public static String checkForNull(String aString, String aReturnValue)
{
String tBack = aString;
if (aString == null)
{
tBack = aReturnValue;
}
else if (aString.equalsIgnoreCase("null")
{
tBack = aReturnValue;
}
els if (aString.trim().equals(""))
{
tBack = aReturnValue;
}
return tBack;
}
第二个是运行在调试模式下,这意味着添加调试参数到VM或添加任何调试语句的代码的代码。 (这是使用System.out.println()
的原因,因为任何尝试调试都可以解决问题,所以调试器在这种情况下确实没有帮助)
代码在应用程序中运行良好并且在编译和运行时没有问题与Java6 32Bit,Java6 64Bit和Java7 32Bit。这个错误只发生在使用Java7 64Bit版本编译和运行( - >测试了几个从7_5到7_40的补丁)时
有没有人知道这里的问题会是什么? 我可以提供一个主类,其中包含所有代码和一些带有测试字符串的文件,以便在任何人感兴趣时重现错误。
编辑:进一步的测试表明,错误只发生在Windows系统上。在linux上没有问题。