1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.core;
13 import java.util.StringTokenizer;
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 net.sourceforge.phpdt.core.compiler.*;
22 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
23 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
24 import net.sourceforge.phpdt.internal.compiler.util.SuffixConstants;
25 import net.sourceforge.phpdt.internal.core.*;
26 import net.sourceforge.phpdt.internal.core.JavaModelStatus;
27 import net.sourceforge.phpdt.internal.core.util.Util;
30 * Provides methods for checking Java-specific conventions such as name syntax.
32 * This class provides static methods and constants only; it is not intended to be
33 * instantiated or subclassed by clients.
36 public final class JavaConventions {
38 private final static char DOT= '.';
39 private final static Scanner SCANNER = new Scanner();
41 private JavaConventions() {
46 * Returns whether the given package fragment root paths are considered
49 * Two root paths overlap if one is a prefix of the other, or they point to
50 * the same location. However, a JAR is allowed to be nested in a root.
52 * @param rootPath1 the first root path
53 * @param rootPath2 the second root path
54 * @return true if the given package fragment root paths are considered to overlap, false otherwise
55 * @deprecated Overlapping roots are allowed in 2.1
57 public static boolean isOverlappingRoots(IPath rootPath1, IPath rootPath2) {
58 if (rootPath1 == null || rootPath2 == null) {
61 // String extension1 = rootPath1.getFileExtension();
62 // String extension2 = rootPath2.getFileExtension();
63 // if (extension1 != null && (extension1.equalsIgnoreCase(SuffixConstants.EXTENSION_JAR) || extension1.equalsIgnoreCase(SuffixConstants.EXTENSION_ZIP))) {
66 // if (extension2 != null && (extension2.equalsIgnoreCase(SuffixConstants.EXTENSION_JAR) || extension2.equalsIgnoreCase(SuffixConstants.EXTENSION_ZIP))) {
69 return rootPath1.isPrefixOf(rootPath2) || rootPath2.isPrefixOf(rootPath1);
73 * Returns the current identifier extracted by the scanner (without unicode
74 * escapes) from the given id.
75 * Returns <code>null</code> if the id was not valid
77 private static synchronized char[] scannedIdentifier(String id) {
81 String trimmed = id.trim();
82 if (!trimmed.equals(id)) {
86 SCANNER.setSource(id.toCharArray());
87 int token = SCANNER.getNextToken();
88 char[] currentIdentifier;
90 currentIdentifier = SCANNER.getCurrentIdentifierSource();
91 } catch (ArrayIndexOutOfBoundsException e) {
94 int nextToken= SCANNER.getNextToken();
95 if (token == ITerminalSymbols.TokenNameIdentifier
96 && nextToken == ITerminalSymbols.TokenNameEOF
97 && SCANNER.startPosition == SCANNER.source.length) { // to handle case where we had an ArrayIndexOutOfBoundsException
98 // while reading the last token
99 return currentIdentifier;
104 catch (InvalidInputException e) {
110 * Validate the given compilation unit name.
111 * A compilation unit name must obey the following rules:
113 * <li> it must not be null
114 * <li> it must include the <code>".java"</code> suffix
115 * <li> its prefix must be a valid identifier
116 * <li> it must not contain any characters or substrings that are not valid
117 * on the file system on which workspace root is located.
120 * @param name the name of a compilation unit
121 * @return a status object with code <code>IStatus.OK</code> if
122 * the given name is valid as a compilation unit name, otherwise a status
123 * object indicating what is wrong with the name
125 public static IStatus validateCompilationUnitName(String name) {
127 return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Util.bind("convention.unit.nullName"), null); //$NON-NLS-1$
129 if (!net.sourceforge.phpdt.internal.compiler.util.Util.isJavaFileName(name)) {
130 return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Util.bind("convention.unit.notJavaName"), null); //$NON-NLS-1$
134 index = name.lastIndexOf('.');
136 return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Util.bind("convention.unit.notJavaName"), null); //$NON-NLS-1$
138 identifier = name.substring(0, index);
139 // JSR-175 metadata strongly recommends "package-info.java" as the
140 // file in which to store package annotations and
141 // the package-level spec (replaces package.html)
142 if (!identifier.equals("package-info")) { //$NON-NLS-1$
143 IStatus status = validateIdentifier(identifier);
144 if (!status.isOK()) {
148 IStatus status = ResourcesPlugin.getWorkspace().validateName(name, IResource.FILE);
149 if (!status.isOK()) {
152 return JavaModelStatus.VERIFIED_OK;
156 * Validate the given .class file name.
157 * A .class file name must obey the following rules:
159 * <li> it must not be null
160 * <li> it must include the <code>".class"</code> suffix
161 * <li> its prefix must be a valid identifier
162 * <li> it must not contain any characters or substrings that are not valid
163 * on the file system on which workspace root is located.
166 * @param name the name of a .class file
167 * @return a status object with code <code>IStatus.OK</code> if
168 * the given name is valid as a .class file name, otherwise a status
169 * object indicating what is wrong with the name
172 // public static IStatus validateClassFileName(String name) {
173 // if (name == null) {
174 // return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Util.bind("convention.classFile.nullName"), null); //$NON-NLS-1$
176 // if (!net.sourceforge.phpdt.internal.compiler.util.Util.isClassFileName(name)) {
177 // return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Util.bind("convention.classFile.notClassFileName"), null); //$NON-NLS-1$
179 // String identifier;
181 // index = name.lastIndexOf('.');
182 // if (index == -1) {
183 // return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Util.bind("convention.classFile.notClassFileName"), null); //$NON-NLS-1$
185 // identifier = name.substring(0, index);
186 // IStatus status = validateIdentifier(identifier);
187 // if (!status.isOK()) {
190 // status = ResourcesPlugin.getWorkspace().validateName(name, IResource.FILE);
191 // if (!status.isOK()) {
194 // return JavaModelStatus.VERIFIED_OK;
198 * Validate the given field name.
200 * Syntax of a field name corresponds to VariableDeclaratorId (JLS2 8.3).
201 * For example, <code>"x"</code>.
203 * @param name the name of a field
204 * @return a status object with code <code>IStatus.OK</code> if
205 * the given name is valid as a field name, otherwise a status
206 * object indicating what is wrong with the name
208 public static IStatus validateFieldName(String name) {
209 return validateIdentifier(name);
213 * Validate the given Java identifier.
214 * The identifier must not have the same spelling as a Java keyword,
215 * boolean literal (<code>"true"</code>, <code>"false"</code>), or null literal (<code>"null"</code>).
216 * See section 3.8 of the <em>Java Language Specification, Second Edition</em> (JLS2).
217 * A valid identifier can act as a simple type name, method name or field name.
219 * @param id the Java identifier
220 * @return a status object with code <code>IStatus.OK</code> if
221 * the given identifier is a valid Java identifier, otherwise a status
222 * object indicating what is wrong with the identifier
224 public static IStatus validateIdentifier(String id) {
225 if (scannedIdentifier(id) != null) {
226 return JavaModelStatus.VERIFIED_OK;
228 return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Util.bind("convention.illegalIdentifier", id), null); //$NON-NLS-1$
233 * Validate the given import declaration name.
235 * The name of an import corresponds to a fully qualified type name
236 * or an on-demand package name as defined by ImportDeclaration (JLS2 7.5).
237 * For example, <code>"java.util.*"</code> or <code>"java.util.Hashtable"</code>.
239 * @param name the import declaration
240 * @return a status object with code <code>IStatus.OK</code> if
241 * the given name is valid as an import declaration, otherwise a status
242 * object indicating what is wrong with the name
244 public static IStatus validateImportDeclaration(String name) {
245 if (name == null || name.length() == 0) {
246 return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Util.bind("convention.import.nullImport"), null); //$NON-NLS-1$
248 if (name.charAt(name.length() - 1) == '*') {
249 if (name.charAt(name.length() - 2) == '.') {
250 return validatePackageName(name.substring(0, name.length() - 2));
252 return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Util.bind("convention.import.unqualifiedImport"), null); //$NON-NLS-1$
255 return validatePackageName(name);
259 * Validate the given Java type name, either simple or qualified.
260 * For example, <code>"java.lang.Object"</code>, or <code>"Object"</code>.
263 * @param name the name of a type
264 * @return a status object with code <code>IStatus.OK</code> if
265 * the given name is valid as a Java type name,
266 * a status with code <code>IStatus.WARNING</code>
267 * indicating why the given name is discouraged,
268 * otherwise a status object indicating what is wrong with
271 public static IStatus validateJavaTypeName(String name) {
273 return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Util.bind("convention.type.nullName"), null); //$NON-NLS-1$
275 String trimmed = name.trim();
276 if (!name.equals(trimmed)) {
277 return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Util.bind("convention.type.nameWithBlanks"), null); //$NON-NLS-1$
279 int index = name.lastIndexOf('.');
283 scannedID = scannedIdentifier(name);
286 String pkg = name.substring(0, index).trim();
287 IStatus status = validatePackageName(pkg);
288 if (!status.isOK()) {
291 String type = name.substring(index + 1).trim();
292 scannedID = scannedIdentifier(type);
295 if (scannedID != null) {
296 IStatus status = ResourcesPlugin.getWorkspace().validateName(new String(scannedID), IResource.FILE);
297 if (!status.isOK()) {
300 if (CharOperation.contains('$', scannedID)) {
301 return new Status(IStatus.WARNING, JavaCore.PLUGIN_ID, -1, Util.bind("convention.type.dollarName"), null); //$NON-NLS-1$
303 if ((scannedID.length > 0 && Character.isLowerCase(scannedID[0]))) {
304 return new Status(IStatus.WARNING, JavaCore.PLUGIN_ID, -1, Util.bind("convention.type.lowercaseName"), null); //$NON-NLS-1$
306 return JavaModelStatus.VERIFIED_OK;
308 return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Util.bind("convention.type.invalidName", name), null); //$NON-NLS-1$
313 * Validate the given method name.
314 * The special names "<init>" and "<clinit>" are not valid.
316 * The syntax for a method name is defined by Identifier
317 * of MethodDeclarator (JLS2 8.4). For example "println".
319 * @param name the name of a method
320 * @return a status object with code <code>IStatus.OK</code> if
321 * the given name is valid as a method name, otherwise a status
322 * object indicating what is wrong with the name
324 public static IStatus validateMethodName(String name) {
326 return validateIdentifier(name);
330 * Validate the given package name.
332 * The syntax of a package name corresponds to PackageName as
333 * defined by PackageDeclaration (JLS2 7.4). For example, <code>"java.lang"</code>.
335 * Note that the given name must be a non-empty package name (that is, attempting to
336 * validate the default package will return an error status.)
337 * Also it must not contain any characters or substrings that are not valid
338 * on the file system on which workspace root is located.
340 * @param name the name of a package
341 * @return a status object with code <code>IStatus.OK</code> if
342 * the given name is valid as a package name, otherwise a status
343 * object indicating what is wrong with the name
345 public static IStatus validatePackageName(String name) {
348 return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Util.bind("convention.package.nullName"), null); //$NON-NLS-1$
351 if ((length = name.length()) == 0) {
352 return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Util.bind("convention.package.emptyName"), null); //$NON-NLS-1$
354 if (name.charAt(0) == DOT || name.charAt(length-1) == DOT) {
355 return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Util.bind("convention.package.dotName"), null); //$NON-NLS-1$
357 if (CharOperation.isWhitespace(name.charAt(0)) || CharOperation.isWhitespace(name.charAt(name.length() - 1))) {
358 return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Util.bind("convention.package.nameWithBlanks"), null); //$NON-NLS-1$
361 while (dot != -1 && dot < length-1) {
362 if ((dot = name.indexOf(DOT, dot+1)) != -1 && dot < length-1 && name.charAt(dot+1) == DOT) {
363 return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Util.bind("convention.package.consecutiveDotsName"), null); //$NON-NLS-1$
366 IWorkspace workspace = ResourcesPlugin.getWorkspace();
367 StringTokenizer st = new StringTokenizer(name, new String(new char[] {DOT}));
368 boolean firstToken = true;
369 IStatus warningStatus = null;
370 while (st.hasMoreTokens()) {
371 String typeName = st.nextToken();
372 typeName = typeName.trim(); // grammar allows spaces
373 char[] scannedID = scannedIdentifier(typeName);
374 if (scannedID == null) {
375 return new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, -1, Util.bind("convention.illegalIdentifier", typeName), null); //$NON-NLS-1$
377 IStatus status = workspace.validateName(new String(scannedID), IResource.FOLDER);
378 if (!status.isOK()) {
381 if (firstToken && scannedID.length > 0 && Character.isUpperCase(scannedID[0])) {
382 if (warningStatus == null) {
383 warningStatus = new Status(IStatus.WARNING, JavaCore.PLUGIN_ID, -1, Util.bind("convention.package.uppercaseName"), null); //$NON-NLS-1$
388 if (warningStatus != null) {
389 return warningStatus;
391 return JavaModelStatus.VERIFIED_OK;
395 * Validate a given classpath and output location for a project, using the following rules:
397 * <li> Classpath entries cannot collide with each other; that is, all entry paths must be unique.
398 * <li> The project output location path cannot be null, must be absolute and located inside the project.
399 * <li> Specific output locations (specified on source entries) can be null, if not they must be located inside the project,
400 * <li> A project entry cannot refer to itself directly (that is, a project cannot prerequisite itself).
401 * <li> Classpath entries or output locations cannot coincidate or be nested in each other, except for the following scenarii listed below:
402 * <ul><li> A source folder can coincidate with its own output location, in which case this output can then contain library archives.
403 * However, a specific output location cannot coincidate with any library or a distinct source folder than the one referring to it. </li>
404 * <li> A source/library folder can be nested in any source folder as long as the nested folder is excluded from the enclosing one. </li>
405 * <li> An output location can be nested in a source folder, if the source folder coincidates with the project itself, or if the output
406 * location is excluded from the source folder.
410 * Note that the classpath entries are not validated automatically. Only bound variables or containers are considered
411 * in the checking process (this allows to perform a consistency check on a classpath which has references to
412 * yet non existing projects, folders, ...).
414 * This validation is intended to anticipate classpath issues prior to assigning it to a project. In particular, it will automatically
415 * be performed during the classpath setting operation (if validation fails, the classpath setting will not complete).
417 * @param javaProject the given java project
418 * @param rawClasspath the given classpath
419 * @param projectOutputLocation the given output location
420 * @return a status object with code <code>IStatus.OK</code> if
421 * the given classpath and output location are compatible, otherwise a status
422 * object indicating what is wrong with the classpath or output location
425 public static IJavaModelStatus validateClasspath(IJavaProject javaProject, IClasspathEntry[] rawClasspath, IPath projectOutputLocation) {
427 return ClasspathEntry.validateClasspath(javaProject, rawClasspath, projectOutputLocation);
431 * Returns a Java model status describing the problem related to this classpath entry if any,
432 * a status object with code <code>IStatus.OK</code> if the entry is fine (that is, if the
433 * given classpath entry denotes a valid element to be referenced onto a classpath).
435 * @param project the given java project
436 * @param entry the given classpath entry
437 * @param checkSourceAttachment a flag to determine if source attachement should be checked
438 * @return a java model status describing the problem related to this classpath entry if any, a status object with code <code>IStatus.OK</code> if the entry is fine
441 public static IJavaModelStatus validateClasspathEntry(IJavaProject project, IClasspathEntry entry, boolean checkSourceAttachment){
442 return ClasspathEntry.validateClasspathEntry(project, entry, checkSourceAttachment, true/*recurse in container*/);