1 11 package org.eclipse.jdt.core; 12 13 import java.util.StringTokenizer ; 14 15 import org.eclipse.core.resources.IResource; 16 import org.eclipse.core.resources.IWorkspace; 17 import org.eclipse.core.resources.ResourcesPlugin; 18 import org.eclipse.core.runtime.IPath; 19 import org.eclipse.core.runtime.IStatus; 20 import org.eclipse.core.runtime.Status; 21 import org.eclipse.jdt.core.compiler.*; 22 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; 23 import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; 24 import org.eclipse.jdt.internal.compiler.lookup.TypeConstants; 25 import org.eclipse.jdt.internal.compiler.parser.Scanner; 26 import org.eclipse.jdt.internal.compiler.parser.ScannerHelper; 27 import org.eclipse.jdt.internal.compiler.parser.TerminalTokens; 28 import org.eclipse.jdt.internal.compiler.util.SuffixConstants; 29 import org.eclipse.jdt.internal.core.*; 30 import org.eclipse.jdt.internal.core.util.Messages; 31 32 39 public final class JavaConventions { 40 41 private static final char DOT= '.'; 42 private static final String PACKAGE_INFO = new String (TypeConstants.PACKAGE_INFO_NAME); 43 private static final Scanner SCANNER = new Scanner(false , true , false , ClassFileConstants.JDK1_3 , null, null, true ); 44 45 private JavaConventions() { 46 } 48 49 61 public static boolean isOverlappingRoots(IPath rootPath1, IPath rootPath2) { 62 if (rootPath1 == null || rootPath2 == null) { 63 return false; 64 } 65 String extension1 = rootPath1.getFileExtension(); 66 String extension2 = rootPath2.getFileExtension(); 67 if (extension1 != null && (extension1.equalsIgnoreCase(SuffixConstants.EXTENSION_JAR) || extension1.equalsIgnoreCase(SuffixConstants.EXTENSION_ZIP))) { 68 return false; 69 } 70 if (extension2 != null && (extension2.equalsIgnoreCase(SuffixConstants.EXTENSION_JAR) || extension2.equalsIgnoreCase(SuffixConstants.EXTENSION_ZIP))) { 71 return false; 72 } 73 return rootPath1.isPrefixOf(rootPath2) || rootPath2.isPrefixOf(rootPath1); 74 } 75 76 81 private static synchronized char[] scannedIdentifier(String id, String sourceLevel, String complianceLevel) { 82 if (id == null) { 83 return null; 84 } 85 SCANNER.sourceLevel = sourceLevel == null ? ClassFileConstants.JDK1_3 : CompilerOptions.versionToJdkLevel(sourceLevel); 87 SCANNER.complianceLevel = complianceLevel == null ? ClassFileConstants.JDK1_3 : CompilerOptions.versionToJdkLevel(complianceLevel); 88 89 try { 90 SCANNER.setSource(id.toCharArray()); 91 int token = SCANNER.scanIdentifier(); 92 if (token != TerminalTokens.TokenNameIdentifier) return null; 93 if (SCANNER.currentPosition == SCANNER.eofPosition) { try { 95 return SCANNER.getCurrentIdentifierSource(); 96 } catch (ArrayIndexOutOfBoundsException e) { 97 return null; 98 } 99 } else { 100 return null; 101 } 102 } 103 catch (InvalidInputException e) { 104 return null; 105 } 106 } 107 108 127 public static IStatus validateCompilationUnitName(String name) { 128 return validateCompilationUnitName(name,CompilerOptions.VERSION_1_3,CompilerOptions.VERSION_1_3); 129 } 130 131 152 public static IStatus validateCompilationUnitName(String name, String sourceLevel, String complianceLevel) { 153 if (name == null) { 154 return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Messages.convention_unit_nullName, null); 155 } 156 if (!org.eclipse.jdt.internal.core.util.Util.isJavaLikeFileName(name)) { 157 return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Messages.convention_unit_notJavaName, null); 158 } 159 String identifier; 160 int index; 161 index = name.lastIndexOf('.'); 162 if (index == -1) { 163 return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Messages.convention_unit_notJavaName, null); 164 } 165 identifier = name.substring(0, index); 166 if (!identifier.equals(PACKAGE_INFO)) { 170 IStatus status = validateIdentifier(identifier, sourceLevel, complianceLevel); 171 if (!status.isOK()) { 172 return status; 173 } 174 } 175 IStatus status = ResourcesPlugin.getWorkspace().validateName(name, IResource.FILE); 176 if (!status.isOK()) { 177 return status; 178 } 179 return JavaModelStatus.VERIFIED_OK; 180 } 181 182 201 public static IStatus validateClassFileName(String name) { 202 return validateClassFileName(name, CompilerOptions.VERSION_1_3, CompilerOptions.VERSION_1_3); 203 } 204 205 225 public static IStatus validateClassFileName(String name, String sourceLevel, String complianceLevel) { 226 if (name == null) { 227 return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Messages.convention_classFile_nullName, null); } 228 if (!org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(name)) { 229 return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Messages.convention_classFile_notClassFileName, null); 230 } 231 String identifier; 232 int index; 233 index = name.lastIndexOf('.'); 234 if (index == -1) { 235 return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Messages.convention_classFile_notClassFileName, null); 236 } 237 identifier = name.substring(0, index); 238 if (!identifier.equals(PACKAGE_INFO)) { 242 IStatus status = validateIdentifier(identifier, sourceLevel, complianceLevel); 243 if (!status.isOK()) { 244 return status; 245 } 246 } 247 IStatus status = ResourcesPlugin.getWorkspace().validateName(name, IResource.FILE); 248 if (!status.isOK()) { 249 return status; 250 } 251 return JavaModelStatus.VERIFIED_OK; 252 } 253 254 266 public static IStatus validateFieldName(String name) { 267 return validateIdentifier(name, CompilerOptions.VERSION_1_3,CompilerOptions.VERSION_1_3); 268 } 269 270 284 public static IStatus validateFieldName(String name, String sourceLevel, String complianceLevel) { 285 return validateIdentifier(name, sourceLevel, complianceLevel); 286 } 287 288 301 public static IStatus validateIdentifier(String id) { 302 return validateIdentifier(id,CompilerOptions.VERSION_1_3,CompilerOptions.VERSION_1_3); 303 } 304 305 320 public static IStatus validateIdentifier(String id, String sourceLevel, String complianceLevel) { 321 if (scannedIdentifier(id, sourceLevel, complianceLevel) != null) { 322 return JavaModelStatus.VERIFIED_OK; 323 } else { 324 return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Messages.bind(Messages.convention_illegalIdentifier, id), null); 325 } 326 } 327 328 341 public static IStatus validateImportDeclaration(String name) { 342 return validateImportDeclaration(name,CompilerOptions.VERSION_1_3,CompilerOptions.VERSION_1_3); 343 } 344 345 360 public static IStatus validateImportDeclaration(String name, String sourceLevel, String complianceLevel) { 361 if (name == null || name.length() == 0) { 362 return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Messages.convention_import_nullImport, null); 363 } 364 if (name.charAt(name.length() - 1) == '*') { 365 if (name.charAt(name.length() - 2) == '.') { 366 return validatePackageName(name.substring(0, name.length() - 2), sourceLevel, complianceLevel); 367 } else { 368 return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Messages.convention_import_unqualifiedImport, null); 369 } 370 } 371 return validatePackageName(name, sourceLevel, complianceLevel); 372 } 373 374 388 public static IStatus validateJavaTypeName(String name) { 389 return validateJavaTypeName(name, CompilerOptions.VERSION_1_3,CompilerOptions.VERSION_1_3); 390 } 391 392 408 public static IStatus validateJavaTypeName(String name, String sourceLevel, String complianceLevel) { 409 if (name == null) { 410 return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Messages.convention_type_nullName, null); 411 } 412 String trimmed = name.trim(); 413 if (!name.equals(trimmed)) { 414 return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Messages.convention_type_nameWithBlanks, null); 415 } 416 int index = name.lastIndexOf('.'); 417 char[] scannedID; 418 if (index == -1) { 419 scannedID = scannedIdentifier(name, sourceLevel, complianceLevel); 421 } else { 422 String pkg = name.substring(0, index).trim(); 424 IStatus status = validatePackageName(pkg, sourceLevel, complianceLevel); 425 if (!status.isOK()) { 426 return status; 427 } 428 String type = name.substring(index + 1).trim(); 429 scannedID = scannedIdentifier(type, sourceLevel, complianceLevel); 430 } 431 432 if (scannedID != null) { 433 IStatus status = ResourcesPlugin.getWorkspace().validateName(new String (scannedID), IResource.FILE); 434 if (!status.isOK()) { 435 return status; 436 } 437 if (CharOperation.contains('$', scannedID)) { 438 return new Status(IStatus.WARNING, JavaCore.PLUGIN_ID, -1, Messages.convention_type_dollarName, null); 439 } 440 if ((scannedID.length > 0 && ScannerHelper.isLowerCase(scannedID[0]))) { 441 return new Status(IStatus.WARNING, JavaCore.PLUGIN_ID, -1, Messages.convention_type_lowercaseName, null); 442 } 443 return JavaModelStatus.VERIFIED_OK; 444 } else { 445 return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Messages.bind(Messages.convention_type_invalidName, name), null); 446 } 447 } 448 449 462 public static IStatus validateMethodName(String name) { 463 return validateMethodName(name, CompilerOptions.VERSION_1_3,CompilerOptions.VERSION_1_3); 464 } 465 466 481 public static IStatus validateMethodName(String name, String sourceLevel, String complianceLevel) { 482 return validateIdentifier(name, sourceLevel,complianceLevel); 483 } 484 485 502 public static IStatus validatePackageName(String name) { 503 return validatePackageName(name, CompilerOptions.VERSION_1_3,CompilerOptions.VERSION_1_3); 504 } 505 506 525 public static IStatus validatePackageName(String name, String sourceLevel, String complianceLevel) { 526 527 if (name == null) { 528 return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Messages.convention_package_nullName, null); 529 } 530 int length; 531 if ((length = name.length()) == 0) { 532 return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Messages.convention_package_emptyName, null); 533 } 534 if (name.charAt(0) == DOT || name.charAt(length-1) == DOT) { 535 return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Messages.convention_package_dotName, null); 536 } 537 if (CharOperation.isWhitespace(name.charAt(0)) || CharOperation.isWhitespace(name.charAt(name.length() - 1))) { 538 return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Messages.convention_package_nameWithBlanks, null); 539 } 540 int dot = 0; 541 while (dot != -1 && dot < length-1) { 542 if ((dot = name.indexOf(DOT, dot+1)) != -1 && dot < length-1 && name.charAt(dot+1) == DOT) { 543 return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Messages.convention_package_consecutiveDotsName, null); 544 } 545 } 546 IWorkspace workspace = ResourcesPlugin.getWorkspace(); 547 StringTokenizer st = new StringTokenizer (name, "."); boolean firstToken = true; 549 IStatus warningStatus = null; 550 while (st.hasMoreTokens()) { 551 String typeName = st.nextToken(); 552 typeName = typeName.trim(); char[] scannedID = scannedIdentifier(typeName, sourceLevel, complianceLevel); 554 if (scannedID == null) { 555 return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Messages.bind(Messages.convention_illegalIdentifier, typeName), null); 556 } 557 IStatus status = workspace.validateName(new String (scannedID), IResource.FOLDER); 558 if (!status.isOK()) { 559 return status; 560 } 561 if (firstToken && scannedID.length > 0 && ScannerHelper.isUpperCase(scannedID[0])) { 562 if (warningStatus == null) { 563 warningStatus = new Status(IStatus.WARNING, JavaCore.PLUGIN_ID, -1, Messages.convention_package_uppercaseName, null); 564 } 565 } 566 firstToken = false; 567 } 568 if (warningStatus != null) { 569 return warningStatus; 570 } 571 return JavaModelStatus.VERIFIED_OK; 572 } 573 574 605 public static IJavaModelStatus validateClasspath(IJavaProject javaProject, IClasspathEntry[] rawClasspath, IPath projectOutputLocation) { 606 607 return ClasspathEntry.validateClasspath(javaProject, rawClasspath, projectOutputLocation); 608 } 609 610 621 public static IJavaModelStatus validateClasspathEntry(IJavaProject project, IClasspathEntry entry, boolean checkSourceAttachment){ 622 IJavaModelStatus status = ClasspathEntry.validateClasspathEntry(project, entry, checkSourceAttachment, true); 623 if (status.getCode() == IJavaModelStatusConstants.INVALID_CLASSPATH && ((ClasspathEntry) entry).isOptional()) 624 return JavaModelStatus.VERIFIED_OK; 625 return status; 626 } 627 628 641 public static IStatus validateTypeVariableName(String name) { 642 return validateIdentifier(name, CompilerOptions.VERSION_1_3,CompilerOptions.VERSION_1_3); 643 } 644 645 659 public static IStatus validateTypeVariableName(String name, String sourceLevel, String complianceLevel) { 660 return validateIdentifier(name, sourceLevel, complianceLevel); 661 } 662 663 675 680 681 691 817 } 818 | Popular Tags |