add helper methods to aid refactoring support (FELIX-1346)
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@966636 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/sigil/eclipse/core/src/org/apache/felix/sigil/eclipse/model/util/JavaHelper.java b/sigil/eclipse/core/src/org/apache/felix/sigil/eclipse/model/util/JavaHelper.java
index b0121c6..4ba57f7 100644
--- a/sigil/eclipse/core/src/org/apache/felix/sigil/eclipse/model/util/JavaHelper.java
+++ b/sigil/eclipse/core/src/org/apache/felix/sigil/eclipse/model/util/JavaHelper.java
@@ -65,6 +65,7 @@
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
+import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.content.IContentDescription;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.core.runtime.content.IContentTypeManager;
@@ -78,9 +79,11 @@
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IImportDeclaration;
import org.eclipse.jdt.core.IJavaElement;
+import org.eclipse.jdt.core.IJavaModel;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.ILocalVariable;
import org.eclipse.jdt.core.IMethod;
+import org.eclipse.jdt.core.IPackageDeclaration;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.IParent;
@@ -1150,14 +1153,8 @@
String packageName = pe.getPackageName();
- IPreferenceStore store = SigilCore.getDefault().getPreferenceStore();
- VersionRangeBoundingRule lowerBoundRule = VersionRangeBoundingRule.valueOf( store
- .getString( SigilCore.DEFAULT_VERSION_LOWER_BOUND ) );
- VersionRangeBoundingRule upperBoundRule = VersionRangeBoundingRule.valueOf( store
- .getString( SigilCore.DEFAULT_VERSION_UPPER_BOUND ) );
-
Version version = pe.getVersion();
- VersionRange versions = VersionRange.newInstance( version, lowerBoundRule, upperBoundRule );
+ VersionRange versions = ModelHelper.getDefaultRange(version);
IPackageImport pi = ModelElementFactory.getInstance().newModelElement( IPackageImport.class );
pi.setPackageName( packageName );
@@ -1258,4 +1255,55 @@
throw new JavaModelException( new IllegalStateException( "Missing type for " + root ), IStatus.ERROR );
}
+
+
+ public static Set<String> findLocalPackageDependencies(
+ ISigilProjectModel project, String packageName, IProgressMonitor monitor) throws JavaModelException
+ {
+ Set<String> imports = findJavaImports( project, monitor );
+ imports.remove(packageName);
+ return imports;
+ }
+
+
+ public static Set<String> findLocalPackageUsers(
+ ISigilProjectModel project, String packageName, IProgressMonitor monitor) throws JavaModelException
+ {
+ Set<String> imports = new HashSet<String>();
+ Set<String> check = new HashSet<String>();
+ for ( IPackageFragment root : project.getJavaModel().getPackageFragments() )
+ {
+ IPackageFragmentRoot rt = ( IPackageFragmentRoot ) root
+ .getAncestor( IJavaElement.PACKAGE_FRAGMENT_ROOT );
+
+ if ( isInClassPath( project, rt ) )
+ {
+ for ( ICompilationUnit cu : root.getCompilationUnits() )
+ {
+ IPackageFragment pack = (IPackageFragment) cu.getAncestor(IJavaModel.PACKAGE_FRAGMENT);
+ if ( !pack.getElementName().equals(packageName)) {
+ scanImports( cu, check );
+ if (check.contains(packageName)) {
+ imports.add(pack.getElementName());
+ }
+ }
+ check.clear();
+ }
+
+ for ( IClassFile cf : root.getClassFiles() )
+ {
+ IPackageFragment pack = (IPackageFragment) cf.getAncestor(IJavaModel.PACKAGE_FRAGMENT);
+ if ( !pack.getElementName().equals(packageName)) {
+ scanImports( cf, check );
+ if (check.contains(packageName)) {
+ imports.add(pack.getElementName());
+ }
+ }
+ check.clear();
+ }
+ }
+ }
+
+ return imports;
+ }
}
\ No newline at end of file
diff --git a/sigil/eclipse/core/src/org/apache/felix/sigil/eclipse/model/util/ModelHelper.java b/sigil/eclipse/core/src/org/apache/felix/sigil/eclipse/model/util/ModelHelper.java
index 4f70900..af41e38 100644
--- a/sigil/eclipse/core/src/org/apache/felix/sigil/eclipse/model/util/ModelHelper.java
+++ b/sigil/eclipse/core/src/org/apache/felix/sigil/eclipse/model/util/ModelHelper.java
@@ -20,15 +20,24 @@
package org.apache.felix.sigil.eclipse.model.util;
+import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
+import org.apache.felix.sigil.common.osgi.VersionRange;
+import org.apache.felix.sigil.common.osgi.VersionRangeBoundingRule;
import org.apache.felix.sigil.eclipse.SigilCore;
+import org.apache.felix.sigil.eclipse.model.project.ISigilProjectModel;
import org.apache.felix.sigil.model.ICapabilityModelElement;
import org.apache.felix.sigil.model.ICompoundModelElement;
import org.apache.felix.sigil.model.IModelElement;
import org.apache.felix.sigil.model.IModelWalker;
import org.apache.felix.sigil.model.IRequirementModelElement;
+import org.apache.felix.sigil.model.ModelElementFactory;
+import org.apache.felix.sigil.model.osgi.IPackageExport;
+import org.apache.felix.sigil.model.osgi.IPackageImport;
+import org.eclipse.jface.preference.IPreferenceStore;
+import org.osgi.framework.Version;
public class ModelHelper
@@ -77,5 +86,35 @@
}
}
}
+
+ public static VersionRange getDefaultRange(Version version) {
+ IPreferenceStore store = SigilCore.getDefault().getPreferenceStore();
+
+ VersionRangeBoundingRule lowerBoundRule = VersionRangeBoundingRule.valueOf( store
+ .getString( SigilCore.DEFAULT_VERSION_LOWER_BOUND ) );
+ VersionRangeBoundingRule upperBoundRule = VersionRangeBoundingRule.valueOf( store
+ .getString( SigilCore.DEFAULT_VERSION_UPPER_BOUND ) );
+
+ VersionRange selectedVersions = VersionRange.newInstance( version, lowerBoundRule, upperBoundRule );
+ return selectedVersions;
+ }
+
+ public static IPackageExport findExport(ISigilProjectModel sigil, final String packageName) {
+ final ArrayList<IPackageExport> found = new ArrayList<IPackageExport>(1);
+ sigil.visit(new IModelWalker()
+ {
+ public boolean visit(IModelElement element)
+ {
+ if (element instanceof IPackageExport) {
+ IPackageExport pe = (IPackageExport) element;
+ if (pe.getPackageName().equals(packageName)) {
+ found.add(pe);
+ }
+ }
+ return found.isEmpty();
+ }
+ });
+ return found.isEmpty() ? null : found.get(0);
+ }
}