001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.tools.sourceformatter;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
018    import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
019    import com.liferay.portal.kernel.util.CharPool;
020    import com.liferay.portal.kernel.util.ClassUtil;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.util.Tuple;
026    import com.liferay.portal.kernel.util.Validator;
027    
028    import com.thoughtworks.qdox.JavaDocBuilder;
029    import com.thoughtworks.qdox.model.JavaClass;
030    import com.thoughtworks.qdox.model.JavaSource;
031    
032    import java.io.File;
033    import java.io.IOException;
034    
035    import java.util.ArrayList;
036    import java.util.Collection;
037    import java.util.Iterator;
038    import java.util.List;
039    import java.util.Properties;
040    import java.util.Set;
041    import java.util.TreeSet;
042    import java.util.regex.Matcher;
043    import java.util.regex.Pattern;
044    
045    /**
046     * @author Hugo Huijser
047     */
048    public class JavaSourceProcessor extends BaseSourceProcessor {
049    
050            public static final int TYPE_CLASS_PRIVATE = 24;
051    
052            public static final int TYPE_CLASS_PRIVATE_STATIC = 23;
053    
054            public static final int TYPE_CLASS_PROTECTED = 16;
055    
056            public static final int TYPE_CLASS_PROTECTED_STATIC = 15;
057    
058            public static final int TYPE_CLASS_PUBLIC = 8;
059    
060            public static final int TYPE_CLASS_PUBLIC_STATIC = 7;
061    
062            public static final int[] TYPE_CONSTRUCTOR = {
063                    JavaSourceProcessor.TYPE_CONSTRUCTOR_PRIVATE,
064                    JavaSourceProcessor.TYPE_CONSTRUCTOR_PROTECTED,
065                    JavaSourceProcessor.TYPE_CONSTRUCTOR_PUBLIC
066            };
067    
068            public static final int TYPE_CONSTRUCTOR_PRIVATE = 18;
069    
070            public static final int TYPE_CONSTRUCTOR_PROTECTED = 10;
071    
072            public static final int TYPE_CONSTRUCTOR_PUBLIC = 4;
073    
074            public static final int[] TYPE_METHOD = {
075                    JavaSourceProcessor.TYPE_METHOD_PRIVATE,
076                    JavaSourceProcessor.TYPE_METHOD_PRIVATE_STATIC,
077                    JavaSourceProcessor.TYPE_METHOD_PROTECTED,
078                    JavaSourceProcessor.TYPE_METHOD_PROTECTED_STATIC,
079                    JavaSourceProcessor.TYPE_METHOD_PUBLIC,
080                    JavaSourceProcessor.TYPE_METHOD_PUBLIC_STATIC
081            };
082    
083            public static final int TYPE_METHOD_PRIVATE = 19;
084    
085            public static final int TYPE_METHOD_PRIVATE_STATIC = 17;
086    
087            public static final int TYPE_METHOD_PROTECTED = 11;
088    
089            public static final int TYPE_METHOD_PROTECTED_STATIC = 9;
090    
091            public static final int TYPE_METHOD_PUBLIC = 5;
092    
093            public static final int TYPE_METHOD_PUBLIC_STATIC = 3;
094    
095            public static final int[] TYPE_VARIABLE = {
096                    JavaSourceProcessor.TYPE_VARIABLE_PRIVATE,
097                    JavaSourceProcessor.TYPE_VARIABLE_PRIVATE_STATIC,
098                    JavaSourceProcessor.TYPE_VARIABLE_PRIVATE_STATIC_FINAL,
099                    JavaSourceProcessor.TYPE_VARIABLE_PROTECTED,
100                    JavaSourceProcessor.TYPE_VARIABLE_PROTECTED_STATIC,
101                    JavaSourceProcessor.TYPE_VARIABLE_PROTECTED_STATIC_FINAL,
102                    JavaSourceProcessor.TYPE_VARIABLE_PUBLIC,
103                    JavaSourceProcessor.TYPE_VARIABLE_PUBLIC_STATIC,
104                    JavaSourceProcessor.TYPE_VARIABLE_PUBLIC_STATIC_FINAL
105            };
106    
107            public static final int TYPE_VARIABLE_PRIVATE = 22;
108    
109            public static final int TYPE_VARIABLE_PRIVATE_STATIC = 21;
110    
111            public static final int TYPE_VARIABLE_PRIVATE_STATIC_FINAL = 20;
112    
113            public static final int TYPE_VARIABLE_PROTECTED = 14;
114    
115            public static final int TYPE_VARIABLE_PROTECTED_STATIC = 13;
116    
117            public static final int TYPE_VARIABLE_PROTECTED_STATIC_FINAL = 12;
118    
119            public static final int TYPE_VARIABLE_PUBLIC = 6;
120    
121            public static final int TYPE_VARIABLE_PUBLIC_STATIC = 2;
122    
123            public static final int TYPE_VARIABLE_PUBLIC_STATIC_FINAL = 1;
124    
125            public static String stripJavaImports(
126                            String content, String packageDir, String className)
127                    throws IOException {
128    
129                    Pattern pattern = Pattern.compile(
130                            "(^[ \t]*import\\s+.*;\n+)+", Pattern.MULTILINE);
131    
132                    Matcher matcher = pattern.matcher(content);
133    
134                    if (!matcher.find()) {
135                            return content;
136                    }
137    
138                    String imports = matcher.group();
139    
140                    Set<String> classes = ClassUtil.getClasses(
141                            new UnsyncStringReader(content), className);
142    
143                    StringBundler sb = new StringBundler();
144    
145                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
146                            new UnsyncStringReader(imports));
147    
148                    String line = null;
149    
150                    while ((line = unsyncBufferedReader.readLine()) != null) {
151                            if (!line.contains("import ")) {
152                                    continue;
153                            }
154    
155                            int importX = line.indexOf(" ");
156                            int importY = line.lastIndexOf(".");
157    
158                            String importPackage = line.substring(importX + 1, importY);
159    
160                            if (importPackage.equals(packageDir) ||
161                                    importPackage.equals("java.lang")) {
162    
163                                    continue;
164                            }
165    
166                            String importClass = line.substring(importY + 1, line.length() - 1);
167    
168                            if (importClass.equals("*") || classes.contains(importClass)) {
169                                    sb.append(line);
170                                    sb.append("\n");
171                            }
172                    }
173    
174                    imports = formatImports(sb.toString(), 7);
175    
176                    content =
177                            content.substring(0, matcher.start()) + imports +
178                                    content.substring(matcher.end());
179    
180                    // Ensure a blank line exists between the package and the first import
181    
182                    content = content.replaceFirst(
183                            "(?m)^[ \t]*(package .*;)\\s*^[ \t]*import", "$1\n\nimport");
184    
185                    // Ensure a blank line exists between the last import (or package if
186                    // there are no imports) and the class comment
187    
188                    content = content.replaceFirst(
189                            "(?m)^[ \t]*((?:package|import) .*;)\\s*^[ \t]*/\\*\\*",
190                            "$1\n\n/**");
191    
192                    return content;
193            }
194    
195            protected static boolean isInJavaTermTypeGroup(
196                    int javaTermType, int[] javaTermTypeGroup) {
197    
198                    for (int type : javaTermTypeGroup) {
199                            if (javaTermType == type) {
200                                    return true;
201                            }
202                    }
203    
204                    return false;
205            }
206    
207            protected List<String> addParameterTypes(
208                    String line, List<String> parameterTypes) {
209    
210                    int x = line.indexOf(StringPool.OPEN_PARENTHESIS);
211    
212                    if (x != -1) {
213                            line = line.substring(x + 1);
214    
215                            if (Validator.isNull(line) ||
216                                    line.startsWith(StringPool.CLOSE_PARENTHESIS)) {
217    
218                                    return parameterTypes;
219                            }
220                    }
221    
222                    for (x = 0;;) {
223                            x = line.indexOf(StringPool.SPACE);
224    
225                            if (x == -1) {
226                                    return parameterTypes;
227                            }
228    
229                            String parameterType = line.substring(0, x);
230    
231                            if (parameterType.equals("throws")) {
232                                    return parameterTypes;
233                            }
234    
235                            parameterTypes.add(parameterType);
236    
237                            int y = line.indexOf(StringPool.COMMA);
238                            int z = line.indexOf(StringPool.CLOSE_PARENTHESIS);
239    
240                            if ((y == -1) || ((z != -1) && (z < y))) {
241                                    return parameterTypes;
242                            }
243    
244                            line = line.substring(y + 1);
245                            line = line.trim();
246                    }
247            }
248    
249            protected void checkAnnotationForMethod(
250                    JavaTerm javaTerm, String annotation, String requiredMethodNameRegex,
251                    int requiredMethodType, String fileName) {
252    
253                    String methodContent = javaTerm.getContent();
254                    String methodName = javaTerm.getName();
255    
256                    Pattern pattern = Pattern.compile(requiredMethodNameRegex);
257    
258                    Matcher matcher = pattern.matcher(methodName);
259    
260                    if (methodContent.contains(
261                                    StringPool.TAB + StringPool.AT + annotation + "\n") ||
262                            methodContent.contains(
263                                    StringPool.TAB + StringPool.AT + annotation +
264                                            StringPool.OPEN_PARENTHESIS)) {
265    
266                            if (!matcher.find()) {
267                                    processErrorMessage(
268                                            fileName,
269                                            "LPS-36303: Incorrect method name: " + methodName + " " +
270                                                    fileName);
271                            }
272                            else if (javaTerm.getType() != requiredMethodType) {
273                                    processErrorMessage(
274                                            fileName,
275                                            "LPS-36303: Incorrect method type for " + methodName + " " +
276                                                    fileName);
277                            }
278                    }
279                    else if (matcher.find() &&
280                                     !methodContent.contains(StringPool.TAB + "@Override")) {
281    
282                            processErrorMessage(
283                                    fileName,
284                                    "Annotation @" + annotation + " required for " + methodName +
285                                            " " + fileName);
286                    }
287            }
288    
289            protected String checkIfClause(
290                            String ifClause, String fileName, int lineCount)
291                    throws IOException {
292    
293                    String ifClauseSingleLine = StringUtil.replace(
294                            ifClause,
295                            new String[] {
296                                    StringPool.TAB + StringPool.SPACE, StringPool.TAB,
297                                    StringPool.OPEN_PARENTHESIS + StringPool.NEW_LINE,
298                                    StringPool.NEW_LINE
299                            },
300                            new String[] {
301                                    StringPool.TAB, StringPool.BLANK, StringPool.OPEN_PARENTHESIS,
302                                    StringPool.SPACE
303                            });
304    
305                    checkIfClauseParentheses(ifClauseSingleLine, fileName, lineCount);
306    
307                    return checkIfClauseTabsAndSpaces(ifClause);
308            }
309    
310            protected String checkIfClauseTabsAndSpaces(String ifClause)
311                    throws IOException {
312    
313                    if (ifClause.contains("!(") ||
314                            ifClause.contains(StringPool.TAB + "//")) {
315    
316                            return ifClause;
317                    }
318    
319                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
320                            new UnsyncStringReader(ifClause));
321    
322                    String line = null;
323    
324                    String previousLine = null;
325                    int previousLineLeadingWhiteSpace = 0;
326    
327                    int lastCriteriumLineLeadingWhiteSpace = 0;
328    
329                    int closeParenthesesCount = 0;
330                    int openParenthesesCount = 0;
331    
332                    while ((line = unsyncBufferedReader.readLine()) != null) {
333                            String originalLine = line;
334    
335                            line = StringUtil.replace(
336                                    line, StringPool.TAB, StringPool.FOUR_SPACES);
337    
338                            int leadingWhiteSpace =
339                                    line.length() - StringUtil.trimLeading(line).length();
340    
341                            if (Validator.isNull(previousLine)) {
342                                    lastCriteriumLineLeadingWhiteSpace = line.indexOf(
343                                            StringPool.OPEN_PARENTHESIS);
344                            }
345                            else if (previousLine.endsWith("|") || previousLine.endsWith("&") ||
346                                             previousLine.endsWith("^")) {
347    
348                                    int expectedLeadingWhiteSpace =
349                                            lastCriteriumLineLeadingWhiteSpace +
350                                                    openParenthesesCount - closeParenthesesCount;
351    
352                                    if (leadingWhiteSpace != expectedLeadingWhiteSpace) {
353                                            return fixIfClause(
354                                                    ifClause, originalLine,
355                                                    leadingWhiteSpace - expectedLeadingWhiteSpace);
356                                    }
357    
358                                    lastCriteriumLineLeadingWhiteSpace = leadingWhiteSpace;
359    
360                                    closeParenthesesCount = 0;
361                                    openParenthesesCount = 0;
362                            }
363                            else {
364                                    int expectedLeadingWhiteSpace = 0;
365    
366                                    if (previousLine.contains(StringPool.TAB + "if (")) {
367                                            expectedLeadingWhiteSpace =
368                                                    previousLineLeadingWhiteSpace + 8;
369                                    }
370                                    else if (previousLine.contains(StringPool.TAB + "else if (") ||
371                                                     previousLine.contains(StringPool.TAB + "while (")) {
372    
373                                            expectedLeadingWhiteSpace =
374                                                    previousLineLeadingWhiteSpace + 12;
375                                    }
376    
377                                    if ((expectedLeadingWhiteSpace != 0) &&
378                                            (leadingWhiteSpace != expectedLeadingWhiteSpace)) {
379    
380                                            return fixIfClause(
381                                                    ifClause, originalLine,
382                                                    leadingWhiteSpace - expectedLeadingWhiteSpace);
383                                    }
384                            }
385    
386                            if (line.endsWith(") {")) {
387                                    return ifClause;
388                            }
389    
390                            line = stripQuotes(line, CharPool.QUOTE);
391                            line = stripQuotes(line, CharPool.APOSTROPHE);
392    
393                            closeParenthesesCount += StringUtil.count(
394                                    line, StringPool.CLOSE_PARENTHESIS);
395                            openParenthesesCount += StringUtil.count(
396                                    line, StringPool.OPEN_PARENTHESIS);
397    
398                            previousLine = originalLine;
399                            previousLineLeadingWhiteSpace = leadingWhiteSpace;
400                    }
401    
402                    return ifClause;
403            }
404    
405            protected void checkTestAnnotations(JavaTerm javaTerm, String fileName) {
406                    int methodType = javaTerm.getType();
407    
408                    if ((methodType != TYPE_METHOD_PUBLIC) &&
409                            (methodType != TYPE_METHOD_PUBLIC_STATIC)) {
410    
411                            return;
412                    }
413    
414                    checkAnnotationForMethod(
415                            javaTerm, "After", "^.*tearDown\\z", TYPE_METHOD_PUBLIC, fileName);
416                    checkAnnotationForMethod(
417                            javaTerm, "AfterClass", "^.*tearDownClass\\z",
418                            TYPE_METHOD_PUBLIC_STATIC, fileName);
419                    checkAnnotationForMethod(
420                            javaTerm, "Before", "^.*setUp\\z", TYPE_METHOD_PUBLIC, fileName);
421                    checkAnnotationForMethod(
422                            javaTerm, "BeforeClass", "^.*setUpClass\\z",
423                            TYPE_METHOD_PUBLIC_STATIC, fileName);
424                    checkAnnotationForMethod(
425                            javaTerm, "Test", "^.*test", TYPE_METHOD_PUBLIC, fileName);
426            }
427    
428            protected void checkUnprocessedExceptions(
429                            String content, File file, String packagePath, String fileName)
430                    throws IOException {
431    
432                    List<String> importedExceptionClassNames = null;
433                    JavaDocBuilder javaDocBuilder = null;
434    
435                    Pattern catchExceptionPattern = Pattern.compile(
436                            "\n(\t+)catch \\((.+Exception) (.+)\\) \\{\n");
437    
438                    for (int lineCount = 1;;) {
439                            Matcher catchExceptionMatcher = catchExceptionPattern.matcher(
440                                    content);
441    
442                            if (!catchExceptionMatcher.find()) {
443                                    return;
444                            }
445    
446                            String beforeCatchCode = content.substring(
447                                    0, catchExceptionMatcher.start());
448    
449                            lineCount = lineCount + StringUtil.count(beforeCatchCode, "\n") + 1;
450    
451                            String exceptionClassName = catchExceptionMatcher.group(2);
452                            String exceptionVariableName = catchExceptionMatcher.group(3);
453                            String tabs = catchExceptionMatcher.group(1);
454    
455                            int pos = content.indexOf(
456                                    "\n" + tabs + StringPool.CLOSE_CURLY_BRACE,
457                                    catchExceptionMatcher.end() - 1);
458    
459                            String insideCatchCode = content.substring(
460                                    catchExceptionMatcher.end(), pos + 1);
461    
462                            Pattern exceptionVariablePattern = Pattern.compile(
463                                    "\\W" + exceptionVariableName + "\\W");
464    
465                            Matcher exceptionVariableMatcher = exceptionVariablePattern.matcher(
466                                    insideCatchCode);
467    
468                            if (exceptionVariableMatcher.find()) {
469                                    content = content.substring(catchExceptionMatcher.start() + 1);
470    
471                                    continue;
472                            }
473    
474                            if (javaDocBuilder == null) {
475                                    javaDocBuilder = new JavaDocBuilder();
476    
477                                    javaDocBuilder.addSource(file);
478                            }
479    
480                            if (importedExceptionClassNames == null) {
481                                    importedExceptionClassNames = getImportedExceptionClassNames(
482                                            javaDocBuilder);
483                            }
484    
485                            String originalExceptionClassName = exceptionClassName;
486    
487                            if (!exceptionClassName.contains(StringPool.PERIOD)) {
488                                    for (String exceptionClass : importedExceptionClassNames) {
489                                            if (exceptionClass.endsWith(
490                                                            StringPool.PERIOD + exceptionClassName)) {
491    
492                                                    exceptionClassName = exceptionClass;
493    
494                                                    break;
495                                            }
496                                    }
497                            }
498    
499                            if (!exceptionClassName.contains(StringPool.PERIOD)) {
500                                    exceptionClassName =
501                                            packagePath + StringPool.PERIOD + exceptionClassName;
502                            }
503    
504                            JavaClass exceptionClass = javaDocBuilder.getClassByName(
505                                    exceptionClassName);
506    
507                            while (true) {
508                                    String packageName = exceptionClass.getPackageName();
509    
510                                    if (!packageName.contains("com.liferay")) {
511                                            break;
512                                    }
513    
514                                    exceptionClassName = exceptionClass.getName();
515    
516                                    if (exceptionClassName.equals("PortalException") ||
517                                            exceptionClassName.equals("SystemException")) {
518    
519                                            processErrorMessage(
520                                                    fileName,
521                                                    "Unprocessed " + originalExceptionClassName + ": " +
522                                                            fileName + " " + lineCount);
523    
524                                            break;
525                                    }
526    
527                                    JavaClass exceptionSuperClass =
528                                            exceptionClass.getSuperJavaClass();
529    
530                                    if (exceptionSuperClass == null) {
531                                            break;
532                                    }
533    
534                                    exceptionClass = exceptionSuperClass;
535                            }
536    
537                            content = content.substring(catchExceptionMatcher.start() + 1);
538                    }
539            }
540    
541            protected String fixDataAccessConnection(String className, String content) {
542                    int x = content.indexOf("package ");
543    
544                    int y = content.indexOf(CharPool.SEMICOLON, x);
545    
546                    if ((x == -1) || (y == -1)) {
547                            return content;
548                    }
549    
550                    String packageName = content.substring(x + 8, y);
551    
552                    if (!packageName.startsWith("com.liferay.portal.kernel.upgrade") &&
553                            !packageName.startsWith("com.liferay.portal.kernel.verify") &&
554                            !packageName.startsWith("com.liferay.portal.upgrade") &&
555                            !packageName.startsWith("com.liferay.portal.verify")) {
556    
557                            return content;
558                    }
559    
560                    content = StringUtil.replace(
561                            content, "DataAccess.getConnection",
562                            "DataAccess.getUpgradeOptimizedConnection");
563    
564                    return content;
565            }
566    
567            protected String fixIfClause(String ifClause, String line, int delta) {
568                    String newLine = line;
569    
570                    String whiteSpace = StringPool.BLANK;
571                    int whiteSpaceLength = Math.abs(delta);
572    
573                    while (whiteSpaceLength > 0) {
574                            if (whiteSpaceLength >= 4) {
575                                    whiteSpace += StringPool.TAB;
576    
577                                    whiteSpaceLength -= 4;
578                            }
579                            else {
580                                    whiteSpace += StringPool.SPACE;
581    
582                                    whiteSpaceLength -= 1;
583                            }
584                    }
585    
586                    if (delta > 0) {
587                            if (!line.contains(StringPool.TAB + whiteSpace)) {
588                                    newLine = StringUtil.replaceLast(
589                                            newLine, StringPool.TAB, StringPool.FOUR_SPACES);
590                            }
591    
592                            newLine = StringUtil.replaceLast(
593                                    newLine, StringPool.TAB + whiteSpace, StringPool.TAB);
594                    }
595                    else {
596                            newLine = StringUtil.replaceLast(
597                                    newLine, StringPool.TAB, StringPool.TAB + whiteSpace);
598                    }
599    
600                    return StringUtil.replace(ifClause, line, newLine);
601            }
602    
603            protected String fixIncorrectEmptyLineBeforeCloseCurlyBrace(
604                    String content, String fileName) {
605    
606                    if (fileName.endsWith("AnnotationLocatorTest.java")) {
607                            return content;
608                    }
609    
610                    Pattern pattern = Pattern.compile("\n\n(\t+)}\n");
611    
612                    Matcher matcher = pattern.matcher(content);
613    
614                    while (matcher.find()) {
615                            String tabs = matcher.group(1);
616                            int tabCount = tabs.length();
617    
618                            int pos = matcher.start();
619    
620                            while (true) {
621                                    pos = content.lastIndexOf("\n" + tabs, pos - 1);
622    
623                                    if (content.charAt(pos + tabCount + 1) == CharPool.TAB) {
624                                            continue;
625                                    }
626    
627                                    String codeBlock = content.substring(pos + tabCount + 1);
628    
629                                    String firstLine = codeBlock.substring(
630                                            0, codeBlock.indexOf("\n"));
631    
632                                    if (firstLine.contains(" class ") ||
633                                            firstLine.contains(" enum ") ||
634                                            firstLine.contains(" interface ") ||
635                                            firstLine.startsWith("new ") ||
636                                            firstLine.contains(" new ")) {
637    
638                                            break;
639                                    }
640    
641                                    return StringUtil.replaceFirst(
642                                            content, "\n\n" + tabs + "}\n", "\n" + tabs + "}\n", pos);
643                            }
644                    }
645    
646                    return content;
647            }
648    
649            protected String fixJavaTermsDividers(
650                    String fileName, String content, Set<JavaTerm> javaTerms) {
651    
652                    JavaTerm previousJavaTerm = null;
653    
654                    Iterator<JavaTerm> itr = javaTerms.iterator();
655    
656                    while (itr.hasNext()) {
657                            JavaTerm javaTerm = itr.next();
658    
659                            if (previousJavaTerm == null) {
660                                    previousJavaTerm = javaTerm;
661    
662                                    continue;
663                            }
664    
665                            String javaTermContent = javaTerm.getContent();
666    
667                            if (javaTermContent.startsWith(StringPool.TAB + "//") ||
668                                    javaTermContent.contains(StringPool.TAB + "static {")) {
669    
670                                    previousJavaTerm = javaTerm;
671    
672                                    continue;
673                            }
674    
675                            String previousJavaTermContent = previousJavaTerm.getContent();
676    
677                            if (previousJavaTermContent.startsWith(StringPool.TAB + "//") ||
678                                    previousJavaTermContent.contains(StringPool.TAB + "static {")) {
679    
680                                    previousJavaTerm = javaTerm;
681    
682                                    continue;
683                            }
684    
685                            String javaTermName = javaTerm.getName();
686    
687                            String excluded = null;
688    
689                            if (_javaTermSortExclusions != null) {
690                                    excluded = _javaTermSortExclusions.getProperty(
691                                            fileName + StringPool.AT + javaTerm.getLineCount());
692    
693                                    if (excluded == null) {
694                                            excluded = _javaTermSortExclusions.getProperty(
695                                                    fileName + StringPool.AT + javaTermName);
696                                    }
697    
698                                    if (excluded == null) {
699                                            excluded = _javaTermSortExclusions.getProperty(fileName);
700                                    }
701                            }
702    
703                            if (excluded != null) {
704                                    previousJavaTerm = javaTerm;
705    
706                                    continue;
707                            }
708    
709                            String previousJavaTermName = previousJavaTerm.getName();
710    
711                            boolean requiresEmptyLine = false;
712    
713                            if (previousJavaTerm.getType() != javaTerm.getType()) {
714                                    requiresEmptyLine = true;
715                            }
716                            else if (!isInJavaTermTypeGroup(
717                                                    javaTerm.getType(), TYPE_VARIABLE)) {
718    
719                                    requiresEmptyLine = true;
720                            }
721                            else if ((StringUtil.isUpperCase(javaTermName) &&
722                                              !StringUtil.isLowerCase(javaTermName)) ||
723                                             (StringUtil.isUpperCase(previousJavaTermName) &&
724                                              !StringUtil.isLowerCase(previousJavaTermName))) {
725    
726                                    requiresEmptyLine = true;
727                            }
728                            else if (hasAnnotationCommentOrJavadoc(javaTermContent) ||
729                                             hasAnnotationCommentOrJavadoc(previousJavaTermContent)) {
730    
731                                    requiresEmptyLine = true;
732                            }
733                            else if ((previousJavaTerm.getType() ==
734                                                    TYPE_VARIABLE_PRIVATE_STATIC) &&
735                                             (previousJavaTermName.equals("_log") ||
736                                              previousJavaTermName.equals("_instance"))) {
737    
738                                    requiresEmptyLine = true;
739                            }
740                            else if (previousJavaTermContent.contains("\n\n\t") ||
741                                             javaTermContent.contains("\n\n\t")) {
742    
743                                    requiresEmptyLine = true;
744                            }
745    
746                            if (requiresEmptyLine) {
747                                    if (!content.contains("\n\n" + javaTermContent)) {
748                                            return StringUtil.replace(
749                                                    content, "\n" + javaTermContent,
750                                                    "\n\n" + javaTermContent);
751                                    }
752                            }
753                            else if (content.contains("\n\n" + javaTermContent)) {
754                                    return StringUtil.replace(
755                                            content, "\n\n" + javaTermContent, "\n" + javaTermContent);
756                            }
757    
758                            previousJavaTerm = javaTerm;
759                    }
760    
761                    return content;
762            }
763    
764            @Override
765            protected void format() throws Exception {
766                    Collection<String> fileNames = null;
767    
768                    if (portalSource) {
769                            fileNames = getPortalJavaFiles();
770    
771                            _checkUnprocessedExceptions = GetterUtil.getBoolean(
772                                    System.getProperty(
773                                            "source.formatter.check.unprocessed.exceptions"));
774                    }
775                    else {
776                            fileNames = getPluginJavaFiles();
777                    }
778    
779                    _javaTermSortExclusions = getExclusionsProperties(
780                            "source_formatter_javaterm_sort_exclusions.properties");
781                    _lineLengthExclusions = getExclusionsProperties(
782                            "source_formatter_line_length_exclusions.properties");
783                    _staticLogVariableExclusions = getExclusionsProperties(
784                            "source_formatter_static_log_exclusions.properties");
785                    _upgradeServiceUtilExclusions = getExclusionsProperties(
786                            "source_formatter_upgrade_service_util_exclusions.properties");
787    
788                    for (String fileName : fileNames) {
789                            format(fileName);
790                    }
791            }
792    
793            @Override
794            protected String format(String fileName) throws Exception {
795                    if (fileName.endsWith("SourceProcessor.java")) {
796                            return null;
797                    }
798    
799                    File file = new File(BASEDIR + fileName);
800    
801                    fileName = StringUtil.replace(
802                            fileName, StringPool.BACK_SLASH, StringPool.SLASH);
803    
804                    String content = fileUtil.read(file);
805    
806                    if (isGenerated(content) &&
807                            !fileName.endsWith("JavadocFormatter.java")) {
808    
809                            return null;
810                    }
811    
812                    String className = file.getName();
813    
814                    className = className.substring(0, className.length() - 5);
815    
816                    String packagePath = fileName;
817    
818                    int packagePathX = packagePath.indexOf("/src/");
819                    int packagePathY = packagePath.lastIndexOf(StringPool.SLASH);
820    
821                    if ((packagePathX + 5) >= packagePathY) {
822                            packagePath = StringPool.BLANK;
823                    }
824                    else {
825                            packagePath = packagePath.substring(packagePathX + 5, packagePathY);
826                    }
827    
828                    packagePath = StringUtil.replace(
829                            packagePath, StringPool.SLASH, StringPool.PERIOD);
830    
831                    if (packagePath.endsWith(".model")) {
832                            if (content.contains("extends " + className + "Model")) {
833                                    return null;
834                            }
835                    }
836    
837                    String newContent = content;
838    
839                    if (newContent.contains("$\n */")) {
840                            processErrorMessage(fileName, "*: " + fileName);
841    
842                            newContent = StringUtil.replace(newContent, "$\n */", "$\n *\n */");
843                    }
844    
845                    newContent = fixCopyright(
846                            newContent, getCopyright(), getOldCopyright(), file, fileName);
847    
848                    if (newContent.contains(className + ".java.html")) {
849                            processErrorMessage(fileName, "Java2HTML: " + fileName);
850                    }
851    
852                    if (newContent.contains(" * @author Raymond Aug") &&
853                            !newContent.contains(" * @author Raymond Aug\u00e9")) {
854    
855                            newContent = newContent.replaceFirst(
856                                    "Raymond Aug.++", "Raymond Aug\u00e9");
857    
858                            processErrorMessage(fileName, "UTF-8: " + fileName);
859                    }
860    
861                    newContent = fixDataAccessConnection(className, newContent);
862                    newContent = fixSessionKey(fileName, newContent, sessionKeyPattern);
863    
864                    newContent = StringUtil.replace(
865                            newContent,
866                            new String[] {
867                                    "com.liferay.portal.PortalException",
868                                    "com.liferay.portal.SystemException",
869                                    "com.liferay.util.LocalizationUtil",
870                                    "private static final Log _log"
871                            },
872                            new String[] {
873                                    "com.liferay.portal.kernel.exception.PortalException",
874                                    "com.liferay.portal.kernel.exception.SystemException",
875                                    "com.liferay.portal.kernel.util.LocalizationUtil",
876                                    "private static Log _log"
877                            });
878    
879                    newContent = fixCompatClassImports(file, newContent);
880    
881                    newContent = stripJavaImports(newContent, packagePath, className);
882    
883                    newContent = StringUtil.replace(
884                            newContent,
885                            new String[] {
886                                    ";\n/**", "\t/*\n\t *", "catch(", "else{", "if(", "for(",
887                                    "while(", "List <", "){\n", "]{\n"
888                            },
889                            new String[] {
890                                    ";\n\n/**", "\t/**\n\t *", "catch (", "else {", "if (", "for (",
891                                    "while (", "List<", ") {\n", "] {\n"
892                            });
893    
894                    Pattern pattern = Pattern.compile(
895                            "\t(catch |else |finally |for |if |try |while ).*\\{\n\n\t+\\w");
896    
897                    while (true) {
898                            Matcher matcher = pattern.matcher(newContent);
899    
900                            if (!matcher.find()) {
901                                    break;
902                            }
903    
904                            newContent = StringUtil.replaceFirst(
905                                    newContent, StringPool.NEW_LINE, StringPool.BLANK,
906                                    matcher.start());
907                    }
908    
909                    pattern = Pattern.compile(
910                            "Log _log = LogFactoryUtil.getLog\\(\n*\t*(.+)\\.class\\)");
911    
912                    Matcher matcher = pattern.matcher(newContent);
913    
914                    if (matcher.find()) {
915                            String logClassName = matcher.group(1);
916    
917                            if (!logClassName.equals(className)) {
918                                    newContent = StringUtil.replaceLast(
919                                            newContent, logClassName + ".class)",
920                                            className + ".class)");
921                            }
922                    }
923    
924                    String excluded = null;
925    
926                    if (_staticLogVariableExclusions != null) {
927                            excluded = _staticLogVariableExclusions.getProperty(fileName);
928                    }
929    
930                    if (excluded == null) {
931                            newContent = StringUtil.replace(
932                                    newContent, "private Log _log", "private static Log _log");
933                    }
934    
935                    if (newContent.contains("*/\npackage ")) {
936                            processErrorMessage(fileName, "package: " + fileName);
937                    }
938    
939                    if (!newContent.endsWith("\n\n}") && !newContent.endsWith("{\n}")) {
940                            processErrorMessage(fileName, "}: " + fileName);
941                    }
942    
943                    if (portalSource && !className.equals("BaseServiceImpl") &&
944                            className.endsWith("ServiceImpl") &&
945                            newContent.contains("ServiceUtil.")) {
946    
947                            processErrorMessage(fileName, "ServiceUtil: " + fileName);
948                    }
949    
950                    // LPS-34911
951    
952                    excluded = null;
953    
954                    if (_upgradeServiceUtilExclusions != null) {
955                            excluded = _upgradeServiceUtilExclusions.getProperty(fileName);
956                    }
957    
958                    if ((excluded == null) && portalSource &&
959                            fileName.contains("/portal/upgrade/") &&
960                            !fileName.contains("/test/") &&
961                            newContent.contains("ServiceUtil.")) {
962    
963                            processErrorMessage(fileName, "ServiceUtil: " + fileName);
964                    }
965    
966                    if (!className.equals("DeepNamedValueScanner") &&
967                            !className.equals("ProxyUtil") &&
968                            newContent.contains("import java.lang.reflect.Proxy;")) {
969    
970                            processErrorMessage(fileName, "Proxy: " + fileName);
971                    }
972    
973                    if (newContent.contains("import edu.emory.mathcs.backport.java")) {
974                            processErrorMessage(
975                                    fileName, "edu.emory.mathcs.backport.java: " + fileName);
976                    }
977    
978                    // LPS-28266
979    
980                    for (int pos1 = -1;;) {
981                            pos1 = newContent.indexOf(StringPool.TAB + "try {", pos1 + 1);
982    
983                            if (pos1 == -1) {
984                                    break;
985                            }
986    
987                            int pos2 = newContent.indexOf(StringPool.TAB + "try {", pos1 + 1);
988                            int pos3 = newContent.indexOf("\"select count(", pos1);
989    
990                            if ((pos2 != -1) && (pos3 != -1) && (pos2 < pos3)) {
991                                    continue;
992                            }
993    
994                            int pos4 = newContent.indexOf("rs.getLong(1)", pos1);
995                            int pos5 = newContent.indexOf(StringPool.TAB + "finally {", pos1);
996    
997                            if ((pos3 == -1) || (pos4 == -1) || (pos5 == -1)) {
998                                    break;
999                            }
1000    
1001                            if ((pos3 < pos4) && (pos4 < pos5)) {
1002                                    processErrorMessage(
1003                                            fileName, "Use getInt(1) for count: " + fileName);
1004                            }
1005                    }
1006    
1007                    // LPS-33070
1008    
1009                    if (content.contains("implements ProcessCallable") &&
1010                            !content.contains("private static final long serialVersionUID")) {
1011    
1012                            processErrorMessage(
1013                                    fileName,
1014                                    "Assign ProcessCallable implementation a serialVersionUID: " +
1015                                            fileName);
1016                    }
1017    
1018                    checkLanguageKeys(fileName, newContent, languageKeyPattern);
1019    
1020                    newContent = StringUtil.replace(
1021                            newContent, StringPool.TAB + "for (;;) {",
1022                            StringPool.TAB + "while (true) {");
1023    
1024                    // LPS-36174
1025    
1026                    if (_checkUnprocessedExceptions && !fileName.contains("/test/")) {
1027                            checkUnprocessedExceptions(newContent, file, packagePath, fileName);
1028                    }
1029    
1030                    // LPS-39508
1031    
1032                    if (!fileName.contains("SecureRandomUtil") &&
1033                            content.contains("java.security.SecureRandom") &&
1034                            !content.contains("javax.crypto.KeyGenerator")) {
1035    
1036                            processErrorMessage(
1037                                    fileName,
1038                                    "Use SecureRandomUtil instead of java.security.SecureRandom: " +
1039                                            fileName);
1040                    }
1041    
1042                    String oldContent = newContent;
1043    
1044                    while (true) {
1045                            newContent = fixIncorrectEmptyLineBeforeCloseCurlyBrace(
1046                                    oldContent, fileName);
1047    
1048                            newContent = formatJava(fileName, newContent);
1049    
1050                            newContent = StringUtil.replace(newContent, "\n\n\n", "\n\n");
1051    
1052                            if (oldContent.equals(newContent)) {
1053                                    break;
1054                            }
1055    
1056                            oldContent = newContent;
1057                    }
1058    
1059                    if (isAutoFix() && (newContent != null) &&
1060                            !content.equals(newContent)) {
1061    
1062                            fileUtil.write(file, newContent);
1063    
1064                            sourceFormatterHelper.printError(fileName, file);
1065                    }
1066    
1067                    return newContent;
1068            }
1069    
1070            protected String formatAnnotations(
1071                            String fileName, String content, Set<JavaTerm> javaTerms)
1072                    throws IOException {
1073    
1074                    Iterator<JavaTerm> itr = javaTerms.iterator();
1075    
1076                    while (itr.hasNext()) {
1077                            JavaTerm javaTerm = itr.next();
1078    
1079                            if (fileName.contains("/test/") &&
1080                                    !fileName.endsWith("TestBean.java") &&
1081                                    !fileName.endsWith("TestCase.java")) {
1082    
1083                                    checkTestAnnotations(javaTerm, fileName);
1084                            }
1085    
1086                            while (true) {
1087                                    String javaTermContent = javaTerm.getContent();
1088    
1089                                    javaTerm.sortAnnotations();
1090    
1091                                    String newJavaTermContent = javaTerm.getContent();
1092    
1093                                    if (javaTermContent.equals(newJavaTermContent)) {
1094                                            break;
1095                                    }
1096    
1097                                    content = content.replace(javaTermContent, newJavaTermContent);
1098                            }
1099                    }
1100    
1101                    return content;
1102            }
1103    
1104            protected String formatJava(String fileName, String content)
1105                    throws IOException {
1106    
1107                    StringBundler sb = new StringBundler();
1108    
1109                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
1110                            new UnsyncStringReader(content));
1111    
1112                    int index = 0;
1113                    int lineCount = 0;
1114    
1115                    String line = null;
1116    
1117                    String previousLine = StringPool.BLANK;
1118    
1119                    int lineToSkipIfEmpty = 0;
1120    
1121                    Set<JavaTerm> javaTerms = new TreeSet<JavaTerm>(
1122                            new JavaTermComparator());
1123    
1124                    JavaTerm javaTerm = null;
1125    
1126                    String javaTermName = null;
1127                    int javaTermLineCount = -1;
1128                    int javaTermStartPosition = -1;
1129                    int javaTermType = -1;
1130    
1131                    boolean readParameterTypes = false;
1132                    List<String> parameterTypes = new ArrayList<String>();
1133    
1134                    int lastCommentOrAnnotationPos = -1;
1135    
1136                    String ifClause = StringPool.BLANK;
1137    
1138                    String packageName = StringPool.BLANK;
1139    
1140                    while ((line = unsyncBufferedReader.readLine()) != null) {
1141                            lineCount++;
1142    
1143                            line = trimLine(line, false);
1144    
1145                            if (line.startsWith("package ")) {
1146                                    packageName = line.substring(8, line.length() - 1);
1147                            }
1148    
1149                            if (line.startsWith("import ")) {
1150                                    if (line.endsWith(".*;")) {
1151                                            processErrorMessage(
1152                                                    fileName, "import: " + fileName + " " + lineCount);
1153                                    }
1154    
1155                                    int pos = line.lastIndexOf(StringPool.PERIOD);
1156    
1157                                    if (pos != -1) {
1158                                            String importPackageName = line.substring(7, pos);
1159    
1160                                            if (importPackageName.equals(packageName)) {
1161                                                    continue;
1162                                            }
1163                                    }
1164                            }
1165    
1166                            if (line.contains(StringPool.TAB + "for (") && line.contains(":") &&
1167                                    !line.contains(" :")) {
1168    
1169                                    line = StringUtil.replace(line, ":" , " :");
1170                            }
1171    
1172                            line = replacePrimitiveWrapperInstantiation(
1173                                    fileName, line, lineCount);
1174    
1175                            String trimmedLine = StringUtil.trimLeading(line);
1176    
1177                            checkStringBundler(trimmedLine, fileName, lineCount);
1178    
1179                            if (trimmedLine.startsWith("* @deprecated") &&
1180                                    mainReleaseVersion.equals(MAIN_RELEASE_VERSION_6_2_0)) {
1181    
1182                                    if (!trimmedLine.startsWith("* @deprecated As of ")) {
1183                                            line = StringUtil.replace(
1184                                                    line, "* @deprecated",
1185                                                    "* @deprecated As of " + MAIN_RELEASE_VERSION_6_2_0);
1186                                    }
1187                                    else {
1188                                            String version = trimmedLine.substring(20);
1189    
1190                                            version = StringUtil.split(version, StringPool.SPACE)[0];
1191    
1192                                            version = StringUtil.replace(
1193                                                    version, StringPool.COMMA, StringPool.BLANK);
1194    
1195                                            if (StringUtil.count(version, StringPool.PERIOD) == 1) {
1196                                                    line = StringUtil.replaceFirst(
1197                                                            line, version, version + ".0");
1198                                            }
1199                                    }
1200                            }
1201    
1202                            checkInefficientStringMethods(line, fileName, lineCount);
1203    
1204                            if (trimmedLine.startsWith(StringPool.EQUAL)) {
1205                                    processErrorMessage(
1206                                            fileName, "equal: " + fileName + " " + lineCount);
1207                            }
1208    
1209                            if (line.contains("ActionForm form")) {
1210                                    processErrorMessage(
1211                                            fileName,
1212                                            "Rename form to actionForm: " + fileName + " " + lineCount);
1213                            }
1214    
1215                            if (line.contains("ActionMapping mapping")) {
1216                                    processErrorMessage(
1217                                            fileName,
1218                                            "Rename mapping to ActionMapping: " + fileName + " " +
1219                                                    lineCount);
1220                            }
1221    
1222                            if (fileName.contains("/upgrade/") &&
1223                                    line.contains("rs.getDate(")) {
1224    
1225                                    processErrorMessage(
1226                                            fileName,
1227                                            "Use rs.getTimeStamp: " + fileName + " " + lineCount);
1228                            }
1229    
1230                            if (!trimmedLine.equals("{") && line.endsWith("{") &&
1231                                    !line.endsWith(" {")) {
1232    
1233                                    line = StringUtil.replaceLast(line, "{", " {");
1234                            }
1235    
1236                            line = sortExceptions(line);
1237    
1238                            if (trimmedLine.startsWith("if (") ||
1239                                    trimmedLine.startsWith("else if (") ||
1240                                    trimmedLine.startsWith("while (") ||
1241                                    Validator.isNotNull(ifClause)) {
1242    
1243                                    ifClause = ifClause + line + StringPool.NEW_LINE;
1244    
1245                                    if (line.endsWith(") {")) {
1246                                            String newIfClause = checkIfClause(
1247                                                    ifClause, fileName, lineCount);
1248    
1249                                            if (!ifClause.equals(newIfClause)) {
1250                                                    return StringUtil.replace(
1251                                                            content, ifClause, newIfClause);
1252                                            }
1253    
1254                                            ifClause = StringPool.BLANK;
1255                                    }
1256                                    else if (line.endsWith(StringPool.SEMICOLON)) {
1257                                            ifClause = StringPool.BLANK;
1258                                    }
1259                            }
1260    
1261                            String excluded = null;
1262    
1263                            if (line.startsWith(StringPool.TAB + "private ") ||
1264                                    line.equals(StringPool.TAB + "private") ||
1265                                    line.startsWith(StringPool.TAB + "protected ") ||
1266                                    line.equals(StringPool.TAB + "protected") ||
1267                                    line.startsWith(StringPool.TAB + "public ") ||
1268                                    line.equals(StringPool.TAB + "public")) {
1269    
1270                                    Tuple tuple = getJavaTermTuple(line, content, index, 1, 3);
1271    
1272                                    if (tuple != null) {
1273                                            int javaTermEndPosition = 0;
1274    
1275                                            if (lastCommentOrAnnotationPos == -1) {
1276                                                    javaTermEndPosition = index;
1277                                            }
1278                                            else {
1279                                                    javaTermEndPosition = lastCommentOrAnnotationPos;
1280                                            }
1281    
1282                                            if ((javaTermStartPosition != -1) &&
1283                                                    (javaTermEndPosition < content.length())) {
1284    
1285                                                    String javaTermContent = content.substring(
1286                                                            javaTermStartPosition, javaTermEndPosition);
1287    
1288                                                    if (Validator.isNotNull(javaTermName)) {
1289                                                            javaTerm = new JavaTerm(
1290                                                                    javaTermName, javaTermType, parameterTypes,
1291                                                                    javaTermContent, javaTermLineCount);
1292    
1293                                                            javaTerms.add(javaTerm);
1294                                                    }
1295                                            }
1296    
1297                                            javaTermLineCount = lineCount;
1298                                            javaTermName = (String)tuple.getObject(0);
1299                                            javaTermStartPosition = javaTermEndPosition;
1300                                            javaTermType = (Integer)tuple.getObject(1);
1301    
1302                                            if (Validator.isNotNull(javaTermName)) {
1303                                                    if (isInJavaTermTypeGroup(
1304                                                                    javaTermType, TYPE_CONSTRUCTOR) ||
1305                                                            isInJavaTermTypeGroup(
1306                                                                    javaTermType, TYPE_METHOD)) {
1307    
1308                                                            readParameterTypes = true;
1309    
1310                                                            parameterTypes = new ArrayList<String>();
1311                                                    }
1312                                            }
1313                                    }
1314    
1315                                    lastCommentOrAnnotationPos = -1;
1316                            }
1317                            else if (hasAnnotationCommentOrJavadoc(line)) {
1318                                    if (lastCommentOrAnnotationPos == -1) {
1319                                            lastCommentOrAnnotationPos = index;
1320                                    }
1321                            }
1322    
1323                            if (readParameterTypes) {
1324                                    parameterTypes = addParameterTypes(trimmedLine, parameterTypes);
1325    
1326                                    if (trimmedLine.contains(StringPool.CLOSE_PARENTHESIS)) {
1327                                            readParameterTypes = false;
1328                                    }
1329                            }
1330    
1331                            if (!trimmedLine.contains(StringPool.DOUBLE_SLASH) &&
1332                                    !trimmedLine.startsWith(StringPool.STAR)) {
1333    
1334                                    String strippedQuotesLine = stripQuotes(
1335                                            trimmedLine, CharPool.QUOTE);
1336    
1337                                    for (int x = -1;;) {
1338                                            x = strippedQuotesLine.indexOf(StringPool.EQUAL, x + 1);
1339    
1340                                            if (x == -1) {
1341                                                    break;
1342                                            }
1343    
1344                                            char c = strippedQuotesLine.charAt(x - 1);
1345    
1346                                            if (Character.isLetterOrDigit(c)) {
1347                                                    line = StringUtil.replace(line, c + "=", c + " =");
1348    
1349                                                    break;
1350                                            }
1351    
1352                                            if (x == (strippedQuotesLine.length() - 1)) {
1353                                                    break;
1354                                            }
1355    
1356                                            c = strippedQuotesLine.charAt(x + 1);
1357    
1358                                            if (Character.isLetterOrDigit(c)) {
1359                                                    line = StringUtil.replace(line, "=" + c, "= " + c);
1360    
1361                                                    break;
1362                                            }
1363                                    }
1364    
1365                                    while (trimmedLine.contains(StringPool.TAB)) {
1366                                            line = StringUtil.replaceLast(
1367                                                    line, StringPool.TAB, StringPool.SPACE);
1368    
1369                                            trimmedLine = StringUtil.replaceLast(
1370                                                    trimmedLine, StringPool.TAB, StringPool.SPACE);
1371                                    }
1372    
1373                                    if (line.contains(StringPool.TAB + StringPool.SPACE) &&
1374                                            !previousLine.endsWith("&&") &&
1375                                            !previousLine.endsWith("||") &&
1376                                            !previousLine.contains(StringPool.TAB + "((") &&
1377                                            !previousLine.contains(
1378                                                    StringPool.TAB + StringPool.LESS_THAN) &&
1379                                            !previousLine.contains(StringPool.TAB + StringPool.SPACE) &&
1380                                            !previousLine.contains(StringPool.TAB + "implements ") &&
1381                                            !previousLine.contains(StringPool.TAB + "throws ")) {
1382    
1383                                            line = StringUtil.replace(
1384                                                    line, StringPool.TAB + StringPool.SPACE,
1385                                                    StringPool.TAB);
1386                                    }
1387    
1388                                    while (trimmedLine.contains(StringPool.DOUBLE_SPACE) &&
1389                                               !trimmedLine.contains(
1390                                                       StringPool.QUOTE + StringPool.DOUBLE_SPACE) &&
1391                                               !fileName.contains("Test")) {
1392    
1393                                            line = StringUtil.replaceLast(
1394                                                    line, StringPool.DOUBLE_SPACE, StringPool.SPACE);
1395    
1396                                            trimmedLine = StringUtil.replaceLast(
1397                                                    trimmedLine, StringPool.DOUBLE_SPACE, StringPool.SPACE);
1398                                    }
1399    
1400                                    if (!line.contains(StringPool.QUOTE)) {
1401                                            int pos = line.indexOf(") ");
1402    
1403                                            if (pos != -1) {
1404                                                    String linePart = line.substring(pos + 2);
1405    
1406                                                    if (Character.isLetter(linePart.charAt(0)) &&
1407                                                            !linePart.startsWith("default") &&
1408                                                            !linePart.startsWith("instanceof") &&
1409                                                            !linePart.startsWith("throws")) {
1410    
1411                                                            line = StringUtil.replaceLast(
1412                                                                    line, StringPool.SPACE + linePart, linePart);
1413                                                    }
1414                                            }
1415    
1416                                            if ((trimmedLine.startsWith("private ") ||
1417                                                     trimmedLine.startsWith("protected ") ||
1418                                                     trimmedLine.startsWith("public ")) &&
1419                                                    !line.contains(StringPool.EQUAL) &&
1420                                                    line.contains(" (")) {
1421    
1422                                                    line = StringUtil.replace(line, " (", "(");
1423                                            }
1424    
1425                                            if (line.contains(" [")) {
1426                                                    line = StringUtil.replace(line, " [", "[");
1427                                            }
1428    
1429                                            for (int x = -1;;) {
1430                                                    int posComma = line.indexOf(StringPool.COMMA, x + 1);
1431                                                    int posSemicolon = line.indexOf(
1432                                                            StringPool.SEMICOLON, x + 1);
1433    
1434                                                    if ((posComma == -1) && (posSemicolon == -1)) {
1435                                                            break;
1436                                                    }
1437    
1438                                                    x = Math.min(posComma, posSemicolon);
1439    
1440                                                    if (x == -1) {
1441                                                            x = Math.max(posComma, posSemicolon);
1442                                                    }
1443    
1444                                                    if (line.length() > (x + 1)) {
1445                                                            char nextChar = line.charAt(x + 1);
1446    
1447                                                            if ((nextChar != CharPool.APOSTROPHE) &&
1448                                                                    (nextChar != CharPool.CLOSE_PARENTHESIS) &&
1449                                                                    (nextChar != CharPool.SPACE) &&
1450                                                                    (nextChar != CharPool.STAR)) {
1451    
1452                                                                    line = StringUtil.insert(
1453                                                                            line, StringPool.SPACE, x + 1);
1454                                                            }
1455                                                    }
1456    
1457                                                    if (x > 0) {
1458                                                            char previousChar = line.charAt(x - 1);
1459    
1460                                                            if (previousChar == CharPool.SPACE) {
1461                                                                    line = line.substring(0, x - 1).concat(
1462                                                                            line.substring(x));
1463                                                            }
1464                                                    }
1465                                            }
1466                                    }
1467    
1468                                    if ((line.contains(" && ") || line.contains(" || ")) &&
1469                                            line.endsWith(StringPool.OPEN_PARENTHESIS)) {
1470    
1471                                            processErrorMessage(
1472                                                    fileName, "line break: " + fileName + " " + lineCount);
1473                                    }
1474    
1475                                    if (trimmedLine.endsWith(StringPool.PLUS) &&
1476                                            !trimmedLine.startsWith(StringPool.OPEN_PARENTHESIS)) {
1477    
1478                                            int closeParenthesisCount = StringUtil.count(
1479                                                    strippedQuotesLine, StringPool.CLOSE_PARENTHESIS);
1480                                            int openParenthesisCount = StringUtil.count(
1481                                                    strippedQuotesLine, StringPool.OPEN_PARENTHESIS);
1482    
1483                                            if (openParenthesisCount > closeParenthesisCount) {
1484                                                    processErrorMessage(
1485                                                            fileName,
1486                                                            "line break: " + fileName + " " + lineCount);
1487                                            }
1488                                    }
1489    
1490                                    if (line.contains(StringPool.COMMA) &&
1491                                            !line.contains(StringPool.CLOSE_PARENTHESIS) &&
1492                                            !line.contains(StringPool.GREATER_THAN) &&
1493                                            !line.contains(StringPool.QUOTE) &&
1494                                            line.endsWith(StringPool.OPEN_PARENTHESIS)) {
1495    
1496                                            processErrorMessage(
1497                                                    fileName, "line break: " + fileName + " " + lineCount);
1498                                    }
1499    
1500                                    if (line.endsWith(" +") || line.endsWith(" -") ||
1501                                            line.endsWith(" *") || line.endsWith(" /")) {
1502    
1503                                            int x = line.indexOf(" = ");
1504    
1505                                            if (x != -1) {
1506                                                    int y = line.indexOf(StringPool.QUOTE);
1507    
1508                                                    if ((y == -1) || (x < y)) {
1509                                                            processErrorMessage(
1510                                                                    fileName,
1511                                                                    "line break: " + fileName + " " + lineCount);
1512                                                    }
1513                                            }
1514                                    }
1515    
1516                                    if (line.endsWith(" throws") ||
1517                                            (previousLine.endsWith(
1518                                                    StringPool.OPEN_PARENTHESIS) &&
1519                                             line.contains(" throws " ) &&
1520                                             line.endsWith(StringPool.OPEN_CURLY_BRACE))) {
1521    
1522                                            processErrorMessage(
1523                                                    fileName, "line break: " + fileName + " " + lineCount);
1524                                    }
1525    
1526                                    if (trimmedLine.startsWith(StringPool.PERIOD) ||
1527                                            (line.endsWith(StringPool.PERIOD) &&
1528                                             line.contains(StringPool.EQUAL))) {
1529    
1530                                            processErrorMessage(
1531                                                    fileName, "line break: " + fileName + " " + lineCount);
1532                                    }
1533    
1534                                    if (trimmedLine.startsWith(StringPool.CLOSE_CURLY_BRACE) &&
1535                                            line.endsWith(StringPool.OPEN_CURLY_BRACE)) {
1536    
1537                                            processErrorMessage(
1538                                                    fileName, "line break: " + fileName + " " + lineCount);
1539                                    }
1540                            }
1541    
1542                            if (line.contains("    ") && !line.matches("\\s*\\*.*")) {
1543                                    if (!fileName.endsWith("StringPool.java")) {
1544                                            processErrorMessage(
1545                                                    fileName, "tab: " + fileName + " " + lineCount);
1546                                    }
1547                            }
1548    
1549                            if (line.contains("  {") && !line.matches("\\s*\\*.*")) {
1550                                    processErrorMessage(
1551                                            fileName, "{:" + fileName + " " + lineCount);
1552                            }
1553    
1554                            excluded = null;
1555    
1556                            if (_lineLengthExclusions != null) {
1557                                    excluded = _lineLengthExclusions.getProperty(
1558                                            fileName + StringPool.AT + lineCount);
1559    
1560                                    if (excluded == null) {
1561                                            excluded = _lineLengthExclusions.getProperty(fileName);
1562                                    }
1563                            }
1564    
1565                            Tuple combinedLines = null;
1566                            int lineLength = getLineLength(line);
1567    
1568                            if ((excluded == null) &&
1569                                    !line.startsWith("import ") && !line.startsWith("package ") &&
1570                                    !line.matches("\\s*\\*.*")) {
1571    
1572                                    if (fileName.endsWith("Table.java") &&
1573                                            line.contains("String TABLE_SQL_CREATE = ")) {
1574                                    }
1575                                    else if (fileName.endsWith("Table.java") &&
1576                                                     line.contains("String TABLE_SQL_DROP = ")) {
1577                                    }
1578                                    else if (fileName.endsWith("Table.java") &&
1579                                                     line.contains(" index IX_")) {
1580                                    }
1581                                    else if (lineLength > 80) {
1582                                            processErrorMessage(
1583                                                    fileName, "> 80: " + fileName + " " + lineCount);
1584                                    }
1585                                    else {
1586                                            int lineLeadingTabCount = getLeadingTabCount(line);
1587                                            int previousLineLeadingTabCount = getLeadingTabCount(
1588                                                    previousLine);
1589    
1590                                            if (!trimmedLine.startsWith("//")) {
1591                                                    if (previousLine.endsWith(StringPool.COMMA) &&
1592                                                            previousLine.contains(
1593                                                                    StringPool.OPEN_PARENTHESIS) &&
1594                                                            !previousLine.contains("for (") &&
1595                                                            (lineLeadingTabCount >
1596                                                                    previousLineLeadingTabCount)) {
1597    
1598                                                            processErrorMessage(
1599                                                                    fileName,
1600                                                                    "line break: " + fileName + " " + lineCount);
1601                                                    }
1602    
1603                                                    if (Validator.isNotNull(trimmedLine)) {
1604                                                            if (((previousLine.endsWith(StringPool.COLON) &&
1605                                                                      previousLine.contains(
1606                                                                              StringPool.TAB + "for ")) ||
1607                                                                     (previousLine.endsWith(
1608                                                                             StringPool.OPEN_PARENTHESIS) &&
1609                                                                      previousLine.contains(
1610                                                                              StringPool.TAB + "if "))) &&
1611                                                                    ((previousLineLeadingTabCount + 2) !=
1612                                                                            lineLeadingTabCount)) {
1613    
1614                                                            processErrorMessage(
1615                                                                    fileName,
1616                                                                    "line break: " + fileName + " " + lineCount);
1617                                                            }
1618    
1619                                                            if (previousLine.endsWith(
1620                                                                            StringPool.OPEN_CURLY_BRACE) &&
1621                                                                    !trimmedLine.startsWith(
1622                                                                            StringPool.CLOSE_CURLY_BRACE) &&
1623                                                                    ((previousLineLeadingTabCount + 1) !=
1624                                                                            lineLeadingTabCount)) {
1625    
1626                                                                    processErrorMessage(
1627                                                                            fileName,
1628                                                                            "tab: " + fileName + " " + lineCount);
1629                                                            }
1630                                                    }
1631    
1632                                                    if (previousLine.endsWith(StringPool.PERIOD)) {
1633                                                            int x = trimmedLine.indexOf(
1634                                                                    StringPool.OPEN_PARENTHESIS);
1635    
1636                                                            if ((x != -1) &&
1637                                                                    ((getLineLength(previousLine) + x) < 80) &&
1638                                                                    (trimmedLine.endsWith(
1639                                                                            StringPool.OPEN_PARENTHESIS) ||
1640                                                                     (trimmedLine.charAt(x + 1) !=
1641                                                                             CharPool.CLOSE_PARENTHESIS))) {
1642    
1643                                                                    processErrorMessage(
1644                                                                            fileName,
1645                                                                            "line break: " + fileName + " " +
1646                                                                                    lineCount);
1647                                                            }
1648                                                    }
1649    
1650                                                    if (trimmedLine.startsWith("throws ") &&
1651                                                            (lineLeadingTabCount ==
1652                                                                    previousLineLeadingTabCount)) {
1653    
1654                                                            processErrorMessage(
1655                                                                    fileName, "tab: " + fileName + " " + lineCount);
1656                                                    }
1657    
1658                                                    if ((previousLine.contains(" class " ) ||
1659                                                             previousLine.contains(" enum ")) &&
1660                                                            previousLine.endsWith(
1661                                                                    StringPool.OPEN_CURLY_BRACE) &&
1662                                                            Validator.isNotNull(line) &&
1663                                                            !trimmedLine.startsWith(
1664                                                                    StringPool.CLOSE_CURLY_BRACE)) {
1665    
1666                                                            processErrorMessage(
1667                                                                    fileName,
1668                                                                    "new line: " + fileName + " " + lineCount);
1669                                                    }
1670                                            }
1671    
1672                                            combinedLines = getCombinedLines(
1673                                                    trimmedLine, previousLine, lineLeadingTabCount,
1674                                                    previousLineLeadingTabCount);
1675                                    }
1676                            }
1677    
1678                            if (combinedLines != null) {
1679                                    previousLine = (String)combinedLines.getObject(0);
1680    
1681                                    if (combinedLines.getSize() > 1) {
1682                                            String linePart = (String)combinedLines.getObject(1);
1683                                            boolean addToPreviousLine =
1684                                                    (Boolean)combinedLines.getObject(2);
1685    
1686                                            if (addToPreviousLine) {
1687                                                    previousLine = previousLine + linePart;
1688                                                    line = StringUtil.replaceFirst(
1689                                                            line, linePart, StringPool.BLANK);
1690                                            }
1691                                            else {
1692                                                    if (((linePart.length() + lineLength) <= 80) &&
1693                                                            (line.endsWith(StringPool.OPEN_CURLY_BRACE) ||
1694                                                             line.endsWith(StringPool.SEMICOLON))) {
1695    
1696                                                            previousLine = StringUtil.replaceLast(
1697                                                                    previousLine, StringUtil.trim(linePart),
1698                                                                    StringPool.BLANK);
1699    
1700                                                            line = StringUtil.replaceLast(
1701                                                                    line, StringPool.TAB,
1702                                                                    StringPool.TAB + linePart);
1703                                                    }
1704                                                    else {
1705                                                            processErrorMessage(
1706                                                                    fileName,
1707                                                                    "line break: " + fileName + " " + lineCount);
1708                                                    }
1709                                            }
1710    
1711                                            sb.append(previousLine);
1712                                            sb.append("\n");
1713    
1714                                            previousLine = line;
1715                                    }
1716                                    else if (line.endsWith(StringPool.OPEN_CURLY_BRACE) &&
1717                                                     !previousLine.contains(" class ")) {
1718    
1719                                            lineToSkipIfEmpty = lineCount + 1;
1720                                    }
1721                            }
1722                            else {
1723                                    if ((lineCount > 1) &&
1724                                            (Validator.isNotNull(previousLine) ||
1725                                             (lineToSkipIfEmpty != (lineCount - 1)))) {
1726    
1727                                            sb.append(previousLine);
1728    
1729                                            if (Validator.isNotNull(previousLine) &&
1730                                                    Validator.isNotNull(trimmedLine) &&
1731                                                    !previousLine.contains("/*") &&
1732                                                    !previousLine.endsWith("*/")) {
1733    
1734                                                    String trimmedPreviousLine = StringUtil.trimLeading(
1735                                                            previousLine);
1736    
1737                                                    if ((trimmedPreviousLine.startsWith("// ") &&
1738                                                             !trimmedLine.startsWith("// ")) ||
1739                                                            (!trimmedPreviousLine.startsWith("// ") &&
1740                                                             trimmedLine.startsWith("// "))) {
1741    
1742                                                            sb.append("\n");
1743                                                    }
1744                                                    else if (!trimmedPreviousLine.endsWith(
1745                                                                            StringPool.OPEN_CURLY_BRACE) &&
1746                                                                     !trimmedPreviousLine.endsWith(
1747                                                                            StringPool.COLON) &&
1748                                                                     (trimmedLine.startsWith("for (") ||
1749                                                                      trimmedLine.startsWith("if ("))) {
1750    
1751                                                            sb.append("\n");
1752                                                    }
1753                                                    else if (previousLine.endsWith(
1754                                                                            StringPool.TAB +
1755                                                                                    StringPool.CLOSE_CURLY_BRACE) &&
1756                                                                     !trimmedLine.startsWith(
1757                                                                             StringPool.CLOSE_CURLY_BRACE) &&
1758                                                                     !trimmedLine.startsWith(
1759                                                                             StringPool.CLOSE_PARENTHESIS) &&
1760                                                                     !trimmedLine.startsWith(
1761                                                                             StringPool.DOUBLE_SLASH) &&
1762                                                                     !trimmedLine.startsWith("catch ") &&
1763                                                                     !trimmedLine.startsWith("else ") &&
1764                                                                     !trimmedLine.startsWith("finally ") &&
1765                                                                     !trimmedLine.startsWith("while ")) {
1766    
1767                                                            sb.append("\n");
1768                                                    }
1769                                            }
1770    
1771                                            sb.append("\n");
1772                                    }
1773    
1774                                    previousLine = line;
1775                            }
1776    
1777                            index = index + line.length() + 1;
1778                    }
1779    
1780                    sb.append(previousLine);
1781    
1782                    unsyncBufferedReader.close();
1783    
1784                    String newContent = sb.toString();
1785    
1786                    if (newContent.endsWith("\n")) {
1787                            newContent = newContent.substring(0, newContent.length() - 1);
1788                    }
1789    
1790                    if (content.equals(newContent)) {
1791                            if (javaTermStartPosition != -1) {
1792                                    int javaTermEndPosition = content.length() - 2;
1793    
1794                                    String javaTermContent = content.substring(
1795                                            javaTermStartPosition, javaTermEndPosition);
1796    
1797                                    javaTerm = new JavaTerm(
1798                                            javaTermName, javaTermType, parameterTypes, javaTermContent,
1799                                            javaTermLineCount);
1800    
1801                                    javaTerms.add(javaTerm);
1802                            }
1803    
1804                            newContent = sortJavaTerms(fileName, content, javaTerms);
1805                    }
1806    
1807                    if (content.equals(newContent)) {
1808                            newContent = fixJavaTermsDividers(fileName, content, javaTerms);
1809                    }
1810    
1811                    if (content.equals(newContent)) {
1812                            newContent = formatAnnotations(fileName, content, javaTerms);
1813                    }
1814    
1815                    return newContent;
1816            }
1817    
1818            protected String getClassName(String line) {
1819                    int pos = line.indexOf(" implements ");
1820    
1821                    if (pos == -1) {
1822                            pos = line.indexOf(" extends ");
1823                    }
1824    
1825                    if (pos == -1) {
1826                            pos = line.indexOf(StringPool.OPEN_CURLY_BRACE);
1827                    }
1828    
1829                    if (pos != -1) {
1830                            line = line.substring(0, pos);
1831                    }
1832    
1833                    line = line.trim();
1834    
1835                    pos = line.lastIndexOf(StringPool.SPACE);
1836    
1837                    return line.substring(pos + 1);
1838            }
1839    
1840            protected Tuple getCombinedLines(
1841                    String line, String previousLine, int lineTabCount,
1842                    int previousLineTabCount) {
1843    
1844                    if (Validator.isNull(line) || Validator.isNull(previousLine)) {
1845                            return null;
1846                    }
1847    
1848                    String trimmedPreviousLine = StringUtil.trimLeading(previousLine);
1849    
1850                    int previousLineLength = getLineLength(previousLine);
1851    
1852                    if (line.startsWith("// ") && trimmedPreviousLine.startsWith("// ")) {
1853                            String linePart = line.substring(3);
1854    
1855                            if (!linePart.startsWith("PLACEHOLDER") &&
1856                                    !linePart.startsWith(StringPool.OPEN_BRACKET)) {
1857    
1858                                    int pos = linePart.indexOf(StringPool.SPACE);
1859    
1860                                    if (pos == -1) {
1861                                            pos = linePart.length();
1862                                    }
1863    
1864                                    if ((previousLineLength + pos) < 80) {
1865                                            if (linePart.contains(StringPool.SPACE)) {
1866                                                    return new Tuple(
1867                                                            previousLine + StringPool.SPACE,
1868                                                            linePart.substring(0, pos + 1), true);
1869                                            }
1870                                            else {
1871                                                    return new Tuple(
1872                                                            previousLine + StringPool.SPACE + linePart);
1873                                            }
1874                                    }
1875                            }
1876    
1877                            return null;
1878                    }
1879                    else if (line.startsWith("// ") ||
1880                                     trimmedPreviousLine.startsWith("// ")) {
1881    
1882                            return null;
1883                    }
1884    
1885                    if (previousLine.endsWith(" extends")) {
1886                            return new Tuple(previousLine, "extends ", false);
1887                    }
1888    
1889                    if (previousLine.endsWith(" implements")) {
1890                            return new Tuple(previousLine, "implements ", false);
1891                    }
1892    
1893                    if (line.startsWith("+ ") || line.startsWith("- ") ||
1894                            line.startsWith("|| ") || line.startsWith("&& ")) {
1895    
1896                            int pos = line.indexOf(StringPool.SPACE);
1897    
1898                            String linePart = line.substring(0, pos);
1899    
1900                            return new Tuple(previousLine + StringPool.SPACE, linePart, true);
1901                    }
1902    
1903                    if ((line.length() + previousLineLength) < 80) {
1904                            if (trimmedPreviousLine.startsWith("for ") &&
1905                                    previousLine.endsWith(StringPool.COLON) &&
1906                                    line.endsWith(StringPool.OPEN_CURLY_BRACE)) {
1907    
1908                                    return new Tuple(previousLine + StringPool.SPACE + line);
1909                            }
1910    
1911                            if ((previousLine.endsWith(StringPool.EQUAL) ||
1912                                     previousLine.endsWith(StringPool.PERIOD) ||
1913                                     trimmedPreviousLine.equals("return")) &&
1914                                    line.endsWith(StringPool.SEMICOLON)) {
1915    
1916                                    return new Tuple(previousLine + StringPool.SPACE + line);
1917                            }
1918    
1919                            if ((trimmedPreviousLine.startsWith("if ") ||
1920                                     trimmedPreviousLine.startsWith("else ")) &&
1921                                    (previousLine.endsWith("||") || previousLine.endsWith("&&")) &&
1922                                    line.endsWith(StringPool.OPEN_CURLY_BRACE)) {
1923    
1924                                    return new Tuple(previousLine + StringPool.SPACE + line);
1925                            }
1926    
1927                            if ((line.startsWith("extends ") ||
1928                                     line.startsWith("implements ") ||
1929                                     line.startsWith("throws")) &&
1930                                    line.endsWith(StringPool.OPEN_CURLY_BRACE) &&
1931                                    (lineTabCount == (previousLineTabCount + 1))) {
1932    
1933                                    return new Tuple(previousLine + StringPool.SPACE + line);
1934                            }
1935                    }
1936    
1937                    if (previousLine.endsWith(StringPool.EQUAL) &&
1938                            line.endsWith(StringPool.SEMICOLON)) {
1939    
1940                            String tempLine = line;
1941    
1942                            for (int pos = 0;;) {
1943                                    pos = tempLine.indexOf(StringPool.DASH);
1944    
1945                                    if (pos == -1) {
1946                                            pos = tempLine.indexOf(StringPool.PLUS);
1947                                    }
1948    
1949                                    if (pos == -1) {
1950                                            pos = tempLine.indexOf(StringPool.SLASH);
1951                                    }
1952    
1953                                    if (pos == -1) {
1954                                            pos = tempLine.indexOf(StringPool.STAR);
1955                                    }
1956    
1957                                    if (pos == -1) {
1958                                            pos = tempLine.indexOf("||");
1959                                    }
1960    
1961                                    if (pos == -1) {
1962                                            pos = tempLine.indexOf("&&");
1963                                    }
1964    
1965                                    if (pos == -1) {
1966                                            break;
1967                                    }
1968    
1969                                    String linePart = tempLine.substring(0, pos);
1970    
1971                                    int openParenthesisCount = StringUtil.count(
1972                                            linePart, StringPool.OPEN_PARENTHESIS);
1973                                    int closeParenthesisCount = StringUtil.count(
1974                                            linePart, StringPool.CLOSE_PARENTHESIS);
1975    
1976                                    if (openParenthesisCount == closeParenthesisCount) {
1977                                            return null;
1978                                    }
1979    
1980                                    tempLine =
1981                                            tempLine.substring(0, pos) + tempLine.substring(pos + 1);
1982                            }
1983    
1984                            int x = line.indexOf(StringPool.OPEN_PARENTHESIS);
1985    
1986                            if (x == 0) {
1987                                    x = line.indexOf(StringPool.OPEN_PARENTHESIS, 1);
1988                            }
1989    
1990                            if (x != -1) {
1991                                    int y = line.indexOf(StringPool.CLOSE_PARENTHESIS, x);
1992                                    int z = line.indexOf(StringPool.QUOTE);
1993    
1994                                    if (((x + 1) != y) && ((z == -1) || (z > x))) {
1995                                            char previousChar = line.charAt(x - 1);
1996    
1997                                            if ((previousChar != CharPool.CLOSE_PARENTHESIS) &&
1998                                                    (previousChar != CharPool.OPEN_PARENTHESIS) &&
1999                                                    (previousChar != CharPool.SPACE) &&
2000                                                    (previousLineLength + 1 + x) < 80) {
2001    
2002                                                    String linePart = line.substring(0, x + 1);
2003    
2004                                                    if (linePart.startsWith(StringPool.OPEN_PARENTHESIS) &&
2005                                                            !linePart.contains(
2006                                                                    StringPool.CLOSE_PARENTHESIS)) {
2007    
2008                                                            return null;
2009                                                    }
2010    
2011                                                    return new Tuple(
2012                                                            previousLine + StringPool.SPACE, linePart, true);
2013                                            }
2014                                    }
2015                            }
2016                    }
2017    
2018                    if (previousLine.endsWith(StringPool.COMMA) &&
2019                            (previousLineTabCount == lineTabCount) &&
2020                            !previousLine.contains(StringPool.CLOSE_CURLY_BRACE)) {
2021    
2022                            int x = line.indexOf(StringPool.COMMA);
2023    
2024                            if (x != -1) {
2025                                    while ((previousLineLength + 1 + x) < 80) {
2026                                            String linePart = line.substring(0, x + 1);
2027    
2028                                            if (isValidJavaParameter(linePart)) {
2029                                                    if (line.equals(linePart)) {
2030                                                            return new Tuple(
2031                                                                    previousLine + StringPool.SPACE + linePart);
2032                                                    }
2033                                                    else {
2034                                                            return new Tuple(
2035                                                                    previousLine + StringPool.SPACE,
2036                                                                    linePart + StringPool.SPACE, true);
2037                                                    }
2038                                            }
2039    
2040                                            String partAfterComma = line.substring(x + 1);
2041    
2042                                            int pos = partAfterComma.indexOf(StringPool.COMMA);
2043    
2044                                            if (pos == -1) {
2045                                                    break;
2046                                            }
2047    
2048                                            x = x + pos + 1;
2049                                    }
2050                            }
2051                            else if (!line.endsWith(StringPool.OPEN_PARENTHESIS) &&
2052                                             !line.endsWith(StringPool.PLUS) &&
2053                                             !line.endsWith(StringPool.PERIOD) &&
2054                                             (!line.startsWith("new ") ||
2055                                              !line.endsWith(StringPool.OPEN_CURLY_BRACE)) &&
2056                                             ((line.length() + previousLineLength) < 80)) {
2057    
2058                                    return new Tuple(previousLine + StringPool.SPACE + line);
2059                            }
2060                    }
2061    
2062                    if (!previousLine.endsWith(StringPool.OPEN_PARENTHESIS)) {
2063                            return null;
2064                    }
2065    
2066                    if (StringUtil.count(previousLine, StringPool.OPEN_PARENTHESIS) > 1) {
2067                            int pos = trimmedPreviousLine.lastIndexOf(
2068                                    StringPool.OPEN_PARENTHESIS, trimmedPreviousLine.length() - 2);
2069    
2070                            if ((pos > 0) &&
2071                                    Character.isLetterOrDigit(
2072                                            trimmedPreviousLine.charAt(pos -1 ))) {
2073    
2074                                    String filePart = trimmedPreviousLine.substring(pos + 1);
2075    
2076                                    if (!filePart.contains(StringPool.CLOSE_PARENTHESIS) &&
2077                                            !filePart.contains(StringPool.QUOTE)) {
2078    
2079                                            return new Tuple(previousLine, filePart, false);
2080                                    }
2081                            }
2082                    }
2083    
2084                    if ((line.length() + previousLineLength) > 80) {
2085                            return null;
2086                    }
2087    
2088                    if (line.endsWith(StringPool.SEMICOLON)) {
2089                            return new Tuple(previousLine + line);
2090                    }
2091    
2092                    if (((line.endsWith(StringPool.OPEN_CURLY_BRACE) &&
2093                              !line.startsWith("new ")) ||
2094                             line.endsWith(StringPool.CLOSE_PARENTHESIS)) &&
2095                            (trimmedPreviousLine.startsWith("else ") ||
2096                             trimmedPreviousLine.startsWith("if ") ||
2097                             trimmedPreviousLine.startsWith("private ") ||
2098                             trimmedPreviousLine.startsWith("protected ") ||
2099                             trimmedPreviousLine.startsWith("public "))) {
2100    
2101                            return new Tuple(previousLine + line);
2102                    }
2103    
2104                    return null;
2105            }
2106    
2107            protected String getConstructorOrMethodName(String line, int pos) {
2108                    line = line.substring(0, pos);
2109    
2110                    int x = line.lastIndexOf(StringPool.SPACE);
2111    
2112                    return line.substring(x + 1);
2113            }
2114    
2115            protected List<String> getImportedExceptionClassNames(
2116                    JavaDocBuilder javaDocBuilder) {
2117    
2118                    List<String> exceptionClassNames = new ArrayList<String>();
2119    
2120                    JavaSource javaSource = javaDocBuilder.getSources()[0];
2121    
2122                    for (String importClassName : javaSource.getImports()) {
2123                            if (importClassName.endsWith("Exception") &&
2124                                    !exceptionClassNames.contains(importClassName)) {
2125    
2126                                    exceptionClassNames.add(importClassName);
2127                            }
2128                    }
2129    
2130                    return exceptionClassNames;
2131            }
2132    
2133            protected Tuple getJavaTermTuple(
2134                    String line, String content, int index, int numLines, int maxLines) {
2135    
2136                    int pos = line.indexOf(StringPool.OPEN_PARENTHESIS);
2137    
2138                    if (line.startsWith(StringPool.TAB + "public static final ") &&
2139                            (line.contains(StringPool.EQUAL) ||
2140                             (line.endsWith(StringPool.SEMICOLON) && (pos == -1)))) {
2141    
2142                            return new Tuple(
2143                                    getVariableName(line), TYPE_VARIABLE_PUBLIC_STATIC_FINAL);
2144                    }
2145                    else if (line.startsWith(StringPool.TAB + "public static ")) {
2146                            if (line.startsWith(StringPool.TAB + "public static class ") ||
2147                                    line.startsWith(StringPool.TAB + "public static enum") ||
2148                                    line.startsWith(StringPool.TAB + "public static interface")) {
2149    
2150                                    return new Tuple(getClassName(line), TYPE_CLASS_PUBLIC_STATIC);
2151                            }
2152    
2153                            if (line.contains(StringPool.EQUAL) ||
2154                                    (line.endsWith(StringPool.SEMICOLON) && (pos == -1))) {
2155    
2156                                    return new Tuple(
2157                                            getVariableName(line), TYPE_VARIABLE_PUBLIC_STATIC);
2158                            }
2159    
2160                            if (pos != -1) {
2161                                    return new Tuple(
2162                                            getConstructorOrMethodName(line, pos),
2163                                            TYPE_METHOD_PUBLIC_STATIC);
2164                            }
2165                    }
2166                    else if (line.startsWith(StringPool.TAB + "public ")) {
2167                            if (line.startsWith(StringPool.TAB + "public abstract class ") ||
2168                                    line.startsWith(StringPool.TAB + "public class ") ||
2169                                    line.startsWith(StringPool.TAB + "public enum ") ||
2170                                    line.startsWith(StringPool.TAB + "public interface ")) {
2171    
2172                                    return new Tuple(getClassName(line), TYPE_CLASS_PUBLIC);
2173                            }
2174    
2175                            if (line.contains(StringPool.EQUAL) ||
2176                                    (line.endsWith(StringPool.SEMICOLON) && (pos == -1))) {
2177    
2178                                    return new Tuple(getVariableName(line), TYPE_VARIABLE_PUBLIC);
2179                            }
2180    
2181                            if (pos != -1) {
2182                                    int spaceCount = StringUtil.count(
2183                                            line.substring(0, pos), StringPool.SPACE);
2184    
2185                                    if (spaceCount == 1) {
2186                                            return new Tuple(
2187                                                    getConstructorOrMethodName(line, pos),
2188                                                    TYPE_CONSTRUCTOR_PUBLIC);
2189                                    }
2190    
2191                                    if (spaceCount > 1) {
2192                                            return new Tuple(
2193                                                    getConstructorOrMethodName(line, pos),
2194                                                    TYPE_METHOD_PUBLIC);
2195                                    }
2196                            }
2197                    }
2198                    else if (line.startsWith(StringPool.TAB + "protected static final ")) {
2199                            if (line.contains(StringPool.EQUAL) ||
2200                                    (line.endsWith(StringPool.SEMICOLON) && (pos == -1))) {
2201    
2202                                    return new Tuple(
2203                                            getVariableName(line),
2204                                            TYPE_VARIABLE_PROTECTED_STATIC_FINAL);
2205                            }
2206                    }
2207                    else if (line.startsWith(StringPool.TAB + "protected static ")) {
2208                            if (line.startsWith(StringPool.TAB + "protected static class ") ||
2209                                    line.startsWith(StringPool.TAB + "protected static enum ") ||
2210                                    line.startsWith(
2211                                            StringPool.TAB + "protected static interface ")) {
2212    
2213                                    return new Tuple(
2214                                            getClassName(line), TYPE_CLASS_PROTECTED_STATIC);
2215                            }
2216    
2217                            if (line.contains(StringPool.EQUAL) ||
2218                                    (line.endsWith(StringPool.SEMICOLON) && (pos == -1))) {
2219    
2220                                    return new Tuple(
2221                                            getVariableName(line), TYPE_VARIABLE_PROTECTED_STATIC);
2222                            }
2223    
2224                            if (pos != -1) {
2225                                    return new Tuple(
2226                                            getConstructorOrMethodName(line, pos),
2227                                            TYPE_METHOD_PROTECTED_STATIC);
2228                            }
2229                    }
2230                    else if (line.startsWith(StringPool.TAB + "protected ")) {
2231                            if (line.startsWith(StringPool.TAB + "protected abstract class ") ||
2232                                    line.startsWith(StringPool.TAB + "protected class ") ||
2233                                    line.startsWith(StringPool.TAB + "protected enum ") ||
2234                                    line.startsWith(StringPool.TAB + "protected interface ")) {
2235    
2236                                    return new Tuple(getClassName(line), TYPE_CLASS_PROTECTED);
2237                            }
2238    
2239                            if (pos != -1) {
2240                                    if (!line.contains(StringPool.EQUAL)) {
2241                                            int spaceCount = StringUtil.count(
2242                                                    line.substring(0, pos), StringPool.SPACE);
2243    
2244                                            if (spaceCount == 1) {
2245                                                    return new Tuple(
2246                                                            getConstructorOrMethodName(line, pos),
2247                                                            TYPE_CONSTRUCTOR_PROTECTED);
2248                                            }
2249    
2250                                            if (spaceCount > 1) {
2251                                                    return new Tuple(
2252                                                            getConstructorOrMethodName(line, pos),
2253                                                            TYPE_METHOD_PROTECTED);
2254                                            }
2255                                    }
2256                            }
2257    
2258                            return new Tuple(getVariableName(line), TYPE_VARIABLE_PROTECTED);
2259                    }
2260                    else if (line.startsWith(StringPool.TAB + "private static final ")) {
2261                            if (line.contains(StringPool.EQUAL) ||
2262                                    (line.endsWith(StringPool.SEMICOLON) && (pos == -1))) {
2263    
2264                                    return new Tuple(
2265                                            getVariableName(line), TYPE_VARIABLE_PRIVATE_STATIC_FINAL);
2266                            }
2267                    }
2268                    else if (line.startsWith(StringPool.TAB + "private static ")) {
2269                            if (line.startsWith(StringPool.TAB + "private static class ") ||
2270                                    line.startsWith(StringPool.TAB + "private static enum ") ||
2271                                    line.startsWith(StringPool.TAB + "private static interface ")) {
2272    
2273                                    return new Tuple(getClassName(line), TYPE_CLASS_PRIVATE_STATIC);
2274                            }
2275    
2276                            if (line.contains(StringPool.EQUAL) ||
2277                                    (line.endsWith(StringPool.SEMICOLON) && (pos == -1))) {
2278    
2279                                    return new Tuple(
2280                                            getVariableName(line), TYPE_VARIABLE_PRIVATE_STATIC);
2281                            }
2282    
2283                            if (pos != -1) {
2284                                    return new Tuple(
2285                                            getConstructorOrMethodName(line, pos),
2286                                            TYPE_METHOD_PRIVATE_STATIC);
2287                            }
2288                    }
2289                    else if (line.startsWith(StringPool.TAB + "private ")) {
2290                            if (line.startsWith(StringPool.TAB + "private abstract class ") ||
2291                                    line.startsWith(StringPool.TAB + "private class ") ||
2292                                    line.startsWith(StringPool.TAB + "private enum ") ||
2293                                    line.startsWith(StringPool.TAB + "private interface ")) {
2294    
2295                                    return new Tuple(getClassName(line), TYPE_CLASS_PRIVATE);
2296                            }
2297    
2298                            if (line.contains(StringPool.EQUAL) ||
2299                                    (line.endsWith(StringPool.SEMICOLON) && (pos == -1))) {
2300    
2301                                    return new Tuple(getVariableName(line), TYPE_VARIABLE_PRIVATE);
2302                            }
2303    
2304                            if (pos != -1) {
2305                                    int spaceCount = StringUtil.count(
2306                                            line.substring(0, pos), StringPool.SPACE);
2307    
2308                                    if (spaceCount == 1) {
2309                                            return new Tuple(
2310                                                    getConstructorOrMethodName(line, pos),
2311                                                    TYPE_CONSTRUCTOR_PRIVATE);
2312                                    }
2313    
2314                                    if (spaceCount > 1) {
2315                                            return new Tuple(
2316                                                    getConstructorOrMethodName(line, pos),
2317                                                    TYPE_METHOD_PRIVATE);
2318                                    }
2319                            }
2320                    }
2321    
2322                    if (numLines < maxLines) {
2323                            int posStartNextLine =
2324                                    content.indexOf(StringPool.NEW_LINE, index) + 1;
2325    
2326                            int posEndNextline = content.indexOf(
2327                                    StringPool.NEW_LINE, posStartNextLine);
2328    
2329                            String nextLine = content.substring(
2330                                    posStartNextLine, posEndNextline);
2331    
2332                            if (Validator.isNull(nextLine)) {
2333                                    return null;
2334                            }
2335    
2336                            nextLine = StringUtil.trimLeading(nextLine);
2337    
2338                            return getJavaTermTuple(
2339                                    line + StringPool.SPACE + nextLine, content, posStartNextLine,
2340                                    numLines + 1, maxLines);
2341                    }
2342                    else {
2343                            return null;
2344                    }
2345            }
2346    
2347            protected int getLeadingTabCount(String line) {
2348                    int leadingTabCount = 0;
2349    
2350                    while (line.startsWith(StringPool.TAB)) {
2351                            line = line.substring(1);
2352    
2353                            leadingTabCount++;
2354                    }
2355    
2356                    return leadingTabCount;
2357            }
2358    
2359            protected int getLineLength(String line) {
2360                    int lineLength = 0;
2361    
2362                    int tabLength = 4;
2363    
2364                    for (char c : line.toCharArray()) {
2365                            if (c == CharPool.TAB) {
2366                                    for (int i = 0; i < tabLength; i++) {
2367                                            lineLength++;
2368                                    }
2369    
2370                                    tabLength = 4;
2371                            }
2372                            else {
2373                                    lineLength++;
2374    
2375                                    tabLength--;
2376    
2377                                    if (tabLength <= 0) {
2378                                            tabLength = 4;
2379                                    }
2380                            }
2381                    }
2382    
2383                    return lineLength;
2384            }
2385    
2386            protected Collection<String> getPluginJavaFiles() {
2387                    Collection<String> fileNames = new TreeSet<String>();
2388    
2389                    String[] excludes = new String[] {
2390                            "**\\bin\\**", "**\\model\\*Clp.java",
2391                            "**\\model\\impl\\*BaseImpl.java", "**\\model\\impl\\*Model.java",
2392                            "**\\model\\impl\\*ModelImpl.java",
2393                            "**\\service\\**\\service\\*Service.java",
2394                            "**\\service\\**\\service\\*ServiceClp.java",
2395                            "**\\service\\**\\service\\*ServiceFactory.java",
2396                            "**\\service\\**\\service\\*ServiceUtil.java",
2397                            "**\\service\\**\\service\\*ServiceWrapper.java",
2398                            "**\\service\\**\\service\\ClpSerializer.java",
2399                            "**\\service\\**\\service\\messaging\\*ClpMessageListener.java",
2400                            "**\\service\\**\\service\\persistence\\*Finder.java",
2401                            "**\\service\\**\\service\\persistence\\*Util.java",
2402                            "**\\service\\base\\*ServiceBaseImpl.java",
2403                            "**\\service\\base\\*ServiceClpInvoker.java",
2404                            "**\\service\\http\\*JSONSerializer.java",
2405                            "**\\service\\http\\*ServiceHttp.java",
2406                            "**\\service\\http\\*ServiceJSON.java",
2407                            "**\\service\\http\\*ServiceSoap.java", "**\\tmp\\**"
2408                    };
2409                    String[] includes = new String[] {"**\\*.java"};
2410    
2411                    fileNames.addAll(getFileNames(excludes, includes));
2412    
2413                    return fileNames;
2414            }
2415    
2416            protected Collection<String> getPortalJavaFiles() {
2417                    Collection<String> fileNames = new TreeSet<String>();
2418    
2419                    String[] excludes = new String[] {
2420                            "**\\*_IW.java", "**\\PropsValues.java", "**\\bin\\**",
2421                            "**\\classes\\*", "**\\counter\\service\\**", "**\\jsp\\*",
2422                            "**\\model\\impl\\*BaseImpl.java", "**\\model\\impl\\*Model.java",
2423                            "**\\model\\impl\\*ModelImpl.java", "**\\portal\\service\\**",
2424                            "**\\portal-client\\**", "**\\portal-web\\classes\\**\\*.java",
2425                            "**\\portal-web\\test\\**\\*Test.java",
2426                            "**\\portal-web\\test\\**\\*Tests.java",
2427                            "**\\portlet\\**\\service\\**", "**\\test\\*-generated\\**",
2428                            "**\\tmp\\**", "**\\tools\\tck\\**"
2429                    };
2430                    String[] includes = new String[] {"**\\*.java"};
2431    
2432                    fileNames.addAll(getFileNames(excludes, includes));
2433    
2434                    excludes = new String[] {
2435                            "**\\bin\\**", "**\\portal-client\\**", "**\\tools\\ext_tmpl\\**",
2436                            "**\\*_IW.java", "**\\test\\**\\*PersistenceTest.java"
2437                    };
2438                    includes = new String[] {
2439                            "**\\com\\liferay\\portal\\service\\ServiceContext*.java",
2440                            "**\\model\\BaseModel.java", "**\\model\\impl\\BaseModelImpl.java",
2441                            "**\\service\\Base*.java",
2442                            "**\\service\\PersistedModelLocalService*.java",
2443                            "**\\service\\base\\PrincipalBean.java",
2444                            "**\\service\\http\\*HttpTest.java",
2445                            "**\\service\\http\\*SoapTest.java",
2446                            "**\\service\\http\\TunnelUtil.java", "**\\service\\impl\\*.java",
2447                            "**\\service\\jms\\*.java", "**\\service\\permission\\*.java",
2448                            "**\\service\\persistence\\BasePersistence.java",
2449                            "**\\service\\persistence\\BatchSession*.java",
2450                            "**\\service\\persistence\\*FinderImpl.java",
2451                            "**\\service\\persistence\\*Query.java",
2452                            "**\\service\\persistence\\impl\\*.java",
2453                            "**\\portal-impl\\test\\**\\*.java",
2454                            "**\\portal-service\\**\\liferay\\documentlibrary\\**.java",
2455                            "**\\portal-service\\**\\liferay\\lock\\**.java",
2456                            "**\\portal-service\\**\\liferay\\mail\\**.java",
2457                            "**\\util-bridges\\**\\*.java"
2458                    };
2459    
2460                    fileNames.addAll(getFileNames(excludes, includes));
2461    
2462                    return fileNames;
2463            }
2464    
2465            protected String getVariableName(String line) {
2466                    int x = line.indexOf(StringPool.EQUAL);
2467                    int y = line.lastIndexOf(StringPool.SPACE);
2468    
2469                    if (x != -1) {
2470                            line = line.substring(0, x);
2471                            line = StringUtil.trim(line);
2472    
2473                            y = line.lastIndexOf(StringPool.SPACE);
2474    
2475                            return line.substring(y + 1);
2476                    }
2477    
2478                    if (line.endsWith(StringPool.SEMICOLON)) {
2479                            return line.substring(y + 1, line.length() - 1);
2480                    }
2481    
2482                    return StringPool.BLANK;
2483            }
2484    
2485            protected boolean hasAnnotationCommentOrJavadoc(String s) {
2486                    if (s.startsWith(StringPool.TAB + StringPool.AT) ||
2487                            s.startsWith(StringPool.TAB + "/**") ||
2488                            s.startsWith(StringPool.TAB + "//")) {
2489    
2490                            return true;
2491                    }
2492                    else {
2493                            return false;
2494                    }
2495            }
2496    
2497            protected boolean isGenerated(String content) {
2498                    if (content.contains("* @generated") || content.contains("$ANTLR")) {
2499                            return true;
2500                    }
2501                    else {
2502                            return false;
2503                    }
2504            }
2505    
2506            protected boolean isValidJavaParameter(String javaParameter) {
2507                    int quoteCount = StringUtil.count(javaParameter, StringPool.QUOTE);
2508    
2509                    if ((quoteCount % 2) == 1) {
2510                            return false;
2511                    }
2512    
2513                    javaParameter = stripQuotes(javaParameter, CharPool.QUOTE);
2514    
2515                    int openParenthesisCount = StringUtil.count(
2516                            javaParameter, StringPool.OPEN_PARENTHESIS);
2517                    int closeParenthesisCount = StringUtil.count(
2518                            javaParameter, StringPool.CLOSE_PARENTHESIS);
2519                    int lessThanCount = StringUtil.count(
2520                            javaParameter, StringPool.LESS_THAN);
2521                    int greaterThanCount = StringUtil.count(
2522                            javaParameter, StringPool.GREATER_THAN);
2523                    int openCurlyBraceCount = StringUtil.count(
2524                            javaParameter, StringPool.OPEN_CURLY_BRACE);
2525                    int closeCurlyBraceCount = StringUtil.count(
2526                            javaParameter, StringPool.CLOSE_CURLY_BRACE);
2527    
2528                    if ((openParenthesisCount == closeParenthesisCount) &&
2529                            (lessThanCount == greaterThanCount) &&
2530                            (openCurlyBraceCount == closeCurlyBraceCount)) {
2531    
2532                            return true;
2533                    }
2534    
2535                    return false;
2536            }
2537    
2538            protected String sortExceptions(String line) {
2539                    if (!line.endsWith(StringPool.OPEN_CURLY_BRACE) &&
2540                            !line.endsWith(StringPool.SEMICOLON)) {
2541    
2542                            return line;
2543                    }
2544    
2545                    int x = line.indexOf("throws ");
2546    
2547                    if (x == -1) {
2548                            return line;
2549                    }
2550    
2551                    String previousException = StringPool.BLANK;
2552    
2553                    String[] exceptions = StringUtil.split(
2554                            line.substring(x), CharPool.SPACE);
2555    
2556                    for (int i = 1; i < exceptions.length; i++) {
2557                            String exception = exceptions[i];
2558    
2559                            if (exception.equals(StringPool.OPEN_CURLY_BRACE)) {
2560                                    break;
2561                            }
2562    
2563                            if (exception.endsWith(StringPool.COMMA) ||
2564                                    exception.endsWith(StringPool.SEMICOLON)) {
2565    
2566                                    exception = exception.substring(0, exception.length() - 1);
2567                            }
2568    
2569                            if (Validator.isNotNull(previousException) &&
2570                                    (previousException.compareToIgnoreCase(exception) > 0)) {
2571    
2572                                    return StringUtil.replace(
2573                                            line, previousException + ", " + exception,
2574                                            exception + ", " + previousException);
2575                            }
2576    
2577                            previousException = exception;
2578                    }
2579    
2580                    return line;
2581            }
2582    
2583            protected String sortJavaTerms(
2584                    String fileName, String content, Set<JavaTerm> javaTerms) {
2585    
2586                    JavaTerm previousJavaTerm = null;
2587    
2588                    Iterator<JavaTerm> itr = javaTerms.iterator();
2589    
2590                    while (itr.hasNext()) {
2591                            JavaTerm javaTerm = itr.next();
2592    
2593                            if (previousJavaTerm == null) {
2594                                    previousJavaTerm = javaTerm;
2595    
2596                                    continue;
2597                            }
2598    
2599                            int javaTermLineCount = javaTerm.getLineCount();
2600                            String javaTermName = javaTerm.getName();
2601    
2602                            String excluded = null;
2603    
2604                            if (_javaTermSortExclusions != null) {
2605                                    excluded = _javaTermSortExclusions.getProperty(
2606                                            fileName + StringPool.AT + javaTermLineCount);
2607    
2608                                    if (excluded == null) {
2609                                            excluded = _javaTermSortExclusions.getProperty(
2610                                                    fileName + StringPool.AT + javaTermName);
2611                                    }
2612    
2613                                    if (excluded == null) {
2614                                            excluded = _javaTermSortExclusions.getProperty(fileName);
2615                                    }
2616                            }
2617    
2618                            if (excluded != null) {
2619                                    previousJavaTerm = javaTerm;
2620    
2621                                    continue;
2622                            }
2623    
2624                            String javaTermContent = javaTerm.getContent();
2625                            String previousJavaTermContent = previousJavaTerm.getContent();
2626    
2627                            if (previousJavaTerm.getLineCount() > javaTermLineCount) {
2628                                    String previousJavaTermName = previousJavaTerm.getName();
2629    
2630                                    String javaTermNameLowerCase = javaTermName.toLowerCase();
2631                                    String previousJavaTermNameLowerCase =
2632                                            previousJavaTermName.toLowerCase();
2633    
2634                                    if (fileName.contains("persistence") &&
2635                                            ((previousJavaTermName.startsWith("doCount") &&
2636                                              javaTermName.startsWith("doCount")) ||
2637                                             (previousJavaTermName.startsWith("doFind") &&
2638                                              javaTermName.startsWith("doFind")) ||
2639                                             (previousJavaTermNameLowerCase.startsWith("count") &&
2640                                              javaTermNameLowerCase.startsWith("count")) ||
2641                                             (previousJavaTermNameLowerCase.startsWith("filter") &&
2642                                              javaTermNameLowerCase.startsWith("filter")) ||
2643                                             (previousJavaTermNameLowerCase.startsWith("find") &&
2644                                              javaTermNameLowerCase.startsWith("find")) ||
2645                                             (previousJavaTermNameLowerCase.startsWith("join") &&
2646                                              javaTermNameLowerCase.startsWith("join")))) {
2647                                    }
2648                                    else {
2649                                            content = StringUtil.replaceFirst(
2650                                                    content, "\n" + javaTermContent,
2651                                                    "\n" + previousJavaTermContent);
2652                                            content = StringUtil.replaceLast(
2653                                                    content, "\n" + previousJavaTermContent,
2654                                                    "\n" + javaTermContent);
2655    
2656                                            return content;
2657                                    }
2658                            }
2659    
2660                            previousJavaTerm = javaTerm;
2661                    }
2662    
2663                    return content;
2664            }
2665    
2666            private boolean _checkUnprocessedExceptions;
2667            private Properties _javaTermSortExclusions;
2668            private Properties _lineLengthExclusions;
2669            private Properties _staticLogVariableExclusions;
2670            private Properties _upgradeServiceUtilExclusions;
2671    
2672    }