Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Pimp IAdaptable

(cherry picked from commit e546976)
  • Loading branch information...
commit 219cd644f0e34ea783ccf2bede7418d33a035f0c 1 parent 5260ad1
@mdr mdr authored
View
3  org.scala-ide.sdt.core/src/scala/tools/eclipse/actions/AbstractPopupAction.scala
@@ -8,6 +8,7 @@ import org.eclipse.jface.viewers.{ ISelection, IStructuredSelection }
import org.eclipse.core.runtime.Platform
import org.eclipse.ui.{ IObjectActionDelegate, IWorkbenchPart }
import ScalaPlugin.plugin
+import scala.tools.eclipse.util.EclipseUtils._
trait AbstractPopupAction extends IObjectActionDelegate {
private var selectionOption: Option[ISelection] = None
@@ -26,7 +27,7 @@ trait AbstractPopupAction extends IObjectActionDelegate {
private def selectionObjectToProject(selectionElement: Object): Option[IProject] = selectionElement match {
case project: IProject => Some(project)
- case adaptable: IAdaptable => Option(adaptable.getAdapter(classOf[IProject]).asInstanceOf[IProject])
+ case adaptable: IAdaptable => adaptable.adaptToSafe[IProject]
case _ => None
}
View
1  org.scala-ide.sdt.core/src/scala/tools/eclipse/actions/ToggleScalaNatureAction.scala
@@ -7,7 +7,6 @@ package scala.tools.eclipse
package actions
import org.eclipse.core.resources.{ IProject }
-import org.eclipse.core.runtime.IAdaptable
import org.eclipse.jface.action.IAction
import org.eclipse.jface.viewers.{ ISelection, IStructuredSelection }
import org.eclipse.core.runtime.Platform
View
2  org.scala-ide.sdt.core/src/scala/tools/eclipse/diagnostic/DiagnosticDialog.scala
@@ -16,7 +16,7 @@ import org.eclipse.jdt.ui.PreferenceConstants
import org.eclipse.jdt.internal.ui.preferences.PreferencesMessages
import org.eclipse.jdt.internal.corext.util.Messages
-import org.eclipse.core.runtime.{ IAdaptable, IStatus }
+import org.eclipse.core.runtime.IStatus
import org.eclipse.contribution.jdt.preferences.{ WeavingStateConfigurer, WeavingStateConfigurerUI, JDTWeavingPreferences }
import org.eclipse.ui.PlatformUI
import org.eclipse.ui.browser.IWorkbenchBrowserSupport
View
3  org.scala-ide.sdt.core/src/scala/tools/eclipse/launching/ScalaLaunchShortcut.scala
@@ -19,6 +19,7 @@ import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants
import org.eclipse.jface.operation.IRunnableContext
import scala.tools.eclipse.javaelements.{ ScalaModuleElement, ScalaClassElement, ScalaSourceFile }
+import scala.tools.eclipse.util.EclipseUtils._
/* This class can be eliminated in favour of JavaApplicationLaunch shortcut as soon as
* the JDTs method search works correctly for Scala.
@@ -170,7 +171,7 @@ object ScalaLaunchShortcut {
})
}
- val je = element.asInstanceOf[IAdaptable].getAdapter(classOf[IJavaElement]).asInstanceOf[IJavaElement]
+ val je = element.asInstanceOf[IAdaptable].adaptTo[IJavaElement]
je.getOpenable match {
case scu: ScalaSourceFile =>
def isTopLevel(tpe: IType) = tpe.getDeclaringType == null
View
35 org.scala-ide.sdt.core/src/scala/tools/eclipse/launching/ScalaLaunchableTester.scala
@@ -6,25 +6,26 @@
package scala.tools.eclipse.launching
-import org.eclipse.core.expressions.PropertyTester;
-import org.eclipse.core.resources.IContainer;
-import org.eclipse.core.resources.IResource;
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.IAdaptable;
-import org.eclipse.jdt.core.Flags;
-import org.eclipse.jdt.core.IClassFile;
-import org.eclipse.jdt.core.ICompilationUnit;
-import org.eclipse.jdt.core.IJavaElement;
-import org.eclipse.jdt.core.IMember;
-import org.eclipse.jdt.core.IMethod;
-import org.eclipse.jdt.core.IType;
-import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.core.expressions.PropertyTester
+import org.eclipse.core.resources.IContainer
+import org.eclipse.core.resources.IResource
+import org.eclipse.core.runtime.CoreException
+import org.eclipse.core.runtime.IAdaptable
+import org.eclipse.jdt.core.Flags
+import org.eclipse.jdt.core.IClassFile
+import org.eclipse.jdt.core.ICompilationUnit
+import org.eclipse.jdt.core.IJavaElement
+import org.eclipse.jdt.core.IMember
+import org.eclipse.jdt.core.IMethod
+import org.eclipse.jdt.core.IType
+import org.eclipse.jdt.core.JavaModelException
+import scala.tools.eclipse.util.EclipseUtils._
class ScalaLaunchableTester extends PropertyTester {
/**
* name for the "has main" property
*/
- private val PROPERTY_HAS_MAIN = "hasMain"; //$NON-NLS-1$
+ private val PROPERTY_HAS_MAIN = "hasMain" //$NON-NLS-1$
/**
* Determines if the Scala element contains main method(s).
@@ -59,12 +60,12 @@ class ScalaLaunchableTester extends PropertyTester {
* @return true if the specified tests pass, false otherwise
*/
def test(receiver: Object, property: String, args: Array[Object], expectedValue: Object): Boolean = {
- var element: IJavaElement = null;
+ var element: IJavaElement = null
if (receiver.isInstanceOf[IAdaptable]) {
- element = (receiver.asInstanceOf[IAdaptable]).getAdapter(classOf[IJavaElement]).asInstanceOf[IJavaElement];
+ element = receiver.asInstanceOf[IAdaptable].adaptTo[IJavaElement]
if (element != null && !element.exists()) {
- return false;
+ return false
}
}
View
16 org.scala-ide.sdt.core/src/scala/tools/eclipse/util/EclipseUtils.scala
@@ -0,0 +1,16 @@
+package scala.tools.eclipse.util
+
+import org.eclipse.core.runtime.IAdaptable
+object EclipseUtils {
+
+ implicit def adaptableToPimpedAdaptable(adaptable: IAdaptable): PimpedAdaptable = new PimpedAdaptable(adaptable)
+
+ class PimpedAdaptable(adaptable: IAdaptable) {
+
+ def adaptTo[T](implicit m: Manifest[T]): T = adaptable.getAdapter(m.erasure).asInstanceOf[T]
+
+ def adaptToSafe[T](implicit m: Manifest[T]): Option[T] = Option(adaptable.getAdapter(m.erasure).asInstanceOf[T])
+
+ }
+
+}
View
6 org.scala-ide.sdt.core/src/scala/tools/eclipse/wizards/NewResourceWizard.scala
@@ -23,6 +23,7 @@ import org.eclipse.ui.wizards.newresource.BasicNewResourceWizard
import scala.tools.nsc.util.Chars
import scala.tools.eclipse.{ ScalaPlugin, ScalaProject }
+import scala.tools.eclipse.util.EclipseUtils._
trait NewResourceWizard extends BasicNewResourceWizard {
def kind : String
@@ -65,10 +66,7 @@ trait NewResourceWizard extends BasicNewResourceWizard {
val i = getSelection.iterator
while (i.hasNext) i.next match {
case res : IResource => populate(res.getProject)
- case res : IAdaptable => res.getAdapter(classOf[IResource]) match {
- case res : IResource => populate(res.getProject)
- case null =>
- }
+ case adaptable : IAdaptable => adaptable.adaptToSafe[IResource] foreach { res => populate(res.getProject) }
case _ =>
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.