Eclipse插件开发中(右键属性插件)遇到的ClassCastEcxeption

在开发针对Eclipse中右键属性列表的插件时,遇到CompilationUnit、PackageFragment、PackageFragmentRoot、JavaProject无法正常转化,选中的对象携带的信息无法正常的通过类的转换得到。

解决方案:java的反射机制

主要代码:

1. CompilationUnit

Class<?> clazz = element.getClass().getSuperclass().getSuperclass();

Field field = clazz .getDeclaredField("parent");

field.setAccessible(true);

Object packageFragment = field.get(element);

Object packageFragmentRoot = field.get(packageFragment);

field = packageFragmentRoot.getClass().getDeclaredField("resource");

field.setAccessible(true);

Object folder = field.get(packageFragmentRoot);

field = folder.getClass().getSuperclass().getSuperclass().getDeclaredField("workspace");

field.setAccessible(true);

Object workspace = field.get(folder);

field = workspace.getClass().getDeclaredField("defaultRoot");

field.setAccessible(true);

Object defaultRoot = field.get(workspace);

field = defaultRoot.getClass().getDeclaredField("workspaceLocation");

field.setAccessible(true);

Object workspaceLocation = field.get(defaultRoot);

workspaceLocation.toString();

 

2. PackageFragment

Class<?> clazz = element.getClass().getSuperclass().getSuperclass();

Field field = clazz .getDeclaredField("parent");

field.setAccessible(true);

Object packageFragment = field.get(element);

field = packageFragment.getClass().getDeclaredField("resource");

field.setAccessible(true);

Object folder = field.get(packageFragment);

field = folder.getClass().getSuperclass().getSuperclass().getDeclaredField("workspace");

field.setAccessible(true);

Object workspace = field.get(folder);

field = workspace.getClass().getDeclaredField("defaultRoot");

field.setAccessible(true);

Object defaultRoot = field.get(workspace);

field = defaultRoot.getClass().getDeclaredField("workspaceLocation");

field.setAccessible(true);

Object workspaceLocation = field.get(defaultRoot);

workspaceLocation.toString();

 

3. PackageFragmentRoot

Class<?> clazz = element.getClass().getSuperclass().getSuperclass();

Field field = clazz .getDeclaredField("parent");

field.setAccessible(true);

Object parent = field.get(element);

field = parent.getClass().getDeclaredField("project");

field.setAccessible(true);

Object folder = field.get(parent);

field = folder.getClass().getSuperclass().getSuperclass().getDeclaredField("workspace");

field.setAccessible(true);

Object workspace = field.get(folder);

field = workspace.getClass().getDeclaredField("defaultRoot");

field.setAccessible(true);

Object defaultRoot = field.get(workspace);

field = defaultRoot.getClass().getDeclaredField("workspaceLocation");

field.setAccessible(true);

Object workspaceLocation = field.get(defaultRoot);

workspaceLocation.toString();

 

4. JavaProject

Class<?> clazz = element.getClass().getSuperclass().getSuperclass();

Field field = clazz .getDeclaredField("workspace");

field.setAccessible(true);

Object workspace = field.get(element);

field = workspace.getClass().getDeclaredField("defaultRoot");

field.setAccessible(true);

Object defaultRoot = field.get(workspace);

field = defaultRoot.getClass().getDeclaredField("workspaceLocation");

field.setAccessible(true);

Object workspaceLocation = field.get(defaultRoot);

workspaceLocation.toString();

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值