java 这个标准化函数是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1279910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does this normalize function work?
提问by Egg
I was doing a Junit tutorial and I came across this normalize function that was being tested. It was defined like this:
我正在做一个 Junit 教程,我遇到了这个正在测试的 normalize 函数。它是这样定义的:
public static String normalizeWord(String word) {
try {
int i;
Class<?> normalizerClass = Class.forName("java.text.Normalizer");
Class<?> normalizerFormClass = null;
Class<?>[] nestedClasses = normalizerClass.getDeclaredClasses();
for (i = 0; i < nestedClasses.length; i++) {
Class<?> nestedClass = nestedClasses[i];
if (nestedClass.getName().equals("java.text.Normalizer$Form")) {
normalizerFormClass = nestedClass;
}
}
assert normalizerFormClass.isEnum();
Method methodNormalize = normalizerClass.getDeclaredMethod(
"normalize",
CharSequence.class,
normalizerFormClass);
Object nfcNormalization = null;
Object[] constants = normalizerFormClass.getEnumConstants();
for (i = 0; i < constants.length; i++) {
Object constant = constants[i];
if (constant.toString().equals("NFC")) {
nfcNormalization = constant;
}
}
return (String) methodNormalize.invoke(null, word, nfcNormalization);
} catch (Exception ex) {
return null;
}
}
How does this function work? What is it actually doing?
这个功能是如何工作的?它实际上在做什么?
回答by Pavel Minaev
It does the same as:
它的作用与:
import java.text.Normalizer;
try {
return Normalizer.normalize(word, Normalizer.Form.NFC);
} catch (Exception ex) {
return null;
}
Except that all operations are performed via Reflection.
除了所有操作都是通过反射执行的。
回答by Ben Lings
It's using reflection to call
它使用反射来调用
java.text.Normalizer.normalize(word, java.text.Normalizer.Form.NFC);
Presumably to allow it to run on Java versions before 1.6 which don't have this class.
大概是为了允许它在没有此类的 1.6 之前的 Java 版本上运行。
回答by Marian
This function offers services regarding strings normalization for Unicode. In Unicode, you can represent the same thing in many ways. For example, you have a character with accent. You can represent it joined, using one single Unicode character, or decomposed (the original letter, without accents, then the modifier - the accent).
此函数提供有关 Unicode 字符串规范化的服务。在 Unicode 中,您可以用多种方式表示相同的事物。例如,您有一个带重音的字符。您可以使用单个 Unicode 字符或分解(原始字母,不带重音,然后是修饰符 - 重音)来表示它的连接。
The class comes in Java 6. For Java 5, there's a SUN proprietary class.
该类来自 Java 6。对于 Java 5,有一个 SUN 专有类。
See class info.olteanu.utils.TextNormalizerin Phramer project (http://sourceforge.net/projects/phramer/, www.phramer.org ) for a way to get a normalizer both in Java 5 (SUN JDK) and in Java 6, without any compilation issues (the code will compile in any version >= 5 and the code will run in both JVMs, although SUN discarded the Java 5 proprietary class).
请参阅Phramer 项目(http://sourceforge.net/projects/phramer/, www.phramer.org )中的class info.olteanu.utils.TextNormalizer,了解在 Java 5 (SUN JDK) 和 Java 中获取规范化器的方法6,没有任何编译问题(代码将在任何版本>= 5 中编译,代码将在两个 JVM 中运行,尽管 SUN 放弃了 Java 5 专有类)。

