1 package com.stateofflow.eclipse.metrics.builder.list;
2
3 import org.eclipse.core.resources.IFile;
4 import org.eclipse.jdt.core.ICompilationUnit;
5 import org.eclipse.jdt.core.IJavaElement;
6 import org.eclipse.jdt.core.IJavaProject;
7 import org.eclipse.jdt.core.IPackageFragment;
8 import org.eclipse.jdt.core.JavaModelException;
9
10 import com.stateofflow.eclipse.metrics.properties.exclusions.ExcludedResources;
11 import com.stateofflow.eclipse.metrics.util.ProgressMonitor;
12
13 class JavaElementListBuilder {
14 private final IJavaProject project;
15 private final CompilationUnitList list;
16 private final ExcludedResources excluded;
17
18 public JavaElementListBuilder(final IJavaProject project, final CompilationUnitList list, final ExcludedResources excluded) {
19 this.project = project;
20 this.list = list;
21 this.excluded = excluded;
22 }
23
24 public void addTypesForJavaElement(final IJavaElement element, final ProgressMonitor monitor) throws JavaModelException {
25 if (isInCorrectProject(element)) {
26 addTypesForJavaElementKnownToBeInCorrectProject(element, monitor);
27 }
28 }
29
30 private boolean isInCorrectProject(final IJavaElement element) {
31 return project.equals(element.getJavaProject());
32 }
33
34 private void addTypesForJavaElementKnownToBeInCorrectProject(final IJavaElement element, final ProgressMonitor monitor) throws JavaModelException {
35 switch (element.getElementType()) {
36 case IJavaElement.PACKAGE_FRAGMENT : {
37 addTypesForJavaElements(((IPackageFragment) element).getChildren(), element.getElementName(), monitor);
38 break;
39 }
40 case IJavaElement.COMPILATION_UNIT : {
41 addCompilationUnitUnlessExcluded((ICompilationUnit) element);
42 break;
43 }
44 }
45 }
46
47 public void addTypesForJavaElements(final IJavaElement[] elements, final String name, final ProgressMonitor monitor) throws JavaModelException {
48 monitor.beginTask("Adding types for " + name, elements.length);
49 for (int i = 0; i < elements.length; i++) {
50 addTypesForJavaElement(elements[i], monitor.newChild(1));
51 }
52 }
53
54 private void addCompilationUnitUnlessExcluded(final ICompilationUnit compilationUnit) throws JavaModelException {
55 if (!isExcluded(compilationUnit)) {
56 list.addCompilationUnit(compilationUnit);
57 }
58 }
59
60 private boolean isExcluded(final ICompilationUnit compilationUnit) throws JavaModelException {
61 return excluded.contains((IFile) compilationUnit.getCorrespondingResource());
62 }
63 }
64