]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/core extension/org/eclipse/jdt/internal/corext/util/JavaConventionsUtil.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / core extension / org / eclipse / jdt / internal / corext / util / JavaConventionsUtil.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.corext.util;
12
13 import org.eclipse.core.runtime.IStatus;
14
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;
19
20 /**
21  * Provides methods for checking Java-specific conventions such as name syntax.
22  * <p>
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)}).
27  * </p>
28  */
29 public class JavaConventionsUtil {
30
31         /**
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>.
37          */
38         public static String[] getSourceComplianceLevels(IJavaElement context) {
39                 if (context != null) {
40                         IJavaProject javaProject= context.getJavaProject();
41                         if (javaProject != null) {
42                                 return new String[] {
43                                                 javaProject.getOption(JavaCore.COMPILER_SOURCE, true),
44                                                 javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true)
45                                 };
46                         }
47                 }
48                 return new String[] {
49                                 JavaCore.getOption(JavaCore.COMPILER_SOURCE),
50                                 JavaCore.getOption(JavaCore.COMPILER_COMPLIANCE)
51                 };
52         }
53
54         /**
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
58          *
59          * @see JavaConventions#validateCompilationUnitName(String, String, String)
60          */
61         public static IStatus validateCompilationUnitName(String name, IJavaElement context) {
62                 String[] sourceComplianceLevels= getSourceComplianceLevels(context);
63                 return JavaConventions.validateCompilationUnitName(name, sourceComplianceLevels[0], sourceComplianceLevels[1]);
64         }
65
66         /**
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
70          *
71          * @see JavaConventions#validateClassFileName(String, String, String)
72          */
73         public static IStatus validateClassFileName(String name, IJavaElement context) {
74                 String[] sourceComplianceLevels= getSourceComplianceLevels(context);
75                 return JavaConventions.validateClassFileName(name, sourceComplianceLevels[0], sourceComplianceLevels[1]);
76         }
77
78         /**
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
82          *
83          * @see JavaConventions#validateFieldName(String, String, String)
84          */
85         public static IStatus validateFieldName(String name, IJavaElement context) {
86                 String[] sourceComplianceLevels= getSourceComplianceLevels(context);
87                 return JavaConventions.validateFieldName(name, sourceComplianceLevels[0], sourceComplianceLevels[1]);
88         }
89
90         /**
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
94          *
95          * @see JavaConventions#validateIdentifier(String, String, String)
96          */
97         public static IStatus validateIdentifier(String name, IJavaElement context) {
98                 String[] sourceComplianceLevels= getSourceComplianceLevels(context);
99                 return JavaConventions.validateIdentifier(name, sourceComplianceLevels[0], sourceComplianceLevels[1]);
100         }
101
102         /**
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
106          *
107          * @see JavaConventions#validateImportDeclaration(String, String, String)
108          */
109         public static IStatus validateImportDeclaration(String name, IJavaElement context) {
110                 String[] sourceComplianceLevels= getSourceComplianceLevels(context);
111                 return JavaConventions.validateImportDeclaration(name, sourceComplianceLevels[0], sourceComplianceLevels[1]);
112         }
113
114         /**
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
118          *
119          * @see JavaConventions#validateJavaTypeName(String, String, String)
120          */
121         public static IStatus validateJavaTypeName(String name, IJavaElement context) {
122                 String[] sourceComplianceLevels= getSourceComplianceLevels(context);
123                 return JavaConventions.validateJavaTypeName(name, sourceComplianceLevels[0], sourceComplianceLevels[1]);
124         }
125
126         /**
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
130          *
131          * @see JavaConventions#validateMethodName(String, String, String)
132          */
133         public static IStatus validateMethodName(String name, IJavaElement context) {
134                 String[] sourceComplianceLevels= getSourceComplianceLevels(context);
135                 return JavaConventions.validateMethodName(name, sourceComplianceLevels[0], sourceComplianceLevels[1]);
136         }
137
138         /**
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
142          *
143          * @see JavaConventions#validatePackageName(String, String, String)
144          */
145         public static IStatus validatePackageName(String name, IJavaElement context) {
146                 String[] sourceComplianceLevels= getSourceComplianceLevels(context);
147                 return JavaConventions.validatePackageName(name, sourceComplianceLevels[0], sourceComplianceLevels[1]);
148         }
149
150         /**
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
154          *
155          * @see JavaConventions#validateTypeVariableName(String, String, String)
156          */
157         public static IStatus validateTypeVariableName(String name, IJavaElement context) {
158                 String[] sourceComplianceLevels= getSourceComplianceLevels(context);
159                 return JavaConventions.validateTypeVariableName(name, sourceComplianceLevels[0], sourceComplianceLevels[1]);
160         }
161 }