Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2007 IBM Corporation and others. |
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 |
* IBM Corporation - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.pde.internal.ui.correction.java; |
12 |
|
13 |
import java.util.ArrayList; |
14 |
|
15 |
import org.eclipse.core.runtime.CoreException; |
16 |
import org.eclipse.jdt.core.ICompilationUnit; |
17 |
import org.eclipse.jdt.core.IJavaElement; |
18 |
import org.eclipse.jdt.core.IJavaProject; |
19 |
import org.eclipse.jdt.core.IPackageFragment; |
20 |
import org.eclipse.jdt.core.compiler.IProblem; |
21 |
import org.eclipse.jdt.core.dom.ASTNode; |
22 |
import org.eclipse.jdt.core.dom.IBinding; |
23 |
import org.eclipse.jdt.core.dom.Name; |
24 |
import org.eclipse.jdt.core.dom.Type; |
25 |
import org.eclipse.jdt.ui.text.java.IInvocationContext; |
26 |
import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal; |
27 |
import org.eclipse.jdt.ui.text.java.IProblemLocation; |
28 |
import org.eclipse.jdt.ui.text.java.IQuickFixProcessor; |
29 |
import org.eclipse.osgi.service.resolver.ExportPackageDescription; |
30 |
import org.eclipse.pde.core.plugin.IPluginModelBase; |
31 |
import org.eclipse.pde.internal.core.PDECore; |
32 |
import org.eclipse.pde.internal.core.WorkspaceModelManager; |
33 |
|
34 |
public class QuickFixProcessor implements IQuickFixProcessor { |
35 |
|
36 |
/* (non-Javadoc) |
37 |
* @see org.eclipse.jdt.ui.text.java.IQuickFixProcessor#getCorrections(org.eclipse.jdt.ui.text.java.IInvocationContext, org.eclipse.jdt.ui.text.java.IProblemLocation[]) |
38 |
*/ |
39 |
public IJavaCompletionProposal[] getCorrections(IInvocationContext context, |
40 |
IProblemLocation[] locations) throws CoreException { |
41 |
ArrayList results = new ArrayList(); |
42 |
for (int i = 0; i < locations.length; i++) { |
43 |
int id = locations[i].getProblemId(); |
44 |
switch (id) { |
45 |
case IProblem.ForbiddenReference: |
46 |
handleAccessRestrictionProblem(context, locations[i], results); |
47 |
default: |
48 |
} |
49 |
} |
50 |
return (IJavaCompletionProposal[])results.toArray(new IJavaCompletionProposal[results.size()]); |
51 |
} |
52 |
|
53 |
private void handleAccessRestrictionProblem(IInvocationContext context, |
54 |
IProblemLocation location, ArrayList results) { |
55 |
IBinding referencedElement= null; |
56 |
ASTNode node= location.getCoveredNode(context.getASTRoot()); |
57 |
if (node instanceof Type) { |
58 |
referencedElement= ((Type) node).resolveBinding(); |
59 |
} else if (node instanceof Name) { |
60 |
referencedElement= ((Name) node).resolveBinding(); |
61 |
} |
62 |
if (referencedElement != null) { |
63 |
// get the project that contains the reference element |
64 |
// ensure it exists in the workspace and is a plug-in project |
65 |
IJavaProject referencedJavaProject = referencedElement.getJavaElement().getJavaProject(); |
66 |
if (referencedJavaProject != null && WorkspaceModelManager.isPluginProject(referencedJavaProject.getProject())) { |
67 |
// get the packages exported by the referenced plug-in project |
68 |
IPluginModelBase referencedModel = PDECore.getDefault().getModelManager().findModel(referencedJavaProject.getProject()); |
69 |
ExportPackageDescription[] exportPackages = referencedModel.getBundleDescription().getExportPackages(); |
70 |
// check if the required package is exported already |
71 |
boolean packageExported = false; |
72 |
IPackageFragment referencedPackage = (IPackageFragment)referencedElement.getJavaElement().getAncestor(IJavaElement.PACKAGE_FRAGMENT); |
73 |
if (referencedPackage != null) { |
74 |
for (int i = 0; i < exportPackages.length; i++) { |
75 |
if (exportPackages[i].getName().equals(referencedPackage.getElementName())){ |
76 |
packageExported = true; |
77 |
break; |
78 |
} |
79 |
} |
80 |
// if the package is not exported, add the quickfix |
81 |
if (!packageExported) { |
82 |
results.add(new ForbiddenAccessProposal(referencedPackage, referencedJavaProject.getProject())); |
83 |
} |
84 |
} |
85 |
} |
86 |
} |
87 |
} |
88 |
|
89 |
/* (non-Javadoc) |
90 |
* @see org.eclipse.jdt.ui.text.java.IQuickFixProcessor#hasCorrections(org.eclipse.jdt.core.ICompilationUnit, int) |
91 |
*/ |
92 |
public boolean hasCorrections(ICompilationUnit unit, int problemId) { |
93 |
switch (problemId) { |
94 |
case IProblem.ForbiddenReference: |
95 |
IJavaElement parent = unit.getParent(); |
96 |
if (parent != null) { |
97 |
IJavaProject project = parent.getJavaProject(); |
98 |
if (project != null) |
99 |
return WorkspaceModelManager.isPluginProject(project.getProject()); |
100 |
} |
101 |
default: |
102 |
} |
103 |
return false; |
104 |
} |
105 |
|
106 |
} |