01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.jdt.internal.corext.refactoring.util;
11:
12: import java.util.ArrayList;
13: import java.util.List;
14:
15: import org.eclipse.core.resources.IFile;
16: import org.eclipse.core.resources.IResource;
17:
18: import org.eclipse.jdt.core.ICompilationUnit;
19: import org.eclipse.jdt.core.IJavaElement;
20: import org.eclipse.jdt.core.IOpenable;
21:
22: public class ResourceUtil {
23:
24: private ResourceUtil() {
25: }
26:
27: public static IFile[] getFiles(ICompilationUnit[] cus) {
28: List files = new ArrayList(cus.length);
29: for (int i = 0; i < cus.length; i++) {
30: IResource resource = cus[i].getResource();
31: if (resource != null
32: && resource.getType() == IResource.FILE)
33: files.add(resource);
34: }
35: return (IFile[]) files.toArray(new IFile[files.size()]);
36: }
37:
38: public static IFile getFile(ICompilationUnit cu) {
39: IResource resource = cu.getResource();
40: if (resource != null && resource.getType() == IResource.FILE)
41: return (IFile) resource;
42: else
43: return null;
44: }
45:
46: //----- other ------------------------------
47:
48: public static IResource getResource(Object o) {
49: if (o instanceof IResource)
50: return (IResource) o;
51: if (o instanceof IJavaElement)
52: return getResource((IJavaElement) o);
53: return null;
54: }
55:
56: private static IResource getResource(IJavaElement element) {
57: if (element.getElementType() == IJavaElement.COMPILATION_UNIT)
58: return ((ICompilationUnit) element).getResource();
59: else if (element instanceof IOpenable)
60: return element.getResource();
61: else
62: return null;
63: }
64: }
|