aboutsummaryrefslogtreecommitdiffstats
blob: 771d591572bc3a0a201a97ccdf70fce12dac2e98 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*******************************************************************************
 * Copyright (c) 2008-2018 Sonatype, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *      Sonatype, Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.m2e.jdt.internal;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragmentRoot;

import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.embedder.ArtifactKey;
import org.eclipse.m2e.core.project.IMavenProjectFacade;
import org.eclipse.m2e.core.project.IMavenProjectRegistry;
import org.eclipse.m2e.jdt.MavenJdtPlugin;


/**
 * Adapter factory for Java elements
 * 
 * @author Igor Fedorenko
 * @author Eugene Kuleshov
 * @author Miles Parker (Split out into JavaUiElementsAdapterFactory)
 */
@SuppressWarnings({"rawtypes"})
public class JavaElementsAdapterFactory implements IAdapterFactory {
  private static final Logger log = LoggerFactory.getLogger(JavaElementsAdapterFactory.class);

  private static final Class[] ADAPTER_LIST = new Class[] {ArtifactKey.class, IPath.class, IMavenProjectFacade.class};

  public Class<?>[] getAdapterList() {
    return ADAPTER_LIST;
  }

  public <T> T getAdapter(Object adaptableObject, Class<T> adapterType) {
    if(adapterType == ArtifactKey.class) {
      if(adaptableObject instanceof IPackageFragmentRoot) {
        IPackageFragmentRoot fragment = (IPackageFragmentRoot) adaptableObject;
        IProject project = fragment.getJavaProject().getProject();
        if(project.isAccessible() && fragment.isArchive()) {
          try {
            return adapterType.cast(getBuildPathManager().findArtifact(project, fragment.getPath()));
          } catch(CoreException ex) {
            log.error("Can't find artifact for " + fragment, ex);
            return null;
          }
        }

      } else if(adaptableObject instanceof IJavaProject) {
        return adapterType.cast(((IJavaProject) adaptableObject).getProject().getAdapter(ArtifactKey.class));

      }

    } else if(adapterType == IPath.class) {
      if(adaptableObject instanceof IJavaElement) {
        IResource resource = ((IJavaElement) adaptableObject).getResource();
        if(resource != null) {
          return adapterType.cast(resource.getLocation());
        }
      }

    } else if(adapterType == IMavenProjectFacade.class) {
      if(adaptableObject instanceof IJavaElement) {
        IProject project = ((IJavaElement) adaptableObject).getJavaProject().getProject();
        IMavenProjectFacade projectFacade = getProjectFacade(project);
        if(projectFacade != null) {
          return adapterType.cast(projectFacade);
        }
      }
    }
    return null;
  }

  private BuildPathManager getBuildPathManager() {
    return (BuildPathManager) MavenJdtPlugin.getDefault().getBuildpathManager();
  }

  private IMavenProjectFacade getProjectFacade(IProject project) {
    IMavenProjectRegistry projectManager = MavenPlugin.getMavenProjectRegistry();
    return projectManager.create(project, new NullProgressMonitor());
  }

}