Example usage for org.eclipse.jdt.internal.core JavaModelManager removeInfoAndChildren

List of usage examples for org.eclipse.jdt.internal.core JavaModelManager removeInfoAndChildren

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.core JavaModelManager removeInfoAndChildren.

Prototype

public synchronized Object removeInfoAndChildren(JavaElement element) throws JavaModelException 

Source Link

Usage

From source file:org.codehaus.jdt.groovy.integration.internal.GroovyLanguageSupport.java

License:Open Source License

/**
 * Go through the bunary children and remove all children that do not have a real source location
 *///  w w w .  java2  s .  co m
public void filterNonSourceMembers(BinaryType binaryType) {
    try {
        IJavaElement[] childrenArr = binaryType.getChildren();
        List<IJavaElement> children = new ArrayList<IJavaElement>(Arrays.asList(childrenArr));
        List<JavaElement> removedChildren = new LinkedList<JavaElement>();
        for (Iterator<IJavaElement> childIter = children.iterator(); childIter.hasNext();) {
            IJavaElement child = childIter.next();
            if (child instanceof BinaryMember) {
                BinaryMember binaryChild = (BinaryMember) child;
                ISourceRange range = binaryChild.getSourceRange();
                if (range == null || range.getOffset() == -1) {
                    removedChildren.add(binaryChild);
                    childIter.remove();
                }
            }
        }
        JavaElement[] newChildrenArr = children.toArray(new JavaElement[children.size()]);

        // now comes the icky part.

        // we need to set the children of the ClassFileInfo to the new children
        // but this class is package protected, so can't access it directly.
        Object /* ClassFileInfo */ classFileInfo = ((ClassFile) binaryType.getParent()).getElementInfo();
        ReflectionUtils.setPrivateField(classFileInfo.getClass(), "binaryChildren", classFileInfo,
                newChildrenArr);

        // also need to remove these children from the JavaModelManager
        JavaModelManager manager = JavaModelManager.getJavaModelManager();
        for (JavaElement removedChild : removedChildren) {
            if (removedChild instanceof BinaryType) {
                manager.removeInfoAndChildren((JavaElement) removedChild.getParent());
            } else {
                manager.removeInfoAndChildren(removedChild);
            }
        }

    } catch (JavaModelException e) {
        e.printStackTrace();
    }
}