[325894] Protect EARUtilities.getJ2EEModuleReferences from NPE
diff --git a/plugins/org.eclipse.jst.j2ee/j2eecreation/org/eclipse/jst/j2ee/project/EarUtilities.java b/plugins/org.eclipse.jst.j2ee/j2eecreation/org/eclipse/jst/j2ee/project/EarUtilities.java
index 5d58a69..bc7de44 100644
--- a/plugins/org.eclipse.jst.j2ee/j2eecreation/org/eclipse/jst/j2ee/project/EarUtilities.java
+++ b/plugins/org.eclipse.jst.j2ee/j2eecreation/org/eclipse/jst/j2ee/project/EarUtilities.java
@@ -48,16 +48,30 @@
 	}
 
 	/**
-	 * This method will return the an IVirtualComponent for the given module name. The method take
-	 * either moduleName or moduleName + ".module_extension" (module_extension = ".jar" || ".war" ||
-	 * ".rar") which allows users to get a IVirtualComponent for a given entry in an application.xml
+	 * This method will return the an {@link IVirtualComponent} for the given
+	 * module name. The method take either moduleName or moduleName +
+	 * ".module_extension" (module_extension = ".jar" || ".war" || ".rar") which
+	 * allows users to get a IVirtualComponent for a given entry in an
+	 * application.xml
 	 * 
-	 * @return - a IVirtualComponent for module name
+	 * @param earComponent
+	 *            The EAR's {@link IVirtualComponent}. Passing in
+	 *            <code>null</code> will result with <code>null</code> being
+	 *            returned.
+	 * 
+	 * @param moduleName
+	 *            The name of the module to look for. Passing in
+	 *            <code>null</code> will result with <code>null</code> being
+	 *            returned.
+	 * 
+	 * @return an {@link IVirtualComponent} for module name or <code>null</code>
+	 *         if none exists.
 	 */
