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 if (trimmedLine.startsWith(StringPool.EQUAL)) { 1203 processErrorMessage( 1204 fileName, "equal: " + fileName + " " + lineCount); 1205 } 1206 1207 if (line.contains("ActionForm form")) { 1208 processErrorMessage( 1209 fileName, 1210 "Rename form to actionForm: " + fileName + " " + lineCount); 1211 } 1212 1213 if (line.contains("ActionMapping mapping")) { 1214 processErrorMessage( 1215 fileName, 1216 "Rename mapping to ActionMapping: " + fileName + " " + 1217 lineCount); 1218 } 1219 1220 if (!trimmedLine.equals("{") && line.endsWith("{") && 1221 !line.endsWith(" {")) { 1222 1223 line = StringUtil.replaceLast(line, "{", " {"); 1224 } 1225 1226 line = sortExceptions(line); 1227 1228 if (trimmedLine.startsWith("if (") || 1229 trimmedLine.startsWith("else if (") || 1230 trimmedLine.startsWith("while (") || 1231 Validator.isNotNull(ifClause)) { 1232 1233 ifClause = ifClause + line + StringPool.NEW_LINE; 1234 1235 if (line.endsWith(") {")) { 1236 String newIfClause = checkIfClause( 1237 ifClause, fileName, lineCount); 1238 1239 if (!ifClause.equals(newIfClause)) { 1240 return StringUtil.replace( 1241 content, ifClause, newIfClause); 1242 } 1243 1244 ifClause = StringPool.BLANK; 1245 } 1246 else if (line.endsWith(StringPool.SEMICOLON)) { 1247 ifClause = StringPool.BLANK; 1248 } 1249 } 1250 1251 String excluded = null; 1252 1253 if (line.startsWith(StringPool.TAB + "private ") || 1254 line.equals(StringPool.TAB + "private") || 1255 line.startsWith(StringPool.TAB + "protected ") || 1256 line.equals(StringPool.TAB + "protected") || 1257 line.startsWith(StringPool.TAB + "public ") || 1258 line.equals(StringPool.TAB + "public")) { 1259 1260 Tuple tuple = getJavaTermTuple(line, content, index, 1, 3); 1261 1262 if (tuple != null) { 1263 int javaTermEndPosition = 0; 1264 1265 if (lastCommentOrAnnotationPos == -1) { 1266 javaTermEndPosition = index; 1267 } 1268 else { 1269 javaTermEndPosition = lastCommentOrAnnotationPos; 1270 } 1271 1272 if ((javaTermStartPosition != -1) && 1273 (javaTermEndPosition < content.length())) { 1274 1275 String javaTermContent = content.substring( 1276 javaTermStartPosition, javaTermEndPosition); 1277 1278 if (Validator.isNotNull(javaTermName)) { 1279 javaTerm = new JavaTerm( 1280 javaTermName, javaTermType, parameterTypes, 1281 javaTermContent, javaTermLineCount); 1282 1283 javaTerms.add(javaTerm); 1284 } 1285 } 1286 1287 javaTermLineCount = lineCount; 1288 javaTermName = (String)tuple.getObject(0); 1289 javaTermStartPosition = javaTermEndPosition; 1290 javaTermType = (Integer)tuple.getObject(1); 1291 1292 if (Validator.isNotNull(javaTermName)) { 1293 if (isInJavaTermTypeGroup( 1294 javaTermType, TYPE_CONSTRUCTOR) || 1295 isInJavaTermTypeGroup( 1296 javaTermType, TYPE_METHOD)) { 1297 1298 readParameterTypes = true; 1299 1300 parameterTypes = new ArrayList<String>(); 1301 } 1302 } 1303 } 1304 1305 lastCommentOrAnnotationPos = -1; 1306 } 1307 else if (hasAnnotationCommentOrJavadoc(line)) { 1308 if (lastCommentOrAnnotationPos == -1) { 1309 lastCommentOrAnnotationPos = index; 1310 } 1311 } 1312 1313 if (readParameterTypes) { 1314 parameterTypes = addParameterTypes(trimmedLine, parameterTypes); 1315 1316 if (trimmedLine.contains(StringPool.CLOSE_PARENTHESIS)) { 1317 readParameterTypes = false; 1318 } 1319 } 1320 1321 if (!trimmedLine.contains(StringPool.DOUBLE_SLASH) && 1322 !trimmedLine.startsWith(StringPool.STAR)) { 1323 1324 String strippedQuotesLine = stripQuotes( 1325 trimmedLine, CharPool.QUOTE); 1326 1327 for (int x = -1;;) { 1328 x = strippedQuotesLine.indexOf(StringPool.EQUAL, x + 1); 1329 1330 if (x == -1) { 1331 break; 1332 } 1333 1334 char c = strippedQuotesLine.charAt(x - 1); 1335 1336 if (Character.isLetterOrDigit(c)) { 1337 line = StringUtil.replace(line, c + "=", c + " ="); 1338 1339 break; 1340 } 1341 1342 if (x == (strippedQuotesLine.length() - 1)) { 1343 break; 1344 } 1345 1346 c = strippedQuotesLine.charAt(x + 1); 1347 1348 if (Character.isLetterOrDigit(c)) { 1349 line = StringUtil.replace(line, "=" + c, "= " + c); 1350 1351 break; 1352 } 1353 } 1354 1355 while (trimmedLine.contains(StringPool.TAB)) { 1356 line = StringUtil.replaceLast( 1357 line, StringPool.TAB, StringPool.SPACE); 1358 1359 trimmedLine = StringUtil.replaceLast( 1360 trimmedLine, StringPool.TAB, StringPool.SPACE); 1361 } 1362 1363 if (line.contains(StringPool.TAB + StringPool.SPACE) && 1364 !previousLine.endsWith("&&") && 1365 !previousLine.endsWith("||") && 1366 !previousLine.contains(StringPool.TAB + "((") && 1367 !previousLine.contains( 1368 StringPool.TAB + StringPool.LESS_THAN) && 1369 !previousLine.contains(StringPool.TAB + StringPool.SPACE) && 1370 !previousLine.contains(StringPool.TAB + "implements ") && 1371 !previousLine.contains(StringPool.TAB + "throws ")) { 1372 1373 line = StringUtil.replace( 1374 line, StringPool.TAB + StringPool.SPACE, 1375 StringPool.TAB); 1376 } 1377 1378 while (trimmedLine.contains(StringPool.DOUBLE_SPACE) && 1379 !trimmedLine.contains( 1380 StringPool.QUOTE + StringPool.DOUBLE_SPACE) && 1381 !fileName.contains("Test")) { 1382 1383 line = StringUtil.replaceLast( 1384 line, StringPool.DOUBLE_SPACE, StringPool.SPACE); 1385 1386 trimmedLine = StringUtil.replaceLast( 1387 trimmedLine, StringPool.DOUBLE_SPACE, StringPool.SPACE); 1388 } 1389 1390 if (!line.contains(StringPool.QUOTE)) { 1391 int pos = line.indexOf(") "); 1392 1393 if (pos != -1) { 1394 String linePart = line.substring(pos + 2); 1395 1396 if (Character.isLetter(linePart.charAt(0)) && 1397 !linePart.startsWith("default") && 1398 !linePart.startsWith("instanceof") && 1399 !linePart.startsWith("throws")) { 1400 1401 line = StringUtil.replaceLast( 1402 line, StringPool.SPACE + linePart, linePart); 1403 } 1404 } 1405 1406 if ((trimmedLine.startsWith("private ") || 1407 trimmedLine.startsWith("protected ") || 1408 trimmedLine.startsWith("public ")) && 1409 !line.contains(StringPool.EQUAL) && 1410 line.contains(" (")) { 1411 1412 line = StringUtil.replace(line, " (", "("); 1413 } 1414 1415 if (line.contains(" [")) { 1416 line = StringUtil.replace(line, " [", "["); 1417 } 1418 1419 for (int x = -1;;) { 1420 int posComma = line.indexOf(StringPool.COMMA, x + 1); 1421 int posSemicolon = line.indexOf( 1422 StringPool.SEMICOLON, x + 1); 1423 1424 if ((posComma == -1) && (posSemicolon == -1)) { 1425 break; 1426 } 1427 1428 x = Math.min(posComma, posSemicolon); 1429 1430 if (x == -1) { 1431 x = Math.max(posComma, posSemicolon); 1432 } 1433 1434 if (line.length() > (x + 1)) { 1435 char nextChar = line.charAt(x + 1); 1436 1437 if ((nextChar != CharPool.APOSTROPHE) && 1438 (nextChar != CharPool.CLOSE_PARENTHESIS) && 1439 (nextChar != CharPool.SPACE) && 1440 (nextChar != CharPool.STAR)) { 1441 1442 line = StringUtil.insert( 1443 line, StringPool.SPACE, x + 1); 1444 } 1445 } 1446 1447 if (x > 0) { 1448 char previousChar = line.charAt(x - 1); 1449 1450 if (previousChar == CharPool.SPACE) { 1451 line = line.substring(0, x - 1).concat( 1452 line.substring(x)); 1453 } 1454 } 1455 } 1456 } 1457 1458 if ((line.contains(" && ") || line.contains(" || ")) && 1459 line.endsWith(StringPool.OPEN_PARENTHESIS)) { 1460 1461 processErrorMessage( 1462 fileName, "line break: " + fileName + " " + lineCount); 1463 } 1464 1465 if (trimmedLine.endsWith(StringPool.PLUS) && 1466 !trimmedLine.startsWith(StringPool.OPEN_PARENTHESIS)) { 1467 1468 int closeParenthesisCount = StringUtil.count( 1469 strippedQuotesLine, StringPool.CLOSE_PARENTHESIS); 1470 int openParenthesisCount = StringUtil.count( 1471 strippedQuotesLine, StringPool.OPEN_PARENTHESIS); 1472 1473 if (openParenthesisCount > closeParenthesisCount) { 1474 processErrorMessage( 1475 fileName, 1476 "line break: " + fileName + " " + lineCount); 1477 } 1478 } 1479 1480 if (line.contains(StringPool.COMMA) && 1481 !line.contains(StringPool.CLOSE_PARENTHESIS) && 1482 !line.contains(StringPool.GREATER_THAN) && 1483 !line.contains(StringPool.QUOTE) && 1484 line.endsWith(StringPool.OPEN_PARENTHESIS)) { 1485 1486 processErrorMessage( 1487 fileName, "line break: " + fileName + " " + lineCount); 1488 } 1489 1490 if (line.endsWith(" +") || line.endsWith(" -") || 1491 line.endsWith(" *") || line.endsWith(" /")) { 1492 1493 int x = line.indexOf(" = "); 1494 1495 if (x != -1) { 1496 int y = line.indexOf(StringPool.QUOTE); 1497 1498 if ((y == -1) || (x < y)) { 1499 processErrorMessage( 1500 fileName, 1501 "line break: " + fileName + " " + lineCount); 1502 } 1503 } 1504 } 1505 1506 if (line.endsWith(" throws") || 1507 (previousLine.endsWith( 1508 StringPool.OPEN_PARENTHESIS) && 1509 line.contains(" throws " ) && 1510 line.endsWith(StringPool.OPEN_CURLY_BRACE))) { 1511 1512 processErrorMessage( 1513 fileName, "line break: " + fileName + " " + lineCount); 1514 } 1515 1516 if (trimmedLine.startsWith(StringPool.PERIOD) || 1517 (line.endsWith(StringPool.PERIOD) && 1518 line.contains(StringPool.EQUAL))) { 1519 1520 processErrorMessage( 1521 fileName, "line break: " + fileName + " " + lineCount); 1522 } 1523 1524 if (trimmedLine.startsWith(StringPool.CLOSE_CURLY_BRACE) && 1525 line.endsWith(StringPool.OPEN_CURLY_BRACE)) { 1526 1527 processErrorMessage( 1528 fileName, "line break: " + fileName + " " + lineCount); 1529 } 1530 } 1531 1532 if (line.contains(" ") && !line.matches("\\s*\\*.*")) { 1533 if (!fileName.endsWith("StringPool.java")) { 1534 processErrorMessage( 1535 fileName, "tab: " + fileName + " " + lineCount); 1536 } 1537 } 1538 1539 if (line.contains(" {") && !line.matches("\\s*\\*.*")) { 1540 processErrorMessage( 1541 fileName, "{:" + fileName + " " + lineCount); 1542 } 1543 1544 excluded = null; 1545 1546 if (_lineLengthExclusions != null) { 1547 excluded = _lineLengthExclusions.getProperty( 1548 fileName + StringPool.AT + lineCount); 1549 1550 if (excluded == null) { 1551 excluded = _lineLengthExclusions.getProperty(fileName); 1552 } 1553 } 1554 1555 Tuple combinedLines = null; 1556 int lineLength = getLineLength(line); 1557 1558 if ((excluded == null) && 1559 !line.startsWith("import ") && !line.startsWith("package ") && 1560 !line.matches("\\s*\\*.*")) { 1561 1562 if (fileName.endsWith("Table.java") && 1563 line.contains("String TABLE_SQL_CREATE = ")) { 1564 } 1565 else if (fileName.endsWith("Table.java") && 1566 line.contains("String TABLE_SQL_DROP = ")) { 1567 } 1568 else if (fileName.endsWith("Table.java") && 1569 line.contains(" index IX_")) { 1570 } 1571 else if (lineLength > 80) { 1572 processErrorMessage( 1573 fileName, "> 80: " + fileName + " " + lineCount); 1574 } 1575 else { 1576 int lineLeadingTabCount = getLeadingTabCount(line); 1577 int previousLineLeadingTabCount = getLeadingTabCount( 1578 previousLine); 1579 1580 if (!trimmedLine.startsWith("//")) { 1581 if (previousLine.endsWith(StringPool.COMMA) && 1582 previousLine.contains( 1583 StringPool.OPEN_PARENTHESIS) && 1584 !previousLine.contains("for (") && 1585 (lineLeadingTabCount > 1586 previousLineLeadingTabCount)) { 1587 1588 processErrorMessage( 1589 fileName, 1590 "line break: " + fileName + " " + lineCount); 1591 } 1592 1593 if (Validator.isNotNull(trimmedLine)) { 1594 if (((previousLine.endsWith(StringPool.COLON) && 1595 previousLine.contains( 1596 StringPool.TAB + "for ")) || 1597 (previousLine.endsWith( 1598 StringPool.OPEN_PARENTHESIS) && 1599 previousLine.contains( 1600 StringPool.TAB + "if "))) && 1601 ((previousLineLeadingTabCount + 2) != 1602 lineLeadingTabCount)) { 1603 1604 processErrorMessage( 1605 fileName, 1606 "line break: " + fileName + " " + lineCount); 1607 } 1608 1609 if (previousLine.endsWith( 1610 StringPool.OPEN_CURLY_BRACE) && 1611 !trimmedLine.startsWith( 1612 StringPool.CLOSE_CURLY_BRACE) && 1613 ((previousLineLeadingTabCount + 1) != 1614 lineLeadingTabCount)) { 1615 1616 processErrorMessage( 1617 fileName, 1618 "tab: " + fileName + " " + lineCount); 1619 } 1620 } 1621 1622 if (previousLine.endsWith(StringPool.PERIOD)) { 1623 int x = trimmedLine.indexOf( 1624 StringPool.OPEN_PARENTHESIS); 1625 1626 if ((x != -1) && 1627 ((getLineLength(previousLine) + x) < 80) && 1628 (trimmedLine.endsWith( 1629 StringPool.OPEN_PARENTHESIS) || 1630 (trimmedLine.charAt(x + 1) != 1631 CharPool.CLOSE_PARENTHESIS))) { 1632 1633 processErrorMessage( 1634 fileName, 1635 "line break: " + fileName + " " + 1636 lineCount); 1637 } 1638 } 1639 1640 if (trimmedLine.startsWith("throws ") && 1641 (lineLeadingTabCount == 1642 previousLineLeadingTabCount)) { 1643 1644 processErrorMessage( 1645 fileName, "tab: " + fileName + " " + lineCount); 1646 } 1647 1648 if ((previousLine.contains(" class " ) || 1649 previousLine.contains(" enum ")) && 1650 previousLine.endsWith( 1651 StringPool.OPEN_CURLY_BRACE) && 1652 Validator.isNotNull(line) && 1653 !trimmedLine.startsWith( 1654 StringPool.CLOSE_CURLY_BRACE)) { 1655 1656 processErrorMessage( 1657 fileName, 1658 "new line: " + fileName + " " + lineCount); 1659 } 1660 } 1661 1662 combinedLines = getCombinedLines( 1663 trimmedLine, previousLine, lineLeadingTabCount, 1664 previousLineLeadingTabCount); 1665 } 1666 } 1667 1668 if (combinedLines != null) { 1669 previousLine = (String)combinedLines.getObject(0); 1670 1671 if (combinedLines.getSize() > 1) { 1672 String linePart = (String)combinedLines.getObject(1); 1673 boolean addToPreviousLine = 1674 (Boolean)combinedLines.getObject(2); 1675 1676 if (addToPreviousLine) { 1677 previousLine = previousLine + linePart; 1678 line = StringUtil.replaceFirst( 1679 line, linePart, StringPool.BLANK); 1680 } 1681 else { 1682 if (((linePart.length() + lineLength) <= 80) && 1683 (line.endsWith(StringPool.OPEN_CURLY_BRACE) || 1684 line.endsWith(StringPool.SEMICOLON))) { 1685 1686 previousLine = StringUtil.replaceLast( 1687 previousLine, StringUtil.trim(linePart), 1688 StringPool.BLANK); 1689 1690 line = StringUtil.replaceLast( 1691 line, StringPool.TAB, 1692 StringPool.TAB + linePart); 1693 } 1694 else { 1695 processErrorMessage( 1696 fileName, 1697 "line break: " + fileName + " " + lineCount); 1698 } 1699 } 1700 1701 sb.append(previousLine); 1702 sb.append("\n"); 1703 1704 previousLine = line; 1705 } 1706 else if (line.endsWith(StringPool.OPEN_CURLY_BRACE) && 1707 !previousLine.contains(" class ")) { 1708 1709 lineToSkipIfEmpty = lineCount + 1; 1710 } 1711 } 1712 else { 1713 if ((lineCount > 1) && 1714 (Validator.isNotNull(previousLine) || 1715 (lineToSkipIfEmpty != (lineCount - 1)))) { 1716 1717 sb.append(previousLine); 1718 1719 if (Validator.isNotNull(previousLine) && 1720 Validator.isNotNull(trimmedLine) && 1721 !previousLine.contains("/*") && 1722 !previousLine.endsWith("*/")) { 1723 1724 String trimmedPreviousLine = StringUtil.trimLeading( 1725 previousLine); 1726 1727 if ((trimmedPreviousLine.startsWith("// ") && 1728 !trimmedLine.startsWith("// ")) || 1729 (!trimmedPreviousLine.startsWith("// ") && 1730 trimmedLine.startsWith("// "))) { 1731 1732 sb.append("\n"); 1733 } 1734 else if (!trimmedPreviousLine.endsWith( 1735 StringPool.OPEN_CURLY_BRACE) && 1736 !trimmedPreviousLine.endsWith( 1737 StringPool.COLON) && 1738 (trimmedLine.startsWith("for (") || 1739 trimmedLine.startsWith("if ("))) { 1740 1741 sb.append("\n"); 1742 } 1743 else if (previousLine.endsWith( 1744 StringPool.TAB + 1745 StringPool.CLOSE_CURLY_BRACE) && 1746 !trimmedLine.startsWith( 1747 StringPool.CLOSE_CURLY_BRACE) && 1748 !trimmedLine.startsWith( 1749 StringPool.CLOSE_PARENTHESIS) && 1750 !trimmedLine.startsWith( 1751 StringPool.DOUBLE_SLASH) && 1752 !trimmedLine.startsWith("catch ") && 1753 !trimmedLine.startsWith("else ") && 1754 !trimmedLine.startsWith("finally ") && 1755 !trimmedLine.startsWith("while ")) { 1756 1757 sb.append("\n"); 1758 } 1759 } 1760 1761 sb.append("\n"); 1762 } 1763 1764 previousLine = line; 1765 } 1766 1767 index = index + line.length() + 1; 1768 } 1769 1770 sb.append(previousLine); 1771 1772 unsyncBufferedReader.close(); 1773 1774 String newContent = sb.toString(); 1775 1776 if (newContent.endsWith("\n")) { 1777 newContent = newContent.substring(0, newContent.length() - 1); 1778 } 1779 1780 if (content.equals(newContent)) { 1781 if (javaTermStartPosition != -1) { 1782 int javaTermEndPosition = content.length() - 2; 1783 1784 String javaTermContent = content.substring( 1785 javaTermStartPosition, javaTermEndPosition); 1786 1787 javaTerm = new JavaTerm( 1788 javaTermName, javaTermType, parameterTypes, javaTermContent, 1789 javaTermLineCount); 1790 1791 javaTerms.add(javaTerm); 1792 } 1793 1794 newContent = sortJavaTerms(fileName, content, javaTerms); 1795 } 1796 1797 if (content.equals(newContent)) { 1798 newContent = fixJavaTermsDividers(fileName, content, javaTerms); 1799 } 1800 1801 if (content.equals(newContent)) { 1802 newContent = formatAnnotations(fileName, content, javaTerms); 1803 } 1804 1805 return newContent; 1806 } 1807 1808 protected String getClassName(String line) { 1809 int pos = line.indexOf(" implements "); 1810 1811 if (pos == -1) { 1812 pos = line.indexOf(" extends "); 1813 } 1814 1815 if (pos == -1) { 1816 pos = line.indexOf(StringPool.OPEN_CURLY_BRACE); 1817 } 1818 1819 if (pos != -1) { 1820 line = line.substring(0, pos); 1821 } 1822 1823 line = line.trim(); 1824 1825 pos = line.lastIndexOf(StringPool.SPACE); 1826 1827 return line.substring(pos + 1); 1828 } 1829 1830 protected Tuple getCombinedLines( 1831 String line, String previousLine, int lineTabCount, 1832 int previousLineTabCount) { 1833 1834 if (Validator.isNull(line) || Validator.isNull(previousLine)) { 1835 return null; 1836 } 1837 1838 String trimmedPreviousLine = StringUtil.trimLeading(previousLine); 1839 1840 int previousLineLength = getLineLength(previousLine); 1841 1842 if (line.startsWith("// ") && trimmedPreviousLine.startsWith("// ")) { 1843 String linePart = line.substring(3); 1844 1845 if (!linePart.startsWith("PLACEHOLDER") && 1846 !linePart.startsWith(StringPool.OPEN_BRACKET)) { 1847 1848 int pos = linePart.indexOf(StringPool.SPACE); 1849 1850 if (pos == -1) { 1851 pos = linePart.length(); 1852 } 1853 1854 if ((previousLineLength + pos) < 80) { 1855 if (linePart.contains(StringPool.SPACE)) { 1856 return new Tuple( 1857 previousLine + StringPool.SPACE, 1858 linePart.substring(0, pos + 1), true); 1859 } 1860 else { 1861 return new Tuple( 1862 previousLine + StringPool.SPACE + linePart); 1863 } 1864 } 1865 } 1866 1867 return null; 1868 } 1869 else if (line.startsWith("// ") || 1870 trimmedPreviousLine.startsWith("// ")) { 1871 1872 return null; 1873 } 1874 1875 if (previousLine.endsWith(" extends")) { 1876 return new Tuple(previousLine, "extends ", false); 1877 } 1878 1879 if (previousLine.endsWith(" implements")) { 1880 return new Tuple(previousLine, "implements ", false); 1881 } 1882 1883 if (line.startsWith("+ ") || line.startsWith("- ") || 1884 line.startsWith("|| ") || line.startsWith("&& ")) { 1885 1886 int pos = line.indexOf(StringPool.SPACE); 1887 1888 String linePart = line.substring(0, pos); 1889 1890 return new Tuple(previousLine + StringPool.SPACE, linePart, true); 1891 } 1892 1893 if ((line.length() + previousLineLength) < 80) { 1894 if (trimmedPreviousLine.startsWith("for ") && 1895 previousLine.endsWith(StringPool.COLON) && 1896 line.endsWith(StringPool.OPEN_CURLY_BRACE)) { 1897 1898 return new Tuple(previousLine + StringPool.SPACE + line); 1899 } 1900 1901 if ((previousLine.endsWith(StringPool.EQUAL) || 1902 previousLine.endsWith(StringPool.PERIOD) || 1903 trimmedPreviousLine.equals("return")) && 1904 line.endsWith(StringPool.SEMICOLON)) { 1905 1906 return new Tuple(previousLine + StringPool.SPACE + line); 1907 } 1908 1909 if ((trimmedPreviousLine.startsWith("if ") || 1910 trimmedPreviousLine.startsWith("else ")) && 1911 (previousLine.endsWith("||") || previousLine.endsWith("&&")) && 1912 line.endsWith(StringPool.OPEN_CURLY_BRACE)) { 1913 1914 return new Tuple(previousLine + StringPool.SPACE + line); 1915 } 1916 1917 if ((line.startsWith("extends ") || 1918 line.startsWith("implements ") || 1919 line.startsWith("throws")) && 1920 line.endsWith(StringPool.OPEN_CURLY_BRACE) && 1921 (lineTabCount == (previousLineTabCount + 1))) { 1922 1923 return new Tuple(previousLine + StringPool.SPACE + line); 1924 } 1925 } 1926 1927 if (previousLine.endsWith(StringPool.EQUAL) && 1928 line.endsWith(StringPool.SEMICOLON)) { 1929 1930 String tempLine = line; 1931 1932 for (int pos = 0;;) { 1933 pos = tempLine.indexOf(StringPool.DASH); 1934 1935 if (pos == -1) { 1936 pos = tempLine.indexOf(StringPool.PLUS); 1937 } 1938 1939 if (pos == -1) { 1940 pos = tempLine.indexOf(StringPool.SLASH); 1941 } 1942 1943 if (pos == -1) { 1944 pos = tempLine.indexOf(StringPool.STAR); 1945 } 1946 1947 if (pos == -1) { 1948 pos = tempLine.indexOf("||"); 1949 } 1950 1951 if (pos == -1) { 1952 pos = tempLine.indexOf("&&"); 1953 } 1954 1955 if (pos == -1) { 1956 break; 1957 } 1958 1959 String linePart = tempLine.substring(0, pos); 1960 1961 int openParenthesisCount = StringUtil.count( 1962 linePart, StringPool.OPEN_PARENTHESIS); 1963 int closeParenthesisCount = StringUtil.count( 1964 linePart, StringPool.CLOSE_PARENTHESIS); 1965 1966 if (openParenthesisCount == closeParenthesisCount) { 1967 return null; 1968 } 1969 1970 tempLine = 1971 tempLine.substring(0, pos) + tempLine.substring(pos + 1); 1972 } 1973 1974 int x = line.indexOf(StringPool.OPEN_PARENTHESIS); 1975 1976 if (x == 0) { 1977 x = line.indexOf(StringPool.OPEN_PARENTHESIS, 1); 1978 } 1979 1980 if (x != -1) { 1981 int y = line.indexOf(StringPool.CLOSE_PARENTHESIS, x); 1982 int z = line.indexOf(StringPool.QUOTE); 1983 1984 if (((x + 1) != y) && ((z == -1) || (z > x))) { 1985 char previousChar = line.charAt(x - 1); 1986 1987 if ((previousChar != CharPool.CLOSE_PARENTHESIS) && 1988 (previousChar != CharPool.OPEN_PARENTHESIS) && 1989 (previousChar != CharPool.SPACE) && 1990 (previousLineLength + 1 + x) < 80) { 1991 1992 String linePart = line.substring(0, x + 1); 1993 1994 if (linePart.startsWith(StringPool.OPEN_PARENTHESIS) && 1995 !linePart.contains( 1996 StringPool.CLOSE_PARENTHESIS)) { 1997 1998 return null; 1999 } 2000 2001 return new Tuple( 2002 previousLine + StringPool.SPACE, linePart, true); 2003 } 2004 } 2005 } 2006 } 2007 2008 if (previousLine.endsWith(StringPool.COMMA) && 2009 (previousLineTabCount == lineTabCount) && 2010 !previousLine.contains(StringPool.CLOSE_CURLY_BRACE)) { 2011 2012 int x = line.indexOf(StringPool.COMMA); 2013 2014 if (x != -1) { 2015 while ((previousLineLength + 1 + x) < 80) { 2016 String linePart = line.substring(0, x + 1); 2017 2018 if (isValidJavaParameter(linePart)) { 2019 if (line.equals(linePart)) { 2020 return new Tuple( 2021 previousLine + StringPool.SPACE + linePart); 2022 } 2023 else { 2024 return new Tuple( 2025 previousLine + StringPool.SPACE, 2026 linePart + StringPool.SPACE, true); 2027 } 2028 } 2029 2030 String partAfterComma = line.substring(x + 1); 2031 2032 int pos = partAfterComma.indexOf(StringPool.COMMA); 2033 2034 if (pos == -1) { 2035 break; 2036 } 2037 2038 x = x + pos + 1; 2039 } 2040 } 2041 else if (!line.endsWith(StringPool.OPEN_PARENTHESIS) && 2042 !line.endsWith(StringPool.PLUS) && 2043 !line.endsWith(StringPool.PERIOD) && 2044 (!line.startsWith("new ") || 2045 !line.endsWith(StringPool.OPEN_CURLY_BRACE)) && 2046 ((line.length() + previousLineLength) < 80)) { 2047 2048 return new Tuple(previousLine + StringPool.SPACE + line); 2049 } 2050 } 2051 2052 if (!previousLine.endsWith(StringPool.OPEN_PARENTHESIS)) { 2053 return null; 2054 } 2055 2056 if (StringUtil.count(previousLine, StringPool.OPEN_PARENTHESIS) > 1) { 2057 int pos = trimmedPreviousLine.lastIndexOf( 2058 StringPool.OPEN_PARENTHESIS, trimmedPreviousLine.length() - 2); 2059 2060 if ((pos > 0) && 2061 Character.isLetterOrDigit( 2062 trimmedPreviousLine.charAt(pos -1 ))) { 2063 2064 String filePart = trimmedPreviousLine.substring(pos + 1); 2065 2066 if (!filePart.contains(StringPool.CLOSE_PARENTHESIS) && 2067 !filePart.contains(StringPool.QUOTE)) { 2068 2069 return new Tuple(previousLine, filePart, false); 2070 } 2071 } 2072 } 2073 2074 if ((line.length() + previousLineLength) > 80) { 2075 return null; 2076 } 2077 2078 if (line.endsWith(StringPool.SEMICOLON)) { 2079 return new Tuple(previousLine + line); 2080 } 2081 2082 if (((line.endsWith(StringPool.OPEN_CURLY_BRACE) && 2083 !line.startsWith("new ")) || 2084 line.endsWith(StringPool.CLOSE_PARENTHESIS)) && 2085 (trimmedPreviousLine.startsWith("else ") || 2086 trimmedPreviousLine.startsWith("if ") || 2087 trimmedPreviousLine.startsWith("private ") || 2088 trimmedPreviousLine.startsWith("protected ") || 2089 trimmedPreviousLine.startsWith("public "))) { 2090 2091 return new Tuple(previousLine + line); 2092 } 2093 2094 return null; 2095 } 2096 2097 protected String getConstructorOrMethodName(String line, int pos) { 2098 line = line.substring(0, pos); 2099 2100 int x = line.lastIndexOf(StringPool.SPACE); 2101 2102 return line.substring(x + 1); 2103 } 2104 2105 protected List<String> getImportedExceptionClassNames( 2106 JavaDocBuilder javaDocBuilder) { 2107 2108 List<String> exceptionClassNames = new ArrayList<String>(); 2109 2110 JavaSource javaSource = javaDocBuilder.getSources()[0]; 2111 2112 for (String importClassName : javaSource.getImports()) { 2113 if (importClassName.endsWith("Exception") && 2114 !exceptionClassNames.contains(importClassName)) { 2115 2116 exceptionClassNames.add(importClassName); 2117 } 2118 } 2119 2120 return exceptionClassNames; 2121 } 2122 2123 protected Tuple getJavaTermTuple( 2124 String line, String content, int index, int numLines, int maxLines) { 2125 2126 int pos = line.indexOf(StringPool.OPEN_PARENTHESIS); 2127 2128 if (line.startsWith(StringPool.TAB + "public static final ") && 2129 (line.contains(StringPool.EQUAL) || 2130 (line.endsWith(StringPool.SEMICOLON) && (pos == -1)))) { 2131 2132 return new Tuple( 2133 getVariableName(line), TYPE_VARIABLE_PUBLIC_STATIC_FINAL); 2134 } 2135 else if (line.startsWith(StringPool.TAB + "public static ")) { 2136 if (line.startsWith(StringPool.TAB + "public static class ") || 2137 line.startsWith(StringPool.TAB + "public static enum") || 2138 line.startsWith(StringPool.TAB + "public static interface")) { 2139 2140 return new Tuple(getClassName(line), TYPE_CLASS_PUBLIC_STATIC); 2141 } 2142 2143 if (line.contains(StringPool.EQUAL) || 2144 (line.endsWith(StringPool.SEMICOLON) && (pos == -1))) { 2145 2146 return new Tuple( 2147 getVariableName(line), TYPE_VARIABLE_PUBLIC_STATIC); 2148 } 2149 2150 if (pos != -1) { 2151 return new Tuple( 2152 getConstructorOrMethodName(line, pos), 2153 TYPE_METHOD_PUBLIC_STATIC); 2154 } 2155 } 2156 else if (line.startsWith(StringPool.TAB + "public ")) { 2157 if (line.startsWith(StringPool.TAB + "public abstract class ") || 2158 line.startsWith(StringPool.TAB + "public class ") || 2159 line.startsWith(StringPool.TAB + "public enum ") || 2160 line.startsWith(StringPool.TAB + "public interface ")) { 2161 2162 return new Tuple(getClassName(line), TYPE_CLASS_PUBLIC); 2163 } 2164 2165 if (line.contains(StringPool.EQUAL) || 2166 (line.endsWith(StringPool.SEMICOLON) && (pos == -1))) { 2167 2168 return new Tuple(getVariableName(line), TYPE_VARIABLE_PUBLIC); 2169 } 2170 2171 if (pos != -1) { 2172 int spaceCount = StringUtil.count( 2173 line.substring(0, pos), StringPool.SPACE); 2174 2175 if (spaceCount == 1) { 2176 return new Tuple( 2177 getConstructorOrMethodName(line, pos), 2178 TYPE_CONSTRUCTOR_PUBLIC); 2179 } 2180 2181 if (spaceCount > 1) { 2182 return new Tuple( 2183 getConstructorOrMethodName(line, pos), 2184 TYPE_METHOD_PUBLIC); 2185 } 2186 } 2187 } 2188 else if (line.startsWith(StringPool.TAB + "protected static final ")) { 2189 if (line.contains(StringPool.EQUAL) || 2190 (line.endsWith(StringPool.SEMICOLON) && (pos == -1))) { 2191 2192 return new Tuple( 2193 getVariableName(line), 2194 TYPE_VARIABLE_PROTECTED_STATIC_FINAL); 2195 } 2196 } 2197 else if (line.startsWith(StringPool.TAB + "protected static ")) { 2198 if (line.startsWith(StringPool.TAB + "protected static class ") || 2199 line.startsWith(StringPool.TAB + "protected static enum ") || 2200 line.startsWith( 2201 StringPool.TAB + "protected static interface ")) { 2202 2203 return new Tuple( 2204 getClassName(line), TYPE_CLASS_PROTECTED_STATIC); 2205 } 2206 2207 if (line.contains(StringPool.EQUAL) || 2208 (line.endsWith(StringPool.SEMICOLON) && (pos == -1))) { 2209 2210 return new Tuple( 2211 getVariableName(line), TYPE_VARIABLE_PROTECTED_STATIC); 2212 } 2213 2214 if (pos != -1) { 2215 return new Tuple( 2216 getConstructorOrMethodName(line, pos), 2217 TYPE_METHOD_PROTECTED_STATIC); 2218 } 2219 } 2220 else if (line.startsWith(StringPool.TAB + "protected ")) { 2221 if (line.startsWith(StringPool.TAB + "protected abstract class ") || 2222 line.startsWith(StringPool.TAB + "protected class ") || 2223 line.startsWith(StringPool.TAB + "protected enum ") || 2224 line.startsWith(StringPool.TAB + "protected interface ")) { 2225 2226 return new Tuple(getClassName(line), TYPE_CLASS_PROTECTED); 2227 } 2228 2229 if (pos != -1) { 2230 if (!line.contains(StringPool.EQUAL)) { 2231 int spaceCount = StringUtil.count( 2232 line.substring(0, pos), StringPool.SPACE); 2233 2234 if (spaceCount == 1) { 2235 return new Tuple( 2236 getConstructorOrMethodName(line, pos), 2237 TYPE_CONSTRUCTOR_PROTECTED); 2238 } 2239 2240 if (spaceCount > 1) { 2241 return new Tuple( 2242 getConstructorOrMethodName(line, pos), 2243 TYPE_METHOD_PROTECTED); 2244 } 2245 } 2246 } 2247 2248 return new Tuple(getVariableName(line), TYPE_VARIABLE_PROTECTED); 2249 } 2250 else if (line.startsWith(StringPool.TAB + "private static final ")) { 2251 if (line.contains(StringPool.EQUAL) || 2252 (line.endsWith(StringPool.SEMICOLON) && (pos == -1))) { 2253 2254 return new Tuple( 2255 getVariableName(line), TYPE_VARIABLE_PRIVATE_STATIC_FINAL); 2256 } 2257 } 2258 else if (line.startsWith(StringPool.TAB + "private static ")) { 2259 if (line.startsWith(StringPool.TAB + "private static class ") || 2260 line.startsWith(StringPool.TAB + "private static enum ") || 2261 line.startsWith(StringPool.TAB + "private static interface ")) { 2262 2263 return new Tuple(getClassName(line), TYPE_CLASS_PRIVATE_STATIC); 2264 } 2265 2266 if (line.contains(StringPool.EQUAL) || 2267 (line.endsWith(StringPool.SEMICOLON) && (pos == -1))) { 2268 2269 return new Tuple( 2270 getVariableName(line), TYPE_VARIABLE_PRIVATE_STATIC); 2271 } 2272 2273 if (pos != -1) { 2274 return new Tuple( 2275 getConstructorOrMethodName(line, pos), 2276 TYPE_METHOD_PRIVATE_STATIC); 2277 } 2278 } 2279 else if (line.startsWith(StringPool.TAB + "private ")) { 2280 if (line.startsWith(StringPool.TAB + "private abstract class ") || 2281 line.startsWith(StringPool.TAB + "private class ") || 2282 line.startsWith(StringPool.TAB + "private enum ") || 2283 line.startsWith(StringPool.TAB + "private interface ")) { 2284 2285 return new Tuple(getClassName(line), TYPE_CLASS_PRIVATE); 2286 } 2287 2288 if (line.contains(StringPool.EQUAL) || 2289 (line.endsWith(StringPool.SEMICOLON) && (pos == -1))) { 2290 2291 return new Tuple(getVariableName(line), TYPE_VARIABLE_PRIVATE); 2292 } 2293 2294 if (pos != -1) { 2295 int spaceCount = StringUtil.count( 2296 line.substring(0, pos), StringPool.SPACE); 2297 2298 if (spaceCount == 1) { 2299 return new Tuple( 2300 getConstructorOrMethodName(line, pos), 2301 TYPE_CONSTRUCTOR_PRIVATE); 2302 } 2303 2304 if (spaceCount > 1) { 2305 return new Tuple( 2306 getConstructorOrMethodName(line, pos), 2307 TYPE_METHOD_PRIVATE); 2308 } 2309 } 2310 } 2311 2312 if (numLines < maxLines) { 2313 int posStartNextLine = 2314 content.indexOf(StringPool.NEW_LINE, index) + 1; 2315 2316 int posEndNextline = content.indexOf( 2317 StringPool.NEW_LINE, posStartNextLine); 2318 2319 String nextLine = content.substring( 2320 posStartNextLine, posEndNextline); 2321 2322 if (Validator.isNull(nextLine)) { 2323 return null; 2324 } 2325 2326 nextLine = StringUtil.trimLeading(nextLine); 2327 2328 return getJavaTermTuple( 2329 line + StringPool.SPACE + nextLine, content, posStartNextLine, 2330 numLines + 1, maxLines); 2331 } 2332 else { 2333 return null; 2334 } 2335 } 2336 2337 protected int getLeadingTabCount(String line) { 2338 int leadingTabCount = 0; 2339 2340 while (line.startsWith(StringPool.TAB)) { 2341 line = line.substring(1); 2342 2343 leadingTabCount++; 2344 } 2345 2346 return leadingTabCount; 2347 } 2348 2349 protected int getLineLength(String line) { 2350 int lineLength = 0; 2351 2352 int tabLength = 4; 2353 2354 for (char c : line.toCharArray()) { 2355 if (c == CharPool.TAB) { 2356 for (int i = 0; i < tabLength; i++) { 2357 lineLength++; 2358 } 2359 2360 tabLength = 4; 2361 } 2362 else { 2363 lineLength++; 2364 2365 tabLength--; 2366 2367 if (tabLength <= 0) { 2368 tabLength = 4; 2369 } 2370 } 2371 } 2372 2373 return lineLength; 2374 } 2375 2376 protected Collection<String> getPluginJavaFiles() { 2377 Collection<String> fileNames = new TreeSet<String>(); 2378 2379 String[] excludes = new String[] { 2380 "**\\bin\\**", "**\\model\\*Clp.java", 2381 "**\\model\\impl\\*BaseImpl.java", "**\\model\\impl\\*Model.java", 2382 "**\\model\\impl\\*ModelImpl.java", 2383 "**\\service\\**\\service\\*Service.java", 2384 "**\\service\\**\\service\\*ServiceClp.java", 2385 "**\\service\\**\\service\\*ServiceFactory.java", 2386 "**\\service\\**\\service\\*ServiceUtil.java", 2387 "**\\service\\**\\service\\*ServiceWrapper.java", 2388 "**\\service\\**\\service\\ClpSerializer.java", 2389 "**\\service\\**\\service\\messaging\\*ClpMessageListener.java", 2390 "**\\service\\**\\service\\persistence\\*Finder.java", 2391 "**\\service\\**\\service\\persistence\\*Util.java", 2392 "**\\service\\base\\*ServiceBaseImpl.java", 2393 "**\\service\\base\\*ServiceClpInvoker.java", 2394 "**\\service\\http\\*JSONSerializer.java", 2395 "**\\service\\http\\*ServiceHttp.java", 2396 "**\\service\\http\\*ServiceJSON.java", 2397 "**\\service\\http\\*ServiceSoap.java", "**\\tmp\\**" 2398 }; 2399 String[] includes = new String[] {"**\\*.java"}; 2400 2401 fileNames.addAll(getFileNames(excludes, includes)); 2402 2403 return fileNames; 2404 } 2405 2406 protected Collection<String> getPortalJavaFiles() { 2407 Collection<String> fileNames = new TreeSet<String>(); 2408 2409 String[] excludes = new String[] { 2410 "**\\*_IW.java", "**\\PropsValues.java", "**\\bin\\**", 2411 "**\\classes\\*", "**\\counter\\service\\**", "**\\jsp\\*", 2412 "**\\model\\impl\\*BaseImpl.java", "**\\model\\impl\\*Model.java", 2413 "**\\model\\impl\\*ModelImpl.java", "**\\portal\\service\\**", 2414 "**\\portal-client\\**", "**\\portal-web\\classes\\**\\*.java", 2415 "**\\portal-web\\test\\**\\*Test.java", 2416 "**\\portal-web\\test\\**\\*Tests.java", 2417 "**\\portlet\\**\\service\\**", "**\\test\\*-generated\\**", 2418 "**\\tmp\\**", "**\\tools\\tck\\**" 2419 }; 2420 String[] includes = new String[] {"**\\*.java"}; 2421 2422 fileNames.addAll(getFileNames(excludes, includes)); 2423 2424 excludes = new String[] { 2425 "**\\bin\\**", "**\\portal-client\\**", "**\\tools\\ext_tmpl\\**", 2426 "**\\*_IW.java", "**\\test\\**\\*PersistenceTest.java" 2427 }; 2428 includes = new String[] { 2429 "**\\com\\liferay\\portal\\service\\ServiceContext*.java", 2430 "**\\model\\BaseModel.java", "**\\model\\impl\\BaseModelImpl.java", 2431 "**\\service\\Base*.java", 2432 "**\\service\\PersistedModelLocalService*.java", 2433 "**\\service\\base\\PrincipalBean.java", 2434 "**\\service\\http\\*HttpTest.java", 2435 "**\\service\\http\\*SoapTest.java", 2436 "**\\service\\http\\TunnelUtil.java", "**\\service\\impl\\*.java", 2437 "**\\service\\jms\\*.java", "**\\service\\permission\\*.java", 2438 "**\\service\\persistence\\BasePersistence.java", 2439 "**\\service\\persistence\\BatchSession*.java", 2440 "**\\service\\persistence\\*FinderImpl.java", 2441 "**\\service\\persistence\\*Query.java", 2442 "**\\service\\persistence\\impl\\*.java", 2443 "**\\portal-impl\\test\\**\\*.java", 2444 "**\\portal-service\\**\\liferay\\documentlibrary\\**.java", 2445 "**\\portal-service\\**\\liferay\\lock\\**.java", 2446 "**\\portal-service\\**\\liferay\\mail\\**.java", 2447 "**\\util-bridges\\**\\*.java" 2448 }; 2449 2450 fileNames.addAll(getFileNames(excludes, includes)); 2451 2452 return fileNames; 2453 } 2454 2455 protected String getVariableName(String line) { 2456 int x = line.indexOf(StringPool.EQUAL); 2457 int y = line.lastIndexOf(StringPool.SPACE); 2458 2459 if (x != -1) { 2460 line = line.substring(0, x); 2461 line = StringUtil.trim(line); 2462 2463 y = line.lastIndexOf(StringPool.SPACE); 2464 2465 return line.substring(y + 1); 2466 } 2467 2468 if (line.endsWith(StringPool.SEMICOLON)) { 2469 return line.substring(y + 1, line.length() - 1); 2470 } 2471 2472 return StringPool.BLANK; 2473 } 2474 2475 protected boolean hasAnnotationCommentOrJavadoc(String s) { 2476 if (s.startsWith(StringPool.TAB + StringPool.AT) || 2477 s.startsWith(StringPool.TAB + "/**") || 2478 s.startsWith(StringPool.TAB + "//")) { 2479 2480 return true; 2481 } 2482 else { 2483 return false; 2484 } 2485 } 2486 2487 protected boolean isGenerated(String content) { 2488 if (content.contains("* @generated") || content.contains("$ANTLR")) { 2489 return true; 2490 } 2491 else { 2492 return false; 2493 } 2494 } 2495 2496 protected boolean isValidJavaParameter(String javaParameter) { 2497 int quoteCount = StringUtil.count(javaParameter, StringPool.QUOTE); 2498 2499 if ((quoteCount % 2) == 1) { 2500 return false; 2501 } 2502 2503 javaParameter = stripQuotes(javaParameter, CharPool.QUOTE); 2504 2505 int openParenthesisCount = StringUtil.count( 2506 javaParameter, StringPool.OPEN_PARENTHESIS); 2507 int closeParenthesisCount = StringUtil.count( 2508 javaParameter, StringPool.CLOSE_PARENTHESIS); 2509 int lessThanCount = StringUtil.count( 2510 javaParameter, StringPool.LESS_THAN); 2511 int greaterThanCount = StringUtil.count( 2512 javaParameter, StringPool.GREATER_THAN); 2513 int openCurlyBraceCount = StringUtil.count( 2514 javaParameter, StringPool.OPEN_CURLY_BRACE); 2515 int closeCurlyBraceCount = StringUtil.count( 2516 javaParameter, StringPool.CLOSE_CURLY_BRACE); 2517 2518 if ((openParenthesisCount == closeParenthesisCount) && 2519 (lessThanCount == greaterThanCount) && 2520 (openCurlyBraceCount == closeCurlyBraceCount)) { 2521 2522 return true; 2523 } 2524 2525 return false; 2526 } 2527 2528 protected String sortExceptions(String line) { 2529 if (!line.endsWith(StringPool.OPEN_CURLY_BRACE) && 2530 !line.endsWith(StringPool.SEMICOLON)) { 2531 2532 return line; 2533 } 2534 2535 int x = line.indexOf("throws "); 2536 2537 if (x == -1) { 2538 return line; 2539 } 2540 2541 String previousException = StringPool.BLANK; 2542 2543 String[] exceptions = StringUtil.split( 2544 line.substring(x), CharPool.SPACE); 2545 2546 for (int i = 1; i < exceptions.length; i++) { 2547 String exception = exceptions[i]; 2548 2549 if (exception.equals(StringPool.OPEN_CURLY_BRACE)) { 2550 break; 2551 } 2552 2553 if (exception.endsWith(StringPool.COMMA) || 2554 exception.endsWith(StringPool.SEMICOLON)) { 2555 2556 exception = exception.substring(0, exception.length() - 1); 2557 } 2558 2559 if (Validator.isNotNull(previousException) && 2560 (previousException.compareToIgnoreCase(exception) > 0)) { 2561 2562 return StringUtil.replace( 2563 line, previousException + ", " + exception, 2564 exception + ", " + previousException); 2565 } 2566 2567 previousException = exception; 2568 } 2569 2570 return line; 2571 } 2572 2573 protected String sortJavaTerms( 2574 String fileName, String content, Set<JavaTerm> javaTerms) { 2575 2576 JavaTerm previousJavaTerm = null; 2577 2578 Iterator<JavaTerm> itr = javaTerms.iterator(); 2579 2580 while (itr.hasNext()) { 2581 JavaTerm javaTerm = itr.next(); 2582 2583 if (previousJavaTerm == null) { 2584 previousJavaTerm = javaTerm; 2585 2586 continue; 2587 } 2588 2589 int javaTermLineCount = javaTerm.getLineCount(); 2590 String javaTermName = javaTerm.getName(); 2591 2592 String excluded = null; 2593 2594 if (_javaTermSortExclusions != null) { 2595 excluded = _javaTermSortExclusions.getProperty( 2596 fileName + StringPool.AT + javaTermLineCount); 2597 2598 if (excluded == null) { 2599 excluded = _javaTermSortExclusions.getProperty( 2600 fileName + StringPool.AT + javaTermName); 2601 } 2602 2603 if (excluded == null) { 2604 excluded = _javaTermSortExclusions.getProperty(fileName); 2605 } 2606 } 2607 2608 if (excluded != null) { 2609 previousJavaTerm = javaTerm; 2610 2611 continue; 2612 } 2613 2614 String javaTermContent = javaTerm.getContent(); 2615 String previousJavaTermContent = previousJavaTerm.getContent(); 2616 2617 if (previousJavaTerm.getLineCount() > javaTermLineCount) { 2618 String previousJavaTermName = previousJavaTerm.getName(); 2619 2620 String javaTermNameLowerCase = javaTermName.toLowerCase(); 2621 String previousJavaTermNameLowerCase = 2622 previousJavaTermName.toLowerCase(); 2623 2624 if (fileName.contains("persistence") && 2625 ((previousJavaTermName.startsWith("doCount") && 2626 javaTermName.startsWith("doCount")) || 2627 (previousJavaTermName.startsWith("doFind") && 2628 javaTermName.startsWith("doFind")) || 2629 (previousJavaTermNameLowerCase.startsWith("count") && 2630 javaTermNameLowerCase.startsWith("count")) || 2631 (previousJavaTermNameLowerCase.startsWith("filter") && 2632 javaTermNameLowerCase.startsWith("filter")) || 2633 (previousJavaTermNameLowerCase.startsWith("find") && 2634 javaTermNameLowerCase.startsWith("find")) || 2635 (previousJavaTermNameLowerCase.startsWith("join") && 2636 javaTermNameLowerCase.startsWith("join")))) { 2637 } 2638 else { 2639 content = StringUtil.replaceFirst( 2640 content, "\n" + javaTermContent, 2641 "\n" + previousJavaTermContent); 2642 content = StringUtil.replaceLast( 2643 content, "\n" + previousJavaTermContent, 2644 "\n" + javaTermContent); 2645 2646 return content; 2647 } 2648 } 2649 2650 previousJavaTerm = javaTerm; 2651 } 2652 2653 return content; 2654 } 2655 2656 private boolean _checkUnprocessedExceptions; 2657 private Properties _javaTermSortExclusions; 2658 private Properties _lineLengthExclusions; 2659 private Properties _staticLogVariableExclusions; 2660 private Properties _upgradeServiceUtilExclusions; 2661 2662 }