2015-12-25 39 views
-1

我正在嘗試使用Eclipse編寫Eclipse-Plugin。該插件使用org.eclipse.jdt.core。 在我的代碼中的某個點,我從菜單動作處理程序中獲取CompilcationUnit的一個對象,並且我想要找到使用我的插件單擊的java文件的路徑。首先,在獲得「CompilcationUnit」類型的對象的那一行,我得到了一個N​​oClassDefFoundError(我引用了在我的eclipse/plugins文件夾中的org.eclipse.jdt.core.jar)。 這個線程解決了這個問題: Eclipse plugin: java.lang.NoClassDefFoundErrorEclipse插件 - 添加eclipse本身使用的jar

類似的解決方案發現: Java eclipse : Importing jar file in eclipse plugin project

現在,我有一個不同的錯誤。我試圖驗證元素是CompilationUnit類型,然後使用它。 我寫了下面的代碼:

System.out.println(element.getClass()); 
System.out.println(org.eclipse.jdt.internal.core.CompilationUnit.class); 

System.out.println(element instanceof org.eclipse.jdt.internal.core.CompilationUnit); 

System.out.println(element.getClass().getPackage()); 
System.out.println(org.eclipse.jdt.internal.core.CompilationUnit.class.getPackage()); 

System.out.println(element.getClass().getProtectionDomain().getCodeSource().getLocation()); 
System.out.println(org.eclipse.jdt.internal.core.CompilationUnit.class.getProtectionDomain().getCodeSource().getLocation()); 

org.eclipse.jdt.internal.core.CompilationUnit cu = (org.eclipse.jdt.internal.core.CompilationUnit)element; 

輸出是這樣的:

類org.eclipse.jdt.internal.core.CompilationUnit

類org.eclipse.jdt.internal。 core.CompilationUnit

包org.eclipse.jdt.internal.core

包org.eclipse.jdt.internal.core

文件:/ C:/ Program Files文件/日蝕/插件/ org.eclipse.jdt.core_3.11.1.v20150902-1521.jar

文件:/ d:/Svn/OrderingTR/Source/ClassMethodsRearranger/external/org.eclipse.jdt.core_3.11.1.v20150902-1521.jar

然後我得到一個異常:

org.eclipse.e4.core.di.InjectionException: java.lang.ClassCastException: org.eclipse.jdt.internal.core.CompilationUnit cannot be cast to org.eclipse.jdt.internal.core.CompilationUnit 

我不明白這一點。看起來這個元素與我的代碼引用的是相同的類型。只有我的代碼引用了ile中的類型:/ D:/Svn/OrderingTR/Source/ClassMethodsRearranger/external/org.eclipse.jdt.core_3.11.1.v20150902-1521.jar

and the element is the same type只能位於文件:/ C:/ Program Files/Eclipse/plugins/org.eclipse.jdt.core_3.11.1.v20150902-1521.jar中。

現在,我需要引用D:/ Svn/...中的文件,因爲那是項目的jar,並且它是eclipse插件所需的(否則我會在之前解決NoClassDefFoundError問題)。 但我無法使用我從菜單動作處理程序獲得的元素(我相信因爲它源自不同的jar文件)。我需要使用該元素。 我能做什麼?有人遇到過這種問題嗎?我搜索和搜索,沒有發現任何東西......看起來這是特定於eclipse插件和使用jdt.core。

非常感謝,我真的很感激長時間的閱讀。

+0

我用反射解決了它。而不是使用引用(它引發異常),我通過反射調用方法,直到代碼不再位於jdt.core包中。 –

回答

0

我最終通過在MANIFEST.MF中添加引用來解決它。 在依賴關係上,我添加了org.eclipse.jdt。核心在必需的插件中。