-	public static IVirtualComponent getModule(IVirtualComponent earComponent, String moduleName) {
-		String innerModuleName = moduleName;
-		if (innerModuleName == null)
+	public static IVirtualComponent getModule(final IVirtualComponent earComponent, final String moduleName) {
+		if (earComponent == null || moduleName == null) {
 			return null;
+		}
+		String innerModuleName = moduleName;
 		if (innerModuleName.endsWith(IJ2EEModuleConstants.JAR_EXT) || innerModuleName.endsWith(IJ2EEModuleConstants.WAR_EXT) || innerModuleName.endsWith(IJ2EEModuleConstants.RAR_EXT))
 			innerModuleName = innerModuleName.substring(0, (innerModuleName.length() - IJ2EEModuleConstants.JAR_EXT.length()));
 		IVirtualReference[] references = getComponentReferences(earComponent);
@@ -71,12 +85,21 @@
 	}
 
 	/**
-	 * This method will return the list of IVirtualReferences for the J2EE module components
-	 * contained in this EAR application.
+	 * This method will return an of {@link IVirtualReference} for the J2EE
+	 * module components contained in this EAR application.
 	 * 
-	 * @return - an array of IVirtualReferences for J2EE modules in the EAR
+	 * @param earComponent
+	 *            The EAR's {@link IVirtualComponent}. Passing in
+	 *            <code>null</code> will result with a zero length
+	 *            {@link IVirtualReference} array being returned.
+	 * 
+	 * @return an array of {@link IVirtualReference} for J2EE modules in the
+	 *         EAR.
 	 */
 	public static IVirtualReference[] getJ2EEModuleReferences(IVirtualComponent earComponent) {
+		if (earComponent == null) {
+			return NO_REFERENCES;
+		}
 		List j2eeTypes = new ArrayList();
 		j2eeTypes.add(IJ2EEFacetConstants.APPLICATION_CLIENT);
 		j2eeTypes.add(IJ2EEFacetConstants.JCA);
@@ -84,12 +107,12 @@
 		j2eeTypes.add(IJ2EEFacetConstants.DYNAMIC_WEB);
 		IVirtualReference[] refs = getComponentReferences(earComponent, j2eeTypes);
 		String earLib = getEARLibDir(earComponent);
-		if(earLib == null)
+		if (earLib == null)
 			return refs;
-		ArrayList <IVirtualReference> moduleRefs = new ArrayList <IVirtualReference>();
+		ArrayList<IVirtualReference> moduleRefs = new ArrayList<IVirtualReference>();
 		Path earLibPath = new Path(earLib);
-		for(int i = 0; i < refs.length; i++) {
-			if(!refs[i].getRuntimePath().equals(earLibPath)) {
+		for (int i = 0; i < refs.length; i++) {
+			if (!refs[i].getRuntimePath().equals(earLibPath)) {
 				moduleRefs.add(refs[i]);
 			}
 		}
@@ -97,21 +120,42 @@
 	}
 
 	/**
-	 * This method will return the list of IVirtualReferences for all of the components contained in
-	 * an EAR application.
+	 * This method will return any array of {@link IVirtualReference} for all of
+	 * the components contained in an EAR application.
 	 * 
-	 * @return - an array of IVirtualReferences for components in the EAR
+	 * @param earComponent
+	 *            The EAR's {@link IVirtualComponent}. Passing in
+	 *            <code>null</code> will result with a zero length
+	 *            {@link IVirtualReference} array being returned.
+	 * 
+	 * @return an array of {@link IVirtualReference} for components in the EAR.
 	 */
 	public static IVirtualReference[] getComponentReferences(IVirtualComponent earComponent) {
+		if (earComponent == null) {
+			return NO_REFERENCES;
+		}
 		return getComponentReferences(earComponent, Collections.EMPTY_LIST);
 	}
 
 	/**
-	 * This method will return the IVirtualReference to the component of the given name
+	 * This method will return the {@link IVirtualReference} to the component of
+	 * the given name
+	 * 
+	 * @param earComponent
+	 *            The EAR's {@link IVirtualComponent}. Passing in
+	 *            <code>null</code> will result with <code>null</code> being
+	 *            returned.
+	 * @param componentName
+	 *            The name of the componentName to look for. Passing in
+	 *            <code>null</code> will result with <code>null</code> being
+	 *            returned.
 	 * 
 	 * @return - IVirtualReference or null if not found
 	 */
 	public static IVirtualReference getComponentReference(IVirtualComponent earComponent, String componentName) {
+		if (earComponent == null || componentName == null) {
+			return null;
+		}
 		IVirtualReference[] refs = getComponentReferences(earComponent, Collections.EMPTY_LIST);
 		for (int i = 0; i < refs.length; i++) {
 			IVirtualReference reference = refs[i];
@@ -124,21 +168,21 @@
 
 	private static IVirtualReference[] getComponentReferences(IVirtualComponent earComponent, List componentTypes) {
 		List components = getComponentReferencesAsList(earComponent, componentTypes);
-		if(components.size() > 0)
+		if (components.size() > 0)
 			return (IVirtualReference[]) components.toArray(new IVirtualReference[components.size()]);
 		return NO_REFERENCES;
-	} 
-	
+	}
+
 	private static List getComponentReferencesAsList(IVirtualComponent earComponent, List componentTypes) {
 		return getComponentReferencesAsList(earComponent, componentTypes, false);
 	}
 
 	/**
+	 * This method is copied from EARArtifactEdit. Any bug fixes should occur in
+	 * both locations.
 	 * 
 	 * @param componentTypes
 	 * @return A List of {@link IVirtualReference}s.
-	 * 
-	 * This method is copied from EARArtifactEdit.  Any bug fixes should occur in both locations.
 	 */
 	private static List getComponentReferencesAsList(IVirtualComponent earComponent, List componentTypes, boolean expandLibraries) {
 		List components = new ArrayList();
@@ -148,14 +192,15 @@
 				options.put(J2EEModuleVirtualComponent.GET_EXPANDED_LIB_REFS, Boolean.TRUE);
 			}
 			IVirtualReference[] refComponents = earComponent.getReferences(options);
-	
+
 			for (int i = 0; i < refComponents.length; i++) {
 				IVirtualReference reference = refComponents[i];
 				IVirtualComponent module = reference.getReferencedComponent();
 				if (module == null)
 					continue;
 
-				// if component types passed in is null then return all components
+				// if component types passed in is null then return all
+				// components
 				if (componentTypes == null || componentTypes.size() == 0)
 					components.add(reference);
 				else {
@@ -167,26 +212,34 @@
 		}
 		return components;
 	}
-	
+
 	/**
 	 * Checks if the uri mapping already exists.
 	 * 
-	 * @param String
-	 *            currentURI - The current uri of the module.
+	 * @param currentURI
+	 *            The current uri of the module. Passing in <code>null</code>
+	 *            will result with <code>false</code> being returned.
+	 * 
+	 * @param earProject
+	 *            The EAR's project. Passing in <code>null</code> will result
+	 *            with <code>false</code> being returned.
 	 * @return boolean
 	 */
 	public static boolean uriExists(String currentURI, IProject earProject) {
+		if (currentURI == null || earProject == null) {
+			return false;
+		}
 		StructureEdit edit = null;
 		try {
 			edit = StructureEdit.getStructureEditForRead(earProject);
-			if (edit!=null && edit.getComponent()!=null) {
+			if (edit != null && edit.getComponent() != null) {
 				List referencedComps = edit.getComponent().getReferencedComponents();
-				for (int i=0; i<referencedComps.size(); i++) {
+				for (int i = 0; i < referencedComps.size(); i++) {
 					ReferencedComponent ref = (ReferencedComponent) referencedComps.get(i);
 					Object module = ref.getDependentObject();
-					if (module!=null && module instanceof Module) {
-						String existingURI = ((Module)module).getUri();
-						if (existingURI!=null && existingURI.equals(currentURI))
+					if (module != null && module instanceof Module) {
+						String existingURI = ((Module) module).getUri();
+						if (existingURI != null && existingURI.equals(currentURI))
 							return true;
 					}
 				}
@@ -200,15 +253,20 @@
 
 	/**
 	 * Returns all referencing EAR projects.
-	 * @param project Project to check. If null or an EAR, returns a zero length array.
+	 * 
+	 * @param project
+	 *            Project to check. If <code>null</code> then a zero length
+	 *            array is returned. If an EAR, then a one element array will be
+	 *            returned.
 	 * @return Array of referencing EAR projects.
 	 */
 	public static IProject[] getReferencingEARProjects(final IProject project) {
-		// TODO - should this go here, or in JavaEEProjectUtilities?
-		if(project != null && isEARProject(project)){
-			return new IProject[] {project};
+		if (project == null) {
+			return new IProject[0];
+		} else if (isEARProject(project)) {
+			return new IProject[] { project };
 		}
-		
+
 		List result = new ArrayList();
 		IVirtualComponent component = ComponentCore.createComponent(project);
 		if (component != null) {
@@ -218,9 +276,9 @@
 					result.add(refComponents[i].getProject());
 			}
 		}
-		
-		IProject [] earProjects = (IProject[]) result.toArray(new IProject[result.size()]);
-		//sort the list so it is consistent
+
+		IProject[] earProjects = (IProject[]) result.toArray(new IProject[result.size()]);
+		// sort the list so it is consistent
 		Arrays.sort(earProjects, new Comparator<IProject>() {
 			public int compare(IProject p0, IProject p1) {
 				return p0.getName().compareTo(p1.getName());
@@ -230,83 +288,99 @@
 		return earProjects;
 	}
 
+	/**
+	 * Equivalent to getReferencingEARProjects(project).length == 0; Will return
+	 * <code>true</code> if the specified project is not linked to an EAR.
+	 * 
+	 * @project Project to check
+	 * @return
+	 */
 	public static boolean isStandaloneProject(IProject project) {
 		return getReferencingEARProjects(project).length == 0;
 	}
 
+	/**
+	 * Equivalent to calling
+	 * {@link #getUtilityModuleReferences(IVirtualComponent, boolean)} passing
+	 * <code>false</code>.
+	 * 
+	 * @param earComponent
+	 * @return
+	 */
 	public static IVirtualReference[] getUtilityModuleReferences(IVirtualComponent earComponent) {
 		return getUtilityModuleReferences(earComponent, false);
 	}
 
 	/**
-	 * This method will return the list of IVirtualReferences for all of the utility modules
-	 * contained in the EAR application
+	 * This method will return the array of {@link IVirtualReference} for all of
+	 * the utility modules contained in the EAR application
 	 * 
-	 * @return - an array of IVirtualReferences for utility modules in the EAR
+	 * @param earComponent
+	 *            The EAR's {@link IVirtualComponent}. Passing in
+	 *            <code>null</code> or a non ear project will result with a zero
+	 *            length {@link IVirtualReference} array being returned.
+	 * 
+	 * @return - an array of {@link IVirtualReference} for utility modules in
+	 *         the EAR
 	 */
 	public static IVirtualReference[] getUtilityModuleReferences(IVirtualComponent earComponent, boolean expandLibraries) {
 		if (earComponent != null && isEARProject(earComponent.getProject())) {
-			List explicitUtilityReferences = 
-				getComponentReferencesAsList(earComponent, Collections.singletonList(IJ2EEFacetConstants.UTILITY), expandLibraries);
-			
-			// fetch other Utility Jars attached to the EAR project 
-			List implicitUtilityReferenceTypes =
-				Arrays.asList(new String[] {  IModuleConstants.JST_APPCLIENT_MODULE, 
-											   IModuleConstants.JST_WEB_MODULE,  
-											   IModuleConstants.JST_EJB_MODULE }); 
-	
-			List implicitUtilityReferences = 
-				getComponentReferencesAsList(earComponent, implicitUtilityReferenceTypes, expandLibraries);
-			
-			IEARModelProvider earModel = (IEARModelProvider)ModelProviderManager.getModelProvider(earComponent.getProject());
-			ICommonApplication application = (ICommonApplication)earModel.getModelObject();
+			List explicitUtilityReferences = getComponentReferencesAsList(earComponent, Collections.singletonList(IJ2EEFacetConstants.UTILITY), expandLibraries);
+
+			// fetch other Utility Jars attached to the EAR project
+			List implicitUtilityReferenceTypes = Arrays.asList(new String[] { IModuleConstants.JST_APPCLIENT_MODULE, IModuleConstants.JST_WEB_MODULE, IModuleConstants.JST_EJB_MODULE });
+
+			List implicitUtilityReferences = getComponentReferencesAsList(earComponent, implicitUtilityReferenceTypes, expandLibraries);
+
+			IEARModelProvider earModel = (IEARModelProvider) ModelProviderManager.getModelProvider(earComponent.getProject());
+			ICommonApplication application = (ICommonApplication) earModel.getModelObject();
 			Object module = null;
 			IVirtualReference reference = null;
-			for (Iterator referenceItr = implicitUtilityReferences.iterator(); referenceItr.hasNext(); ) {
+			for (Iterator referenceItr = implicitUtilityReferences.iterator(); referenceItr.hasNext();) {
 				module = null;
 				reference = (IVirtualReference) referenceItr.next();
-				if (application instanceof org.eclipse.jst.javaee.application.internal.impl.ApplicationImpl)
-				{
-					module = ((org.eclipse.jst.javaee.application.internal.impl.ApplicationImpl)application).getFirstModule(reference.getArchiveName());
+				if (application instanceof org.eclipse.jst.javaee.application.internal.impl.ApplicationImpl) {
+					module = ((org.eclipse.jst.javaee.application.internal.impl.ApplicationImpl) application).getFirstModule(reference.getArchiveName());
+				} else if (application instanceof org.eclipse.jst.j2ee.application.internal.impl.ApplicationImpl) {
+					module = ((org.eclipse.jst.j2ee.application.internal.impl.ApplicationImpl) application).getFirstModule(reference.getArchiveName());
 				}
-				else if (application instanceof org.eclipse.jst.j2ee.application.internal.impl.ApplicationImpl)
-				{
-					module = ((org.eclipse.jst.j2ee.application.internal.impl.ApplicationImpl)application).getFirstModule(reference.getArchiveName()); 
-				}
-				if(module != null) 
-					referenceItr.remove(); 
+				if (module != null)
+					referenceItr.remove();
 			}
-			
+
 			List allUtilityModuleReferences = new ArrayList();
 			allUtilityModuleReferences.addAll(explicitUtilityReferences);
 			allUtilityModuleReferences.addAll(implicitUtilityReferences);
-			
-			if(allUtilityModuleReferences.size() > 0)
+
+			if (allUtilityModuleReferences.size() > 0)
 				return (IVirtualReference[]) allUtilityModuleReferences.toArray(new IVirtualReference[allUtilityModuleReferences.size()]);
 		}
 		return NO_REFERENCES;
-		
+
 	}
-	
+
 	/**
-	 * Returns a List of ProjectFacetVersions for a given module type that are supported by a given ear version
-	 * @param earProjectFacetVersion - the ProjectFacetVersion for the ENTERPRISE_APPLICATION that the module will be added to
-	 * @param moduleProjectFacet - the module type whose appropriate ProjectFacetVersions are desired
-	 * @return List of compatible ProjectFacetVersions of the moduleProjectFacet type
+	 * Returns a List of ProjectFacetVersions for a given module type that are
+	 * supported by a given ear version
+	 * 
+	 * @param earProjectFacetVersion
+	 *            - the ProjectFacetVersion for the ENTERPRISE_APPLICATION that
+	 *            the module will be added to
+	 * @param moduleProjectFacet
+	 *            - the module type whose appropriate ProjectFacetVersions are
+	 *            desired
+	 * @return List of compatible ProjectFacetVersions of the moduleProjectFacet
+	 *         type
 	 */
-	public static List<IProjectFacetVersion> getSupportedFacets(IProjectFacetVersion earProjectFacetVersion, IProjectFacet moduleProjectFacet)
-	{
+	public static List<IProjectFacetVersion> getSupportedFacets(IProjectFacetVersion earProjectFacetVersion, IProjectFacet moduleProjectFacet) {
 		List<IProjectFacetVersion> retVal = new ArrayList<IProjectFacetVersion>();
 
-		if (earProjectFacetVersion != null && ENTERPRISE_APPLICATION.equals(earProjectFacetVersion.getProjectFacet().getId()))
-		{
+		if (earProjectFacetVersion != null && ENTERPRISE_APPLICATION.equals(earProjectFacetVersion.getProjectFacet().getId())) {
 			String moduleProjectFacetId = moduleProjectFacet.getId();
 			int earVersion = J2EEVersionUtil.convertVersionStringToInt(earProjectFacetVersion.getVersionString());
 
-			if (DYNAMIC_WEB.equals(moduleProjectFacetId))
-			{
-				switch (earVersion)
-				{
+			if (DYNAMIC_WEB.equals(moduleProjectFacetId)) {
+				switch (earVersion) {
 				case J2EEVersionConstants.VERSION_6_0:
 					retVal.add(DYNAMIC_WEB_30);
 				case J2EEVersionConstants.VERSION_5_0:
@@ -322,11 +396,8 @@
 					//$FALL-THROUGH$
 					retVal.add(DYNAMIC_WEB_22);
 				}
-			}
-			else if (EJB.equals(moduleProjectFacetId))
-			{
-				switch (earVersion)
-				{
+			} else if (EJB.equals(moduleProjectFacetId)) {
+				switch (earVersion) {
 				case J2EEVersionConstants.VERSION_6_0:
 					retVal.add(EJB_31);
 				case J2EEVersionConstants.VERSION_5_0:
@@ -341,11 +412,8 @@
 				case J2EEVersionConstants.VERSION_1_2:
 					retVal.add(EJB_11);
 				}
-			}
-			else if (JCA.equals(moduleProjectFacetId))
-			{
-				switch (earVersion)
-				{
+			} else if (JCA.equals(moduleProjectFacetId)) {
+				switch (earVersion) {
 				case J2EEVersionConstants.VERSION_6_0:
 					retVal.add(JCA_16);
 				case J2EEVersionConstants.VERSION_5_0:
@@ -358,11 +426,8 @@
 				case J2EEVersionConstants.VERSION_1_2:
 					// there is no JCA in EAR 1.2
 				}
-			}
-			else if (APPLICATION_CLIENT.equals(moduleProjectFacetId))
-			{
-				switch (earVersion)
-				{
+			} else if (APPLICATION_CLIENT.equals(moduleProjectFacetId)) {
+				switch (earVersion) {
 				case J2EEVersionConstants.VERSION_6_0:
 					retVal.add(APPLICATION_CLIENT_60);
 				case J2EEVersionConstants.VERSION_5_0:
@@ -377,19 +442,13 @@
 				case J2EEVersionConstants.VERSION_1_2:
 					retVal.add(APPLICATION_CLIENT_12);
 				}
-			}
-			else if (UTILITY.equals(moduleProjectFacetId))
-			{
+			} else if (UTILITY.equals(moduleProjectFacetId)) {
 				retVal.add(UTILITY_FACET_10);
-			}
-			else
-			{
+			} else {
 				// invalid module type
 				throw new IllegalArgumentException("The moduleProjectFacet parameter must be a valid Java EE module type."); //$NON-NLS-1$
 			}
-		}
-		else
-		{
+		} else {
 			// invalid EAR facet
 			if (earProjectFacetVersion == null)
 				throw new IllegalArgumentException("The earProjectFacetVersion parameter cannot be null"); //$NON-NLS-1$
@@ -403,47 +462,53 @@
 	 * Get the library directory from an EAR virtual component
 	 * 
 	 * @param earComponent
-	 *            the EAR virtual component
+	 *            The EAR's {@link IVirtualComponent}. Passing in
+	 *            <code>null</code> will result with <code>null</code> being
+	 *            returned.
 	 * 
 	 * @return a runtime representation of the library directory path or null if
 	 *         the EAR's version is lower than 5
 	 */
 	public static String getEARLibDir(IVirtualComponent earComponent) {
-		// check if the EAR component's version is 5 or greater
-		IProject earProject = earComponent.getProject();
-		if (!JavaEEProjectUtilities.isJEEComponent(earComponent, JavaEEProjectUtilities.DD_VERSION)
-				|| !JavaEEProjectUtilities.isJEEComponent(earComponent, JavaEEProjectUtilities.FACET_VERSION)) {
+		if (earComponent == null) {
 			return null;
 		}
-		
+
+		// check if the EAR component's version is 5 or greater
+		IProject earProject = earComponent.getProject();
+		if (!JavaEEProjectUtilities.isJEEComponent(earComponent, JavaEEProjectUtilities.DD_VERSION) || !JavaEEProjectUtilities.isJEEComponent(earComponent, JavaEEProjectUtilities.FACET_VERSION)) {
+			return null;
+		}
+
 		// default lib dir if there is no deployment descriptor
 		// or if the deployment descriptor does not override
 		String libDir = J2EEConstants.EAR_DEFAULT_LIB_DIR;
-		
+
 		// retrieve the model provider
 		IModelProvider modelProvider = ModelProviderManager.getModelProvider(earProject);
-		if (modelProvider == null){
+		if (modelProvider == null) {
 			return libDir;
 		}
-		
-		// retrieve the EAR's deployment descriptor model object 
+
+		// retrieve the EAR's deployment descriptor model object
 		Object o = modelProvider.getModelObject(new Path(J2EEConstants.APPLICATION_DD_URI));
 		Application app = null;
-		// check to see the type - if this is in a project with a j2ee 1.4 or older facet, lib dir is unavailable.
-		if (o instanceof Application){
+		// check to see the type - if this is in a project with a j2ee 1.4 or
+		// older facet, lib dir is unavailable.
+		if (o instanceof Application) {
 			app = (Application) o;
 		}
-		if (app == null){
+		if (app == null) {
 			return libDir;
 		}
-		
+
 		// retrieve the library directory from the model
 		String ddLibDir = app.getLibraryDirectory();
 		if (ddLibDir != null) {
 			libDir = ddLibDir;
 		}
-		
+
 		return libDir;
 	}
-	
+
 }