All,

Konstantin Kolinko pointed out that some code in the JDTCompiler was
particularly ugly, so I took a shot at improving it.

Unfortunately, I don't have much experience with that component so I
didn't want to commit anything. Can I get a review on this patch?

I'm basically just replacing string concatenation with StringBuilder but
I'm also changing the way that the "separator" characters are added a bit.

There's a similar optimization that could be made under "new
ICompilerRequestor() {" to remove additional looped string concatenation
in that source file. If this patch looks okay to a few folks, I'll make
a similar change there and commit the change.

Since this is not really a "bug" per se, I decided not to create a BZ
issue. If it would be easier for everyone if I attached the patch to a
BZ issue I'm happy to do that as well.

Thanks,
-chris

Index: java/org/apache/jasper/compiler/JDTCompiler.java
===================================================================
--- java/org/apache/jasper/compiler/JDTCompiler.java    (revision 1592807)
+++ java/org/apache/jasper/compiler/JDTCompiler.java    (working copy)
@@ -156,14 +156,13 @@
                 @Override
                 public NameEnvironmentAnswer
                     findType(char[][] compoundTypeName) {
-                    String result = "";
-                    String sep = "";
+                    StringBuilder result = new StringBuilder();
                     for (int i = 0; i < compoundTypeName.length; i++) {
-                        result += sep;
-                        result += new String(compoundTypeName[i]);
-                        sep = ".";
+                        if(i > 0)
+                            result.append('.');
+                        result.append(compoundTypeName[i]);
                     }
-                    return findType(result);
+                    return findType(result.toString());
                 }

                 @Override
@@ -170,16 +169,17 @@
                 public NameEnvironmentAnswer
                     findType(char[] typeName,
                              char[][] packageName) {
-                        String result = "";
-                        String sep = "";
-                        for (int i = 0; i < packageName.length; i++) {
-                            result += sep;
-                            result += new String(packageName[i]);
-                            sep = ".";
+                        StringBuilder result = new StringBuilder();
+                        int i=0;
+                        for ( ; i < packageName.length; i++) {
+                            if(i > 0)
+                                result.append('.');
+                            result.append(packageName[i]);
                         }
-                        result += sep;
-                        result += new String(typeName);
-                        return findType(result);
+                        if(i > 0)
+                            result.append('.');
+                        result.append(typeName);
+                        return findType(result.toString());
                 }

                 private NameEnvironmentAnswer findType(String className) {
@@ -234,25 +234,26 @@
                 @Override
                 public boolean isPackage(char[][] parentPackageName,
                                          char[] packageName) {
-                    String result = "";
-                    String sep = "";
+                    StringBuilder result = new StringBuilder();
+                    int i=0;
                     if (parentPackageName != null) {
-                        for (int i = 0; i < parentPackageName.length;
i++) {
-                            result += sep;
-                            String str = new String(parentPackageName[i]);
-                            result += str;
-                            sep = ".";
+                        for (; i < parentPackageName.length; i++) {
+                            if(i > 0)
+                                result.append('.');
+                            result.append(parentPackageName[i]);
                         }
                     }
-                    String str = new String(packageName);
-                    if (Character.isUpperCase(str.charAt(0))) {
-                        if (!isPackage(result)) {
+
+                    if (Character.isUpperCase(packageName[0])) {
+                        if (!isPackage(result.toString())) {
                             return false;
                         }
                     }
-                    result += sep;
-                    result += str;
-                    return isPackage(result);
+                    if(i > 0)
+                        result.append('.');
+                    result.append(packageName);
+
+                    return isPackage(result.toString());
                 }

                 @Override

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to