KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > apt > core > internal > util > PackageUtil


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 BEA Systems, Inc.
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  * mkaufman@bea.com - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.apt.core.internal.util;
12 import java.util.ArrayList JavaDoc;
13 import java.util.List JavaDoc;
14
15 import org.eclipse.jdt.apt.core.internal.env.BaseProcessorEnv;
16 import org.eclipse.jdt.core.IPackageFragment;
17 import org.eclipse.jdt.core.IPackageFragmentRoot;
18 import org.eclipse.jdt.core.JavaModelException;
19
20 /**
21  * Utility class for dealing with packages, using
22  * Eclipse's underlying SearchEngine
23  */

24 public class PackageUtil {
25     
26     private PackageUtil() {}
27     
28     public static IPackageFragment[] getPackageFragments(
29             final String JavaDoc packageName,
30             final BaseProcessorEnv env) {
31         
32         List JavaDoc<IPackageFragment> packages = new ArrayList JavaDoc<IPackageFragment>();
33         try {
34             // The environment caches our package fragment roots
35
IPackageFragmentRoot[] roots = env.getAllPackageFragmentRoots();
36             for (IPackageFragmentRoot root : roots) {
37                 IPackageFragment fragment = root.getPackageFragment(packageName);
38                 if (fragment != null && fragment.exists())
39                     packages.add(fragment);
40             }
41         }
42         catch (JavaModelException e) {
43             return new IPackageFragment[0];
44         }
45         
46         return packages.toArray(new IPackageFragment[packages.size()]);
47     }
48
49 }
50
Popular Tags