1 /*******************************************************************************
2  * Copyright (c) 2005, 2015 BEA Systems, Inc.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     mkaufman@bea.com - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.jdt.apt.core.internal.util;
15 import java.util.ArrayList;
16 import java.util.List;
17 
18 import org.eclipse.jdt.apt.core.internal.env.BaseProcessorEnv;
19 import org.eclipse.jdt.core.IPackageFragment;
20 import org.eclipse.jdt.core.IPackageFragmentRoot;
21 import org.eclipse.jdt.core.JavaModelException;
22 
23 /**
24  * Utility class for dealing with packages, using
25  * Eclipse's underlying SearchEngine
26  */
27 public class PackageUtil {
28 
PackageUtil()29 	private PackageUtil() {}
30 
getPackageFragments( final String packageName, final BaseProcessorEnv env)31 	public static IPackageFragment[] getPackageFragments(
32 			final String packageName,
33 			final BaseProcessorEnv env) {
34 
35 		List<IPackageFragment> packages = new ArrayList<>();
36 		try {
37 			// The environment caches our package fragment roots
38 			IPackageFragmentRoot[] roots = env.getAllPackageFragmentRoots();
39 			for (IPackageFragmentRoot root : roots) {
40 				IPackageFragment fragment = root.getPackageFragment(packageName);
41 				if (fragment != null && fragment.exists())
42 					packages.add(fragment);
43 			}
44 		}
45 		catch (JavaModelException e) {
46 			return new IPackageFragment[0];
47 		}
48 
49 		return packages.toArray(new IPackageFragment[packages.size()]);
50 	}
51 
52 }
53