1 /*******************************************************************************
2 * Copyright (c) 2007, 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.corext.util;
13 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.jdt.core.IJavaElement;
16 import org.eclipse.jdt.core.IJavaProject;
17 import org.eclipse.jdt.core.JavaConventions;
18 import org.eclipse.jdt.core.JavaCore;
21 * Provides methods for checking Java-specific conventions such as name syntax.
23 * This is a wrapper for {@link JavaConventions} with more convenient
24 * <code>validate*(..)</code> methods. If multiple validations are planned on
25 * the same element, please use {@link JavaConventions} directly (with the
26 * arguments from {@link #getSourceComplianceLevels(IJavaElement)}).
29 public class JavaConventionsUtil {
32 * @param context an {@link IJavaElement} or <code>null</code>
33 * @return a <code>String[]</code> whose <code>[0]</code> is the
34 * {@link JavaCore#COMPILER_SOURCE} and whose <code>[1]</code> is
35 * the {@link JavaCore#COMPILER_COMPLIANCE} level at the given
36 * <code>context</code>.
38 public static String[] getSourceComplianceLevels(IJavaElement context) {
39 if (context != null) {
40 IJavaProject javaProject= context.getJavaProject();
41 if (javaProject != null) {
43 javaProject.getOption(JavaCore.COMPILER_SOURCE, true),
44 javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true)
49 JavaCore.getOption(JavaCore.COMPILER_SOURCE),
50 JavaCore.getOption(JavaCore.COMPILER_COMPLIANCE)
55 * @param name the name to validate
56 * @param context an {@link IJavaElement} or <code>null</code>
57 * @return validation status in <code>context</code>'s project or in the workspace
59 * @see JavaConventions#validateCompilationUnitName(String, String, String)
61 public static IStatus validateCompilationUnitName(String name, IJavaElement context) {
62 String[] sourceComplianceLevels= getSourceComplianceLevels(context);
63 return JavaConventions.validateCompilationUnitName(name, sourceComplianceLevels[0], sourceComplianceLevels[1]);
67 * @param name the name to validate
68 * @param context an {@link IJavaElement} or <code>null</code>
69 * @return validation status in <code>context</code>'s project or in the workspace
71 * @see JavaConventions#validateClassFileName(String, String, String)
73 public static IStatus validateClassFileName(String name, IJavaElement context) {
74 String[] sourceComplianceLevels= getSourceComplianceLevels(context);
75 return JavaConventions.validateClassFileName(name, sourceComplianceLevels[0], sourceComplianceLevels[1]);
79 * @param name the name to validate
80 * @param context an {@link IJavaElement} or <code>null</code>
81 * @return validation status in <code>context</code>'s project or in the workspace
83 * @see JavaConventions#validateFieldName(String, String, String)
85 public static IStatus validateFieldName(String name, IJavaElement context) {
86 String[] sourceComplianceLevels= getSourceComplianceLevels(context);
87 return JavaConventions.validateFieldName(name, sourceComplianceLevels[0], sourceComplianceLevels[1]);
91 * @param name the name to validate
92 * @param context an {@link IJavaElement} or <code>null</code>
93 * @return validation status in <code>context</code>'s project or in the workspace
95 * @see JavaConventions#validateIdentifier(String, String, String)
97 public static IStatus validateIdentifier(String name, IJavaElement context) {
98 String[] sourceComplianceLevels= getSourceComplianceLevels(context);
99 return JavaConventions.validateIdentifier(name, sourceComplianceLevels[0], sourceComplianceLevels[1]);
103 * @param name the name to validate
104 * @param context an {@link IJavaElement} or <code>null</code>
105 * @return validation status in <code>context</code>'s project or in the workspace
107 * @see JavaConventions#validateImportDeclaration(String, String, String)
109 public static IStatus validateImportDeclaration(String name, IJavaElement context) {
110 String[] sourceComplianceLevels= getSourceComplianceLevels(context);
111 return JavaConventions.validateImportDeclaration(name, sourceComplianceLevels[0], sourceComplianceLevels[1]);
115 * @param name the name to validate
116 * @param context an {@link IJavaElement} or <code>null</code>
117 * @return validation status in <code>context</code>'s project or in the workspace
119 * @see JavaConventions#validateJavaTypeName(String, String, String)
121 public static IStatus validateJavaTypeName(String name, IJavaElement context) {
122 String[] sourceComplianceLevels= getSourceComplianceLevels(context);
123 return JavaConventions.validateJavaTypeName(name, sourceComplianceLevels[0], sourceComplianceLevels[1]);
127 * @param name the name to validate
128 * @param context an {@link IJavaElement} or <code>null</code>
129 * @return validation status in <code>context</code>'s project or in the workspace
131 * @see JavaConventions#validateMethodName(String, String, String)
133 public static IStatus validateMethodName(String name, IJavaElement context) {
134 String[] sourceComplianceLevels= getSourceComplianceLevels(context);
135 return JavaConventions.validateMethodName(name, sourceComplianceLevels[0], sourceComplianceLevels[1]);
139 * @param name the name to validate
140 * @param context an {@link IJavaElement} or <code>null</code>
141 * @return validation status in <code>context</code>'s project or in the workspace
143 * @see JavaConventions#validatePackageName(String, String, String)
145 public static IStatus validatePackageName(String name, IJavaElement context) {
146 String[] sourceComplianceLevels= getSourceComplianceLevels(context);
147 return JavaConventions.validatePackageName(name, sourceComplianceLevels[0], sourceComplianceLevels[1]);
151 * @param name the name to validate
152 * @param context an {@link IJavaElement} or <code>null</code>
153 * @return validation status in <code>context</code>'s project or in the workspace
155 * @see JavaConventions#validateTypeVariableName(String, String, String)
157 public static IStatus validateTypeVariableName(String name, IJavaElement context) {
158 String[] sourceComplianceLevels= getSourceComplianceLevels(context);
159 return JavaConventions.validateTypeVariableName(name, sourceComplianceLevels[0], sourceComplianceLevels[1]);