Search in sources :

Example 1 with IPackageFragmentStub

use of com.devonfw.cobigen.eclipse.wizard.common.model.stubs.IPackageFragmentStub in project cobigen by devonfw.

the class SelectFileContentProvider method stubNonExistentChildren.

/**
 * Stubs all non existent Packages, which are selected to be generated and returns all stubbed packages
 *
 * @param parentElement parent {@link IJavaElement} to retrieve the children from
 * @param considerPackages states whether packages should be considered when retrieving the children. This also
 *        includes recursively retrieving children of packages
 * @return List of {@link IPackageFragment}s, which will be stubbed
 * @throws JavaModelException if an internal exception occurs while accessing the eclipse jdt java model
 */
private List<Object> stubNonExistentChildren(Object parentElement, boolean considerPackages) throws JavaModelException {
    // Default package handling
    if (parentElement instanceof IPackageFragment && ((IPackageFragment) parentElement).isDefaultPackage()) {
        // a default package cannot have packages as children
        return Lists.newArrayList();
    }
    List<Object> stubbedChildren = new LinkedList<>();
    String debugInfo = null;
    if (parentElement instanceof IJavaElement) {
        List<String> nonExistentChildren = getNonExistentChildren(((IJavaElement) parentElement).getPath());
        for (String path : nonExistentChildren) {
            // check inclusion and exclusion patterns to stub the correct elements
            if (!JavaClasspathUtil.isCompiledSource((IJavaElement) parentElement, path)) {
                stubNonExistentChildren(((IJavaElement) parentElement).getCorrespondingResource(), stubbedChildren);
                continue;
            }
            IPath elementpath = new Path(path);
            IJavaElementStub javaElementStub;
            if (targetIsFile(elementpath, nonExistentChildren)) {
                // If the file is not a direct child of the parent, we will skip it
                IPath p = elementpath.removeFirstSegments(((IJavaElement) parentElement).getPath().segmentCount());
                if (p.segmentCount() != 1) {
                    continue;
                } else if (this._cachedProvidedResources.containsKey(path) && !stubbedChildren.contains(this._cachedProvidedResources.get(path))) {
                    // if already seen, just get it and skip creation
                    stubbedChildren.add(this._cachedProvidedResources.get(path));
                    continue;
                }
                // Create CompilationUnit Stub
                javaElementStub = new ICompilationUnitStub();
                javaElementStub.setElementType(IJavaElement.COMPILATION_UNIT);
                debugInfo = "COMPILATION_UNIT";
                javaElementStub.setElementName(elementpath.lastSegment());
                javaElementStub.setPath(elementpath);
            } else {
                // it
                if (!isDefinedInSourceFolder(path) || !considerPackages) {
                    continue;
                } else if (this._cachedProvidedResources.containsKey(path) && !stubbedChildren.contains(this._cachedProvidedResources.get(path))) {
                    // if already seen, just get it and skip creation
                    stubbedChildren.add(this._cachedProvidedResources.get(path));
                    continue;
                }
                // Create PackageFragment Stub
                javaElementStub = new IPackageFragmentStub();
                javaElementStub.setElementType(IJavaElement.PACKAGE_FRAGMENT);
                debugInfo = "PACKAGE_FRAGMENT";
                ((IPackageFragmentStub) javaElementStub).setNonJavaResources(new Object[0]);
                javaElementStub.setPath(elementpath);
                IJavaElement parent = determineJavaModelParent((IJavaElement) parentElement, javaElementStub);
                IPath packagePath = elementpath.removeFirstSegments(parent.getPath().segmentCount());
                packagePath.removeTrailingSeparator();
                String elementName = packagePath.toString().replaceAll("/", ".");
                javaElementStub.setElementName(elementName);
            }
            if (((IJavaElement) parentElement) instanceof IPackageFragment) {
                javaElementStub.setParent(((IJavaElement) parentElement).getParent());
            } else {
                javaElementStub.setParent(determineJavaModelParent((IJavaElement) parentElement, javaElementStub));
            }
            IJavaElement[] javaChildren = new IJavaElement[0];
            if (considerPackages) {
                Object[] childrenArr = stubNonExistentChildren(javaElementStub, false).toArray();
                javaChildren = Arrays.copyOf(childrenArr, childrenArr.length, IJavaElement[].class);
            }
            javaElementStub.setChildren(javaChildren);
            if (!stubbedChildren.contains(javaElementStub)) {
                stubbedChildren.add(javaElementStub);
                this._cachedProvidedResources.put(javaElementStub.getPath().toString(), javaElementStub);
                LOG.debug("Stub created for {} with element name '{}' and path '{}'", debugInfo, javaElementStub.getElementName(), javaElementStub.getPath().toString());
            }
        }
        // forget any stubbed file #151/#137
        if (parentElement instanceof IPackageFragmentRoot) {
            List<IPackageFragment> allExistingPackageFragments = HierarchicalTreeOperator.retrievePackageChildren((IPackageFragmentRoot) parentElement, Lists.newArrayListWithCapacity(0));
            for (IPackageFragment frag : allExistingPackageFragments) {
                List<Object> recStubbedChildren = stubNonExistentChildren(frag, considerPackages);
                for (Object c : recStubbedChildren) {
                    if (!stubbedChildren.contains(c)) {
                        stubbedChildren.add(c);
                    }
                }
            }
        }
    } else if (parentElement instanceof IResource) {
        stubNonExistentChildren((IResource) parentElement, stubbedChildren);
    }
    return stubbedChildren;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) ICompilationUnitStub(com.devonfw.cobigen.eclipse.wizard.common.model.stubs.ICompilationUnitStub) IJavaElement(org.eclipse.jdt.core.IJavaElement) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IPath(org.eclipse.core.runtime.IPath) IJavaElementStub(com.devonfw.cobigen.eclipse.wizard.common.model.stubs.IJavaElementStub) LinkedList(java.util.LinkedList) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) IPackageFragmentStub(com.devonfw.cobigen.eclipse.wizard.common.model.stubs.IPackageFragmentStub) IResource(org.eclipse.core.resources.IResource)

Aggregations

ICompilationUnitStub (com.devonfw.cobigen.eclipse.wizard.common.model.stubs.ICompilationUnitStub)1 IJavaElementStub (com.devonfw.cobigen.eclipse.wizard.common.model.stubs.IJavaElementStub)1 IPackageFragmentStub (com.devonfw.cobigen.eclipse.wizard.common.model.stubs.IPackageFragmentStub)1 LinkedList (java.util.LinkedList)1 IResource (org.eclipse.core.resources.IResource)1 IPath (org.eclipse.core.runtime.IPath)1 Path (org.eclipse.core.runtime.Path)1 IJavaElement (org.eclipse.jdt.core.IJavaElement)1 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)1 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)1