在开发针对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();