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.internal.corext.refactoring.util;
13 import java.util.ArrayList;
14 import java.util.List;
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IResource;
19 import net.sourceforge.phpdt.core.ICompilationUnit;
20 import net.sourceforge.phpdt.core.IJavaElement;
21 import net.sourceforge.phpdt.core.IMember;
22 import net.sourceforge.phpdt.core.IOpenable;
24 import net.sourceforge.phpdt.internal.corext.Assert;
26 public class ResourceUtil {
28 private ResourceUtil(){
31 public static IFile[] getFiles(ICompilationUnit[] cus) {
32 List files= new ArrayList(cus.length);
33 for (int i= 0; i < cus.length; i++) {
34 IResource resource= ResourceUtil.getResource(cus[i]);
35 if (resource != null && resource.getType() == IResource.FILE)
38 return (IFile[]) files.toArray(new IFile[files.size()]);
41 public static IFile getFile(ICompilationUnit cu) {
42 IResource resource= ResourceUtil.getResource(cu);
43 if (resource != null && resource.getType() == IResource.FILE)
44 return (IFile)resource;
49 //----- other ------------------------------
52 * Finds an <code>IResource</code> for a given <code>ICompilationUnit</code>.
53 * If the parameter is a working copy then the <code>IResource</code> for
54 * the original element is returned.
56 public static IResource getResource(ICompilationUnit cu) {
57 return cu.getResource();
62 * Returns the <code>IResource</code> that the given <code>IMember</code> is defined in.
65 public static IResource getResource(IMember member) {
66 Assert.isTrue(!member.isBinary());
67 return getResource(member.getCompilationUnit());
70 public static IResource getResource(Object o){
71 if (o instanceof IResource)
73 if (o instanceof IJavaElement)
74 return getResource((IJavaElement)o);
78 private static IResource getResource(IJavaElement element){
79 if (element.getElementType() == IJavaElement.COMPILATION_UNIT)
80 return getResource((ICompilationUnit) element);
81 else if (element instanceof IOpenable)
82 return element.getResource();