Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: f883dd34c9124838f988f9049134d1119e49d5ec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.jst.ws.internal.common;

import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jem.util.emf.workbench.nature.EMFNature;
import org.eclipse.jem.workbench.utility.JemProjectUtilities;
import org.eclipse.jem.java.JavaClass;
import org.eclipse.jem.java.JavaRefFactory;
import org.eclipse.jem.java.JavaVisibilityKind;
import org.eclipse.jem.java.Method;

/**
* This class contains some useful utilities for dealing with the JavaMOF
*/

public final class JavaMOFUtils
{
 /**
  * Determine whether this class of the given name is loadable in the given
  * project.
  * @param className The fully qualified name of the class
  * @param project The project
  * @return True if the class is loadable and no exceptions are thrown. 
  */
  public static boolean isClassLoadable(String className, IProject project) throws CoreException
  {
   	return isClassLoadable(getJavaClass(className,project));
  }

  public static boolean isClassLoadable(JavaClass javaClass)
  {
	return javaClass.isExistingType();    
  }

  /**
  * Determine whether the specified class has a public constructor
  * with an empty parameter list.
  * @param className The fully qualified name of the class
  * @param project The project
  * @return True if the class is loadable from the project and the 
  *         class has a public constructor with an empty parameter list  
  */
  public static boolean hasPublicDefaultCtor(String className, IProject project) throws CoreException
  {
    return hasPublicDefaultCtor(getJavaClass(className, project));
  }


 public static boolean hasPublicDefaultCtor(JavaClass javaClass)
  {
   
    List methodList = javaClass.getMethods();
    if (methodList==null)
      return true;

    Iterator iMethods = methodList.iterator();
    boolean userDefinedCtor = false;
    while (iMethods.hasNext())
    {
      Method thisMethod = (Method)iMethods.next();

      if (thisMethod.isConstructor())
      {
        userDefinedCtor = true; 

        //Since the user has defined a ctor, the default ctor is no longer available.
        //We must ensure that the user has provided a public ctor with no parameters.

        //check if public
        if (thisMethod.getJavaVisibility().getValue() == JavaVisibilityKind.PUBLIC)
        {
          //see if parameter list is empty
          List paramList = thisMethod.getParameters();
          if (paramList.isEmpty() )
          {
	        return true;             
          }
        }
      }

    }

    if (userDefinedCtor)
    {
	  return false; //if the user defined a ctor and we still haven't
	  	            //returned true, none of them were public with no parameters.
    }
    else
    { 
       return true;    
    }

  }
  /**
  * Determine whether the specified class implements the specified interface.
  * @param className The fully qualified name of the class
  * @param interfaceName The fully qualified name of the interface
  * @param project The project
  * @return True if the class and interface are loadable from the project and
  *         the class implements the interface.
  */
  public static boolean implementsInterface(String className, String interfaceName, IProject project)
  			throws CoreException
  {
   return implementsInterface(getJavaClass(className, project), getJavaClass(interfaceName, project));
  }

public static boolean implementsInterface(JavaClass javaClass, JavaClass interfaceClass)
  {
   
    if (!javaClass.isExistingType())     return false;

    if (!interfaceClass.isExistingType())  return false;
        
    return javaClass.implementsInterface(interfaceClass);
  }

  /**
  * Determine whether the specified class extends the specified superclass
  * @param className The fully qualified name of the class
  * @param superClassName The fully qualified name of the superclass
  * @param project The project
  * @return True if the class and superClass are loadable from the project and
  *         the class extends the superClass
  */
  public static boolean extendsClass(String className, String superClassName, IProject project)
         throws CoreException
  {
    return extendsClass(getJavaClass(className,project), getJavaClass(superClassName, project));
  }

  public static boolean extendsClass(JavaClass javaClass, JavaClass superClass)
  {
    return superClass.isAssignableFrom(javaClass);
  }

  /**
  * Determine whether the specified class is actually an interface
  * @param className The fully qualified name of the class
  * @param project The project
  * @return True if the class is loadable from the project and is 
  *         actually an interface
  */
  public static boolean isInterface(String className, IProject project) throws CoreException
  {
     return isInterface(getJavaClass(className, project));
  }

 public static boolean isInterface(JavaClass javaClass)
  {
      return javaClass.isInterface();
  }
 public static boolean hasAbstractMethods (String className, IProject project) throws CoreException
 	{
	    return hasAbstractMethods(getJavaClass(className, project));
  	}

  public static boolean hasAbstractMethods (JavaClass javaClass)
 	{
	   List methodList = javaClass.getMethods();
       if (methodList==null)
      	return false;

	   Iterator iMethods = methodList.iterator();
	   while (iMethods.hasNext())
    	{
      		Method thisMethod = (Method)iMethods.next();
			if ( thisMethod.isAbstract()) return true;
	   	}
		return false;
 	}

  /* 
   * 
   */
 /**
 * @param classQName Fully qualified classname (packageName + typeName)
 * @param project
 * @return
 * @throws CoreException
 */
public static JavaClass getJavaClass(String classQName , IProject project) throws CoreException
 	{
	 	EMFNature jMOF = JemProjectUtilities.getJEM_EMF_Nature(project, true);
	 	return (JavaClass)JavaRefFactory.eINSTANCE.reflectType(classQName,jMOF.getResourceSet());
 	}
 
 /**
 * @param packageName
 * @param typeName
 * @param project
 * @return
 * @throws CoreException
 */
public static JavaClass getJavaClass(String packageName, String typeName , IProject project) throws CoreException
	{
	 	EMFNature jMOF = JemProjectUtilities.getJEM_EMF_Nature(project, true);
	 	return (JavaClass)JavaRefFactory.eINSTANCE.reflectType(packageName, typeName, jMOF.getResourceSet());
	}

 public static boolean isValidSEIFile(JavaClass beanClass, JavaClass seiClass)
 	{
 		if (!seiClass.isInterface())
 			return false;

		Vector beanMethodsList = new Vector();
 		List beanMethods = beanClass.getMethods();
		Iterator beanMethodsIterator = beanMethods.iterator();
	   	while (beanMethodsIterator.hasNext())
    	{
      		Method thisMethod = (Method)beanMethodsIterator.next();
			beanMethodsList.add(thisMethod.getMethodElementSignature());
	   	}

		List seiMethods = seiClass.getMethods();
 		if (beanMethods == null ) {
 			if ( seiMethods == null) return true;
 			else	return false;
 		}
		if (seiMethods == null) return false;

		Iterator seiMethodsIterator = seiMethods.iterator();
	   	while (seiMethodsIterator.hasNext())
    	{
      		Method thisMethod = (Method)seiMethodsIterator.next();
			if ( !beanMethodsList.contains(thisMethod.getMethodElementSignature())) {
				return false;
			}
	   	}
		return true;
 		
 	}
 
}

Back to the top