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