PageRenderTime 38ms CodeModel.GetById 13ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/eclipse-plugin/plugins/com.google.test.metric.eclipse.core/src/main/java/com/google/test/metric/eclipse/internal/util/JavaProjectHelper.java

http://testability-explorer.googlecode.com/
Java | 103 lines | 73 code | 9 blank | 21 comment | 12 complexity | 838939432e4743b4e91bbd2713c63d50 MD5 | raw file
  1/*
  2 * Copyright 2009 Google Inc.
  3 * 
  4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5 * use this file except in compliance with the License. You may obtain a copy of
  6 * the License at
  7 * 
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 * 
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 13 * License for the specific language governing permissions and limitations under
 14 * the License.
 15 */
 16package com.google.test.metric.eclipse.internal.util;
 17
 18import org.eclipse.core.resources.IContainer;
 19import org.eclipse.core.resources.IProject;
 20import org.eclipse.core.resources.IResource;
 21import org.eclipse.core.resources.IWorkspaceRoot;
 22import org.eclipse.core.resources.ResourcesPlugin;
 23import org.eclipse.core.runtime.CoreException;
 24import org.eclipse.core.runtime.IPath;
 25import org.eclipse.jdt.core.IJavaModel;
 26import org.eclipse.jdt.core.IJavaProject;
 27import org.eclipse.jdt.core.IPackageFragmentRoot;
 28import org.eclipse.jdt.core.JavaCore;
 29import org.eclipse.jdt.core.JavaModelException;
 30
 31import java.util.ArrayList;
 32import java.util.List;
 33
 34/**
 35 * Provides utility methods for accessing Java projects.
 36 * 
 37 * @author klundberg@google.com (Karin Lundberg)
 38 * 
 39 */
 40public class JavaProjectHelper {
 41
 42  private final Logger logger = new Logger();
 43
 44  public IJavaProject[] getJavaProjects() {
 45    IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
 46    IJavaModel javaModel = JavaCore.create(workspaceRoot);
 47    IJavaProject[] javaProjects = null;
 48    try {
 49      javaProjects = javaModel.getJavaProjects();
 50    } catch (JavaModelException e) {
 51      logger.logException("Error getting Java Projects", e);
 52    }
 53    return javaProjects;
 54  }
 55
 56  public IJavaProject getJavaProject(String projectName) {
 57    if (projectName != null && projectName.length() > 0) {
 58      IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
 59      IJavaModel javaModel = JavaCore.create(workspaceRoot);
 60      return javaModel.getJavaProject(projectName);
 61    } else {
 62      return null;
 63    }
 64  }
 65
 66  public String getProjectLocation(IJavaProject javaProject) {
 67    IProject project = javaProject.getProject();
 68    IPath rawLocation = project.getRawLocation();
 69    IPath projectLocation;
 70    if (rawLocation != null) {
 71      projectLocation = rawLocation.removeLastSegments(1);
 72    } else {
 73      projectLocation = project.getParent().getLocation();
 74    }
 75    return projectLocation.toOSString();
 76  }
 77
 78  public List<String> getAllJavaPackages(IJavaProject javaProject) throws JavaModelException,
 79      CoreException {
 80    List<String> allJavaPackages = new ArrayList<String>();
 81    IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
 82    for (IPackageFragmentRoot root : roots) {
 83      if (!root.isArchive()) {
 84        IResource rootResource = root.getCorrespondingResource();
 85        String rootURL = rootResource.getFullPath().toOSString();
 86        rootResource.accept(new JavaPackageVisitor(allJavaPackages, rootURL), IContainer.NONE);
 87      }
 88    }
 89    return allJavaPackages;
 90  }
 91  
 92  public List<IPackageFragmentRoot> getAllJavaPackageFragmentRoots(IJavaProject javaProject)
 93      throws JavaModelException, CoreException {
 94    List<IPackageFragmentRoot> allJavaPackageFragmentRoot = new ArrayList<IPackageFragmentRoot>();
 95    IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
 96    for (IPackageFragmentRoot root : roots) {
 97      if (!root.isArchive() && !root.getElementName().equals("")) {
 98        allJavaPackageFragmentRoot.add(root);
 99      }
100    }
101    return allJavaPackageFragmentRoot;
102  }
103}